1 /*=========================================================================
3 Program: instantHandler
4 Module: $RCSfile: InstantHandler_Txx.h,v $
6 Date: $Date: 2008/10/31 15:08:40 $
7 Version: $Revision: 1.1 $
8 Objective: Handles the change of instant among the concept already inscribed
9 Authot: Monica Maria Lozano Romero
14 This software is distributed WITHOUT ANY WARRANTY; without even
15 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 PURPOSE. See the above copyright notice for more information.
19 =========================================================================*/
28 //#include "Instant.h"
35 //====== LIFECYCLE ========
38 InstantHandler<P>::InstantHandler(SomeEnvironment<P>* environment)throw (ViewerNDimensionsException)
42 this->hasPrevious=true;
43 this->actualConcept=NULL;
48 this->environment=environment;
49 this->actualInstant=new Instant(environment->getNumberOfConcepts());
53 this->environment=NULL;
54 this->actualInstant=NULL;
55 throw new ViewerNDimensionsException("ERROR ENVIRONMENT NULL");
61 InstantHandler<P>::~InstantHandler()
64 //deleting conceptsToChange
65 size=getSizeConceptsToChange();
68 if(conceptsToChange[i])
69 delete conceptsToChange[i];
71 this->conceptsToChange.clear();
72 //deleting actual concept
73 if(actualInstant) delete actualInstant;
79 //====== OPERATIONS =======
81 * Adds a concept to handled in the instant, it means, when
82 * the next instant is used we are going to change this concepts in the instant
84 * the calls to this functions are made in order of position, where the last position (it means
85 * the higher position defined) is the concept thats is going to handled the real time
86 * THE LAST POSITION HANDLEDS THE REAL TIME DOESNT MATTER THE CONCEPT
88 * The user wants to reproduce doctors, pacients and time, and positions given are:
89 * 0=reproduce doctors, 1= reproduce pacients, 2= time, then 1,2 are going to be reproduced
90 * by PLUS PLUS, and time by REAL TIME
91 * @param nameConcept, name of the concept
92 * @param mode, mode of change
93 * @param position, position of reproduction
94 * @return true if succesful, false otherwise
97 bool InstantHandler<P>::addConceptToHandled(std::string nameConcept,int mode,int position) throw (ViewerNDimensionsException)
101 //creating the concept
102 ConceptInstantHandler* conceptInstantHandler=new ConceptInstantHandler(nameConcept,mode,position);
103 //looking for its index
104 int indexInInstant=environment->getIndexConcept(nameConcept);
105 if(indexInInstant!=-1)
106 conceptInstantHandler->setIndexInInstant(indexInInstant);
108 throw new ViewerNDimensionsException("ERROR CONCEPT ISN'T IN ENVIRONMENT");
109 //setting it maximums
110 int maximumIndex=environment->getSizeConcept(nameConcept);
112 conceptInstantHandler->setMaximumIndex(maximumIndex);
114 throw new ViewerNDimensionsException("ERROR CONCEPT ISN'T IN ENVIRONMENT");
116 this->conceptsToChange.push_back(conceptInstantHandler);
117 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
118 actualInstant->setConcept(indexInInstant,conceptInstantHandler->geActualIndex());
129 * Next instant of the concepts to change
131 * conceptsToChange is ordered by the position of the concepts handled
136 void InstantHandler<P>::nextInstant() throw (ViewerNDimensionsException)
140 bool hasNextIndex=actualConcept->hasNextIndex();
141 int position=actualConcept->getPosition();
144 actualConcept->nextIndex();
145 actualInstant->setConcept(actualConcept->getIndexInInstant(),actualConcept->geActualIndex());
150 while( (position-1)>=0 && !hasNextIndex)
152 actualConcept=conceptsToChange[position-1];
153 hasNextIndex=actualConcept->hasNextIndex();
154 position=actualConcept->getPosition();
158 actualConcept->nextIndex();
159 actualInstant->setConcept(actualConcept->getIndexInInstant(),actualConcept->geActualIndex());
161 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
164 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
171 throw new ViewerNDimensionsException("ERROR THERE ISN'T CONCEPTS TO HANDLED IN THE PLAYER");
176 * Previous instant of the actual concept
181 void InstantHandler<P>::previousInstant()throw (ViewerNDimensionsException)
185 bool hasPreviousIndex=actualConcept->hasPreviousIndex();
186 int position=actualConcept->getPosition();
189 actualConcept->previousIndex();
190 actualInstant->setConcept(actualConcept->getIndexInInstant(),actualConcept->geActualIndex());
195 while( (position-1)>=0 && !hasPreviousIndex)
197 actualConcept=conceptsToChange[position-1];
198 hasPreviousIndex=actualConcept->hasPreviousIndex();
199 position=actualConcept->getPosition();
203 actualConcept->previousIndex();
204 actualInstant->setConcept(actualConcept->getIndexInInstant(),actualConcept->geActualIndex());
206 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
209 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
216 throw new ViewerNDimensionsException("ERROR THERE ISN'T CONCEPTS TO HANDLED IN THE PLAYER");
220 * Removes all the concept handled
223 void InstantHandler<P>::removeAllConcepts()
226 size=getSizeConceptsToChange();
229 if(conceptsToChange[i])
230 delete conceptsToChange[i];
232 conceptsToChange.clear();
238 * Removes the concept with the name given
239 * @param nameConcept, name of the concept to remove
240 * WARNING the user know wich concept is erasing, also knows the
241 * mode of other concepts, if the user is deleting the concept that
242 * handled the real time, should change the mode to other concept that exist
245 void InstantHandler<P>::removeConceptToHandled(std::string nameConcept)
248 size=getSizeConceptsToChange();
249 std::vector<ConceptInstantHandler*>::iterator iteratorConcepts=conceptsToChange.begin();
250 ConceptInstantHandler* concepti;
251 std::string nameConcepti;
252 bool isConcept=false;
253 for(i=0;i<size && !isConcept;i++)
255 concepti=conceptsToChange[i];
256 nameConcepti=concepti->getName();
257 if(nameConcepti.compare(nameConcept)==0)
259 if(actualConcept==concepti)
261 conceptsToChange.erase(iteratorConcepts);
262 actualConcept=conceptsToChange[size-1];
270 * sets all the concept to handled in 0
273 void InstantHandler<P>::resetConceptsHandled()
276 size=getSizeConceptsToChange();
277 ConceptInstantHandler* concepti;
278 int indexInInstant=-1;
281 concepti=conceptsToChange[i];
282 concepti->setActualIndex(0);
283 indexInInstant=concepti->getIndexInInstant();
284 actualInstant->setConcept(indexInInstant,0);
294 void InstantHandler<P>::pause()
296 time_t seconds=time(NULL);
297 pauseTime=seconds*1000;
300 //====== INQUIRY =========
303 * get the actual instant fot the concept identified by the name
306 Instant* InstantHandler<P>::getActualInstant()throw (ViewerNDimensionsException)
308 if(this->actualInstant)
309 return this->actualInstant;
311 throw new ViewerNDimensionsException("ERROR ACTUAL INSTANT IS NOT SETTED");
316 * If the actual concept has next instant, it means, if the index of the concept
317 * in the instant is in the maximum value
318 * @param nameConcept, concept for which we need to know if we are
319 * in its maximum value
320 * @return true if IS NOT in the maximum, false otherwise
323 bool InstantHandler<P>::hasNextInstant()
331 * If the concept has previous instant, it means, if the index of the concept
332 * in the instant is positive
333 * @param nameConcept, concept for which we need to know if we are
335 * @return true if IS NOT in zero, false otherwise
338 bool InstantHandler<P>::hasPreviousInstant()
343 * returns the number con conceptsToHandled
346 int InstantHandler<P>::getSizeConceptsToChange()
348 return conceptsToChange.size();
351 * returns the number of possibles instants of the handler
352 * it depends of the concepts added in conceptsTochange
355 int InstantHandler<P>::getNumOfInstants()
357 int i,size,numInstants=1;
358 size=getSizeConceptsToChange();
359 ConceptInstantHandler* concepti;
362 concepti=conceptsToChange[i];
363 numInstants*=concepti->getMaximumIndex();
370 * Returns the names of the concepts handled and its modes
374 void InstantHandler<P>::getConceptsInfo(std::vector<std::string>& namesConcepts,std::vector<int>& modes)
377 size=getSizeConceptsToChange();
378 ConceptInstantHandler* concepti;
381 concepti=conceptsToChange[i];
382 namesConcepts.push_back(concepti->getName());
383 modes.push_back(concepti->getMode());
387 * Returns the name of the concept that handled the real time
391 void InstantHandler<P>::getTimeConcept(std::string& nameConcept)
393 ConceptInstantHandler* concept=conceptsToChange[getSizeConceptsToChange()-1];
394 nameConcept=concept->getName();
397 //====== ACCESS ==========
399 * Sets the environment that is going to be handled
400 * @param environment, a pointer to the environmen
404 void InstantHandler<P>::setEnvironment(SomeEnvironment<P>* environment)
406 this->environment=environment;
409 * Sets the actual instant of the instant handler
412 void InstantHandler<P>::setActualInstant(Instant* instant)
415 actualInstant=new Instant(instant->getInstant());
419 size=actualInstant->getSize();
421 actualInstant->setConcept(i,instant->getIndexInConcept(i));
423 setConceptsToChange();
427 * Sets the time of reproduction to the concept
428 * that depends of the real time of the reproduction
430 * @param timeReproduction
433 void InstantHandler<P>::setTimeReproduction(double time)
435 if(getSizeConceptsToChange()>0)
437 ConceptInstantHandler* concept=conceptsToChange[getSizeConceptsToChange()-1];
438 concept->setTimeReproduction(time);
442 * Sets the actual time of the reproduction,
443 * is the real time (seconds)
447 void InstantHandler<P>::setActualTime(double actualTime)
449 ConceptInstantHandler* concept=conceptsToChange[getSizeConceptsToChange()-1];
450 concept->setActualTime(actualTime);
451 this->actualTime=actualTime;
454 * Set the mode of a concept that's being handled
457 void InstantHandler<P>::setModeOfConcept(std::string nameConcept,int mode)
460 size=getSizeConceptsToChange();
461 ConceptInstantHandler* concepti;
462 std::string nameConcepti;
463 bool isConcept=false;
464 for(i=0;i<size && !isConcept;i++)
466 concepti=conceptsToChange[i];
467 nameConcepti=concepti->getName();
468 if(nameConcepti.compare(nameConcept)==0)
471 concepti->setMode(mode);
480 void InstantHandler<P>::set()
482 if(getSizeConceptsToChange()!=0)
483 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
486 * Set the increase Value for changing the instant
489 void InstantHandler<P>::setIncreaseValue(int increase)
492 size=conceptsToChange.size();
493 ConceptInstantHandler* concepti;
496 concepti=conceptsToChange[i];
497 concepti->setIncreaseValue(increase);
502 * Set the decrease Value for changing the instant
505 void InstantHandler<P>::setDecreaseValue(int decrease)
508 size=conceptsToChange.size();
509 ConceptInstantHandler* concepti;
512 concepti=conceptsToChange[i];
513 concepti->setDecreaseValue(decrease);
517 //====== PRIVATE METHODS=========
520 * Sets if the instant has next or not
524 void InstantHandler<P>::setHasNext()
526 int i, sizeConceptsToChange;
527 ConceptInstantHandler* concepti;
529 sizeConceptsToChange=getSizeConceptsToChange();
530 for(i=0;i<sizeConceptsToChange && !hasNext;i++)
532 concepti=conceptsToChange[i];
533 if(concepti->hasNextIndex())
540 * Sets if the instant has previous or not
543 void InstantHandler<P>::setHasPrevious()
545 int i, sizeConceptsToChange;
546 ConceptInstantHandler* concepti;
548 sizeConceptsToChange=getSizeConceptsToChange();
549 for(i=0;i<sizeConceptsToChange && !hasPrevious;i++)
551 concepti=conceptsToChange[i];
552 if(concepti->hasPreviousIndex())
557 * actualize the actual index of the concepts handled
560 void InstantHandler<P>::setConceptsToChange()
562 int i,sizeConceptsToChange, indexInInstant,indexInConcept;
563 sizeConceptsToChange=getSizeConceptsToChange();
564 ConceptInstantHandler* concepti;
565 for(i=0;i<sizeConceptsToChange;i++)
567 concepti=conceptsToChange[i];
568 indexInInstant=concepti->getIndexInInstant();
569 indexInConcept=actualInstant->getIndexInConcept(indexInInstant);
570 concepti->setActualIndex(indexInConcept);
574 * resets all the next concepts to zero
577 void InstantHandler<P>::resetNextConcepts()
580 index=actualConcept->getPosition();
581 size=getSizeConceptsToChange();
582 ConceptInstantHandler* concepti;
583 for(i=index+1;i<size;i++)
585 concepti=conceptsToChange[i];
586 concepti->setActualIndex(0);
587 concepti->resetInitialTime();
588 actualInstant->setConcept(concepti->getIndexInInstant(),0);
593 * Sets the next index of concepts to handled to the maximum value
596 void InstantHandler<P>::setNextConcepts()
599 index=actualConcept->getPosition();
600 size=getSizeConceptsToChange();
601 ConceptInstantHandler* concepti;
602 for(i=index+1;i<size;i++)
604 concepti=conceptsToChange[i];
605 concepti->setActualIndex(concepti->getMaximumIndex()-1);
606 //concepti->resetInitialTime();
607 actualInstant->setConcept(concepti->getIndexInInstant(),concepti->geActualIndex());
611 * this method locates the new actual concept
612 * @param next, if has to locate the actual concept to the first concept that has next
613 * next=true, else search for the first concept that has previous
616 void InstantHandler<P>::toLocateActualConcept(bool next)
618 int size=getSizeConceptsToChange();
624 bool hasNextIndex=actualConcept->hasNextIndex();
625 int position=actualConcept->getPosition();
626 while( (position-1)>=0 && !hasNextIndex)
628 actualConcept=conceptsToChange[position-1];
629 hasNextIndex=actualConcept->hasNextIndex();
630 position=actualConcept->getPosition();
636 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];
643 bool hasPreviousIndex=actualConcept->hasPreviousIndex();
644 int position=actualConcept->getPosition();
645 while( (position-1)>=0 && !hasPreviousIndex)
647 actualConcept=conceptsToChange[position-1];
648 hasPreviousIndex=actualConcept->hasPreviousIndex();
649 position=actualConcept->getPosition();
655 actualConcept=conceptsToChange[getSizeConceptsToChange()-1];