/******************************************************************************
** Copyright (C) 2001, University of Alabama in Huntsville. 
** All rights reserved.
**
** File   : CSheetOfAssertion.cpp
**
** Author : David Phillips
**
** Description:
**    This file provides implementation for the CSheetOfAssertion
**    class.
**
** History:
** 24-Nov-2001 : David Phillips
**     Created file.
** 02-Dec-2001 : Satish Hota
**     Modified the Insert method.  Added this SOA as the parent of the
**     Concept or Relation being inserted.
******************************************************************************/

#include "CGEngine\CSheetOfAssertion.h"

CSheetOfAssertion::CSheetOfAssertion()
{
}

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


CSheetOfAssertion::~CSheetOfAssertion()
{
}


void CSheetOfAssertion::Insert (ACConstruct* pConstruct)
{
	CConcept *pConcept = dynamic_cast<CConcept*>(pConstruct);
	CRelation *pRelation = dynamic_cast<CRelation*>(pConstruct);

	if (pConcept)
	{
	   std::cout << "Inserting Concept into SOA" << std::endl;

	   bool bNotFound = true;

	   CConcept::ConceptVector::const_iterator iter;

	   iter = m_vectorConcept.begin();

	   while (iter != m_vectorConcept.end())
	   {
		   if ((*iter) == pConcept)
		   {
			   bNotFound = false;
			   break;
		   }
		   iter++;
	   }

	   if (bNotFound)
	   {
		   m_vectorConcept.push_back(pConcept);
		   
		   pConcept->SetParent(this);
		   
		   if (pConcept->GetLabel() != "")
		   {
			   std::cout << "Concept " << pConcept->GetLabel() << " inserted into SOA" << std::endl;
			   m_mapLabel.insert(LabelMap::value_type(pConcept->GetLabel(), pConcept));
		   }
		   pConcept->Commit();
	   }
	}
	else if (pRelation)
	{
  	   std::cout << "Inserting Relation into SOA" << std::endl;
		m_vectorRelation.push_back(pRelation);
	   pRelation->SetParent(this);
	}
	else
	{
		std::cout << "*** Non-Concept inserted into Sheet of Assertion" << std::endl;
	}
}

void CSheetOfAssertion::Remove (ACConstruct* pConstruct)
{
	CConcept *pConcept = dynamic_cast<CConcept*>(pConstruct);
	CRelation *pRelation = dynamic_cast<CRelation*>(pConstruct);

	if (pConcept)
	{
  	   std::cout << "Removing Concept from SOA" << std::endl;
	   CConcept::ConceptVector::iterator iterConcept = m_vectorConcept.begin();

	   while (iterConcept != m_vectorConcept.end())
	   {
		   if ((*iterConcept) == pConcept)
		   {
			   m_vectorConcept.erase(iterConcept);
			   break;
		   }
		   iterConcept++;
	   }

       if (pConcept->GetLabel() != "")
	   {
		   LabelMap::iterator iterLabel = m_mapLabel.find(pConcept->GetLabel());
		   if (iterLabel != m_mapLabel.end())
		   {
			   m_mapLabel.erase(iterLabel);
		   }
	   }
	}
	else if (pRelation)
	{
  	   std::cout << "Removing Relation from SOA" << std::endl;
		m_vectorRelation.push_back(pRelation);

		CRelation::RelationVector::iterator iterRelation = m_vectorRelation.begin();

	   while (iterRelation != m_vectorRelation.end())
	   {
		   if ((*iterRelation) = pRelation)
		   {
			   m_vectorRelation.erase(iterRelation);
			   break;
		   }
		   iterRelation++;
	   }
	}
	else
	{
		std::cout << "*** Can not remove construct from SOA" << std::endl;
	}
}

CConcept* CSheetOfAssertion::FindByLabel(std::string sLabel)
{
	LabelMap::iterator iter;
	
	iter = m_mapLabel.find(sLabel);
	
	if (iter == m_mapLabel.end())
	{
		return NULL;
	}
	else
	{
		return (*iter).second;
	}
}

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


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

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

const CSheetOfAssertion::LabelMap* CSheetOfAssertion::GetLabels() const
{
	return &m_mapLabel;
};
