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;
52 this->nameOtherConceptsFile = nameOtherConceptsFile;
53 this->environment= new SomeEnvironment<T>();
55 NUMCONCEPTS="numConcepts";
58 NUMOBJECTS="numObjects";
63 NUMINSTANTS="numInstants";
69 nameOtherConceptsFile = "";
71 //build the environment
76 ReaderEnvironment<T> :: ReaderEnvironment(std::string nameOtherConceptsFile, std::string nameConceptFile,std::string nameObjectFile, std::map<std::string,T>* things)
78 this->nameConceptFile=nameConceptFile;
79 this->nameObjectFile=nameObjectFile;
80 this->nameOtherConceptsFile = nameOtherConceptsFile;
82 this->environment= new SomeEnvironment<T>();
84 NUMCONCEPTS="numConcepts";
87 NUMOBJECTS="numObjects";
92 NUMINSTANTS="numInstants";
99 //build the environment
103 ReaderEnvironment<T>::~ReaderEnvironment()
107 if(things) delete this->things;
108 nameConceptFile.clear();
109 nameConfigurationFile.clear();
110 nameObjectFile.clear();
112 nameOtherConceptsFile.clear();
113 if(instantHandler) delete instantHandler;
117 //====== OPERATIONS =======
119 * @return environment builded
122 SomeEnvironment<T>* ReaderEnvironment<T>::getEnvironment()
124 return this->environment;
127 * build the environment
130 void ReaderEnvironment<T>::buildEnvironment()
134 if(!nameConfigurationFile.empty())
135 readFile(nameConfigurationFile);
138 readFile(nameConceptFile);
139 readFile(nameObjectFile);
140 if(!nameOtherConceptsFile.empty())
142 readFile(nameOtherConceptsFile);
145 //adding the data of the concepts to the environment
147 //setting the instant handler
148 this->instantHandler=new InstantHandler<T>(environment);
149 //adding the things to the environment
153 //======== PRIVATE METHODS ==============
156 * build the instant from the string readed in the file
157 * @param instant, string with the description of the instant
158 * @param nameObject, name of the object for wich we can add an instant or several instants
160 * TODO: when is an r(i j) and when the user doesnt write all the information
161 * of the instants it is considered like an all
162 * for example, there is 3 concepts, the data complete for an instant is of size 3
163 * but the user can write only an index of the first concept, the others is assumed
164 * to be all, is like instant= i, then it is assumed like instant= i all all
169 bool ReaderEnvironment<T>::addInstants(std::string nameObject,std::string instant)
172 int indexAll,numConcept,sizeConcept=-1,i,k=0;
174 //copying the string of the instant
175 std::string instantCopy=instant;
177 indexAll=instantCopy.find(ALL);
179 char* indexConcept=(char*)malloc(sizeof(char));
180 Instant* instantThing;
185 std::string nameConcept;
186 //setting the first instant of the handler, using all the all's defined in the string
189 //itoa(0,indexConcept,10); // itoa not ANSI
190 sprintf(indexConcept, "%d", 0);
191 instantCopy.replace(indexAll,ALL.size(),indexConcept);
192 numConcept=getNumConcept(instantCopy,indexAll);
193 getConceptName( nameConcept,numConcept);
194 //setting the instanHandler
195 instantHandler->addConceptToHandled(nameConcept,2,k);
198 indexAll=instantCopy.find(ALL);
203 instantThing=getInstant(instantCopy);
204 //adding to the environment
207 if(environment->getNumberOfConcepts()== instantThing->getSize())
209 environment->addInstantToThing(nameObject,instantThing);
210 //starting the actual instant
211 instantHandler->setActualInstant(instantThing);
213 //else THROW EXCEPTION
215 //else THROW AN EXCEPTION
218 //calculatig the number of instants
220 int numInstants=instantHandler->getNumOfInstants();
222 //calculating all the instants
223 for(i=1;i<numInstants;i++)
225 instantHandler->nextInstant();
226 instantThing=instantHandler->getActualInstant();
228 int sizeI=instantThing->getSize();
229 for(int j=0;j<sizeI;j++)
230 int q=instantThing->getIndexInConcept(j);
232 environment->addInstantToThing(nameObject,instantThing);
239 instantThing=getInstant(instantCopy);
242 environment->addInstantToThing(nameObject,instantThing);
246 //else throw an exception
254 * Returns the name of the concept in the concepts defined given the number
255 * of the concept in the instant
256 * @param nameConcept, string where is going to be load the name of the concept
257 * @param numConcept, number of the concept in the instant for which we are looking
261 void ReaderEnvironment<T>::getConceptName(std::string& nameConcept,int numConcept)
263 int ncs,conceptIndexInEnvironment,i;
265 //getting the names of the concept defined
266 std::vector<std::string> nameConcepts;
267 environment->getConceptsNames(nameConcepts);
268 //name of the concept i
269 std::string nameConcepti;
270 ncs=nameConcepts.size();
271 bool isConcept=false;
272 for(i=0;i< ncs && !isConcept;i++)
274 nameConcepti=nameConcepts[i];
275 conceptIndexInEnvironment=environment->getIndexConcept(nameConcepti);
276 if(conceptIndexInEnvironment==numConcept)
279 nameConcepti.clear();
282 nameConcept=nameConcepti;
283 nameConcepti.clear();
287 * Returns the number of the concept in the instant string read
288 * @param instant,instant where we are going to search the number of the
289 * concept that is in the position given
290 * @param position, position of the concept (string position) for which we want the number in the instant given
291 * @return the number of concept in the instant
294 int ReaderEnvironment<T>::getNumConcept(std::string& instant,int position )
296 int splitIndex,counter=0;
297 splitIndex=instant.rfind(" ",position);
302 splitIndex=instant.rfind(" ",splitIndex-1);
310 * Returns the instant object of the instant string given
311 * 1 1 1 1 and returns an instant object (1,1,1,1)
312 * @param instant, instant for which we want to construct an Instant object
313 * @return an Instant's object
314 * TODO validation of indexes in instant
317 Instant* ReaderEnvironment<T>:: getInstant(std::string instant)
319 int value,splitIndex;
320 //getting the names of the concept defined
321 std::vector<std::string> nameConcepts;
322 environment->getConceptsNames(nameConcepts);
324 std::vector<int>* instantVector=new std::vector<int>();
327 splitIndex=instant.find(" ");
331 while(!stop && isValid)
336 concept=instant.substr(0,splitIndex);
337 instant.erase(0,splitIndex+1);
338 value=atoi(concept.c_str());
339 isValid=environment->isValidIndex(value,k);
341 instantVector->push_back(value);
342 splitIndex=instant.find(" ");
345 Instant* instantThing;
348 instantThing= new Instant(instantVector);
349 delete instantVector;
357 * add the instants defined in the file for the name of the
359 * @param nameObject, name of the object for which we want to search
363 bool ReaderEnvironment<T>::addInstantToThing(std::string nameObject,std::string index)
366 std::map<std::string,std::string>::iterator iteratorData;
369 //getting the number of instants for the object given
371 std::string s(OBJECT + index + POINT + NUMINSTANTS);
372 iteratorData=dataInFile.find(s);
374 if(iteratorData!=dataInFile.end())
376 std::string numIns=iteratorData->second;
377 numInstants=atoi(numIns.c_str());
380 //else throw exception
381 //getting the instants
385 //finding the number of decimals places
386 int num=getNumberOfDecimalPlaces(numInstants);
387 char* indexi=(char*)malloc(sizeof(char)*(num+1));
388 //std::string indexiStr;
389 for(i=1;i<=numInstants && added;i++)
392 sprintf(indexi, "%d", i);
394 s=OBJECT+index+POINT+INSTANT+indexi;
395 iteratorData=dataInFile.find(s);
396 if(iteratorData!=dataInFile.end())
398 instant=iteratorData->second;
399 addInstants(nameObject,instant);
414 * adds the things to the environment
417 bool ReaderEnvironment<T>::addThings()
420 bool addedInstants=true;
421 typename std::map<std::string,std::string>::iterator iteratorData;
422 typename std::map<std::string,T>::iterator iteratorThings;
423 //getting the number of objects
424 iteratorData=dataInFile.find(NUMOBJECTS);
425 if(iteratorData!=dataInFile.end())
427 std::string numObj=iteratorData->second;
428 numObjects=atoi(numObj.c_str());
431 //finding the number of decimals places
432 int num=getNumberOfDecimalPlaces(numObjects);
433 //getting the objects if does come like objects
437 char* index= (char*)malloc(sizeof(char)*(num+1));
438 std::string objectData;
439 std::string nameObjecti;
440 for(j=1;j<=numObjects && addedInstants;j++)
443 int num2=sprintf(index, "%d", j);
444 objectData=OBJECT+index+POINT+NAME;
445 iteratorData=dataInFile.find(objectData);
446 if(iteratorData!=dataInFile.end())
448 nameObjecti=iteratorData->second;
449 iteratorThings=things->find(nameObjecti);
450 if(iteratorThings!=things->end())
452 environment->addThing(nameObjecti,iteratorThings->second);
454 addedInstants=addInstantToThing(nameObjecti,index);
457 //the names given en execution and of the file should be the same
471 //if we have to charge it from a file
472 //if is like that we should ask for the objecti.path
481 * adds the concepts of the file to the environment
484 bool ReaderEnvironment<T>::addConcepts()
487 std::map<std::string,std::string>::iterator iteratorData;
488 //getting the number of concepts defined
489 iteratorData=dataInFile.find(NUMCONCEPTS);
490 if(iteratorData!=dataInFile.end())
492 std::string numberOfConcepts=iteratorData->second;
493 numConcepts=atoi(numberOfConcepts.c_str());
495 //finding the number of decimals places
496 int num=getNumberOfDecimalPlaces(numConcepts);
497 std::string conceptData;
498 char* index=(char*)malloc(sizeof(char)*(num+1));
499 std::string nameConcepti;
500 std::string sizeConcepti;
501 for(i=1;i<=numConcepts;i++)
505 sprintf(index, "%d", i);
507 conceptData=CONCEPT+index+POINT+NAME;
508 iteratorData=dataInFile.find(conceptData);
509 if(iteratorData!=dataInFile.end())
510 nameConcepti=iteratorData->second;
513 conceptData=CONCEPT+index+POINT+SIZE;
514 iteratorData=dataInFile.find(conceptData);
515 if(iteratorData!=dataInFile.end())
516 sizeConcepti=iteratorData->second;
518 environment->addConcept(nameConcepti,atoi(sizeConcepti.c_str()));
520 nameConcepti.clear();
521 sizeConcepti.clear();
534 * read the configuration file
537 void ReaderEnvironment<T>::readFile(std::string nameFile)
539 bool readingOtherConcepts = nameFile.compare(this->nameOtherConceptsFile) == 0;
540 bool readingConcepts = nameFile.compare(this->nameConceptFile) == 0 || readingOtherConcepts;
541 bool findedConceptOcurrence = false;
544 //std::string conceptKeyPart = "";
547 std::ifstream configFile;
548 //each line of the file
550 //string of the data to save
552 //string with the key of the map
558 if(nameFile.compare("")!=0)
560 configFile.open(nameFile.c_str());
561 if(!configFile.is_open())
563 //should throw an exception
568 //THIS ONLY SAVES NUMBERS OF ONE DIGIT
570 char* index= (char*)malloc(sizeof(char)*5);
571 char* dataString= (char*)malloc(sizeof(char)*10);
572 while(!configFile.eof())
575 std::getline(configFile,line);
577 pos=line.find(EQUAL);
578 lineSize=line.size();
579 //if is not a comment
581 int c=line.find(COMMENT);
582 if (c !=std::string::npos )
583 findedConceptOcurrence = false;
585 else//if(c<0 && pos>=0)
587 data=line.substr(pos+1,lineSize);
588 key=line.substr(0,pos);
589 if ( readingConcepts && (key.rfind( CONCEPT ))!=std::string::npos )
592 if( (key.rfind( NAME ))!=std::string::npos )
594 if ( !findedConceptOcurrence )
597 findedConceptOcurrence = true;
598 //itoa(numberOfConcepts,index,10);
599 sprintf (index, "%d", numberOfConcepts);
601 if ( readingOtherConcepts )
602 key = CONCEPT + index + POINT + NAME;
604 else if ( (key.rfind( SIZE ))!=std::string::npos )
606 if ( !findedConceptOcurrence)
609 findedConceptOcurrence = true;
610 // itoa(numberOfConcepts,index,10);
611 sprintf(index, "%d", numberOfConcepts);
613 if ( readingOtherConcepts )
614 key = CONCEPT + index + POINT + SIZE;
620 int antes = dataInFile.size();
623 if( readingOtherConcepts && key.rfind(NUMCONCEPTS)!=std::string::npos )
625 int otherConceptsValue = atoi(data.c_str());
626 //itoa(numberOfConcepts+otherConceptsValue,dataString,10);
627 sprintf(dataString,"%d", numberOfConcepts+otherConceptsValue);
629 std::map<std::string,std::string>::iterator iter = dataInFile.find(key);
630 dataInFile.erase(iter);
632 dataInFile.insert(std::pair<std::string,std::string>(key,data));
634 int despues = dataInFile.size();
635 //cleaning the data and the key variables
649 * Returns the number of decimals places of the number given
652 int ReaderEnvironment<T>::getNumberOfDecimalPlaces(int number)
666 * Clean the line readed from the file
669 void ReaderEnvironment<T>::cleanLine(std::string & line)
672 pos1=line.find_first_not_of(" ");
674 pos2=line.find_last_not_of(" ");
676 line.erase(pos2+1,size-1);