]> Creatis software - creaEnvironment.git/blob - lib/kernel_Environment/SomeEnvironment_Txx.h
License Files Headers
[creaEnvironment.git] / lib / kernel_Environment / SomeEnvironment_Txx.h
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
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
9  #
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.
16  #
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
21  #  liability.
22  #
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  # ------------------------------------------------------------------------ */
26
27
28 // SYSTEM INCLUDES
29
30 #include <map>
31
32 // PROJECT INCLUDES
33
34
35 // LOCAL INCLUDES
36
37
38
39 // FORWARD REFERENCES
40
41 //NAMESPACE
42
43                //====== LIFECYCLE ========
44  template<class T>         
45 SomeEnvironment<T>::SomeEnvironment()
46 {
47 }
48 template<class T>   
49 SomeEnvironment<T>::SomeEnvironment(std::map<std::string,int>* concepts,std::map< std::string,SomeThing<T> >* things)
50 {
51    this->concepts=concepts;
52    this->things=things;
53 }
54
55 template<class T>   
56 SomeEnvironment<T>::~SomeEnvironment()
57    {
58       int size,i;
59       
60       //deleting existing instants
61       size=existingInstants.size();
62       for(i=0;i<size;i++)
63          delete existingInstants[i];
64       existingInstants.clear();
65       //deleting concepts   
66       concepts.clear();
67       //deleting things
68       things.clear();
69       //indexes of concept
70       indexesConcepts.clear();
71       //things of instant
72       thingsOfInstant.clear();
73    }
74                
75 //====== OPERATIONS =======
76 /*
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
81 */
82
83 template<class T>   
84 bool SomeEnvironment<T>:: addThing(std::string name,T thing)
85    {
86       SomeThing<T> something (name);
87       something.setThing(thing);
88       things.insert(std::pair < std::string, SomeThing<T> >(name,something));
89       return true;
90    }
91
92 /*
93 *   Add thing with the name given to the instant given
94 *   PRECONDITION
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
99 */
100 template<class T>   
101 bool SomeEnvironment<T>::addInstantToThing(std::string name,Instant* instant)
102    {
103       typename std::map< std::string,SomeThing<T> >::iterator thingsIterator;
104       thingsIterator=things.find(name);
105       if(thingsIterator != things.end())
106       {
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
110          
111           //saving instants
112           int indexInstantInside=addInstant(instant);
113           //borrame
114           //int indexInstantInside=getIndexInstantInExistingInstants(instant);
115           Instant* instantInExistingInstants=NULL;
116           if(indexInstantInside!=-1)
117              {
118                instantInExistingInstants=existingInstants[indexInstantInside];
119                something->addInstant(instantInExistingInstants);
120                return true;
121              }
122           return false;
123       }
124       return false;
125    }
126 /*
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
132 */
133 template<class T>   
134 bool SomeEnvironment<T>::addThingWithInstant(std::string name,T thing,Instant* instant)
135    {
136       SomeThing<T> something(name);
137       something.setThing(thing);
138       //saving instants
139       int indexInstantInside=addInstant(instant);
140       //borrame
141       //int indexInstantInside=getIndexInstantInExistingInstants(instant);
142       if(indexInstantInside!=-1)   
143       {
144          Instant* instantInExistingInstants = existingInstants[indexInstantInside];
145          something.addInstant(instantInExistingInstants);
146          things.insert(std::pair< std::string, SomeThing<T> >(name,something));
147          return true;
148       }
149       return false;
150    }
151 /*
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
157 */
158
159 template<class T>   
160 bool SomeEnvironment<T>::addConcept(std::string name, int size)
161    {
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));
165       return true;
166    }
167 /*
168 * Validate the index of a concept in an instant
169 */
170 template<class T>
171 bool SomeEnvironment<T>::isValidIndex(int index, int indexInInstant)
172    {
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())
178          {
179             conceptNamei=conceptsIterator->first;
180             size=conceptsIterator->second;
181             indexInInstantConcepti=getIndexConcept(conceptNamei);
182             if(indexInInstantConcepti==indexInInstant)
183                {
184                 if(index<size)
185                    return true;
186                 else
187                    return false;
188                }
189             conceptNamei.clear();
190             conceptsIterator++;
191          }
192       return false;
193    }
194
195 //====== INQUIRY =========
196
197 template<class T>   
198 std::vector<T*>* SomeEnvironment<T>::getThings(Instant* instant)
199    {
200       //cleaning things of instant
201       thingsOfInstant.clear();
202       //
203       
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)
210          {   
211             instantInExistingInstants=existingInstants[indexInstantInside];
212       
213             while(thingsIterator!=things.end() && instantInExistingInstants)
214                {
215                   SomeThing<T>* something=&thingsIterator->second;
216
217                   bool isInInstantB=isInInstant(something,instantInExistingInstants);
218                   if(isInInstantB)
219                   {
220                      T* thing=something->getThing();
221                      thingsOfInstant.push_back(thing);
222                   }   
223                   thingsIterator++;
224                }
225          }
226       return &thingsOfInstant;
227    }
228
229 /*
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
234 *   @return
235 */
236 template<class T>   
237 void SomeEnvironment<T>::getThings(std::vector< std::string >& names,std::vector< T* >& thingsVector, Instant* instant)
238    {
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)
245       {   
246          instantInExistingInstants=existingInstants[indexInstantInside];
247       
248          while(thingsIterator!=things.end() && instantInExistingInstants)
249             {
250                SomeThing<T>* something=&thingsIterator->second;
251                bool isInInstantB=isInInstant(something,instantInExistingInstants);
252                if(isInInstantB)
253                {
254                   std::string nameThingInEnvironment=thingsIterator->first;
255                   T* thing=something->getThing();
256                   thingsVector.push_back(thing);
257                   names.push_back(nameThingInEnvironment);
258                }   
259                thingsIterator++;
260             }
261       }
262    }
263 /*
264 *   Returns the instants where the thing identified by the name
265 *   it should appears
266 *   @param nameThing, name of the thing in the environment
267 *   @return instants of that thing
268 */
269 template<class T>
270 std::vector<Instant*>* SomeEnvironment<T>::getInstantsOfThing(std::string nameThing)
271    {
272       typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
273       thingsIterator=things.find(nameThing);
274       if(thingsIterator!=things.end())
275          {
276             SomeThing<T>* something=&thingsIterator->second;
277             return something->getInstants();
278          }
279       return (std::vector<Instant*>*)NULL;
280    }
281
282 /*
283 *   Returns the instants define in the environment
284 *   @return existing instants in the environment
285 *   A POINTER TO THE  EXISTINGINSTANTS
286 */
287 template<class T>
288 std::vector<Instant*>* SomeEnvironment<T>::getExistingInstants()
289    {
290       return &existingInstants;
291    }
292
293
294 /*
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
298 *   exist
299 */
300 template<class T>
301 int SomeEnvironment<T>::getSizeConcept(std::string nameConcept)
302    {
303       std::map< std::string, int>::iterator conceptsIterator;
304       conceptsIterator= concepts.find(nameConcept);
305       if(conceptsIterator!= concepts.end())
306        {
307          return conceptsIterator->second;
308        }
309       else
310          return -1;
311    }
312 /*
313 * returns the index of the  concept in the instants
314 */
315 template<class T>
316 int SomeEnvironment<T>::getIndexConcept(std::string nameConcept)
317    {
318       std::map< std::string, int>::iterator indexesConceptsIterator;
319       indexesConceptsIterator= indexesConcepts.find(nameConcept);
320       if(indexesConceptsIterator!= indexesConcepts.end())
321        {
322          return indexesConceptsIterator->second;
323        }
324       else
325          return -1;
326    }
327 /*
328 *   NEW
329 *   Give the names of the names defined
330 *   @param nameConcepts, vector where is goin to be save the names of the concepts
331 */
332 template<class T>
333 void SomeEnvironment<T>::getConceptsNames(std::vector<std::string>& namesConcepts)
334    {
335       std::map<std::string,int>::iterator iteratorConcepts=concepts.begin();
336       std::string nameConcept;
337       while(iteratorConcepts!=concepts.end())
338          {
339             nameConcept=iteratorConcepts->first;
340             namesConcepts.push_back(nameConcept);
341             iteratorConcepts++;
342          }
343    }
344 /*
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
348 */
349 template<class T>
350 void SomeEnvironment<T> :: getConceptsInformation(std::vector<std::string>& conceptNameVect, std::vector<int>& conceptSizeVect)
351 {
352    std::map<std::string,int>::iterator iteratorConcepts=concepts.begin();   
353    while(iteratorConcepts!=concepts.end())
354       {
355          std::string aName = iteratorConcepts->first;
356          int aSize = iteratorConcepts->second;         
357          conceptNameVect.push_back( aName );
358          conceptSizeVect.push_back( aSize );
359          iteratorConcepts++;
360       }
361 }
362
363 /*
364 * returns all the things of the environment
365 */
366 template<class T>
367 void SomeEnvironment<T>::getThingsOfEnvironment(std::vector<T*>* thingsVector)
368    {
369       typename std::map<std::string, SomeThing<T> >::iterator iteratorThings= things.begin();
370       thingsVector->clear();
371       while(iteratorThings!=things.end())
372          {
373             SomeThing<T>* something=&iteratorThings->second;
374             T* thing=something->getThing();
375             thingsVector->push_back(thing);
376             iteratorThings++;
377          }
378    }
379 /*
380 * returns a pointer to the thing with the name given
381 */
382 template<class T>
383 T* SomeEnvironment<T>::getThingByName(std::string name)
384    {
385       T* thing=NULL;
386       typename std::map<std::string, SomeThing<T> >::iterator iteratorThings= things.find(name);
387       if(iteratorThings!=things.end())
388          {
389             SomeThing<T>* something=&iteratorThings->second;
390             thing=something->getThing();
391          }
392       return thing;
393    }
394
395
396 /*
397 * returns the number of concepts defined
398 */
399 template<class T>   
400 int SomeEnvironment<T>::getNumberOfConcepts()
401    {
402       return this->concepts.size();
403    }
404
405    /*
406 * Gets the number of things 
407 * @return Returns the number of existing things in the environment
408 */
409 template<class T>
410 int SomeEnvironment<T>::getNumberOfThings ()
411    {
412       return things.size();
413    }
414    
415
416 //====== ACCESS ==========
417 /*
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)
421 */
422 template<class T>   
423 bool SomeEnvironment<T>::removeThing(std::string name)
424    {
425       typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
426       thingsIterator=things.find(name);
427       if(thingsIterator!=things.end())
428          {
429             things.erase(thingsIterator);
430             return true;
431          }
432       return false;
433    }
434 /*
435 *   Removes the thing with the name given, from the instant
436 *   given
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)
440 */
441 template<class T>   
442 bool SomeEnvironment<T>::removeThingFromInstant(std::string name, Instant* instant)
443    {
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];
451             
452       if(thingsIterator!=things.end() && instantInExistingInstants)
453          {
454             SomeThing<T>* something=&thingsIterator->second;
455             
456             bool isInInstantB=isInInstant(something,instantInExistingInstants);
457             if(isInInstantB)
458                {
459                   something->removeInstant(instant);
460                   return true;
461                }
462          }
463       return false;
464    }
465
466 /*
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)
472 */
473 template<class T>   
474 bool SomeEnvironment<T>::removeInstant(Instant* instant)
475    {
476       typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
477       thingsIterator=things.begin();
478       bool ok=false;
479    
480       //setting the environment instant
481       int indexInstantInside=getIndexInstantInExistingInstants(instant);
482       Instant* instantInExistingInstants=NULL;
483       if(indexInstantInside!=-1)
484          instantInExistingInstants=existingInstants[indexInstantInside];
485       
486       while(thingsIterator!=things.end() && instantInExistingInstants)
487          {
488             SomeThing<T>* something=&thingsIterator->second;
489             bool isInInstantB=isInInstant(something,instantInExistingInstants);
490             if(isInInstantB)
491                ok = something->removeInstant(instantInExistingInstants);
492             thingsIterator++;
493             
494          }
495       //removing from inside instants
496       removeInstantFromExistingInstants(instant);
497       return ok;
498    }
499 /*
500 *   Remove a dimension from the environment
501 *   THE REMOVE OF THE CONCEPT IS WHEN IS BEING DEFINED THE ENVIRONMENT ,NO IN
502 *   EXECUTION
503 *   PRECONDITION
504 *   the name of the concept given is already added to the environment
505 *   @param nameConcept, name of the concept to remove
506 */
507 template<class T>      
508 bool SomeEnvironment<T>::removeConcept(std::string nameConcept)
509    {
510       std::map < std::string,int >::iterator conceptsIterator;
511       conceptsIterator=concepts.find(nameConcept);
512       if(conceptsIterator!=concepts.end())
513          {
514             concepts.erase(conceptsIterator);
515             return true;
516          }
517       return false;
518    }
519 //====== PRIVATE METHODS=========
520 /*
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
524 *   @return
525 */
526 template<class T>   
527 bool SomeEnvironment<T>::isInInstant(SomeThing<T>* something,Instant *instant)
528 {
529    //borrame
530    for(int i=0;i<instant->getSize();i++)
531       /// \TODO fix warning unused variable k
532       int k=instant->getIndexInConcept(i);
533    //         
534    int index=something->hasInstant(instant);
535    if(index!=-1)
536       return true;
537    else
538       return false;
539 }
540 /*
541 *   Remove the instant from the environment
542 *   @param instant, to be erased
543 *   @return
544 */
545 template<class T>   
546 void  SomeEnvironment<T>::removeInstantFromExistingInstants(Instant* instant)
547    {
548       std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
549       bool isEquals=false;
550       while(existingInstantsIterator!=existingInstants.end()&& !isEquals)
551          {
552             isEquals=(*existingInstantsIterator)->isEquals(instant);
553             if(isEquals)
554                existingInstants.erase(existingInstantsIterator);
555             existingInstantsIterator++;
556          }
557    }
558
559 /*
560 *   Adds an instant to the existing instants, verifying if
561 *   the instant is already added
562 *   @param instant, to be added
563 *   @return
564 */
565 template<class T>   
566 int SomeEnvironment<T>::addInstant(Instant* instant)
567    {
568       std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
569       int i=0;
570       bool isEquals=false;
571       while(existingInstantsIterator!=existingInstants.end() && !isEquals)
572          {
573             Instant* existingInstant=*existingInstantsIterator;
574             //borrame
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);
579             //
580             isEquals=existingInstant->isEquals(instant);
581             existingInstantsIterator++;
582             if(!isEquals)
583                i++;  
584          }
585       if(!isEquals)
586          {
587             //copying the instant's information
588             std::vector<int>* instantVector=new std::vector<int>();
589             int sizeInstant=instant->getSize();
590             int k;
591             for(k=0;k<sizeInstant;k++)
592                {
593                  int d=instant->getIndexInConcept(k);
594                  instantVector->push_back(d);
595                }
596             //
597             //saving the instant given in the handler
598             Instant* instantToAdded= new Instant(instantVector);
599             existingInstants.push_back(instantToAdded);
600             i=existingInstants.size()-1;
601          }
602       return i;
603    }
604 /*
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
610 *   being defined
611 */
612 template<class T>   
613 int SomeEnvironment<T>::getIndexInstantInExistingInstants(Instant* instant)
614    {
615       std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
616       int i=0;
617       bool isEquals=false;
618       while(existingInstantsIterator!=existingInstants.end() && !isEquals)
619          {
620             isEquals=(*existingInstantsIterator)->isEquals(instant);
621             existingInstantsIterator++;
622             if(!isEquals)
623                i++;
624          }
625       if(!isEquals)
626             return -1;
627       else
628             return i;
629    }