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

// CRelationDefinition
#include "CGEngine\CRelationDefinition.h"
#include "CGEngine\CRelationHierarchy.h"
#include <iostream>
//## begin module%3BD773E900F0.additionalDeclarations preserve=yes
//## end module%3BD773E900F0.additionalDeclarations


// Class CRelationDefinition 

CRelationDefinition::CRelationDefinition(std::string name)
  : m_name(name), m_bPrimitive(false)
{
	std::cout << "Creating Relation Defintion => " << m_name << std::endl;
}

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


CRelationDefinition::~CRelationDefinition()
{
}


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


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

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



//## Other Operations (implementation)
void CRelationDefinition::AddLambdaElement (std::string String)
{
	throw ACConstruct::Not_Implemented_Yet();
}

//## Other Operations (implementation)
void CRelationDefinition::AddSuperclass (CRelationDefinition *pSuperclass)
{
	m_superclass_vector.push_back(pSuperclass);
	pSuperclass->m_subclass_vector.push_back(this);
}

void  CRelationDefinition::Commit ()
{

	std::cout << "Processing commit for " << m_name << std::endl;
    
	m_clipClass = new CCLPClass;
	
	// Send current class to CLIPS

	m_clipClass->SetName("CG_RELATION_" + m_name);

	if (m_name.compare("Relation") == 0)
	{
        m_clipClass->Define("USER");
	}
	else
	{
		RelationDefVector::iterator iterSuperclass;
		iterSuperclass = m_superclass_vector.begin();

		std::string sSuperclass;

		if (iterSuperclass == m_superclass_vector.end())
		{
			const CRelationHierarchy* rh = dynamic_cast<const CRelationHierarchy*>(GetParent());
			CRelationDefinition* pRelation = rh->FindByName("Relation");
			AddSuperclass(pRelation);
			m_bPrimitive = TRUE;
			sSuperclass = "Relation";
		}
		else
		{
			sSuperclass = (*iterSuperclass)->m_name;
		}

		// NOTE:  Need to be able to install multiple superclasses
		m_clipClass->Define("CG_RELATION_" + sSuperclass);
	}

    // Commit Subclasses
	RelationDefVector::iterator iterSubclass;
	iterSubclass = m_subclass_vector.begin();

	while (iterSubclass != m_subclass_vector.end())
	{
		std::cout << "Calling commit for " << (*iterSubclass)->GetTypeName().c_str() << std::endl;
        (*iterSubclass)->Commit();
		iterSubclass++;
	}

}

void CRelationDefinition::Browse()
{
	m_clipClass->BrowseClass();
}
