]> Creatis software - creaEnvironment.git/blob - lib/kernel_Environment/SomeEnvironment_Txx.h
some more unused variables
[creaEnvironment.git] / lib / kernel_Environment / SomeEnvironment_Txx.h
1
2 // SYSTEM INCLUDES
3
4 #include <map>
5
6 // PROJECT INCLUDES
7
8
9 // LOCAL INCLUDES
10
11
12
13 // FORWARD REFERENCES
14
15 //NAMESPACE
16
17                //====== LIFECYCLE ========
18  template<class T>         
19 SomeEnvironment<T>::SomeEnvironment()
20 {
21 }
22 template<class T>   
23 SomeEnvironment<T>::SomeEnvironment(std::map<std::string,int>* concepts,std::map< std::string,SomeThing<T> >* things)
24 {
25    this->concepts=concepts;
26    this->things=things;
27 }
28
29 template<class T>   
30 SomeEnvironment<T>::~SomeEnvironment()
31    {
32       int size,i;
33       
34       //deleting existing instants
35       size=existingInstants.size();
36       for(i=0;i<size;i++)
37          delete existingInstants[i];
38       existingInstants.clear();
39       //deleting concepts   
40       concepts.clear();
41       //deleting things
42       things.clear();
43       //indexes of concept
44       indexesConcepts.clear();
45       //things of instant
46       thingsOfInstant.clear();
47    }
48                
49 //====== OPERATIONS =======
50 /*
51 *   Adds a thing to the program
52 *   @param name: name of the thing
53 *   @param thing: thing to be added
54 *   @return true if the thing was succesfully added
55 */
56
57 template<class T>   
58 bool SomeEnvironment<T>:: addThing(std::string name,T thing)
59    {
60       SomeThing<T> something (name);
61       something.setThing(thing);
62       things.insert(std::pair < std::string, SomeThing<T> >(name,something));
63       return true;
64    }
65
66 /*
67 *   Add thing with the name given to the instant given
68 *   PRECONDITION
69 *   the thing that has that name IS ALREADY ADDED TO THE PROGRAM
70 *   @param name: name of the EXISTANT thing
71 *   @param instant: instant associated to the thing
72 *   @return true if the thing was succesfully added
73 */
74 template<class T>   
75 bool SomeEnvironment<T>::addInstantToThing(std::string name,Instant* instant)
76    {
77       typename std::map< std::string,SomeThing<T> >::iterator thingsIterator;
78       thingsIterator=things.find(name);
79       if(thingsIterator != things.end())
80       {
81           SomeThing<T>* something=&thingsIterator->second;
82          //getting the address of the instant added to the environment
83          // in the existing instants; EVERYTHING IS HANDLED INSIDE
84          
85           //saving instants
86           int indexInstantInside=addInstant(instant);
87           //borrame
88           //int indexInstantInside=getIndexInstantInExistingInstants(instant);
89           Instant* instantInExistingInstants=NULL;
90           if(indexInstantInside!=-1)
91              {
92                instantInExistingInstants=existingInstants[indexInstantInside];
93                something->addInstant(instantInExistingInstants);
94                return true;
95              }
96           return false;
97       }
98       return false;
99    }
100 /*
101 *   Add thing with the name given, and the data given in the instant given
102 *   @param name: name of the thing
103 *   @param thing: information of the thing to be added
104 *   @param instant: instant associated to the thing
105 *   @return true if the thing was succesfully added
106 */
107 template<class T>   
108 bool SomeEnvironment<T>::addThingWithInstant(std::string name,T thing,Instant* instant)
109    {
110       SomeThing<T> something(name);
111       something.setThing(thing);
112       //saving instants
113       int indexInstantInside=addInstant(instant);
114       //borrame
115       //int indexInstantInside=getIndexInstantInExistingInstants(instant);
116       if(indexInstantInside!=-1)   
117       {
118          Instant* instantInExistingInstants = existingInstants[indexInstantInside];
119          something.addInstant(instantInExistingInstants);
120          things.insert(std::pair< std::string, SomeThing<T> >(name,something));
121          return true;
122       }
123       return false;
124    }
125 /*
126 *   Add a concept to the environment
127 *   @param name: name of the concept
128 *   @param size: size of the concept, it means that its indexes are
129 *             distributed 0..size-1
130 *   @return true if succesful, false otherwise
131 */
132
133 template<class T>   
134 bool SomeEnvironment<T>::addConcept(std::string name, int size)
135    {
136       concepts.insert(std::pair< std::string,int >(name,size));
137       int sizeMap=concepts.size();
138       indexesConcepts.insert(std::pair< std::string,int >(name,sizeMap-1));
139       return true;
140    }
141 /*
142 * Validate the index of a concept in an instant
143 */
144 template<class T>
145 bool SomeEnvironment<T>::isValidIndex(int index, int indexInInstant)
146    {
147       std::map<std::string,int>::iterator conceptsIterator=concepts.begin();
148       std::map<std::string,int>::iterator conceptsIndexesIterator;
149       std::string conceptNamei;
150       int indexInInstantConcepti,size;
151       while(conceptsIterator!=concepts.end())
152          {
153             conceptNamei=conceptsIterator->first;
154             size=conceptsIterator->second;
155             indexInInstantConcepti=getIndexConcept(conceptNamei);
156             if(indexInInstantConcepti==indexInInstant)
157                {
158                 if(index<size)
159                    return true;
160                 else
161                    return false;
162                }
163             conceptNamei.clear();
164             conceptsIterator++;
165          }
166       return false;
167    }
168
169 //====== INQUIRY =========
170
171 template<class T>   
172 std::vector<T*>* SomeEnvironment<T>::getThings(Instant* instant)
173    {
174       //cleaning things of instant
175       thingsOfInstant.clear();
176       //
177       
178       typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
179       thingsIterator = things.begin();
180       //setting the environment instant
181       int indexInstantInside=getIndexInstantInExistingInstants(instant);
182       Instant* instantInExistingInstants=NULL;
183       if(indexInstantInside!=-1)
184          {   
185             instantInExistingInstants=existingInstants[indexInstantInside];
186       
187             while(thingsIterator!=things.end() && instantInExistingInstants)
188                {
189                   SomeThing<T>* something=&thingsIterator->second;
190
191                   bool isInInstantB=isInInstant(something,instantInExistingInstants);
192                   if(isInInstantB)
193                   {
194                      T* thing=something->getThing();
195                      thingsOfInstant.push_back(thing);
196                   }   
197                   thingsIterator++;
198                }
199          }
200       return &thingsOfInstant;
201    }
202
203 /*
204 *   Returns the things with their names in the environment
205 *   @param names, vector where is goint to be safe the names 
206 *   of the things in the environment.
207 *   @param things, vector where is going to be save the things
208 *   @return
209 */
210 template<class T>   
211 void SomeEnvironment<T>::getThings(std::vector< std::string >& names,std::vector< T* >& thingsVector, Instant* instant)
212    {
213       typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
214       thingsIterator=things.begin();
215       //setting the environment instant
216       int indexInstantInside=getIndexInstantInExistingInstants(instant);
217       Instant* instantInExistingInstants=NULL;
218       if(indexInstantInside!=-1)
219       {   
220          instantInExistingInstants=existingInstants[indexInstantInside];
221       
222          while(thingsIterator!=things.end() && instantInExistingInstants)
223             {
224                SomeThing<T>* something=&thingsIterator->second;
225                bool isInInstantB=isInInstant(something,instantInExistingInstants);
226                if(isInInstantB)
227                {
228                   std::string nameThingInEnvironment=thingsIterator->first;
229                   T* thing=something->getThing();
230                   thingsVector.push_back(thing);
231                   names.push_back(nameThingInEnvironment);
232                }   
233                thingsIterator++;
234             }
235       }
236    }
237 /*
238 *   Returns the instants where the thing identified by the name
239 *   it should appears
240 *   @param nameThing, name of the thing in the environment
241 *   @return instants of that thing
242 */
243 template<class T>
244 std::vector<Instant*>* SomeEnvironment<T>::getInstantsOfThing(std::string nameThing)
245    {
246       typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
247       thingsIterator=things.find(nameThing);
248       if(thingsIterator!=things.end())
249          {
250             SomeThing<T>* something=&thingsIterator->second;
251             return something->getInstants();
252          }
253       return (std::vector<Instant*>*)NULL;
254    }
255
256 /*
257 *   Returns the instants define in the environment
258 *   @return existing instants in the environment
259 *   A POINTER TO THE  EXISTINGINSTANTS
260 */
261 template<class T>
262 std::vector<Instant*>* SomeEnvironment<T>::getExistingInstants()
263    {
264       return &existingInstants;
265    }
266
267
268 /*
269 *   Returns the size of the concept identified by nameConcept
270 *   @param nameConcept, name of the concept in the environment
271 *   @return size, of the concept given or -1 if the concept doesnt
272 *   exist
273 */
274 template<class T>
275 int SomeEnvironment<T>::getSizeConcept(std::string nameConcept)
276    {
277       std::map< std::string, int>::iterator conceptsIterator;
278       conceptsIterator= concepts.find(nameConcept);
279       if(conceptsIterator!= concepts.end())
280        {
281          return conceptsIterator->second;
282        }
283       else
284          return -1;
285    }
286 /*
287 * returns the index of the  concept in the instants
288 */
289 template<class T>
290 int SomeEnvironment<T>::getIndexConcept(std::string nameConcept)
291    {
292       std::map< std::string, int>::iterator indexesConceptsIterator;
293       indexesConceptsIterator= indexesConcepts.find(nameConcept);
294       if(indexesConceptsIterator!= indexesConcepts.end())
295        {
296          return indexesConceptsIterator->second;
297        }
298       else
299          return -1;
300    }
301 /*
302 *   NEW
303 *   Give the names of the names defined
304 *   @param nameConcepts, vector where is goin to be save the names of the concepts
305 */
306 template<class T>
307 void SomeEnvironment<T>::getConceptsNames(std::vector<std::string>& namesConcepts)
308    {
309       std::map<std::string,int>::iterator iteratorConcepts=concepts.begin();
310       std::string nameConcept;
311       while(iteratorConcepts!=concepts.end())
312          {
313             nameConcept=iteratorConcepts->first;
314             namesConcepts.push_back(nameConcept);
315             iteratorConcepts++;
316          }
317    }
318 /*
319 *   Method that retorns the name of each concept and the size of it.
320 *   @param conceptNameVect, Vector in which is disposed to be setted the name for each of the included concepts
321 *   @param conceptSizeVect, Vector in which is disposed to be setted the size for each of the included concepts
322 */
323 template<class T>
324 void SomeEnvironment<T> :: getConceptsInformation(std::vector<std::string>& conceptNameVect, std::vector<int>& conceptSizeVect)
325 {
326    std::map<std::string,int>::iterator iteratorConcepts=concepts.begin();   
327    while(iteratorConcepts!=concepts.end())
328       {
329          std::string aName = iteratorConcepts->first;
330          int aSize = iteratorConcepts->second;         
331          conceptNameVect.push_back( aName );
332          conceptSizeVect.push_back( aSize );
333          iteratorConcepts++;
334       }
335 }
336
337 /*
338 * returns all the things of the environment
339 */
340 template<class T>
341 void SomeEnvironment<T>::getThingsOfEnvironment(std::vector<T*>* thingsVector)
342    {
343       typename std::map<std::string, SomeThing<T> >::iterator iteratorThings= things.begin();
344       thingsVector->clear();
345       while(iteratorThings!=things.end())
346          {
347             SomeThing<T>* something=&iteratorThings->second;
348             T* thing=something->getThing();
349             thingsVector->push_back(thing);
350             iteratorThings++;
351          }
352    }
353 /*
354 * returns a pointer to the thing with the name given
355 */
356 template<class T>
357 T* SomeEnvironment<T>::getThingByName(std::string name)
358    {
359       T* thing=NULL;
360       typename std::map<std::string, SomeThing<T> >::iterator iteratorThings= things.find(name);
361       if(iteratorThings!=things.end())
362          {
363             SomeThing<T>* something=&iteratorThings->second;
364             thing=something->getThing();
365          }
366       return thing;
367    }
368
369
370 /*
371 * returns the number of concepts defined
372 */
373 template<class T>   
374 int SomeEnvironment<T>::getNumberOfConcepts()
375    {
376       return this->concepts.size();
377    }
378
379    /*
380 * Gets the number of things 
381 * @return Returns the number of existing things in the environment
382 */
383 template<class T>
384 int SomeEnvironment<T>::getNumberOfThings ()
385    {
386       return things.size();
387    }
388    
389
390 //====== ACCESS ==========
391 /*
392 *   Remove a thing from all the instants, it means from the program
393 *   @param name: name of the thing
394 *   @return: true if the removed is succesful (false if the thing doesn exist)
395 */
396 template<class T>   
397 bool SomeEnvironment<T>::removeThing(std::string name)
398    {
399       typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
400       thingsIterator=things.find(name);
401       if(thingsIterator!=things.end())
402          {
403             things.erase(thingsIterator);
404             return true;
405          }
406       return false;
407    }
408 /*
409 *   Removes the thing with the name given, from the instant
410 *   given
411 *   @param name: name of the thing
412 *   @param Instant: Instant from which it will be removed
413 *   @return true: if the removed is succesful (false if the thing doesn exist)
414 */
415 template<class T>   
416 bool SomeEnvironment<T>::removeThingFromInstant(std::string name, Instant* instant)
417    {
418       typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
419       thingsIterator=things.find(name);
420       //setting the environment instant
421       int indexInstantInside=getIndexInstantInExistingInstants(instant);
422       Instant* instantInExistingInstants=NULL;
423       if(indexInstantInside!=-1)
424          instantInExistingInstants=existingInstants[indexInstantInside];
425             
426       if(thingsIterator!=things.end() && instantInExistingInstants)
427          {
428             SomeThing<T>* something=&thingsIterator->second;
429             
430             bool isInInstantB=isInInstant(something,instantInExistingInstants);
431             if(isInInstantB)
432                {
433                   something->removeInstant(instant);
434                   return true;
435                }
436          }
437       return false;
438    }
439
440 /*
441 *   Remove Instant from the program, it means, that from all the
442 *   somethings that have that instant, after call this method them 
443 *   doesnt have that instant anymore
444 *   @param instant: instant that is going to be removed
445 *   @return true: if the removed is succesful (false other wise)
446 */
447 template<class T>   
448 bool SomeEnvironment<T>::removeInstant(Instant* instant)
449    {
450       typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
451       thingsIterator=things.begin();
452       bool ok=false;
453    
454       //setting the environment instant
455       int indexInstantInside=getIndexInstantInExistingInstants(instant);
456       Instant* instantInExistingInstants=NULL;
457       if(indexInstantInside!=-1)
458          instantInExistingInstants=existingInstants[indexInstantInside];
459       
460       while(thingsIterator!=things.end() && instantInExistingInstants)
461          {
462             SomeThing<T>* something=&thingsIterator->second;
463             bool isInInstantB=isInInstant(something,instantInExistingInstants);
464             if(isInInstantB)
465                ok = something->removeInstant(instantInExistingInstants);
466             thingsIterator++;
467             
468          }
469       //removing from inside instants
470       removeInstantFromExistingInstants(instant);
471       return ok;
472    }
473 /*
474 *   Remove a dimension from the environment
475 *   THE REMOVE OF THE CONCEPT IS WHEN IS BEING DEFINED THE ENVIRONMENT ,NO IN
476 *   EXECUTION
477 *   PRECONDITION
478 *   the name of the concept given is already added to the environment
479 *   @param nameConcept, name of the concept to remove
480 */
481 template<class T>      
482 bool SomeEnvironment<T>::removeConcept(std::string nameConcept)
483    {
484       std::map < std::string,int >::iterator conceptsIterator;
485       conceptsIterator=concepts.find(nameConcept);
486       if(conceptsIterator!=concepts.end())
487          {
488             concepts.erase(conceptsIterator);
489             return true;
490          }
491       return false;
492    }
493 //====== PRIVATE METHODS=========
494 /*
495 *   Checks if in the instant given the something given has it
496 *   @param instant, instant to check
497 *   @param something;, something to check in the instant
498 *   @return
499 */
500 template<class T>   
501 bool SomeEnvironment<T>::isInInstant(SomeThing<T>* something,Instant *instant)
502 {
503    //borrame
504    for(int i=0;i<instant->getSize();i++)
505       /// \TODO fix warning unused variable k
506       int k=instant->getIndexInConcept(i);
507    //         
508    int index=something->hasInstant(instant);
509    if(index!=-1)
510       return true;
511    else
512       return false;
513 }
514 /*
515 *   Remove the instant from the environment
516 *   @param instant, to be erased
517 *   @return
518 */
519 template<class T>   
520 void  SomeEnvironment<T>::removeInstantFromExistingInstants(Instant* instant)
521    {
522       std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
523       bool isEquals=false;
524       while(existingInstantsIterator!=existingInstants.end()&& !isEquals)
525          {
526             isEquals=(*existingInstantsIterator)->isEquals(instant);
527             if(isEquals)
528                existingInstants.erase(existingInstantsIterator);
529             existingInstantsIterator++;
530          }
531    }
532
533 /*
534 *   Adds an instant to the existing instants, verifying if
535 *   the instant is already added
536 *   @param instant, to be added
537 *   @return
538 */
539 template<class T>   
540 int SomeEnvironment<T>::addInstant(Instant* instant)
541    {
542       std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
543       int i=0;
544       bool isEquals=false;
545       while(existingInstantsIterator!=existingInstants.end() && !isEquals)
546          {
547             Instant* existingInstant=*existingInstantsIterator;
548             //borrame
549             int sizeI=existingInstant->getSize();
550             for(int k=0;k<sizeI;k++)
551             /// \ TODO fix warning unused variable r
552                int r=existingInstant->getIndexInConcept(k);
553             //
554             isEquals=existingInstant->isEquals(instant);
555             existingInstantsIterator++;
556             if(!isEquals)
557                i++;  
558          }
559       if(!isEquals)
560          {
561             //copying the instant's information
562             std::vector<int>* instantVector=new std::vector<int>();
563             int sizeInstant=instant->getSize();
564             int k;
565             for(k=0;k<sizeInstant;k++)
566                {
567                  int d=instant->getIndexInConcept(k);
568                  instantVector->push_back(d);
569                }
570             //
571             //saving the instant given in the handler
572             Instant* instantToAdded= new Instant(instantVector);
573             existingInstants.push_back(instantToAdded);
574             i=existingInstants.size()-1;
575          }
576       return i;
577    }
578 /*
579 *   Return the index in the existing instants of the instant 
580 *   that the user had given
581 *   @param instant, instant for which we are looking the index in
582 *   the  existings intants in the environment
583 *   @return an index in (0,existingInstants.size()-1)or -1 if the instant hasnt
584 *   being defined
585 */
586 template<class T>   
587 int SomeEnvironment<T>::getIndexInstantInExistingInstants(Instant* instant)
588    {
589       std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
590       int i=0;
591       bool isEquals=false;
592       while(existingInstantsIterator!=existingInstants.end() && !isEquals)
593          {
594             isEquals=(*existingInstantsIterator)->isEquals(instant);
595             existingInstantsIterator++;
596             if(!isEquals)
597                i++;
598          }
599       if(!isEquals)
600             return -1;
601       else
602             return i;
603    }