/******************************************************************
** Copyright (C) 2001, University of Alabama in Huntsville. 
** All rights reserved.
**
** File   : CGIFGenerator.h
**
** Author : Yangsen Yao
**
** Description:
**    To implement of CGIFGenerator class.
**    
**
** History:
**    02-Dec-2001 : Yangsen Yao
**        Created file.
******************************************************************/

#include "CCGIFGenerator.h"

void CGIFGenerator::DumpTypeHierarchy(CTypeDefinition* pTypeDef, ofstream outParserFile)
{
    
    const CTypeDefinition::TypeDefVector *pVectorSuperclass = pTypeDef->GetSuperclassVector();
    CTypeDefinition::TypeDefVector::const_iterator iterSuperclass = pVectorSuperclass->begin();
    
    while (iterSuperclass != pVectorSuperclass->end())
    {
        outParserFile << "(GT " << '\n';
        outParserFile << "[TypeLabel: \" "<< (*iterSuperclass)->GetTypeName().c_str() << "\"] " << '\n';
        outParserFile << "[TypeLabel: \" "<< pTypeDef->GetTypeName().c_str() <<  "\"] " << '\n';
        
        if (pTypeDef->GetComment() != "")
        {
            outParserFile << ";" << pTypeDef->GetComment().c_str() << ";" << '\n';
        }
        
        outParserFile << ")" << '\n';
        iterSuperclass++;
    }
    
    // Descend for each subclass
    const CTypeDefinition::TypeDefVector *pVectorSubclass = pTypeDef->GetSubclassVector();
    CTypeDefinition::TypeDefVector::const_iterator iterSubclass = pVectorSubclass->begin();
    
    while (iterSubclass != pVectorSubclass->end())
    {
        DumpTypeHierarchy(*iterSubclass, outParserFile);
        iterSuperclass++;
    }
}


void CGIFGenerator::DumpRelationHierarchy(CRelationDefinition* pRelationDef, ofstream
    outParserFile)
{
    
    const CRelationDefinition::RelationDefVector *pVectorSuperclass = pRelationDef->GetSuperclassVector();
    CRelationDefinition::RelationDefVector::const_iterator iterSuperclass = pVectorSuperclass->begin();
    
    while (iterSuperclass != pVectorSuperclass->end())
    {
        outParserFile << "(GT " << '\n';
        outParserFile << "[RelationLabel:  \"" << (*iterSuperclass)->GetTypeName().c_str() << "\"]" << '\n';
        outParserFile << "[RelationLabel:  \"" << pRelationDef->GetTypeName().c_str() << "\"]" << '\n';
        
        if (pRelationDef->GetComment() != "")
        {
            outParserFile << ";" << pRelationDef->GetComment().c_str() << ";" << '\n';
        }
        
        outParserFile << ")" << '\n';
        iterSuperclass++;
    }
    
    // Descend for each subclass
    const CRelationDefinition::RelationDefVector *pVectorSubclass = pRelationDef->GetSubclassVector();
    CRelationDefinition::RelationDefVector::const_iterator iterSubclass = pVectorSubclass->begin();
    
    while (iterSubclass != pVectorSubclass->end())
    {
        DumpRelationHierarchy(*iterSubclass, outParserFile);
        iterSuperclass++;
    }
}

void CGIFGenerator::DumpCatalogOfIndivituals(CCatalogOfIndividuals* pCOI, ofstream outParserFile)
{	
	const CCatalogOfIndividuals::IndividualMap  *pMapIndividual = pCOI->GetIndividuals();
	CCatalogOfIndividuals::IndividualMap::const_iterator iterIndividual;

	iterIndividual = pMapIndividual->begin();
	while ( iterIndividual != pMapIndividual->end())
	{
		CConcept *theConcept = (*iterIndividual).second;
		outParserFile << "[" << theConcept->GetTypeDef()->GetTypeName().c_str() << ":" 
			<< theConcept->GetIndividualMarker().c_str() << "]" << '\n';
		iterIndividual++;
	}
}


bool CGIFGenerator::CCGIFParserGenerate( const std::string& strFileName, /* input */
                          CKnowledgeBase* pCGKB)                      /* input */
{	   
    // open the output file to write
    ofstream outParserFile(strFileName.c_str(), ios::out );
    
    if ( !outParserFile ) {
	  cerr << "File could not be opened." << '\n';
          exit( 1 );
    }
	
	//std::cout << "test here "<< std::endl;
	outParserFile << "[KnowledageBase:" << '\n';	

    CTypeHierarchy* pCTypeHierarchy;
    pCTypeHierarchy = pCGKB->GetTypes(); // get type hierarchy    
    CTypeDefinition* pEntity = pCTypeHierarchy->FindByName("Entity");	
	if(pEntity != NULL) {
		outParserFile << "[TypeHierarchy:" << '\n';
		DumpTypeHierarchy(pEntity, outParserFile);
		outParserFile << "]" << '\n'; // end of type hierarchy
	}
    

     CRelationHierarchy* pCRelationHierarchy;
    pCRelationHierarchy = pCGKB->GetRelations(); // get relation hierarchy
	if(pCRelationHierarchy != NULL) {
		outParserFile << "[RelationHierarchy:" << '\n';
		CRelationDefinition* pRelation = pCRelationHierarchy->FindByName("Relation");
		DumpRelationHierarchy(pRelation, outParserFile);
		outParserFile << "]" << '\n'; // end of relation hierarchy
	}

    CCatalogOfIndividuals* pCCatalogOfIndividuals;
    pCCatalogOfIndividuals = pCGKB->GetCOI(); // get catalog of individuals
    if(pCCatalogOfIndividuals != NULL) {
		outParserFile << "[CatalogOfIndividuals:" << '\n';
		DumpCatalogOfIndivituals(pCCatalogOfIndividuals, outParserFile);
		outParserFile << "]" << '\n'; // end of CatalogOfIndivituals
	}


    CSheetOfAssertion* pCSheetOfAssertion;
    pCSheetOfAssertion = pCGKB->GetSOA(); // get sheets of assertion	


	outParserFile << "]" << '\n'; // end of KnowledgeBase
	
	outParserFile.close();
	return true;
}

