2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
42 //====== LIFECYCLE ========
44 ReaderEnvironment<T>::ReaderEnvironment(std::string nameConfigurationFile,std::map<std::string,T>* things)
46 this->nameConfigurationFile=nameConfigurationFile;
48 this->instantHandler=NULL;
49 this->nameOtherConceptsFile = nameOtherConceptsFile;
50 this->environment= new SomeEnvironment<T>();
52 NUMCONCEPTS="numConcepts";
55 NUMOBJECTS="numObjects";
60 NUMINSTANTS="numInstants";
66 nameOtherConceptsFile = "";
69 //build the environment
73 ReaderEnvironment<T>::ReaderEnvironment(std::string nameConceptFile,std::string nameObjectFile,std::map<std::string,T>* things)
75 this->nameConceptFile=nameConceptFile;
76 this->nameObjectFile=nameObjectFile;
79 this->nameOtherConceptsFile = nameOtherConceptsFile;
80 this->environment= new SomeEnvironment<T>();
82 NUMCONCEPTS="numConcepts";
85 NUMOBJECTS="numObjects";
90 NUMINSTANTS="numInstants";
96 nameOtherConceptsFile = "";
98 //build the environment
103 ReaderEnvironment<T> :: ReaderEnvironment(std::string nameOtherConceptsFile, std::string nameConceptFile,std::string nameObjectFile, std::map<std::string,T>* things)
105 this->nameConceptFile=nameConceptFile;
106 this->nameObjectFile=nameObjectFile;
107 this->nameOtherConceptsFile = nameOtherConceptsFile;
109 this->environment= new SomeEnvironment<T>();
111 NUMCONCEPTS="numConcepts";
114 NUMOBJECTS="numObjects";
119 NUMINSTANTS="numInstants";
125 numberOfConcepts = 0;
126 //build the environment
130 ReaderEnvironment<T>::~ReaderEnvironment()
134 if(things) delete this->things;
135 nameConceptFile.clear();
136 nameConfigurationFile.clear();
137 nameObjectFile.clear();
139 nameOtherConceptsFile.clear();
140 if(instantHandler) delete instantHandler;
144 //====== OPERATIONS =======
146 * @return environment builded
149 SomeEnvironment<T>* ReaderEnvironment<T>::getEnvironment()
151 return this->environment;
154 * build the environment
157 void ReaderEnvironment<T>::buildEnvironment()
161 if(!nameConfigurationFile.empty())
162 readFile(nameConfigurationFile);
165 readFile(nameConceptFile);
166 readFile(nameObjectFile);
167 if(!nameOtherConceptsFile.empty())
169 readFile(nameOtherConceptsFile);
172 //adding the data of the concepts to the environment
174 //setting the instant handler
175 this->instantHandler=new InstantHandler<T>(environment);
176 //adding the things to the environment
180 //======== PRIVATE METHODS ==============
183 * build the instant from the string readed in the file
184 * @param instant, string with the description of the instant
185 * @param nameObject, name of the object for wich we can add an instant or several instants
187 * TODO: when is an r(i j) and when the user doesnt write all the information
188 * of the instants it is considered like an all
189 * for example, there is 3 concepts, the data complete for an instant is of size 3
190 * but the user can write only an index of the first concept, the others is assumed
191 * to be all, is like instant= i, then it is assumed like instant= i all all
196 bool ReaderEnvironment<T>::addInstants(std::string nameObject,std::string instant)
199 int indexAll,numConcept,sizeConcept=-1,i,k=0;
201 //copying the string of the instant
202 std::string instantCopy=instant;
204 indexAll=instantCopy.find(ALL);
206 char* indexConcept=(char*)malloc(sizeof(char));
207 Instant* instantThing;
212 std::string nameConcept;
213 //setting the first instant of the handler, using all the all's defined in the string
216 //itoa(0,indexConcept,10); // itoa not ANSI
217 sprintf(indexConcept, "%d", 0);
218 instantCopy.replace(indexAll,ALL.size(),indexConcept);
219 numConcept=getNumConcept(instantCopy,indexAll);
220 getConceptName( nameConcept,numConcept);
221 //setting the instanHandler
222 instantHandler->addConceptToHandled(nameConcept,2,k);
225 indexAll=instantCopy.find(ALL);
230 instantThing=getInstant(instantCopy);
231 //adding to the environment
234 if(environment->getNumberOfConcepts()== instantThing->getSize())
236 environment->addInstantToThing(nameObject,instantThing);
237 //starting the actual instant
238 instantHandler->setActualInstant(instantThing);
240 //else THROW EXCEPTION
242 //else THROW AN EXCEPTION
245 //calculatig the number of instants
247 int numInstants=instantHandler->getNumOfInstants();
249 //calculating all the instants
250 for(i=1;i<numInstants;i++)
252 instantHandler->nextInstant();
253 instantThing=instantHandler->getActualInstant();
255 int sizeI=instantThing->getSize();
256 for(int j=0;j<sizeI;j++)
257 int q=instantThing->getIndexInConcept(j);
259 environment->addInstantToThing(nameObject,instantThing);
266 instantThing=getInstant(instantCopy);
269 environment->addInstantToThing(nameObject,instantThing);
273 //else throw an exception
281 * Returns the name of the concept in the concepts defined given the number
282 * of the concept in the instant
283 * @param nameConcept, string where is going to be load the name of the concept
284 * @param numConcept, number of the concept in the instant for which we are looking
288 void ReaderEnvironment<T>::getConceptName(std::string& nameConcept,int numConcept)
290 int ncs,conceptIndexInEnvironment,i;
292 //getting the names of the concept defined
293 std::vector<std::string> nameConcepts;
294 environment->getConceptsNames(nameConcepts);
295 //name of the concept i
296 std::string nameConcepti;
297 ncs=nameConcepts.size();
298 bool isConcept=false;
299 for(i=0;i< ncs && !isConcept;i++)
301 nameConcepti=nameConcepts[i];
302 conceptIndexInEnvironment=environment->getIndexConcept(nameConcepti);
303 if(conceptIndexInEnvironment==numConcept)
306 nameConcepti.clear();
309 nameConcept=nameConcepti;
310 nameConcepti.clear();
314 * Returns the number of the concept in the instant string read
315 * @param instant,instant where we are going to search the number of the
316 * concept that is in the position given
317 * @param position, position of the concept (string position) for which we want the number in the instant given
318 * @return the number of concept in the instant
321 int ReaderEnvironment<T>::getNumConcept(std::string& instant,int position )
323 int splitIndex,counter=0;
324 splitIndex=instant.rfind(" ",position);
329 splitIndex=instant.rfind(" ",splitIndex-1);
337 * Returns the instant object of the instant string given
338 * 1 1 1 1 and returns an instant object (1,1,1,1)
339 * @param instant, instant for which we want to construct an Instant object
340 * @return an Instant's object
341 * TODO validation of indexes in instant
344 Instant* ReaderEnvironment<T>:: getInstant(std::string instant)
346 int value,splitIndex;
347 //getting the names of the concept defined
348 std::vector<std::string> nameConcepts;
349 environment->getConceptsNames(nameConcepts);
351 std::vector<int>* instantVector=new std::vector<int>();
354 splitIndex=instant.find(" ");
358 while(!stop && isValid)
363 concept=instant.substr(0,splitIndex);
364 instant.erase(0,splitIndex+1);
365 value=atoi(concept.c_str());
366 isValid=environment->isValidIndex(value,k);
368 instantVector->push_back(value);
369 splitIndex=instant.find(" ");
372 Instant* instantThing;
375 instantThing= new Instant(instantVector);
376 delete instantVector;
384 * add the instants defined in the file for the name of the
386 * @param nameObject, name of the object for which we want to search
390 bool ReaderEnvironment<T>::addInstantToThing(std::string nameObject,std::string index)
393 std::map<std::string,std::string>::iterator iteratorData;
396 //getting the number of instants for the object given
398 std::string s(OBJECT + index + POINT + NUMINSTANTS);
399 iteratorData=dataInFile.find(s);
401 if(iteratorData!=dataInFile.end())
403 std::string numIns=iteratorData->second;
404 numInstants=atoi(numIns.c_str());
407 //else throw exception
408 //getting the instants
412 //finding the number of decimals places
413 int num=getNumberOfDecimalPlaces(numInstants);
414 char* indexi=(char*)malloc(sizeof(char)*(num+1));
415 //std::string indexiStr;
416 for(i=1;i<=numInstants && added;i++)
419 sprintf(indexi, "%d", i);
421 s=OBJECT+index+POINT+INSTANT+indexi;
422 iteratorData=dataInFile.find(s);
423 if(iteratorData!=dataInFile.end())
425 instant=iteratorData->second;
426 addInstants(nameObject,instant);
441 * adds the things to the environment
444 bool ReaderEnvironment<T>::addThings()
446 int numObjects=0,j=0;
447 bool addedInstants=true;
448 typename std::map<std::string,std::string>::iterator iteratorData;
449 typename std::map<std::string,T>::iterator iteratorThings;
450 //getting the number of objects
451 iteratorData=dataInFile.find(NUMOBJECTS);
452 if(iteratorData!=dataInFile.end())
454 std::string numObj=iteratorData->second;
455 numObjects=atoi(numObj.c_str());
458 //finding the number of decimals places
459 int num=getNumberOfDecimalPlaces(numObjects);
460 //getting the objects if does come like objects
464 char* index= (char*)malloc(sizeof(char)*(num+1));
465 std::string objectData;
466 std::string nameObjecti;
467 for(j=1;j<=numObjects && addedInstants;j++)
470 int num2=sprintf(index, "%d", j);
471 objectData=OBJECT+index+POINT+NAME;
472 iteratorData=dataInFile.find(objectData);
473 if(iteratorData!=dataInFile.end())
475 nameObjecti=iteratorData->second;
476 iteratorThings=things->find(nameObjecti);
477 if(iteratorThings!=things->end())
479 environment->addThing(nameObjecti,iteratorThings->second);
481 addedInstants=addInstantToThing(nameObjecti,index);
484 //the names given en execution and of the file should be the same
498 //if we have to charge it from a file
499 //if is like that we should ask for the objecti.path
508 * adds the concepts of the file to the environment
511 bool ReaderEnvironment<T>::addConcepts()
513 int numConcepts=0,i=0;
514 std::map<std::string,std::string>::iterator iteratorData;
515 //getting the number of concepts defined
516 iteratorData=dataInFile.find(NUMCONCEPTS);
517 if(iteratorData!=dataInFile.end())
519 std::string numberOfConcepts=iteratorData->second;
520 numConcepts=atoi(numberOfConcepts.c_str());
522 //finding the number of decimals places
523 int num=getNumberOfDecimalPlaces(numConcepts);
524 std::string conceptData;
525 char* index=(char*)malloc(sizeof(char)*(num+1));
526 std::string nameConcepti;
527 std::string sizeConcepti;
528 for(i=1;i<=numConcepts;i++)
532 sprintf(index, "%d", i);
534 conceptData=CONCEPT+index+POINT+NAME;
535 iteratorData=dataInFile.find(conceptData);
536 if(iteratorData!=dataInFile.end())
537 nameConcepti=iteratorData->second;
540 conceptData=CONCEPT+index+POINT+SIZE;
541 iteratorData=dataInFile.find(conceptData);
542 if(iteratorData!=dataInFile.end())
543 sizeConcepti=iteratorData->second;
545 environment->addConcept(nameConcepti,atoi(sizeConcepti.c_str()));
547 nameConcepti.clear();
548 sizeConcepti.clear();
561 * read the configuration file
564 void ReaderEnvironment<T>::readFile(std::string nameFile)
566 bool readingOtherConcepts = nameFile.compare(this->nameOtherConceptsFile) == 0;
567 bool readingConcepts = nameFile.compare(this->nameConceptFile) == 0 || readingOtherConcepts;
568 bool findedConceptOcurrence = false;
571 //std::string conceptKeyPart = "";
574 std::ifstream configFile;
575 //each line of the file
577 //string of the data to save
579 //string with the key of the map
585 if(nameFile.compare("")!=0)
587 configFile.open(nameFile.c_str());
588 if(!configFile.is_open())
590 //should throw an exception
595 //THIS ONLY SAVES NUMBERS OF ONE DIGIT
597 char* index= (char*)malloc(sizeof(char)*5);
598 char* dataString= (char*)malloc(sizeof(char)*10);
599 while(!configFile.eof())
602 std::getline(configFile,line);
604 pos=line.find(EQUAL);
605 lineSize=line.size();
606 //if is not a comment
608 int c=line.find(COMMENT);
609 if (c !=std::string::npos )
610 findedConceptOcurrence = false;
612 else//if(c<0 && pos>=0)
614 data=line.substr(pos+1,lineSize);
615 key=line.substr(0,pos);
616 if ( readingConcepts && (key.rfind( CONCEPT ))!=std::string::npos )
619 if( (key.rfind( NAME ))!=std::string::npos )
621 if ( !findedConceptOcurrence )
624 findedConceptOcurrence = true;
625 //itoa(numberOfConcepts,index,10);
626 sprintf (index, "%d", numberOfConcepts);
628 if ( readingOtherConcepts )
629 key = CONCEPT + index + POINT + NAME;
631 else if ( (key.rfind( SIZE ))!=std::string::npos )
633 if ( !findedConceptOcurrence)
636 findedConceptOcurrence = true;
637 // itoa(numberOfConcepts,index,10);
638 sprintf(index, "%d", numberOfConcepts);
640 if ( readingOtherConcepts )
641 key = CONCEPT + index + POINT + SIZE;
647 int antes = dataInFile.size();
650 if( readingOtherConcepts && key.rfind(NUMCONCEPTS)!=std::string::npos )
652 int otherConceptsValue = atoi(data.c_str());
653 //itoa(numberOfConcepts+otherConceptsValue,dataString,10);
654 sprintf(dataString,"%d", numberOfConcepts+otherConceptsValue);
656 std::map<std::string,std::string>::iterator iter = dataInFile.find(key);
657 dataInFile.erase(iter);
659 dataInFile.insert(std::pair<std::string,std::string>(key,data));
661 int despues = dataInFile.size();
662 //cleaning the data and the key variables
676 * Returns the number of decimals places of the number given
679 int ReaderEnvironment<T>::getNumberOfDecimalPlaces(int number)
693 * Clean the line readed from the file
696 void ReaderEnvironment<T>::cleanLine(std::string & line)
699 pos1=line.find_first_not_of(" ");
701 pos2=line.find_last_not_of(" ");
703 line.erase(pos2+1,size-1);