17 //====== LIFECYCLE ========
19 SomeEnvironment<T>::SomeEnvironment()
23 SomeEnvironment<T>::SomeEnvironment(std::map<std::string,int>* concepts,std::map< std::string,SomeThing<T> >* things)
25 this->concepts=concepts;
30 SomeEnvironment<T>::~SomeEnvironment()
34 //deleting existing instants
35 size=existingInstants.size();
37 delete existingInstants[i];
38 existingInstants.clear();
44 indexesConcepts.clear();
46 thingsOfInstant.clear();
49 //====== OPERATIONS =======
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
58 bool SomeEnvironment<T>:: addThing(std::string name,T thing)
60 SomeThing<T> something (name);
61 something.setThing(thing);
62 things.insert(std::pair < std::string, SomeThing<T> >(name,something));
67 * Add thing with the name given to the instant given
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
75 bool SomeEnvironment<T>::addInstantToThing(std::string name,Instant* instant)
77 typename std::map< std::string,SomeThing<T> >::iterator thingsIterator;
78 thingsIterator=things.find(name);
79 if(thingsIterator != things.end())
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
86 int indexInstantInside=addInstant(instant);
88 //int indexInstantInside=getIndexInstantInExistingInstants(instant);
89 Instant* instantInExistingInstants=NULL;
90 if(indexInstantInside!=-1)
92 instantInExistingInstants=existingInstants[indexInstantInside];
93 something->addInstant(instantInExistingInstants);
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
108 bool SomeEnvironment<T>::addThingWithInstant(std::string name,T thing,Instant* instant)
110 SomeThing<T> something(name);
111 something.setThing(thing);
113 int indexInstantInside=addInstant(instant);
115 //int indexInstantInside=getIndexInstantInExistingInstants(instant);
116 if(indexInstantInside!=-1)
118 Instant* instantInExistingInstants = existingInstants[indexInstantInside];
119 something.addInstant(instantInExistingInstants);
120 things.insert(std::pair< std::string, SomeThing<T> >(name,something));
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
134 bool SomeEnvironment<T>::addConcept(std::string name, int size)
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));
142 * Validate the index of a concept in an instant
145 bool SomeEnvironment<T>::isValidIndex(int index, int indexInInstant)
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())
153 conceptNamei=conceptsIterator->first;
154 size=conceptsIterator->second;
155 indexInInstantConcepti=getIndexConcept(conceptNamei);
156 if(indexInInstantConcepti==indexInInstant)
163 conceptNamei.clear();
169 //====== INQUIRY =========
172 std::vector<T*>* SomeEnvironment<T>::getThings(Instant* instant)
174 //cleaning things of instant
175 thingsOfInstant.clear();
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)
185 instantInExistingInstants=existingInstants[indexInstantInside];
187 while(thingsIterator!=things.end() && instantInExistingInstants)
189 SomeThing<T>* something=&thingsIterator->second;
191 bool isInInstantB=isInInstant(something,instantInExistingInstants);
194 T* thing=something->getThing();
195 thingsOfInstant.push_back(thing);
200 return &thingsOfInstant;
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
211 void SomeEnvironment<T>::getThings(std::vector< std::string >& names,std::vector< T* >& thingsVector, Instant* instant)
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)
220 instantInExistingInstants=existingInstants[indexInstantInside];
222 while(thingsIterator!=things.end() && instantInExistingInstants)
224 SomeThing<T>* something=&thingsIterator->second;
225 bool isInInstantB=isInInstant(something,instantInExistingInstants);
228 std::string nameThingInEnvironment=thingsIterator->first;
229 T* thing=something->getThing();
230 thingsVector.push_back(thing);
231 names.push_back(nameThingInEnvironment);
238 * Returns the instants where the thing identified by the name
240 * @param nameThing, name of the thing in the environment
241 * @return instants of that thing
244 std::vector<Instant*>* SomeEnvironment<T>::getInstantsOfThing(std::string nameThing)
246 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
247 thingsIterator=things.find(nameThing);
248 if(thingsIterator!=things.end())
250 SomeThing<T>* something=&thingsIterator->second;
251 return something->getInstants();
253 return (std::vector<Instant*>*)NULL;
257 * Returns the instants define in the environment
258 * @return existing instants in the environment
259 * A POINTER TO THE EXISTINGINSTANTS
262 std::vector<Instant*>* SomeEnvironment<T>::getExistingInstants()
264 return &existingInstants;
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
275 int SomeEnvironment<T>::getSizeConcept(std::string nameConcept)
277 std::map< std::string, int>::iterator conceptsIterator;
278 conceptsIterator= concepts.find(nameConcept);
279 if(conceptsIterator!= concepts.end())
281 return conceptsIterator->second;
287 * returns the index of the concept in the instants
290 int SomeEnvironment<T>::getIndexConcept(std::string nameConcept)
292 std::map< std::string, int>::iterator indexesConceptsIterator;
293 indexesConceptsIterator= indexesConcepts.find(nameConcept);
294 if(indexesConceptsIterator!= indexesConcepts.end())
296 return indexesConceptsIterator->second;
303 * Give the names of the names defined
304 * @param nameConcepts, vector where is goin to be save the names of the concepts
307 void SomeEnvironment<T>::getConceptsNames(std::vector<std::string>& namesConcepts)
309 std::map<std::string,int>::iterator iteratorConcepts=concepts.begin();
310 std::string nameConcept;
311 while(iteratorConcepts!=concepts.end())
313 nameConcept=iteratorConcepts->first;
314 namesConcepts.push_back(nameConcept);
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
324 void SomeEnvironment<T> :: getConceptsInformation(std::vector<std::string>& conceptNameVect, std::vector<int>& conceptSizeVect)
326 std::map<std::string,int>::iterator iteratorConcepts=concepts.begin();
327 while(iteratorConcepts!=concepts.end())
329 std::string aName = iteratorConcepts->first;
330 int aSize = iteratorConcepts->second;
331 conceptNameVect.push_back( aName );
332 conceptSizeVect.push_back( aSize );
338 * returns all the things of the environment
341 void SomeEnvironment<T>::getThingsOfEnvironment(std::vector<T*>* thingsVector)
343 typename std::map<std::string, SomeThing<T> >::iterator iteratorThings= things.begin();
344 thingsVector->clear();
345 while(iteratorThings!=things.end())
347 SomeThing<T>* something=&iteratorThings->second;
348 T* thing=something->getThing();
349 thingsVector->push_back(thing);
354 * returns a pointer to the thing with the name given
357 T* SomeEnvironment<T>::getThingByName(std::string name)
360 typename std::map<std::string, SomeThing<T> >::iterator iteratorThings= things.find(name);
361 if(iteratorThings!=things.end())
363 SomeThing<T>* something=&iteratorThings->second;
364 thing=something->getThing();
371 * returns the number of concepts defined
374 int SomeEnvironment<T>::getNumberOfConcepts()
376 return this->concepts.size();
380 * Gets the number of things
381 * @return Returns the number of existing things in the environment
384 int SomeEnvironment<T>::getNumberOfThings ()
386 return things.size();
390 //====== ACCESS ==========
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)
397 bool SomeEnvironment<T>::removeThing(std::string name)
399 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
400 thingsIterator=things.find(name);
401 if(thingsIterator!=things.end())
403 things.erase(thingsIterator);
409 * Removes the thing with the name given, from the instant
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)
416 bool SomeEnvironment<T>::removeThingFromInstant(std::string name, Instant* instant)
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];
426 if(thingsIterator!=things.end() && instantInExistingInstants)
428 SomeThing<T>* something=&thingsIterator->second;
430 bool isInInstantB=isInInstant(something,instantInExistingInstants);
433 something->removeInstant(instant);
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)
448 bool SomeEnvironment<T>::removeInstant(Instant* instant)
450 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
451 thingsIterator=things.begin();
454 //setting the environment instant
455 int indexInstantInside=getIndexInstantInExistingInstants(instant);
456 Instant* instantInExistingInstants=NULL;
457 if(indexInstantInside!=-1)
458 instantInExistingInstants=existingInstants[indexInstantInside];
460 while(thingsIterator!=things.end() && instantInExistingInstants)
462 SomeThing<T>* something=&thingsIterator->second;
463 bool isInInstantB=isInInstant(something,instantInExistingInstants);
465 ok = something->removeInstant(instantInExistingInstants);
469 //removing from inside instants
470 removeInstantFromExistingInstants(instant);
474 * Remove a dimension from the environment
475 * THE REMOVE OF THE CONCEPT IS WHEN IS BEING DEFINED THE ENVIRONMENT ,NO IN
478 * the name of the concept given is already added to the environment
479 * @param nameConcept, name of the concept to remove
482 bool SomeEnvironment<T>::removeConcept(std::string nameConcept)
484 std::map < std::string,int >::iterator conceptsIterator;
485 conceptsIterator=concepts.find(nameConcept);
486 if(conceptsIterator!=concepts.end())
488 concepts.erase(conceptsIterator);
493 //====== PRIVATE METHODS=========
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
501 bool SomeEnvironment<T>::isInInstant(SomeThing<T>* something,Instant *instant)
504 for(int i=0;i<instant->getSize();i++)
505 /// \TODO fix warning unused variable k
506 int k=instant->getIndexInConcept(i);
508 int index=something->hasInstant(instant);
515 * Remove the instant from the environment
516 * @param instant, to be erased
520 void SomeEnvironment<T>::removeInstantFromExistingInstants(Instant* instant)
522 std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
524 while(existingInstantsIterator!=existingInstants.end()&& !isEquals)
526 isEquals=(*existingInstantsIterator)->isEquals(instant);
528 existingInstants.erase(existingInstantsIterator);
529 existingInstantsIterator++;
534 * Adds an instant to the existing instants, verifying if
535 * the instant is already added
536 * @param instant, to be added
540 int SomeEnvironment<T>::addInstant(Instant* instant)
542 std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
545 while(existingInstantsIterator!=existingInstants.end() && !isEquals)
547 Instant* existingInstant=*existingInstantsIterator;
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);
554 isEquals=existingInstant->isEquals(instant);
555 existingInstantsIterator++;
561 //copying the instant's information
562 std::vector<int>* instantVector=new std::vector<int>();
563 int sizeInstant=instant->getSize();
565 for(k=0;k<sizeInstant;k++)
567 int d=instant->getIndexInConcept(k);
568 instantVector->push_back(d);
571 //saving the instant given in the handler
572 Instant* instantToAdded= new Instant(instantVector);
573 existingInstants.push_back(instantToAdded);
574 i=existingInstants.size()-1;
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
587 int SomeEnvironment<T>::getIndexInstantInExistingInstants(Instant* instant)
589 std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
592 while(existingInstantsIterator!=existingInstants.end() && !isEquals)
594 isEquals=(*existingInstantsIterator)->isEquals(instant);
595 existingInstantsIterator++;