/******************************************************************
** Copyright (C) 2001, University of Alabama in Huntsville. 
** All rights reserved.
**
** File   : CConcept.cpp
**
** Author : David Phillips
**
** Description:
**    This file provides implementation for the CConcept
**    class.
**
** History:
**    24-Nov-2001 : David Phillips
**        Created file.
******************************************************************/

#include "CGEngine\ACCGFactory.h"
#include "CGEngine\CConcept.h"
#include "CGEngine\CRelation.h"

CConcept::CConcept()
: m_pClpInstance (NULL)
{
}

CConcept::CConcept(const CConcept &right)
{
}


CConcept::~CConcept()
{
	std::cout << "Deleting Concept..." << std::endl;
	dynamic_cast<CSheetOfAssertion*>(GetParent())->Remove(this);
	if (m_pClpInstance)
	{
		m_pClpInstance->Delete();
	}
}


CConcept & CConcept::operator=(const CConcept &right)
{
	throw CConcept::Not_Implemented_Yet();
}


int CConcept::operator==(const CConcept &right) const
{
	throw CConcept::Not_Implemented_Yet();
}

int CConcept::operator!=(const CConcept &right) const
{
	throw CConcept::Not_Implemented_Yet();
}

void CConcept::Change_Referent ()
{
	throw CConcept::Not_Implemented_Yet();
}

void CConcept::Change_Type ()
{
	throw CConcept::Not_Implemented_Yet();
}

void CConcept::Join (CConcept* pNonSurvivingConcept)
{
	RelationVector::iterator iter;

    iter = pNonSurvivingConcept->m_vectorRelation.begin();

	while (iter != pNonSurvivingConcept->m_vectorRelation.end())
	{
		CRelation* pRelation = (*iter);
		(*iter)->Replace(pNonSurvivingConcept, this);
	}

	delete pNonSurvivingConcept;
}

void CConcept::Detach (ACCGFactory* pFactory)
{
	RelationVector::iterator iter;

    iter = m_vectorRelation.begin();
	iter++; // This concept retains the first relation

	while (iter != m_vectorRelation.end())
	{
		CConcept* pNewConcept = pFactory->NewConcept();
		pNewConcept->SetTypeDef(GetTypeDef());
		GetParent()->Insert(pNewConcept);
		(*iter)->Replace(this, pNewConcept);
	}

}

void CConcept::Copy ()
{
	throw CConcept::Not_Implemented_Yet();
}

void CConcept::Simplify ()
{
	throw CConcept::Not_Implemented_Yet();
}

void CConcept::Add_To_Coreferent_Set ()
{
	throw CConcept::Not_Implemented_Yet();
}

void CConcept::AddRelation(CRelation* pRelation)
{
	m_vectorRelation.push_back(pRelation);
}

void CConcept::RemoveRelation(CRelation* pRelation)
{
	RelationVector::iterator iter;

	iter = m_vectorRelation.begin();
	
	while (iter != m_vectorRelation.end())
	{
		if (*iter = pRelation)
		{
			m_vectorRelation.erase(iter);
			break;
		}
		iter++;
	}

}

void CConcept::Commit()
{
	CCLPInstance clpInstance;
	char tempNum[30];
	sprintf(tempNum, "%d", GetUid());
	
	std::string sName = m_pTypeDef->GetClipsName() + "__" + tempNum;
	clpInstance.Make(sName, m_pTypeDef->GetClipsName());
	
//	try
//	{
//		m_pClpInstance = clpInstance.FindByName(sName);
//	}
//	catch (...)
//	{
//		std::cout << "Could not find instance" << std::endl;
//	}
}
