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()
35 //deleting existing instants
36 size=existingInstants.size();
38 delete existingInstants[i];
39 existingInstants.clear();
45 indexesConcepts.clear();
47 thingsOfInstant.clear();
50 //====== OPERATIONS =======
52 * Adds a thing to the program
53 * @param name: name of the thing
54 * @param thing: thing to be added
55 * @return true if the thing was succesfully added
59 bool SomeEnvironment<T>:: addThing(std::string name,T thing)
61 SomeThing<T> something (name);
62 something.setThing(thing);
63 things.insert(std::pair < std::string, SomeThing<T> >(name,something));
69 * Add thing with the name given to the instant given
71 * the thing that has that name IS ALREADY ADDED TO THE PROGRAM
72 * @param name: name of the EXISTANT thing
73 * @param instant: instant associated to the thing
74 * @return true if the thing was succesfully added
77 bool SomeEnvironment<T>::addInstantToThing(std::string name,Instant* instant)
79 typename std::map< std::string,SomeThing<T> >::iterator thingsIterator;
80 thingsIterator=things.find(name);
81 if(thingsIterator != things.end())
85 SomeThing<T>* something=&thingsIterator->second;
86 //getting the address of the instant added to the environment
87 // in the existing instants; EVERYTHING IS HANDLED INSIDE
90 int indexInstantInside=addInstant(instant);
92 //int indexInstantInside=getIndexInstantInExistingInstants(instant);
93 Instant* instantInExistingInstants=NULL;
94 if(indexInstantInside!=-1)
96 instantInExistingInstants=existingInstants[indexInstantInside];
97 something->addInstant(instantInExistingInstants);
106 * Add thing with the name given, and the data given in the instant given
107 * @param name: name of the thing
108 * @param thing: information of the thing to be added
109 * @param instant: instant associated to the thing
110 * @return true if the thing was succesfully added
113 bool SomeEnvironment<T>::addThingWithInstant(std::string name,T thing,Instant* instant)
115 SomeThing<T> something(name);
116 something.setThing(thing);
118 int indexInstantInside=addInstant(instant);
120 //int indexInstantInside=getIndexInstantInExistingInstants(instant);
121 if(indexInstantInside!=-1)
123 Instant* instantInExistingInstants = existingInstants[indexInstantInside];
124 something.addInstant(instantInExistingInstants);
125 things.insert(std::pair< std::string, SomeThing<T> >(name,something));
131 * Add a concept to the environment
132 * @param name: name of the concept
133 * @param size: size of the concept, it means that its indexes are
134 * distributed 0..size-1
135 * @return true if succesful, false otherwise
139 bool SomeEnvironment<T>::addConcept(std::string name, int size)
141 concepts.insert(std::pair< std::string,int >(name,size));
142 int sizeMap=concepts.size();
143 indexesConcepts.insert(std::pair< std::string,int >(name,sizeMap-1));
147 * Validate the index of a concept in an instant
150 bool SomeEnvironment<T>::isValidIndex(int index, int indexInInstant)
152 std::map<std::string,int>::iterator conceptsIterator=concepts.begin();
153 std::map<std::string,int>::iterator conceptsIndexesIterator;
154 std::string conceptNamei;
155 int indexInInstantConcepti,size;
156 while(conceptsIterator!=concepts.end())
158 conceptNamei=conceptsIterator->first;
159 size=conceptsIterator->second;
160 indexInInstantConcepti=getIndexConcept(conceptNamei);
161 if(indexInInstantConcepti==indexInInstant)
168 conceptNamei.clear();
175 //====== INQUIRY =========
178 std::vector<T*>* SomeEnvironment<T>::getThings(Instant* instant)
180 //cleaning things of instant
181 thingsOfInstant.clear();
184 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
185 thingsIterator = things.begin();
186 //setting the environment instant
187 int indexInstantInside=getIndexInstantInExistingInstants(instant);
188 Instant* instantInExistingInstants=NULL;
189 if(indexInstantInside!=-1)
191 instantInExistingInstants=existingInstants[indexInstantInside];
193 while(thingsIterator!=things.end() && instantInExistingInstants)
195 SomeThing<T>* something=&thingsIterator->second;
197 bool isInInstantB=isInInstant(something,instantInExistingInstants);
200 T* thing=something->getThing();
201 thingsOfInstant.push_back(thing);
208 return &thingsOfInstant;
213 * Returns the things with their names in the environment
214 * @param names, vector where is goint to be safe the names
215 * of the things in the environment.
216 * @param things, vector where is going to be save the things
220 void SomeEnvironment<T>::getThings(std::vector< std::string >& names,std::vector< T* >& thingsVector, Instant* instant)
222 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
223 thingsIterator=things.begin();
224 //setting the environment instant
225 int indexInstantInside=getIndexInstantInExistingInstants(instant);
226 Instant* instantInExistingInstants=NULL;
227 if(indexInstantInside!=-1)
229 instantInExistingInstants=existingInstants[indexInstantInside];
231 while(thingsIterator!=things.end() && instantInExistingInstants)
233 SomeThing<T>* something=&thingsIterator->second;
234 bool isInInstantB=isInInstant(something,instantInExistingInstants);
237 std::string nameThingInEnvironment=thingsIterator->first;
238 T* thing=something->getThing();
239 thingsVector.push_back(thing);
240 names.push_back(nameThingInEnvironment);
248 * Returns the instants where the thing identified by the name
250 * @param nameThing, name of the thing in the environment
251 * @return instants of that thing
254 std::vector<Instant*>* SomeEnvironment<T>::getInstantsOfThing(std::string nameThing)
256 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
257 thingsIterator=things.find(nameThing);
258 if(thingsIterator!=things.end())
260 SomeThing<T>* something=&thingsIterator->second;
261 return something->getInstants();
263 return (std::vector<Instant*>*)NULL;
267 * Returns the instants define in the environment
268 * @return existing instants in the environment
269 * A POINTER TO THE EXISTINGINSTANTS
272 std::vector<Instant*>* SomeEnvironment<T>::getExistingInstants()
274 return &existingInstants;
279 * Returns the size of the concept identified by nameConcept
280 * @param nameConcept, name of the concept in the environment
281 * @return size, of the concept given or -1 if the concept doesnt
285 int SomeEnvironment<T>::getSizeConcept(std::string nameConcept)
287 std::map< std::string, int>::iterator conceptsIterator;
288 conceptsIterator= concepts.find(nameConcept);
289 if(conceptsIterator!= concepts.end())
291 return conceptsIterator->second;
297 * returns the index of the concept in the instants
300 int SomeEnvironment<T>::getIndexConcept(std::string nameConcept)
302 std::map< std::string, int>::iterator indexesConceptsIterator;
303 indexesConceptsIterator= indexesConcepts.find(nameConcept);
304 if(indexesConceptsIterator!= indexesConcepts.end())
306 return indexesConceptsIterator->second;
313 * Give the names of the names defined
314 * @param nameConcepts, vector where is goin to be save the names of the concepts
317 void SomeEnvironment<T>::getConceptsNames(std::vector<std::string>& namesConcepts)
319 std::map<std::string,int>::iterator iteratorConcepts=concepts.begin();
320 std::string nameConcept;
321 while(iteratorConcepts!=concepts.end())
323 nameConcept=iteratorConcepts->first;
324 namesConcepts.push_back(nameConcept);
329 * Method that retorns the name of each concept and the size of it.
330 * @param conceptNameVect, Vector in which is disposed to be setted the name for each of the included concepts
331 * @param conceptSizeVect, Vector in which is disposed to be setted the size for each of the included concepts
334 void SomeEnvironment<T> :: getConceptsInformation(std::vector<std::string>& conceptNameVect, std::vector<int>& conceptSizeVect)
336 std::map<std::string,int>::iterator iteratorConcepts=concepts.begin();
337 while(iteratorConcepts!=concepts.end())
339 std::string aName = iteratorConcepts->first;
340 int aSize = iteratorConcepts->second;
341 conceptNameVect.push_back( aName );
342 conceptSizeVect.push_back( aSize );
348 * returns all the things of the environment
351 void SomeEnvironment<T>::getThingsOfEnvironment(std::vector<T*>* thingsVector)
353 typename std::map<std::string, SomeThing<T> >::iterator iteratorThings= things.begin();
354 thingsVector->clear();
355 while(iteratorThings!=things.end())
357 SomeThing<T>* something=&iteratorThings->second;
358 T* thing=something->getThing();
359 thingsVector->push_back(thing);
365 * returns a pointer to the thing with the name given
368 T* SomeEnvironment<T>::getThingByName(std::string name)
371 typename std::map<std::string, SomeThing<T> >::iterator iteratorThings= things.find(name);
372 if(iteratorThings!=things.end())
374 SomeThing<T>* something=&iteratorThings->second;
375 thing=something->getThing();
382 * returns the number of concepts defined
385 int SomeEnvironment<T>::getNumberOfConcepts()
387 return this->concepts.size();
391 * Gets the number of things
392 * @return Returns the number of existing things in the environment
395 int SomeEnvironment<T>::getNumberOfThings ()
397 return things.size();
401 //====== ACCESS ==========
403 * Remove a thing from all the instants, it means from the program
404 * @param name: name of the thing
405 * @return: true if the removed is succesful (false if the thing doesn exist)
408 bool SomeEnvironment<T>::removeThing(std::string name)
410 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
411 thingsIterator=things.find(name);
412 if(thingsIterator!=things.end())
414 things.erase(thingsIterator);
420 * Removes the thing with the name given, from the instant
422 * @param name: name of the thing
423 * @param Instant: Instant from which it will be removed
424 * @return true: if the removed is succesful (false if the thing doesn exist)
427 bool SomeEnvironment<T>::removeThingFromInstant(std::string name, Instant* instant)
429 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
430 thingsIterator=things.find(name);
431 //setting the environment instant
432 int indexInstantInside=getIndexInstantInExistingInstants(instant);
433 Instant* instantInExistingInstants=NULL;
434 if(indexInstantInside!=-1)
435 instantInExistingInstants=existingInstants[indexInstantInside];
437 if(thingsIterator!=things.end() && instantInExistingInstants)
439 SomeThing<T>* something=&thingsIterator->second;
441 bool isInInstantB=isInInstant(something,instantInExistingInstants);
444 something->removeInstant(instant);
452 * Remove Instant from the program, it means, that from all the
453 * somethings that have that instant, after call this method them
454 * doesnt have that instant anymore
455 * @param instant: instant that is going to be removed
456 * @return true: if the removed is succesful (false other wise)
459 bool SomeEnvironment<T>::removeInstant(Instant* instant)
461 typename std::map < std::string,SomeThing<T> >::iterator thingsIterator;
462 thingsIterator=things.begin();
465 //setting the environment instant
466 int indexInstantInside=getIndexInstantInExistingInstants(instant);
467 Instant* instantInExistingInstants=NULL;
468 if(indexInstantInside!=-1)
469 instantInExistingInstants=existingInstants[indexInstantInside];
471 while(thingsIterator!=things.end() && instantInExistingInstants)
473 SomeThing<T>* something=&thingsIterator->second;
474 bool isInInstantB=isInInstant(something,instantInExistingInstants);
476 ok = something->removeInstant(instantInExistingInstants);
480 //removing from inside instants
481 removeInstantFromExistingInstants(instant);
486 * Remove a dimension from the environment
487 * THE REMOVE OF THE CONCEPT IS WHEN IS BEING DEFINED THE ENVIRONMENT ,NO IN
490 * the name of the concept given is already added to the environment
491 * @param nameConcept, name of the concept to remove
494 bool SomeEnvironment<T>::removeConcept(std::string nameConcept)
496 std::map < std::string,int >::iterator conceptsIterator;
497 conceptsIterator=concepts.find(nameConcept);
498 if(conceptsIterator!=concepts.end())
500 concepts.erase(conceptsIterator);
505 //====== PRIVATE METHODS=========
507 * Checks if in the instant given the something given has it
508 * @param instant, instant to check
509 * @param something;, something to check in the instant
513 bool SomeEnvironment<T>::isInInstant(SomeThing<T>* something,Instant *instant)
516 for(int i=0;i<instant->getSize();i++)
517 int k=instant->getIndexInConcept(i);
519 int index=something->hasInstant(instant);
526 * Remove the instant from the environment
527 * @param instant, to be erased
531 void SomeEnvironment<T>::removeInstantFromExistingInstants(Instant* instant)
533 std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
535 while(existingInstantsIterator!=existingInstants.end()&& !isEquals)
537 isEquals=(*existingInstantsIterator)->isEquals(instant);
539 existingInstants.erase(existingInstantsIterator);
540 existingInstantsIterator++;
547 * Adds an instant to the existing instants, verifying if
548 * the instant is already added
549 * @param instant, to be added
553 int SomeEnvironment<T>::addInstant(Instant* instant)
555 std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
558 while(existingInstantsIterator!=existingInstants.end() && !isEquals)
560 Instant* existingInstant=*existingInstantsIterator;
562 int sizeI=existingInstant->getSize();
563 for(int k=0;k<sizeI;k++)
564 int r=existingInstant->getIndexInConcept(k);
566 isEquals=existingInstant->isEquals(instant);
567 existingInstantsIterator++;
574 //copying the instant's information
575 std::vector<int>* instantVector=new std::vector<int>();
576 int sizeInstant=instant->getSize();
578 for(k=0;k<sizeInstant;k++)
580 int d=instant->getIndexInConcept(k);
581 instantVector->push_back(d);
584 //saving the instant given in the handler
585 Instant* instantToAdded= new Instant(instantVector);
586 existingInstants.push_back(instantToAdded);
587 i=existingInstants.size()-1;
592 * Return the index in the existing instants of the instant
593 * that the user had given
594 * @param instant, instant for which we are looking the index in
595 * the existings intants in the environment
596 * @return an index in (0,existingInstants.size()-1)or -1 if the instant hasnt
600 int SomeEnvironment<T>::getIndexInstantInExistingInstants(Instant* instant)
602 std::vector<Instant*>::iterator existingInstantsIterator=existingInstants.begin();
605 while(existingInstantsIterator!=existingInstants.end() && !isEquals)
607 isEquals=(*existingInstantsIterator)->isEquals(instant);
608 existingInstantsIterator++;