16 //====== LIFECYCLE ========
18 ReaderEnvironment<T>::ReaderEnvironment(std::string nameConfigurationFile,std::map<std::string,T>* things)
20 this->nameConfigurationFile=nameConfigurationFile;
22 this->instantHandler=NULL;
23 this->nameOtherConceptsFile = nameOtherConceptsFile;
24 this->environment= new SomeEnvironment<T>();
26 NUMCONCEPTS="numConcepts";
29 NUMOBJECTS="numObjects";
34 NUMINSTANTS="numInstants";
40 nameOtherConceptsFile = "";
43 //build the environment
47 ReaderEnvironment<T>::ReaderEnvironment(std::string nameConceptFile,std::string nameObjectFile,std::map<std::string,T>* things)
49 this->nameConceptFile=nameConceptFile;
50 this->nameObjectFile=nameObjectFile;
53 this->nameOtherConceptsFile = nameOtherConceptsFile;
54 this->environment= new SomeEnvironment<T>();
56 NUMCONCEPTS="numConcepts";
59 NUMOBJECTS="numObjects";
64 NUMINSTANTS="numInstants";
70 nameOtherConceptsFile = "";
72 //build the environment
77 ReaderEnvironment<T> :: ReaderEnvironment(std::string nameOtherConceptsFile, std::string nameConceptFile,std::string nameObjectFile, std::map<std::string,T>* things)
79 this->nameConceptFile=nameConceptFile;
80 this->nameObjectFile=nameObjectFile;
81 this->nameOtherConceptsFile = nameOtherConceptsFile;
83 this->environment= new SomeEnvironment<T>();
85 NUMCONCEPTS="numConcepts";
88 NUMOBJECTS="numObjects";
93 NUMINSTANTS="numInstants";
100 //build the environment
104 ReaderEnvironment<T>::~ReaderEnvironment()
108 if(things) delete this->things;
109 nameConceptFile.clear();
110 nameConfigurationFile.clear();
111 nameObjectFile.clear();
113 nameOtherConceptsFile.clear();
114 if(instantHandler) delete instantHandler;
118 //====== OPERATIONS =======
120 * @return environment builded
123 SomeEnvironment<T>* ReaderEnvironment<T>::getEnvironment()
125 return this->environment;
128 * build the environment
131 void ReaderEnvironment<T>::buildEnvironment()
135 if(!nameConfigurationFile.empty())
136 readFile(nameConfigurationFile);
139 readFile(nameConceptFile);
140 readFile(nameObjectFile);
141 if(!nameOtherConceptsFile.empty())
143 readFile(nameOtherConceptsFile);
146 //adding the data of the concepts to the environment
148 //setting the instant handler
149 this->instantHandler=new InstantHandler<T>(environment);
150 //adding the things to the environment
154 //======== PRIVATE METHODS ==============
157 * build the instant from the string readed in the file
158 * @param instant, string with the description of the instant
159 * @param nameObject, name of the object for wich we can add an instant or several instants
161 * TODO: when is an r(i j) and when the user doesnt write all the information
162 * of the instants it is considered like an all
163 * for example, there is 3 concepts, the data complete for an instant is of size 3
164 * but the user can write only an index of the first concept, the others is assumed
165 * to be all, is like instant= i, then it is assumed like instant= i all all
170 bool ReaderEnvironment<T>::addInstants(std::string nameObject,std::string instant)
173 int indexAll,numConcept,sizeConcept=-1,i,k=0;
175 //copying the string of the instant
176 std::string instantCopy=instant;
178 indexAll=instantCopy.find(ALL);
180 char* indexConcept=(char*)malloc(sizeof(char));
181 Instant* instantThing;
186 std::string nameConcept;
187 //setting the first instant of the handler, using all the all's defined in the string
190 //itoa(0,indexConcept,10); // itoa not ANSI
191 sprintf(indexConcept, "%d", 0);
192 instantCopy.replace(indexAll,ALL.size(),indexConcept);
193 numConcept=getNumConcept(instantCopy,indexAll);
194 getConceptName( nameConcept,numConcept);
195 //setting the instanHandler
196 instantHandler->addConceptToHandled(nameConcept,2,k);
199 indexAll=instantCopy.find(ALL);
204 instantThing=getInstant(instantCopy);
205 //adding to the environment
208 if(environment->getNumberOfConcepts()== instantThing->getSize())
210 environment->addInstantToThing(nameObject,instantThing);
211 //starting the actual instant
212 instantHandler->setActualInstant(instantThing);
214 //else THROW EXCEPTION
216 //else THROW AN EXCEPTION
219 //calculatig the number of instants
221 int numInstants=instantHandler->getNumOfInstants();
223 //calculating all the instants
224 for(i=1;i<numInstants;i++)
226 instantHandler->nextInstant();
227 instantThing=instantHandler->getActualInstant();
229 int sizeI=instantThing->getSize();
230 for(int j=0;j<sizeI;j++)
231 int q=instantThing->getIndexInConcept(j);
233 environment->addInstantToThing(nameObject,instantThing);
240 instantThing=getInstant(instantCopy);
243 environment->addInstantToThing(nameObject,instantThing);
247 //else throw an exception
255 * Returns the name of the concept in the concepts defined given the number
256 * of the concept in the instant
257 * @param nameConcept, string where is going to be load the name of the concept
258 * @param numConcept, number of the concept in the instant for which we are looking
262 void ReaderEnvironment<T>::getConceptName(std::string& nameConcept,int numConcept)
264 int ncs,conceptIndexInEnvironment,i;
266 //getting the names of the concept defined
267 std::vector<std::string> nameConcepts;
268 environment->getConceptsNames(nameConcepts);
269 //name of the concept i
270 std::string nameConcepti;
271 ncs=nameConcepts.size();
272 bool isConcept=false;
273 for(i=0;i< ncs && !isConcept;i++)
275 nameConcepti=nameConcepts[i];
276 conceptIndexInEnvironment=environment->getIndexConcept(nameConcepti);
277 if(conceptIndexInEnvironment==numConcept)
280 nameConcepti.clear();
283 nameConcept=nameConcepti;
284 nameConcepti.clear();
288 * Returns the number of the concept in the instant string read
289 * @param instant,instant where we are going to search the number of the
290 * concept that is in the position given
291 * @param position, position of the concept (string position) for which we want the number in the instant given
292 * @return the number of concept in the instant
295 int ReaderEnvironment<T>::getNumConcept(std::string& instant,int position )
297 int splitIndex,counter=0;
298 splitIndex=instant.rfind(" ",position);
303 splitIndex=instant.rfind(" ",splitIndex-1);
311 * Returns the instant object of the instant string given
312 * 1 1 1 1 and returns an instant object (1,1,1,1)
313 * @param instant, instant for which we want to construct an Instant object
314 * @return an Instant's object
315 * TODO validation of indexes in instant
318 Instant* ReaderEnvironment<T>:: getInstant(std::string instant)
320 int value,splitIndex;
321 //getting the names of the concept defined
322 std::vector<std::string> nameConcepts;
323 environment->getConceptsNames(nameConcepts);
325 std::vector<int>* instantVector=new std::vector<int>();
328 splitIndex=instant.find(" ");
332 while(!stop && isValid)
337 concept=instant.substr(0,splitIndex);
338 instant.erase(0,splitIndex+1);
339 value=atoi(concept.c_str());
340 isValid=environment->isValidIndex(value,k);
342 instantVector->push_back(value);
343 splitIndex=instant.find(" ");
346 Instant* instantThing;
349 instantThing= new Instant(instantVector);
350 delete instantVector;
358 * add the instants defined in the file for the name of the
360 * @param nameObject, name of the object for which we want to search
364 bool ReaderEnvironment<T>::addInstantToThing(std::string nameObject,std::string index)
367 std::map<std::string,std::string>::iterator iteratorData;
370 //getting the number of instants for the object given
372 std::string s(OBJECT + index + POINT + NUMINSTANTS);
373 iteratorData=dataInFile.find(s);
375 if(iteratorData!=dataInFile.end())
377 std::string numIns=iteratorData->second;
378 numInstants=atoi(numIns.c_str());
381 //else throw exception
382 //getting the instants
386 //finding the number of decimals places
387 int num=getNumberOfDecimalPlaces(numInstants);
388 char* indexi=(char*)malloc(sizeof(char)*(num+1));
389 //std::string indexiStr;
390 for(i=1;i<=numInstants && added;i++)
393 sprintf(indexi, "%d", i);
395 s=OBJECT+index+POINT+INSTANT+indexi;
396 iteratorData=dataInFile.find(s);
397 if(iteratorData!=dataInFile.end())
399 instant=iteratorData->second;
400 addInstants(nameObject,instant);
415 * adds the things to the environment
418 bool ReaderEnvironment<T>::addThings()
420 int numObjects=0,j=0;
421 bool addedInstants=true;
422 typename std::map<std::string,std::string>::iterator iteratorData;
423 typename std::map<std::string,T>::iterator iteratorThings;
424 //getting the number of objects
425 iteratorData=dataInFile.find(NUMOBJECTS);
426 if(iteratorData!=dataInFile.end())
428 std::string numObj=iteratorData->second;
429 numObjects=atoi(numObj.c_str());
432 //finding the number of decimals places
433 int num=getNumberOfDecimalPlaces(numObjects);
434 //getting the objects if does come like objects
438 char* index= (char*)malloc(sizeof(char)*(num+1));
439 std::string objectData;
440 std::string nameObjecti;
441 for(j=1;j<=numObjects && addedInstants;j++)
444 int num2=sprintf(index, "%d", j);
445 objectData=OBJECT+index+POINT+NAME;
446 iteratorData=dataInFile.find(objectData);
447 if(iteratorData!=dataInFile.end())
449 nameObjecti=iteratorData->second;
450 iteratorThings=things->find(nameObjecti);
451 if(iteratorThings!=things->end())
453 environment->addThing(nameObjecti,iteratorThings->second);
455 addedInstants=addInstantToThing(nameObjecti,index);
458 //the names given en execution and of the file should be the same
472 //if we have to charge it from a file
473 //if is like that we should ask for the objecti.path
482 * adds the concepts of the file to the environment
485 bool ReaderEnvironment<T>::addConcepts()
487 int numConcepts=0,i=0;
488 std::map<std::string,std::string>::iterator iteratorData;
489 //getting the number of concepts defined
490 iteratorData=dataInFile.find(NUMCONCEPTS);
491 if(iteratorData!=dataInFile.end())
493 std::string numberOfConcepts=iteratorData->second;
494 numConcepts=atoi(numberOfConcepts.c_str());
496 //finding the number of decimals places
497 int num=getNumberOfDecimalPlaces(numConcepts);
498 std::string conceptData;
499 char* index=(char*)malloc(sizeof(char)*(num+1));
500 std::string nameConcepti;
501 std::string sizeConcepti;
502 for(i=1;i<=numConcepts;i++)
506 sprintf(index, "%d", i);
508 conceptData=CONCEPT+index+POINT+NAME;
509 iteratorData=dataInFile.find(conceptData);
510 if(iteratorData!=dataInFile.end())
511 nameConcepti=iteratorData->second;
514 conceptData=CONCEPT+index+POINT+SIZE;
515 iteratorData=dataInFile.find(conceptData);
516 if(iteratorData!=dataInFile.end())
517 sizeConcepti=iteratorData->second;
519 environment->addConcept(nameConcepti,atoi(sizeConcepti.c_str()));
521 nameConcepti.clear();
522 sizeConcepti.clear();
535 * read the configuration file
538 void ReaderEnvironment<T>::readFile(std::string nameFile)
540 bool readingOtherConcepts = nameFile.compare(this->nameOtherConceptsFile) == 0;
541 bool readingConcepts = nameFile.compare(this->nameConceptFile) == 0 || readingOtherConcepts;
542 bool findedConceptOcurrence = false;
545 //std::string conceptKeyPart = "";
548 std::ifstream configFile;
549 //each line of the file
551 //string of the data to save
553 //string with the key of the map
559 if(nameFile.compare("")!=0)
561 configFile.open(nameFile.c_str());
562 if(!configFile.is_open())
564 //should throw an exception
569 //THIS ONLY SAVES NUMBERS OF ONE DIGIT
571 char* index= (char*)malloc(sizeof(char)*5);
572 char* dataString= (char*)malloc(sizeof(char)*10);
573 while(!configFile.eof())
576 std::getline(configFile,line);
578 pos=line.find(EQUAL);
579 lineSize=line.size();
580 //if is not a comment
582 int c=line.find(COMMENT);
583 if (c !=std::string::npos )
584 findedConceptOcurrence = false;
586 else//if(c<0 && pos>=0)
588 data=line.substr(pos+1,lineSize);
589 key=line.substr(0,pos);
590 if ( readingConcepts && (key.rfind( CONCEPT ))!=std::string::npos )
593 if( (key.rfind( NAME ))!=std::string::npos )
595 if ( !findedConceptOcurrence )
598 findedConceptOcurrence = true;
599 //itoa(numberOfConcepts,index,10);
600 sprintf (index, "%d", numberOfConcepts);
602 if ( readingOtherConcepts )
603 key = CONCEPT + index + POINT + NAME;
605 else if ( (key.rfind( SIZE ))!=std::string::npos )
607 if ( !findedConceptOcurrence)
610 findedConceptOcurrence = true;
611 // itoa(numberOfConcepts,index,10);
612 sprintf(index, "%d", numberOfConcepts);
614 if ( readingOtherConcepts )
615 key = CONCEPT + index + POINT + SIZE;
621 int antes = dataInFile.size();
624 if( readingOtherConcepts && key.rfind(NUMCONCEPTS)!=std::string::npos )
626 int otherConceptsValue = atoi(data.c_str());
627 //itoa(numberOfConcepts+otherConceptsValue,dataString,10);
628 sprintf(dataString,"%d", numberOfConcepts+otherConceptsValue);
630 std::map<std::string,std::string>::iterator iter = dataInFile.find(key);
631 dataInFile.erase(iter);
633 dataInFile.insert(std::pair<std::string,std::string>(key,data));
635 int despues = dataInFile.size();
636 //cleaning the data and the key variables
650 * Returns the number of decimals places of the number given
653 int ReaderEnvironment<T>::getNumberOfDecimalPlaces(int number)
667 * Clean the line readed from the file
670 void ReaderEnvironment<T>::cleanLine(std::string & line)
673 pos1=line.find_first_not_of(" ");
675 pos2=line.find_last_not_of(" ");
677 line.erase(pos2+1,size-1);