Beispiel #1
0
void KnowledgeBase::buildFullConstantMap(){
    constantToRelationMap.clear();
    for(LRelation relation : relationList){
        LTerm head = relation->getHead();
        if(!constantToRelationMap.contains(head)){
            constantToRelationMap.insert(head, QList<LRelation>());
        }
        constantToRelationMap[head].append(relation);
    }
    
    constantToRuleMap.clear();
    for(LRule rule : ruleList){
        LTerm head = rule->getHead()->getHead();
        if(!constantToRuleMap.contains(head)){
            constantToRuleMap.insert(head, QList<LRule>());
        }
        constantToRuleMap[head].append(rule);
    }

#ifndef QT_NO_DEBUG
    qDebug() << "";
    qDebug() << "How many rules/relations for a specific Object Constant : ";
    int padSpaceSize = 16;
    for(LTerm head : constantToRelationMap.keys()){
        qDebug() << "Relation " << padSpace(head->toString(), padSpaceSize) << "has nb entries : " << constantToRelationMap[head].size();
    }

    for(LTerm head : constantToRuleMap.keys()){
        qDebug() << "Rule     " << padSpace(head->toString(), padSpaceSize) << "has nb entries : " << constantToRuleMap[head].size();
    }
#endif
}
Beispiel #2
0
/**
 * @brief KnowledgeBase::storeConstants
 * We have :
 *  - object constants
 *  - relation constants
 *  - function constants
 * Let's store all of them in memory
 */
void KnowledgeBase::storeConstants(){
    //    qDebug() << "Store constants";
    constantMap.clear();
    objectConstantSet.clear();
    functionConstantSet.clear();
    relationConstantSet.clear();
    
    QList<LRelation> clonedRelationList;
    for(LRelation relation : relationList){
        clonedRelationList.append(manageRelation(relation));
    }
    relationList = clonedRelationList;
    
    QList<LRule> clonedRuleList;
    for(LRule rule : ruleList){
        clonedRuleList.append(manageRule(rule));
    }
    ruleList = clonedRuleList;
    
#ifndef QT_NO_DEBUG
    qDebug() << "";
    qDebug() << "Ensuring all the Object Constants with the same name share the same address";

    QMap<QString, QString> mapTermToPointer;

    int padSpaceSize = 16;
    for(LRelation relation : relationList){
        LTerm head = relation->getHead();


        if(!mapTermToPointer.contains(head->toString())){
            mapTermToPointer.insert(head->toString(), QString("0x%1").arg((quintptr)head.data(), QT_POINTER_SIZE * 2, 16, QChar('0')));
            qDebug() << "Relation constant " << padSpace(head->toString(), padSpaceSize)  << "address " << head.data();
        }
        Q_ASSERT(mapTermToPointer[head->toString()] == QString("0x%1").arg((quintptr)head.data(), QT_POINTER_SIZE * 2, 16, QChar('0')));
    }
    for(LRule rule : ruleList){
        LTerm head = rule->getHead()->getHead();

        if(!mapTermToPointer.contains(head->toString())){
            mapTermToPointer.insert(head->toString(), QString("0x%1").arg((quintptr)head.data(), QT_POINTER_SIZE * 2, 16, QChar('0')));
            qDebug() << "Relation constant " << padSpace(head->toString(), padSpaceSize) << "address " << head.data();
        }
        Q_ASSERT(mapTermToPointer[head->toString()] == QString("0x%1").arg((quintptr)head.data(), QT_POINTER_SIZE * 2, 16, QChar('0')));

        for(LRelation relation : rule->getBody()){
            LTerm head = relation->getHead();

            if(!mapTermToPointer.contains(head->toString())){
                mapTermToPointer.insert(head->toString(), QString("0x%1").arg((quintptr)head.data(), QT_POINTER_SIZE * 2, 16, QChar('0')));
                qDebug() << "Relation constant " << padSpace(head->toString(), padSpaceSize) << "address " << head.data();
            }
            Q_ASSERT(mapTermToPointer[head->toString()] == QString("0x%1").arg((quintptr)head.data(), QT_POINTER_SIZE * 2, 16, QChar('0')));
        }
    }
#endif   
}
Beispiel #3
0
void KnowledgeBase::printConstantsWithArity(){
#ifndef QT_NO_DEBUG
    int padSpaceSize = 16;
    qDebug() << "\n\nLIST OF CONSTANTS";
    for(LTerm constant : objectConstantSet){
        qDebug() << "Object constant : " << padSpace(constant->toString(), padSpaceSize) << " with address : " << constant.data();
    }
    for(LTerm constant : relationConstantSet){
        qDebug() << "Relation constant : " << padSpace(constant->toString(), padSpaceSize) << " with address : " << constant.data()<< "\tand arity " << arity[constant];
    }
    for(LTerm constant : functionConstantSet){
        qDebug() << "Function constant : " << padSpace(constant->toString(), padSpaceSize) << " with address : " << constant.data()<< "\tand arity " << arity[constant];
    }
#endif
}
    void buildLine(string &tmp, int spaceCount, int L)
    {
         if(spaceCount == 0)
        {
            padSpace(tmp, L);
            return;
        }
        int extraSpaceCount = L - tmp.size();
        int unEvenlySpace = extraSpaceCount % spaceCount;
        int basicSpace = extraSpaceCount / spaceCount;
        string sp(basicSpace, ' ');
        size_t found = tmp.find(' ');
        while(found != string::npos)
        {
            tmp.insert(found, sp);
            found = tmp.find(' ', found + sp.size() + 1);
        }
        found = 0;
        while(unEvenlySpace > 0)
        {
            found = tmp.find_first_of(' ', found+1);
            tmp.insert(found, 1, ' ');
            unEvenlySpace--;
            found = tmp.find_first_not_of(' ', found+1);
        }

    }
 void helper(vector<string> &words, int start, int L, vector<string> &result)
 {
     int spaceCount = 0;
     string tmp = "";
     tmp += words[0];
     for(int i = 0; i<words.size() - 1; ++i)
     {
         if(tmp.size() + words[i+1].size() >= L)
         {
             buildLine(tmp, spaceCount, L);
             result.push_back(tmp);
             tmp = words[i+1];
             spaceCount = 0;
             continue;
         }
         else
         {
             tmp = tmp + " " + words[i+1];
             spaceCount++;
         }
     }
     padSpace(tmp, L);
     result.push_back(tmp);
 }
