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 # ------------------------------------------------------------------------ */
43 //====== LIFECYCLE ========
45 SomeEnvironment<T>::SomeEnvironment()
49 SomeEnvironment<T>::SomeEnvironment(std::map<std::string,int>* concepts,std::map< std::string,SomeThing<T> >* things)
51 this->concepts=concepts;
56 SomeEnvironment<T>::~SomeEnvironment()
60 //deleting existing instants
61 size=existingInstants.size();
63 delete existingInstants[i];
64 existingInstants.clear();
70 indexesConcepts.clear();
72 thingsOfInstant.clear();
75 //====== OPERATIONS =======
77 * Adds a thing to the program
78 * @param name: name of the thing
79 * @param thing: thing to be added
80 * @return true if the thing was succesfully added
84 bool SomeEnvironment<T>:: addThing(std::string name,T thing)
86 SomeThing<T> something (name);
87 something.setThing(thing);
88 things.insert(std::pair < std::string, SomeThing<T> >(name,something));
93 * Add thing with the name given to the instant given
95 * the thing that has that name IS ALREADY ADDED TO THE PROGRAM
96 * @param name: name of the EXISTANT thing
97 * @param instant: instant associated to the thing
98 * @return true if the thing was succesfully added
101 bool SomeEnvironment<T>::addInstantToThing(std::string name,Instant* instant)
103 typename std::map< std::string,SomeThing<T> >::iterator thingsIterator;
104 thingsIterator=things.find(name);
105 if(thingsIterator != things.end())
107 SomeThing<T>* something=&thingsIterator->second;
108 //getting the address of the instant added to the environment
109 // in the existing instants; EVERYTHING IS HANDLED INSIDE
112 int indexInstantInside=addInstant(instant);
114 //int indexInstantInside=getIndexInstantInExistingInstants(instant);
115 Instant* instantInExistingInstants=NULL;
116 if(indexInstantInside!=-1)
118 instantInExistingInstants=existingInstants[indexInstantInside];
119 something->addInstant(instantInExistingInstants);
127 * Add thing with the name given, and the data given in the instant given
128 * @param name: name of the thing
129 * @param thing: information of the thing to be added
130 * @param instant: instant associated to the thing
131 * @return true if the thing was succesfully added
134 bool SomeEnvironment<T>::addThingWithInstant(std::string name,T thing,Instant* instant)
136 SomeThing<T> something(name);
137 something.setThing(thing);
139 int indexInstantInside=addInstant(instant);
141 //int indexInstantInside=getIndexInstantInExistingInstants(instant);
142 if(indexInstantInside!=-1)
144 Instant* instantInExistingInstants = existingInstants[indexInstantInside];
145 something.addInstant(instantInExistingInstants);
146 things.insert(std::pair< std::string, SomeThing<T> >(name,something));
152 * Add a concept to the environment
153 * @param name: name of the concept
154 * @param size: size of the concept, it means that its indexes are
155 * distributed 0..size-1
156 * @return true if succesful, false otherwise
160 bool SomeEnvironment<T>::addConcept(std::string name, int size)
162 concepts.insert(std::pair< std::string,int >(name,size));
163 int sizeMap=concepts.size();
164 indexesConcepts.insert(std::pair< std::string,int >(name,sizeMap-1));
168 * Validate the index of a concept in an instant
171 bool SomeEnvironment<T>::isValidIndex(int index, int indexInInstant)
173 std::map<std::string,int>::iterator conceptsIterator=concepts.begin();
174 std::map<std::string,int>::iterator conceptsIndexesIterator;
175 std::string conceptNamei;
176 int indexInInstantConcepti,size;
177 while(conceptsIterator!=concepts.end())
179 conceptNamei=conceptsIterator->first;
180 size=conceptsIterator->second;
181 indexInInstantConcepti=getIndexConcept(conceptNamei);
182 if(indexInInstantConcepti==indexInInstant)
189 conceptNamei.clear();
195 //====== INQUIRY =========
198 std::vector<T*>* SomeEnvironment<T>::getThings(Instant* instant)
200 //cleaning things of instant
201 thingsOfInstant.clear();
204 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
205 thingsIterator = things.begin();
206 //setting the environment instant
207 int indexInstantInside=getIndexInstantInExistingInstants(instant);
208 Instant* instantInExistingInstants=NULL;
209 if(indexInstantInside!=-1)
211 instantInExistingInstants=existingInstants[indexInstantInside];
213 while(thingsIterator!=things.end() && instantInExistingInstants)
215 SomeThing<T>* something=&thingsIterator->second;
217 bool isInInstantB=isInInstant(something,instantInExistingInstants);
220 T* thing=something->getThing();
221 thingsOfInstant.push_back(thing);
226 return &thingsOfInstant;
230 * Returns the things with their names in the environment
231 * @param names, vector where is goint to be safe the names
232 * of the things in the environment.
233 * @param things, vector where is going to be save the things
237 void SomeEnvironment<T>::getThings(std::vector< std::string >& names,std::vector< T* >& thingsVector, Instant* instant)
239 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
240 thingsIterator=things.begin();
241 //setting the environment instant
242 int indexInstantInside=getIndexInstantInExistingInstants(instant);
243 Instant* instantInExistingInstants=NULL;
244 if(indexInstantInside!=-1)
246 instantInExistingInstants=existingInstants[indexInstantInside];
248 while(thingsIterator!=things.end() && instantInExistingInstants)
250 SomeThing<T>* something=&thingsIterator->second;
251 bool isInInstantB=isInInstant(something,instantInExistingInstants);
254 std::string nameThingInEnvironment=thingsIterator->first;
255 T* thing=something->getThing();
256 thingsVector.push_back(thing);
257 names.push_back(nameThingInEnvironment);
264 * Returns the instants where the thing identified by the name
266 * @param nameThing, name of the thing in the environment
267 * @return instants of that thing
270 std::vector<Instant*>* SomeEnvironment<T>::getInstantsOfThing(std::string nameThing)
272 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
273 thingsIterator=things.find(nameThing);
274 if(thingsIterator!=things.end())
276 SomeThing<T>* something=&thingsIterator->second;
277 return something->getInstants();
279 return (std::vector<Instant*>*)NULL;
283 * Returns the instants define in the environment
284 * @return existing instants in the environment
285 * A POINTER TO THE EXISTINGINSTANTS
288 std::vector<Instant*>* SomeEnvironment<T>::getExistingInstants()
290 return &existingInstants;
295 * Returns the size of the concept identified by nameConcept
296 * @param nameConcept, name of the concept in the environment
297 * @return size, of the concept given or -1 if the concept doesnt
301 int SomeEnvironment<T>::getSizeConcept(std::string nameConcept)
303 std::map< std::string, int>::iterator conceptsIterator;
304 conceptsIterator= concepts.find(nameConcept);
305 if(conceptsIterator!= concepts.end())
307 return conceptsIterator->second;
313 * returns the index of the concept in the instants
316 int SomeEnvironment<T>::getIndexConcept(std::string nameConcept)
318 std::map< std::string, int>::iterator indexesConceptsIterator;
319 indexesConceptsIterator= indexesConcepts.find(nameConcept);
320 if(indexesConceptsIterator!= indexesConcepts.end())
322 return indexesConceptsIterator->second;
329 * Give the names of the names defined
330 * @param nameConcepts, vector where is goin to be save the names of the concepts
333 void SomeEnvironment<T>::getConceptsNames(std::vector<std::string>& namesConcepts)
335 std::map<std::string,int>::iterator iteratorConcepts=concepts.begin();
336 std::string nameConcept;
337 while(iteratorConcepts!=concepts.end())
339 nameConcept=iteratorConcepts->first;
340 namesConcepts.push_back(nameConcept);
345 * Method that retorns the name of each concept and the size of it.
346 * @param conceptNameVect, Vector in which is disposed to be setted the name for each of the included concepts
347 * @param conceptSizeVect, Vector in which is disposed to be setted the size for each of the included concepts
350 void SomeEnvironment<T> :: getConceptsInformation(std::vector<std::string>& conceptNameVect, std::vector<int>& conceptSizeVect)
352 std::map<std::string,int>::iterator iteratorConcepts=concepts.begin();
353 while(iteratorConcepts!=concepts.end())
355 std::string aName = iteratorConcepts->first;
356 int aSize = iteratorConcepts->second;
357 conceptNameVect.push_back( aName );
358 conceptSizeVect.push_back( aSize );
364 * returns all the things of the environment
367 void SomeEnvironment<T>::getThingsOfEnvironment(std::vector<T*>* thingsVector)
369 typename std::map<std::string, SomeThing<T> >::iterator iteratorThings= things.begin();
370 thingsVector->clear();
371 while(iteratorThings!=things.end())
373 SomeThing<T>* something=&iteratorThings->second;
374 T* thing=something->getThing();
375 thingsVector->push_back(thing);
380 * returns a pointer to the thing with the name given
383 T* SomeEnvironment<T>::getThingByName(std::string name)
386 typename std::map<std::string, SomeThing<T> >::iterator iteratorThings= things.find(name);
387 if(iteratorThings!=things.end())
389 SomeThing<T>* something=&iteratorThings->second;
390 thing=something->getThing();
397 * returns the number of concepts defined
400 int SomeEnvironment<T>::getNumberOfConcepts()
402 return this->concepts.size();
406 * Gets the number of things
407 * @return Returns the number of existing things in the environment
410 int SomeEnvironment<T>::getNumberOfThings ()
412 return things.size();
416 //====== ACCESS ==========
418 * Remove a thing from all the instants, it means from the program
419 * @param name: name of the thing
420 * @return: true if the removed is succesful (false if the thing doesn exist)
423 bool SomeEnvironment<T>::removeThing(std::string name)
425 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
426 thingsIterator=things.find(name);
427 if(thingsIterator!=things.end())
429 things.erase(thingsIterator);
435 * Removes the thing with the name given, from the instant
437 * @param name: name of the thing
438 * @param Instant: Instant from which it will be removed
439 * @return true: if the removed is succesful (false if the thing doesn exist)
442 bool SomeEnvironment<T>::removeThingFromInstant(std::string name, Instant* instant)
444 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
445 thingsIterator=things.find(name);
446 //setting the environment instant
447 int indexInstantInside=getIndexInstantInExistingInstants(instant);
448 Instant* instantInExistingInstants=NULL;
449 if(indexInstantInside!=-1)
450 instantInExistingInstants=existingInstants[indexInstantInside];
452 if(thingsIterator!=things.end() && instantInExistingInstants)
454 SomeThing<T>* something=&thingsIterator->second;
456 bool isInInstantB=isInInstant(something,instantInExistingInstants);
459 something->removeInstant(instant);
467 * Remove Instant from the program, it means, that from all the
468 * somethings that have that instant, after call this method them
469 * doesnt have that instant anymore
470 * @param instant: instant that is going to be removed
471 * @return true: if the removed is succesful (false other wise)
474 bool SomeEnvironment<T>::removeInstant(Instant* instant)
476 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
477 thingsIterator=things.begin();
480 //setting the environment instant
481 int indexInstantInside=getIndexInstantInExistingInstants(instant);
482 Instant* instantInExistingInstants=NULL;
483 if(indexInstantInside!=-1)
484 instantInExistingInstants=existingInstants[indexInstantInside];
486 while(thingsIterator!=things.end() && instantInExistingInstants)
488 SomeThing<T>* something=&thingsIterator->second;
489 bool isInInstantB=isInInstant(something,instantInExistingInstants);
491 ok = something->removeInstant(instantInExistingInstants);
495 //removing from inside instants
496 removeInstantFromExistingInstants(instant);
500 * Remove a dimension from the environment
501 * THE REMOVE OF THE CONCEPT IS WHEN IS BEING DEFINED THE ENVIRONMENT ,NO IN
504 * the name of the concept given is already added to the environment
505 * @param nameConcept, name of the concept to remove
508 bool SomeEnvironment<T>::removeConcept(std::string nameConcept)
510 std::map < std::string,int >::iterator conceptsIterator;
511 conceptsIterator=concepts.find(nameConcept);
512 if(conceptsIterator!=concepts.end())
514 concepts.erase(conceptsIterator);
519 //====== PRIVATE METHODS=========
521 * Checks if in the instant given the something given has it
522 * @param instant, instant to check
523 * @param something;, something to check in the instant
527 bool SomeEnvironment<T>::isInInstant(SomeThing<T>* something,Instant *instant)
530 for(int i=0;i<instant->getSize();i++)
531 /// \TODO fix warning unused variable k
532 int k=instant->getIndexInConcept(i);
534 int index=something->hasInstant(instant);
541 * Remove the instant from the environment
542 * @param instant, to be erased
546 void SomeEnvironment<T>::removeInstantFromExistingInstants(Instant* instant)
548 std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
550 while(existingInstantsIterator!=existingInstants.end()&& !isEquals)
552 isEquals=(*existingInstantsIterator)->isEquals(instant);
554 existingInstants.erase(existingInstantsIterator);
555 existingInstantsIterator++;
560 * Adds an instant to the existing instants, verifying if
561 * the instant is already added
562 * @param instant, to be added
566 int SomeEnvironment<T>::addInstant(Instant* instant)
568 std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
571 while(existingInstantsIterator!=existingInstants.end() && !isEquals)
573 Instant* existingInstant=*existingInstantsIterator;
575 int sizeI=existingInstant->getSize();
576 for(int k=0;k<sizeI;k++)
577 /// \ TODO fix warning unused variable r
578 int r=existingInstant->getIndexInConcept(k);
580 isEquals=existingInstant->isEquals(instant);
581 existingInstantsIterator++;
587 //copying the instant's information
588 std::vector<int>* instantVector=new std::vector<int>();
589 int sizeInstant=instant->getSize();
591 for(k=0;k<sizeInstant;k++)
593 int d=instant->getIndexInConcept(k);
594 instantVector->push_back(d);
597 //saving the instant given in the handler
598 Instant* instantToAdded= new Instant(instantVector);
599 existingInstants.push_back(instantToAdded);
600 i=existingInstants.size()-1;
605 * Return the index in the existing instants of the instant
606 * that the user had given
607 * @param instant, instant for which we are looking the index in
608 * the existings intants in the environment
609 * @return an index in (0,existingInstants.size()-1)or -1 if the instant hasnt
613 int SomeEnvironment<T>::getIndexInstantInExistingInstants(Instant* instant)
615 std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
618 while(existingInstantsIterator!=existingInstants.end() && !isEquals)
620 isEquals=(*existingInstantsIterator)->isEquals(instant);
621 existingInstantsIterator++;