Beispiel #6
0
/*	Function: gpsMerge
	Pre-Conditions: An opened and filled in GpFile structure and the name of the merging file taken from the command line
	Post-Conditions: A modified GpFile which includes all the information from the merging file
	Errors Returned:EXIT_FAILURE
	Description: This function takes all the information from the merging file and merges it with the first GpFile structure. 
		     The problem of duplicate waypoints, different ID and symbol lengths and date/timezone differences are addressed
*/
int gpsMerge( GpFile *filep, const char *const fnameB ){

	GpFile* fileA = filep;
	GpFile* fileB;
	GpStatus fileStatus;
	FILE* inputfileB;
	int maxIDLen=0;
	int maxSymLen=0;
	int i=0;
	int j=0;
	int oldnwaypts=0;
	int maxRouteno=0;
	int convTZ=0;
	double convDist=0.0;
	double convTime=0.0;
	char* thisStr=NULL;
	char* IDbuffer=NULL;
	char* Symbuffer=NULL;
	char copies;

	fileB = malloc(sizeof(GpFile));
	assert(fileB);

	inputfileB = fopen(fnameB, "r");
	if(inputfileB == NULL) {
		fprintf(stderr, "Fatal Error: Cannot open file B for reading\n");
		return EXIT_FAILURE;
	}

	fileStatus =  readGpFile(inputfileB, fileB);

	if(fileStatus.code != OK) {
		fprintf(stderr, "Fatal Error: readGpFile returns error %d on line %d for file B\n", fileStatus.code, fileStatus.lineno);
		return EXIT_FAILURE;
	}

	if(fileA->nwaypts>0 && fileB->nwaypts>0) {
		fileA->waypt = realloc(fileA->waypt, sizeof(GpWaypt) * (fileA->nwaypts + fileB->nwaypts));
		maxIDLen = maxLength(fileA->waypt[0].ID, fileB->waypt[0].ID);
		maxSymLen = maxLength(fileA->waypt[0].symbol, fileB->waypt[0].symbol);
		IDbuffer = malloc(sizeof(char) * maxIDLen);
		Symbuffer = malloc(sizeof(char) * maxSymLen);

		for(i=0; i<fileB->nwaypts; i++) {
			fileA->waypt[(fileA->nwaypts) + i].textChoice = fileA->waypt[0].textChoice;
			fileA->waypt[(fileA->nwaypts) + i].textPlace = fileA->waypt[0].textPlace;
			fileA->waypt[(fileA->nwaypts) + i].ID = malloc(sizeof(char) * (strlen(fileB->waypt[i].ID)+1));
			assert(fileA->waypt[(fileA->nwaypts) + i].ID);
			strcpy(fileA->waypt[(fileA->nwaypts) + i].ID, fileB->waypt[i].ID);\
			fileA->waypt[(fileA->nwaypts) + i].coord.lat = fileB->waypt[i].coord.lat;
			fileA->waypt[(fileA->nwaypts) + i].coord.lon = fileB->waypt[i].coord.lon;
			fileA->waypt[(fileA->nwaypts) + i].symbol = malloc(sizeof(char) * (strlen(fileB->waypt[i].symbol)+1));
			assert(fileA->waypt[(fileA->nwaypts) + i].symbol);
			strcpy(fileA->waypt[(fileA->nwaypts) + i].symbol, fileB->waypt[i].symbol);
			fileA->waypt[(fileA->nwaypts) + i].comment = malloc(sizeof(char) * (strlen(fileB->waypt[i].comment)+1));
			assert(fileA->waypt[(fileA->nwaypts) + i].comment);
			strcpy(fileA->waypt[(fileA->nwaypts) + i].comment, fileB->waypt[i].comment);
		}
		oldnwaypts = fileA->nwaypts;
		fileA->nwaypts += fileB->nwaypts;

		for(i=0; i<fileA->nwaypts; i++) {
			fileA->waypt[i].ID = padSpace(fileA->waypt[i].ID, maxIDLen);
			fileA->waypt[i].symbol = padSpace(fileA->waypt[i].symbol, maxSymLen);
		}
	}

	/*Duplicates file B's elements into file A*/
	if(fileA->nwaypts==0 && fileB->nwaypts>0) {
		fileA->waypt = realloc(fileA->waypt, sizeof(GpWaypt) * (fileB->nwaypts));

		for(i=0; i<fileB->nwaypts; i++) {
			fileA->waypt[i].ID = strdup(fileB->waypt[i].ID);
			assert(fileA->waypt[i].ID);
			fileA->waypt[i].coord.lat = fileB->waypt[i].coord.lat;
			fileA->waypt[i].coord.lon = fileB->waypt[i].coord.lon;
			fileA->waypt[i].textChoice = fileB->waypt[0].textChoice;
			fileA->waypt[i].textPlace = fileB->waypt[0].textPlace;
			fileA->waypt[i].symbol = strdup(fileB->waypt[i].symbol);
			assert(fileA->waypt[i].symbol);	
			fileA->waypt[i].comment = strdup(fileB->waypt[i].comment);
			assert(fileA->waypt[i].comment);
		}
	}

	thisStr = malloc(sizeof(char) * maxIDLen+1);

	/*Checking through the new filepointer to find duplicate waypoints*/
	for(i=0; i<fileA->nwaypts; i++) {
		copies = '0';
		strcpy(thisStr, fileA->waypt[i].ID);
		for(j=i+1; j<fileA->nwaypts; j++) {
			/*If there is a match, it will append a 0 at the last character*/
			if(strcmp(fileA->waypt[j].ID, thisStr) == 0) {
				fileA->waypt[j].ID[maxIDLen-1] = copies;
				copies++;
			}
		}
	}

	for(i=0; i<fileA->nroutes; i++) {
		if(fileA->route[i]->number > maxRouteno) {
			maxRouteno = fileA->route[i]->number;
		}
	}

	maxRouteno = maxRouteno / 100;
	maxRouteno += 1;
	maxRouteno = maxRouteno*100;
	maxRouteno += 1;

	if(fileA->nroutes>0 && fileB->nroutes>0) {
		fileA->route = realloc(fileA->route, sizeof(GpRoute*) * (fileA->nroutes + fileB->nroutes));

		for(i=0; i<fileB->nroutes; i++) {
			fileA->route[fileA->nroutes + i] = malloc(sizeof(GpRoute) + (sizeof(int) * fileB->route[i]->npoints));
			fileA->route[fileA->nroutes + i]->number = maxRouteno+i;
			fileA->route[fileA->nroutes + i]->comment = malloc(sizeof(char) * (strlen(fileB->route[i]->comment)+1));
			assert(fileA->route[fileA->nroutes + i]->comment);
			strcpy(fileA->route[fileA->nroutes + i]->comment, fileB->route[i]->comment);
			fileA->route[fileA->nroutes + i]->npoints = fileB->route[i]->npoints;
			for(j=0; j<fileB->route[i]->npoints; j++) {
				fileA->route[fileA->nroutes + i]->leg[j] = fileB->route[i]->leg[j] + fileA->nroutes;
			}

		}
	}

	/*Duplicates file B's elements into file A*/
	if(fileA->nroutes==0 && fileB->nroutes>0) {
		fileA->route = realloc(fileA->route, sizeof(GpRoute*) * (fileB->nroutes));
		for(i=0; i<fileB->nroutes; i++) {
			fileA->route[i] = malloc(sizeof(GpRoute) + (sizeof(int) * fileB->route[i]->npoints));
			fileA->route[i]->number = fileB->route[i]->number;
			fileA->route[i]->comment = malloc(sizeof(char) * (strlen(fileB->route[i]->comment)+1));
			assert(fileA->route[i]->comment);
			strcpy(fileA->route[i]->comment, fileB->route[i]->comment);
			fileA->route[i]->npoints = fileB->route[i]->npoints;
			for(j=0; j<fileB->route[i]->npoints; j++) {
				fileA->route[i]->leg[j] = fileB->route[i]->leg[j];
			}
		}
	}
	fileA->nroutes += fileB->nroutes;
	
	if(fileA->ntrkpts>0 && fileB->ntrkpts>0) {
		fileA->trkpt = realloc(fileA->trkpt, sizeof(GpTrkpt) * (fileA->ntrkpts + fileB->ntrkpts));
		convDist = convertDist(fileB->unitHorz, fileA->unitHorz);
		convTime = convertTime(fileA->unitTime, fileB->unitTime);
		convTZ = fileA->timeZone - fileB->timeZone;
		for(i=0; i<fileB->ntrkpts; i++) {
			fileA->trkpt[fileA->ntrkpts + i].coord.lat = fileB->trkpt[i].coord.lat;
			fileA->trkpt[fileA->ntrkpts + i].coord.lon = fileB->trkpt[i].coord.lon;
			fileA->trkpt[fileA->ntrkpts + i].dateTime = fileB->trkpt[i].dateTime + convTZ*3600;	//The datetime needs to be modifed by the timezone difference
			fileA->trkpt[fileA->ntrkpts + i].segFlag = fileB->trkpt[i].segFlag;
			if (fileA->trkpt[fileA->ntrkpts + i].segFlag) {
				fileA->trkpt[fileA->ntrkpts + i].comment = malloc(sizeof(char) * strlen(fileB->trkpt[i].comment)+1);
				assert(fileA->trkpt[fileA->ntrkpts + i].comment);
				strcpy(fileA->trkpt[fileA->ntrkpts + i].comment, fileB->trkpt[i].comment);
			}
 		fileA->trkpt[fileA->ntrkpts + i].speed = ((fileB->trkpt[i].speed * convDist)/convTime);	//speed needs to be converted to file A's specified units
 		fileA->trkpt[fileA->ntrkpts + i].dist = (fileB->trkpt[i].dist / convDist);	//Distance needs to be converted to file A's specified units
 		fileA->trkpt[fileA->ntrkpts + i].duration = (fileB->trkpt[i].duration);
		}

	}
	/*Duplicates file B's elements into file A*/
	else if(fileA->ntrkpts==0 && fileB->ntrkpts>0) {
		for(i=0; i<fileB->ntrkpts; i++) {
			fileA->trkpt = realloc(fileA->trkpt, sizeof(GpTrkpt) * (fileB->ntrkpts));
			fileA->trkpt[i].coord.lat = fileB->trkpt[i].coord.lat;
			fileA->trkpt[i].coord.lon = fileA->trkpt[i].coord.lon;
			fileA->trkpt[i].coord.lon = fileA->trkpt[i].coord.lon;
			fileA->trkpt[i].segFlag = fileB->trkpt[i].segFlag;
			fileA->trkpt[i].comment = malloc(sizeof(char) * strlen(fileB->trkpt[i].comment));
			assert(fileA->trkpt[i].comment);
			strcpy(fileA->trkpt[i].comment, fileB->trkpt[i].comment);
			fileA->trkpt[i].speed = (fileB->trkpt[i].speed);
			fileA->trkpt[i].dist = (fileB->trkpt[i].dist);
			fileA->trkpt[i].duration = (fileB->trkpt[i].duration);
		}
	}

	fileA->ntrkpts += fileB->ntrkpts;

	
	if(fileB) {
		freeGpFile(fileB);
		free(fileB);
	}
	
	if(inputfileB) fclose(inputfileB);
	
	if(IDbuffer) free(IDbuffer);
	
	if(Symbuffer) free(Symbuffer);

	return EXIT_SUCCESS;
	
}