OMKSimulatedObject.cpp

Go to the documentation of this file.
00001 /*
00002  * This file is part of openMask � INRIA, CNRS, Universite de Rennes 1 1993-2002, thereinafter the Software
00003  * 
00004  * The Software has been developped within the Siames Project. 
00005  * INRIA, the University of Rennes 1 and CNRS jointly hold intellectual property rights
00006  * 
00007  * The Software has been registered with the Agence pour la Protection des
00008  * Programmes (APP) under registration number IDDN.FR.001.510008.00.S.P.2001.000.41200
00009  *  
00010  * This file may be distributed under the terms of the Q Public License
00011  * version 1.0 as defined by Trolltech AS of Norway and appearing in the file
00012  * LICENSE.QPL included in the packaging of this file.
00013  *
00014  * Licensees holding valid specific licenses issued by INRIA, CNRS or Universit� de Rennes 1 
00015  * for the software may use this file in accordance with that specific license
00016  *
00017  */
00018 #include "OMKName.h"
00019 #include <OMKSimulatedObject.h>
00020 #include <OMKObjectDescriptor.h>
00021 #include <OMKReferenceObjectHandle.h>
00022 #include <OMKMirrorObjectHandle.h>
00023 #include <OMKOutput.h>
00024 #include <OMKInput.h>
00025 #include <OMKEventIdentifier.h>
00026 #include "OMKEvent.h"
00027 #include "OMKEventListener.h"
00028 #include "OMKSystemEventIdentifier.h"
00029 #ifdef _PSGUI
00030 #include <OMKCtrlLocGui.h>
00031 extern "C" IlvBoolean IlvSetLanguage();
00032 #endif
00033 #ifdef _REACT
00034 #include <OMKReactController.h>
00035 #endif
00036 #include "OMKTracer.h"
00037 #include <algorithm>
00038 //------------------------------------------------------------------
00039 #include <OMKMirrorObjectHandle.h>
00040 #include "OMKSynchronisationMessage.h" 
00041 
00042 using namespace std ;
00043 using namespace OMK ;
00044 
00045 
00046 
00047 //------------------------------------------------------------------
00048 
00049 ObjectHandle * SimulatedObject::getObjectHandle() const 
00050 {
00051    return _objectHandle ;
00052 }
00053 
00054 //------------------------------------------------------------------
00055 
00056 void SimulatedObject::setObjectHandle( ObjectHandle * objectHandle ) 
00057 {
00058    //OMASSERT ( _objectHandle == NULL ) ; // TDTD 09-05-2006 commentaire cause noyau Chadi : pour la migration
00059    _objectHandle = objectHandle ;
00060 }
00061 
00062 //------------------------------------------------------------------
00063 
00064 const ObjectDescriptor & SimulatedObject::getObjectDescriptor () const {
00065    return _objectDescriptor ;
00066 }
00067 
00068 //------------------------------------------------------------------
00069 
00070 SimulatedObject::SimulatedObject( Controller & ctrl, const ObjectDescriptor & objectDecriptor ) : 
00071    Flowable(),
00072    _controller ( ctrl ),
00073    _objectDescriptor ( objectDecriptor ),
00074    _objectHandle ( NULL )
00075 {
00076    _objectDescriptor._pointerToSimulatedObject = this ;
00077 }
00078 
00079 //------------------------------------------------------------------
00080 SimulatedObject::~SimulatedObject()
00081 {
00082   // destroy inputs, outputs and control parameters
00083    
00084   NameToPointerMap<OutputNT>::iterator pOutput; // iterateur
00085   OutputNT *outputCourante;
00086   NameToPointerMap<OutputNT>::iterator pParam; // iterateur
00087   OutputNT *paramCourant;
00088   NameToPointerMap<InputNT>::iterator pInput; // iterateur
00089   InputNT *inputCourant;
00090 
00091   // Pour toutes les outputs, on detruit ;
00092 
00093   OMTRACEID( OMK_DEBUG_OMK_EXEC, "SimulatedObject:: ~SimulatedObject ()  : control parameters destruction" ) ;
00094   for(pParam= _controlParameterTable.begin(); pParam!= _controlParameterTable.end(); pParam++)
00095     {
00096       paramCourant = getPointerToControlParameterNamed ((*pParam).first);
00097       delete paramCourant;
00098     }
00099   OMTRACEID( OMK_DEBUG_OMK_EXEC, "SimulatedObject:: ~SimulatedObject ()  : control parameters destruction done" << std::endl << "inputs destruction" ) ;
00100   for(pInput= _inputTable.begin(); pInput!=_inputTable.end();pInput++)
00101     {
00102       inputCourant = getPointerToInputNamed ((*pInput).first);
00103       delete inputCourant ;
00104     }
00105   OMTRACEID( OMK_DEBUG_OMK_EXEC, "SimulatedObject:: ~SimulatedObject ()  : inputs destruction done" << std::endl << "outputs destruction" ) ;
00106   for(pOutput= _outputTable.begin(); pOutput!= _outputTable.end(); pOutput++)
00107     {
00108        outputCourante = getPointerToOutputNamed ((*pOutput).first);
00109        delete outputCourante;
00110     }
00111   OMTRACEID( OMK_DEBUG_OMK_EXEC, "SimulatedObject:: ~SimulatedObject ()  : outputs destruction done" << std::endl << "encapsulated classes creators destruction " ) ;
00112 
00113   // update the object descriptor
00114   _objectDescriptor._pointerToSimulatedObject = NULL ;
00115 
00116   // Delete all the remaining event listeners
00117   for( std::list <EventListener *>::iterator listener = _listOfEventListeners.begin() ;
00118        listener != _listOfEventListeners.end() ; listener = _listOfEventListeners.begin() )
00119   {
00120     delete *listener ;
00121   }
00122   OMTRACEID( OMK_DEBUG_OMK_EXEC, "SimulatedObject:: ~SimulatedObject ()  : done" ) ;
00123 }
00124 
00125 //---------------------------------------------------------------------
00126 
00127 void SimulatedObject::deleteInput( const Name & name )
00128 {
00129   InputNT * obj = _inputTable[ name ] ;
00130   _inputTable.erase( name ) ;
00131   delete obj ;
00132 }
00133 
00134 //---------------------------------------------------------------------
00135 
00136 OutputNT * SimulatedObject::getPointerToControlParameterNamed( const Name & nomAtt  ) const
00137 {
00138   return _controlParameterTable.getObjectOfIndex (nomAtt) ;
00139 } 
00140  
00141 //---------------------------------------------------------------------
00142 
00143 OutputNT * SimulatedObject::getPointerToOutputNamed( const Name & nomAtt ) const
00144 {
00145    OutputNT * ref = _outputTable.getObjectOfIndex (nomAtt) ;
00146    if (ref == NULL) {
00147      OMTRACEID( OMK_DEBUG_OMK_EXEC, "SimulatedObject::getPointerToOutputNamed: \""
00148                 << nomAtt << "\" is an unknown output for object \"" 
00149                 << getName() << "\"" ) ;
00150    }
00151    return ref ;
00152 }
00153 
00154 //---------------------------------------------------------------------
00155 
00156 InputNT * SimulatedObject::getPointerToInputNamed( const Name & nomAtt ) const
00157 {
00158   return _inputTable.getObjectOfIndex (nomAtt) ;
00159 } 
00160 
00161 
00162 //------------------------------------------------------------------
00163 // Symbolic name for object
00164 const Name & SimulatedObject::getName() const
00165 {
00166   return _objectDescriptor.getName() ;
00167 }
00168 
00169 //------------------------------------------------------------------
00170 // accesseur sur le controleur local de la simulation en lecture
00171 
00172 Controller & SimulatedObject::getController () const {
00173   return _controller;
00174 }
00175 
00177 void SimulatedObject::postConstruction() 
00178 {
00179 }
00180 
00181 void SimulatedObject::init() 
00182 {
00183 }
00184 
00186 void SimulatedObject::emigrate()
00187 {
00188 }
00189 
00190 void SimulatedObject::immigrate(Event * event)
00191 {
00192 }
00196 void SimulatedObject::compute ()
00197 {
00198 }
00199 
00200 //------------------------------------------------------------------
00201 
00202 void SimulatedObject::finish() 
00203 {
00204 }
00205 
00206 //------------------------------------------------------------------
00207 
00208 float SimulatedObject::getPeriod () const 
00209 {
00210    return 1.f / _objectDescriptor.getFrequency () ;
00211 }
00212 
00213 //------------------------------------------
00214 
00215 const ObjectDescriptor & SimulatedObject::getFathersDescriptor () const {
00216    if ( _objectDescriptor.getFathersObjectDescriptor () != NULL ) 
00217       {
00218          return *_objectDescriptor.getFathersObjectDescriptor () ;
00219       }
00220    else 
00221       {
00222          return _objectDescriptor ;
00223       }
00224 } 
00225 
00226 //------------------------------------------
00227 
00228 SimulatedObject * SimulatedObject::getFather () const {
00229    //refObjet is not always initialised in a distributed environment
00230    if ( _objectDescriptor.getFathersObjectDescriptor() != NULL )
00231       {
00232          return  _controller.getPointerToSimulatedObjectNamed(_objectDescriptor.getFathersObjectDescriptor()->getName());
00233       }
00234    else
00235       {
00236          return NULL ;
00237       }
00238 }
00239 const ConfigurationParameterDescriptor * SimulatedObject::getConfigurationParameters() const {
00240    return _objectDescriptor.getConfigurationParameters();
00241 }
00242 
00243 
00244 const NameToPointerMap<InputNT> & SimulatedObject::getInputTable () const 
00245 {
00246    return _inputTable ;
00247 }
00248 
00249 const NameToPointerMap<OutputNT> & SimulatedObject::getOutputTable () const  
00250 {
00251    return _outputTable ;
00252 }
00253 
00254 const NameToPointerMap<OutputNT> & SimulatedObject::getControlParameterTable () const  
00255 {
00256    return _controlParameterTable ;
00257 }
00258 
00259 //------------------------------------------------------------------
00260 
00261 void SimulatedObject::sendEvent( const Event &event ) 
00262 {
00263   OMTRACEID( OMK_DEBUG_OMK_EVENT, "SimulatedObject::sendEvent( const Event & \""
00264              << event.eventId << "\" ) to \"" << event.receiver << "\" at " << event.date ) ;
00265   SimulatedObject *objDest = _controller.getPointerToSimulatedObjectNamed( event.receiver ) ;
00266   if( objDest == NULL )
00267   {
00268     getController().addToPendingEvents( event.clone() ) ; 
00269   }
00270   else if( objDest->getObjectHandle() == NULL )
00271   {
00272     getController().addToPendingEvents( event.clone() ) ; 
00273   }
00274   else
00275   {
00276     objDest->getObjectHandle()->receiveEvent( event ) ;
00277   }
00278 }
00279 
00280 void SimulatedObject::sendEvent( Event *event )
00281 {
00282   OMTRACEID( OMK_DEBUG_OMK_EVENT, "SimulatedObject::sendEvent ( Event * \"" << event->eventId 
00283              << "\" ) to \"" << event->receiver << "\" at " << event->date ) ;
00284   SimulatedObject *objDest = _controller.getPointerToSimulatedObjectNamed( event->receiver ) ;
00285   if( objDest == NULL ) 
00286   {
00287     getController().addToPendingEvents( event ) ; 
00288   }
00289   else if( objDest->getObjectHandle() == NULL )
00290   {
00291     getController().addToPendingEvents( event ) ; 
00292   }
00293   else
00294   {
00295     objDest->getObjectHandle()->receiveEvent( event ) ;
00296   }    
00297 }
00298 
00299 void SimulatedObject::sendEvent( SimulatedObject &receiver, const EventIdentifier &eventId )
00300 {
00301   OMTRACEID( OMK_DEBUG_OMK_EVENT, "SimulatedObject::sendEvent ( SimulatedObject & \"" << receiver.getName()
00302              << "\", const EventIdentifier & \"" << eventId << "\" )" ) ;
00303   sendEvent( &receiver, eventId ) ;
00304 }
00305 
00306 void SimulatedObject::sendEvent( SimulatedObject *receiver, const EventIdentifier &eventId )
00307 {
00308   OMTRACEID( OMK_DEBUG_OMK_EVENT, "SimulatedObject::sendEvent ( SimulatedObject * \"" << receiver
00309              << "\", const EventIdentifier & \"" << eventId << "\" )" ) ;
00310   OMASSERT( receiver != NULL ) ;
00311   if( receiver->getObjectHandle() != NULL )
00312   {
00313     receiver->getObjectHandle()->receiveEvent( new Event( eventId,
00314                                                              _controller.getSimulatedDate (),
00315                                                              getName (),
00316                                                              receiver->getName () ) ) ;
00317   }
00318   else
00319   { //could happen during creation when the object handle is not yet created.
00320     getController().addToPendingEvents( new Event( eventId,
00321                                                       _controller.getSimulatedDate (),
00322                                                       getName (),
00323                                                       receiver->getName () ) ) ;
00324   }
00325 }
00326 
00327 void SimulatedObject::sendEvent( const Name &receiver, const EventIdentifier &eventId )
00328 {
00329   OMTRACEID( OMK_DEBUG_OMK_EVENT, "SimulatedObject::sendEvent( const Name & \"" << receiver 
00330              <<"\", const EventIdentifier & \"" << eventId << "\" ) at " << _controller.getSimulatedDate() ) ;
00331   SimulatedObject *objDest = _controller.getPointerToSimulatedObjectNamed( receiver ) ;
00332   if ( objDest == NULL )
00333   {
00334     getController().addToPendingEvents( new Event( eventId,
00335                                                       _controller.getSimulatedDate (),
00336                                                       getName (),
00337                                                       receiver ) ) ;
00338   }
00339   else
00340   {
00341     sendEvent( objDest, eventId ) ;
00342   }
00343 }
00344 
00345 //------------------------------------------------------------------
00346 
00347  bool SimulatedObject::processEventsASAP () const 
00348  {
00349     //default mode : processEvents at the object's frequency.
00350     return false ;
00351  }
00352 
00353 //------------------------------------------------------------------
00354 void SimulatedObject::prepareEventProcessing ( list <Event *> & ) {
00355 
00356 }
00357 
00358 bool SimulatedObject::processEvent (Event * event) {
00359    return true ;
00360 }
00361 
00362 void SimulatedObject::eventsProcessed ( list <Event *> & ) {
00363 
00364 }
00365 
00366 
00367 //------------------------- insertion et extraction d'un objet dans un flot ---------------------
00368 
00369 //------------------------------------------------------------------
00370 
00371 void SimulatedObject::extract (istream & in)
00372 {
00373   OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject:" << this << ":extract" ) ;
00374   NameToPointerMap<OutputNT>::iterator pOutput; // iterateur
00375   OutputNT *outputCourante;
00376   NameToPointerMap<OutputNT>::iterator pParam; // iterateur
00377   OutputNT *paramCourant;
00378 
00379   // Pour toutes les outputs, on stocke
00380   for(pOutput= _outputTable.begin(); pOutput!= _outputTable.end(); pOutput++)
00381   {
00382     outputCourante = (*pOutput).second;
00383     in >> *outputCourante;
00384   } 
00385   bool avecParam;
00386   in >> avecParam;
00387   if (avecParam) 
00388   {
00389     OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject:" << this << ":extract extract parameters" ) ;
00390     for(pParam= _controlParameterTable.begin(); pParam!= _controlParameterTable.end(); pParam++)
00391     {
00392             paramCourant = (*pParam).second;
00393             in >> *paramCourant;
00394             OMTRACEID( OMK_DEBUG_OMK_MULTI, (*pParam).first ) ;
00395     }
00396   }
00397   OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject:" << this << ":extract done " ) ;
00398 }
00399 
00400 //------------------------------------------------------------------
00401 void SimulatedObject::pack ( OutgoingSynchronisationMessage & out) const
00402 {
00403   //make sure the object has been encapsulated in an object handle
00404   OMASSERT( getObjectHandle() != NULL ) ;
00405   OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject:" << this << ":pack" ) ;
00406 
00407   // store the value of all outputs
00408   for( NameToPointerMap<OutputNT>::const_iterator pOutput = _outputTable.begin(); 
00409        pOutput != _outputTable.end(); 
00410        ++pOutput)
00411   {
00412     OMTRACEID( OMK_DEBUG_OMK_MULTI, (*pOutput).first ) ;
00413     pOutput->second->pack (out) ;
00414   } 
00415   
00416   bool mettreParam = ( getObjectHandle()->controlParametersChanged() ) || 
00417                      (getSimulatedDate() == Controller::initialSimulationDate) ;
00418 
00419   OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject:" << this << ":pack: " << ( mettreParam ? "packing control parameters" : "control parameters not packed" ) );
00420 
00421   if(mettreParam) 
00422   {
00423     out << SynchronisationMessage::controlParameters;
00424     for( NameToPointerMap<OutputNT>::const_iterator pParam= _controlParameterTable.begin(); 
00425                pParam!= _controlParameterTable.end(); 
00426                pParam++) 
00427     {
00428       OMTRACEID( OMK_DEBUG_OMK_MULTI, (*pParam).first ) ;
00429       (*pParam).second->pack(out) ;
00430     }
00431   }
00432   else
00433   {
00434           out << SynchronisationMessage::noControlParameters;
00435   }
00436   OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject:" << this << ":pack done " ) ;
00437   out << SynchronisationMessage::endOfSynchronisationFragment ;
00438 }
00439 
00440 void SimulatedObject::packAllValues ( OutgoingSynchronisationMessage & out) const
00441 {
00442   //make sure the object has been encapsulated in an object handle
00443   OMASSERT (getObjectHandle() != NULL) ;
00444 
00445   OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject:"<< this <<":packAllValues" ) ;
00446 
00447   // store the value of all outputs
00448   for( NameToPointerMap<OutputNT>::const_iterator pOutput = _outputTable.begin(); 
00449        pOutput != _outputTable.end(); 
00450        ++pOutput)
00451   {
00452     OMTRACEID( OMK_DEBUG_OMK_MULTI, (*pOutput).first ) ;
00453           pOutput->second->packAllValues (out) ;
00454   } 
00455   
00456   OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject:"<< this <<":pack: packing control parameters" ) ;
00457   out<<SynchronisationMessage::controlParameters;
00458   for( NameToPointerMap<OutputNT>::const_iterator pParam = _controlParameterTable.begin(); 
00459        pParam!= _controlParameterTable.end(); 
00460        pParam++) 
00461   {
00462     OMTRACEID( OMK_DEBUG_OMK_MULTI, (*pParam).first ) ;
00463     (*pParam).second->packAllValues(out) ;
00464   }
00465 
00466   OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject:"<< this <<":pack done " ) ;
00467   out << SynchronisationMessage::endOfSynchronisationFragment ;
00468 }
00469 
00470 
00471 void SimulatedObject::unpack (IncomingSynchronisationMessage & in) 
00472 {
00473   OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject:"<< this <<":unpack" ) ;
00474   Name attributeName ;
00475   NameToPointerMap<OutputNT>::iterator pOutput = _outputTable.begin();
00476 
00477   attributeName.unpack( in ) ;
00478   while( (attributeName != SynchronisationMessage::controlParameters)
00479             && (attributeName != SynchronisationMessage::noControlParameters) )
00480   {
00481           if ( pOutput->first == attributeName )
00482           {
00483       (pOutput->second)->unpack(in) ;
00484       ++pOutput ;
00485           }
00486           else
00487           {
00488             do 
00489                   {
00490         ++pOutput ;
00491         OMASSERT ( pOutput != _outputTable.end() ) ;// a new output has been added : not accepted
00492                   } while ( pOutput->first != attributeName ) ;
00493 
00494       (pOutput->second)->unpack ( in ) ;
00495       ++pOutput ;
00496     }
00497           attributeName.unpack ( in ) ;
00498   }
00499    
00500   if (attributeName == SynchronisationMessage::controlParameters) 
00501   {
00502     //the control parameters have been added to the synchronisation message
00503     pOutput =  _controlParameterTable.begin();
00504     attributeName.unpack ( in ) ;
00505     while ( attributeName != SynchronisationMessage::endOfSynchronisationFragment )
00506     {
00507       if ( pOutput->first == attributeName )
00508       {
00509         (pOutput->second)->unpack( in ) ;
00510         ++pOutput ;
00511       }
00512       else
00513       {
00514         do 
00515         {
00516           ++pOutput ;
00517           OMASSERT ( pOutput != _controlParameterTable.end() ) ;// a new control parameter has been added : not accepted
00518         } while ( pOutput->first != attributeName ) ;
00519 
00520         (pOutput->second)->unpack (in) ;
00521         ++pOutput ;
00522       }
00523       attributeName.unpack ( in ) ;
00524     }
00525   }
00526   else
00527   { // consume the OMKPvmMessage::endOfSyncronisationMessage 
00528     attributeName.unpack ( in ) ;
00529   }    
00530 }
00531 
00532 
00533 void SimulatedObject::unpackAllValues (IncomingSynchronisationMessage & in) 
00534 {
00535   OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject:"<< this <<":unpackAllValues" ) ;
00536   Name attributeName ;
00537   NameToPointerMap<OutputNT>::iterator pOutput = _outputTable.begin();
00538 
00539   attributeName.unpack( in ) ;
00540   while( (attributeName != SynchronisationMessage::controlParameters)
00541     && (attributeName != SynchronisationMessage::noControlParameters) )
00542   {
00543     if ( pOutput->first == attributeName )
00544     {
00545       (pOutput->second)->unpackAllValues(in) ;
00546       ++pOutput ;
00547     }
00548     else
00549     {
00550       do 
00551       {
00552         ++pOutput ;
00553         OMASSERT ( pOutput != _outputTable.end() ) ;// a new output has been added : not accepted
00554       } while ( pOutput->first != attributeName ) ;
00555 
00556       (pOutput->second)->unpackAllValues ( in ) ;
00557       ++pOutput ;
00558     }
00559     attributeName.unpack ( in ) ;
00560   }
00561 
00562   if (attributeName == SynchronisationMessage::controlParameters) 
00563   {
00564     //the control parameters have been added to the synchronisation message
00565     pOutput =  _controlParameterTable.begin();
00566     attributeName.unpack ( in ) ;
00567     while ( attributeName != SynchronisationMessage::endOfSynchronisationFragment )
00568     {
00569       if ( pOutput->first == attributeName )
00570       {
00571         (pOutput->second)->unpackAllValues( in ) ;
00572         ++pOutput ;
00573       }
00574       else
00575       {
00576         do 
00577         {
00578           ++pOutput ;
00579           OMASSERT ( pOutput != _controlParameterTable.end() ) ;// a new control parameter has been added : not accepted
00580         } while ( pOutput->first != attributeName ) ;
00581 
00582         (pOutput->second)->unpackAllValues (in) ;
00583         ++pOutput ;
00584       }
00585       attributeName.unpack ( in ) ;
00586     }
00587   }
00588   else
00589   {
00590     // consume the OMKPvmMessage::endOfSyncronisationMessage 
00591     attributeName.unpack ( in ) ;
00592   }    
00593 }
00594 
00595 
00596 
00597 void SimulatedObject::insertInStream (ostream & out) const
00598 {
00599   NameToPointerMap<OutputNT>::const_iterator pOutput; // iterateur
00600   OutputNT * outputCourante;
00601   NameToPointerMap<OutputNT>::const_iterator pParam; // iterateur
00602   OutputNT *paramCourant;
00603   OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject::insertInStream" ) ;
00604 
00605   // Pour toutes les outputs, on stocke
00606   for(pOutput= _outputTable.begin(); pOutput!= _outputTable.end(); pOutput++)
00607   {
00608     outputCourante = (*pOutput).second;
00609     OMTRACEID( OMK_DEBUG_OMK_MULTI, (*pOutput).first << endl << *outputCourante ) ;
00610     out << *outputCourante << " ";
00611   }  
00612   bool mettreParam = getObjectHandle()->controlParametersChanged()
00613                   || (getSimulatedDate()==Controller::initialSimulationDate) ;
00614   OMTRACEID( OMK_DEBUG_OMK_MULTI, mettreParam ) ;
00615   out << mettreParam << " ";
00616   if(mettreParam) 
00617   {
00618     for(pParam= _controlParameterTable.begin(); pParam!= _controlParameterTable.end(); pParam++) 
00619     {
00620             paramCourant = (*pParam).second;
00621       OMTRACEID( OMK_DEBUG_OMK_MULTI, (*pParam).first ) ;
00622             out << *paramCourant << " ";
00623     }
00624   }
00625   OMTRACEID( OMK_DEBUG_OMK_MULTI, "SimulatedObject:" << this << ":insertInStream done" ) ;
00626 }
00627 
00628 //------------------------------------------------------------------
00629 
00630 
00631 
00632 void SimulatedObject::fireSignal(const EventIdentifier & sig) 
00633 {
00634   Event event(sig,getSimulatedDate(),getName(),getName()) ;
00635   //does firing a signal during creation have any sense ? yes
00636 
00637 
00638   if ( dynamic_cast<ReferenceObjectHandle *> ( getObjectHandle()) != NULL) {
00639     if (getObjectHandle() != NULL ) 
00640     {
00641       getObjectHandle()->sendEventsForSignal( event , sig ) ;
00642     }
00643     else
00644     {  
00645       // no registration can have taken place, as this should only happen at object creation,
00646       // so just broadcast the signal
00647       getController().broadcastEventsForSignal ( event, sig ) ;
00648     }
00649   }
00650   else OMTRACE("ERREUR No send Event when SimObj DEAD");
00651 }
00652 
00653 bool SimulatedObject::registerForSignalBy ( const EventIdentifier & sig, 
00654                                               const Name & producer )
00655 {
00656    return registerForSignalBy ( sig, producer, sig ) ;
00657 }
00658 
00659 
00660 
00661 bool SimulatedObject::registerForSignalBy ( const EventIdentifier & sig, 
00662                                               const Name & producer,
00663                                               const EventIdentifier & eventId )
00664 {
00665    SimulatedObject * pointerToProducer ;
00666    pointerToProducer = _controller.getPointerToSimulatedObjectNamed ( producer );
00667    if (pointerToProducer != NULL) 
00668       {
00669          return registerForSignalBy(sig, pointerToProducer, eventId);
00670       }
00671    else 
00672       {
00673          return getController().addToPendingRegistrations ( sig , producer, eventId, getName() ) ;
00674       }
00675 }
00676 
00677 
00678 
00679 bool SimulatedObject::registerForSignalBy(const EventIdentifier & sig, 
00680                                             SimulatedObject * producer )
00681 {
00682    return registerForSignalBy ( sig , producer , sig ) ;
00683 }
00684 
00685 
00686 
00687 bool SimulatedObject::registerForSignalBy(const EventIdentifier & sig, 
00688                                             SimulatedObject & producer )
00689 {
00690    return registerForSignalBy ( sig , producer , sig ) ;
00691 }
00692 
00693 
00694 
00695 bool SimulatedObject::registerForSignalBy(const EventIdentifier & sig, 
00696                                             SimulatedObject * producer,
00697                                             const EventIdentifier & eventId )
00698 {
00699    OMASSERT ( producer != NULL ) ;
00700    //unless registring for oneself during creation, the next if branch should allways be verified
00701    if (producer->getObjectHandle() != NULL)
00702       {
00703          return producer->getObjectHandle()->receiveRegistrationForSignal ( sig, getName(), eventId ) ;
00704       }
00705    else 
00706       {
00707          return getController().addToPendingRegistrations (sig, 
00708                                                            producer->getName() ,
00709                                                            eventId,
00710                                                            getName() ) ;
00711       }
00712 }
00713 
00714 
00715 
00716 bool SimulatedObject::registerForSignalBy(const EventIdentifier & sig, 
00717                                             SimulatedObject & producer,
00718                                             const EventIdentifier & eventId )
00719 {
00720    if (producer.getObjectHandle() != NULL)
00721       {
00722          //unless registring for oneself during creation, the next if branch should allways be verified
00723          return producer.getObjectHandle()->receiveRegistrationForSignal ( sig, getName(), eventId ) ;
00724       }
00725    else 
00726       {
00727          return getController().addToPendingRegistrations (sig, 
00728                                                            producer.getName(),
00729                                                            eventId,
00730                                                            getName() ) ;
00731       }
00732 }
00733 
00734 
00735 
00736 bool SimulatedObject::registerForSignal( const EventIdentifier & sig ) 
00737 {
00738   return _controller.receiveRegistrationForSignal ( sig, getName(), sig);
00739 }
00740 
00741 
00742 
00743 bool SimulatedObject::registerForSignal(const EventIdentifier & sig,
00744                                           const EventIdentifier & eventId) 
00745 {
00746   return _controller.receiveRegistrationForSignal ( sig, getName(), eventId );
00747 }
00748 
00749 
00750 
00751 bool SimulatedObject::cancelRegistrationForSignalBy(const EventIdentifier & sig, const Name & producer){
00752    SimulatedObject * pointerToProducer ;
00753    pointerToProducer = _controller.getPointerToSimulatedObjectNamed( producer );
00754    if (pointerToProducer != NULL) 
00755       {
00756          return cancelRegistrationForSignalBy(sig,pointerToProducer);
00757       }
00758    else 
00759       {
00760          return getController().removeFromPendingRegistrations ( sig, producer, getName() ) ;
00761       }
00762 }
00763 
00764 bool SimulatedObject::cancelRegistrationForSignalBy(const EventIdentifier & sig, SimulatedObject * producer)
00765 {
00766    OMASSERT ( producer != NULL ) ;
00767    //unless canceling a registration for oneself during creation, the next if sould be verified
00768    if ( producer->getObjectHandle() != NULL )
00769       {
00770          return producer->getObjectHandle()->cancelRegistrationForSignal ( sig, getName() ) ;
00771       }
00772    else
00773       {
00774          return getController().removeFromPendingRegistrations (sig, producer->getName(), getName() ) ;
00775       }
00776 }
00777 
00778 bool SimulatedObject::cancelRegistrationForSignalBy(const EventIdentifier & sig, SimulatedObject & producer)
00779 {
00780    //unless canceling a registration for oneself during creation, the next assertion sould be verified
00781    if ( producer.getObjectHandle() != NULL )
00782       {    
00783          return producer.getObjectHandle()->cancelRegistrationForSignal ( sig, getName() ) ;
00784       }
00785    else
00786       {
00787          return getController().removeFromPendingRegistrations (sig, producer.getName(), getName() ) ;
00788       }
00789 }
00790 
00791 
00792 bool SimulatedObject::cancelRegistrationForSignal(const EventIdentifier & sig) 
00793 {
00794    return _controller.receiveCancellationForSignal(sig,getName());
00795 }
00796 
00797 
00798 const Date & SimulatedObject::getSimulatedDate () const {
00799    return _controller.getSimulatedDate();
00800 }
00801 
00802 
00803 
00804 bool SimulatedObject::isAncestor(const Name & name) 
00805 {
00806    bool result = false ;
00807    const ObjectDescriptor * ancestorDescriptor = getObjectDescriptor().getFathersObjectDescriptor() ;
00808    while ( (ancestorDescriptor != NULL) &&
00809            !result )
00810       {
00811          if ( ancestorDescriptor->getName() == name)
00812             {
00813                result = true ;
00814             }
00815          ancestorDescriptor = ancestorDescriptor->getFathersObjectDescriptor() ;
00816       }
00817    return result ;
00818 }
00819 
00820 
00821 
00822 const list < EventListener * > & SimulatedObject::getEventListeners () const 
00823 {
00824    return _listOfEventListeners ;
00825 }
00826 
00827  // Modification 2007/05/11 Benoit Chanclou
00828  // The registration must be made for the event listener if the object 
00829  // handler is already created.
00830  // Two case to call 'registerEvents' :
00831  // Case 1 : The event listener is created during the object constructor call,
00832  // the object handler during its constructor calls 'registerEvents' for each 
00833  // event listeners stored in '_listOfEventListeners' (see ReferenceObjectHandle constructor).
00834  // Case 2 : The event listener is created after the object constructor, the 
00835  // 'registerEvents' which is not automatically called by the object handler 
00836  // must be called here
00837 void SimulatedObject::addEventListener ( EventListener & eventListener ) 
00838 {
00839   // To avoid twice registration of the same event handler
00840   if( find(_listOfEventListeners.begin(), _listOfEventListeners.end(), & eventListener ) == _listOfEventListeners.end() )
00841   {
00842     _listOfEventListeners.push_front( &eventListener ) ;
00843     if( getObjectHandle() ) 
00844     {
00845       eventListener.registerEvents() ;
00846     }
00847   }
00848 }
00849 
00850 void SimulatedObject::registerEventListenerForEvent( EventListener & eventListener, 
00851                                                        const EventIdentifier & eventId ) 
00852 {
00853    //this member function should only be called by event listeners when registering the events they are associated to
00854    //this registration process is initiated during the creation of the assocaited object handle
00855    // or after its creation
00856    //therefore, the following assertion should allways be verified
00857    OMASSERT( getObjectHandle() ); 
00858 
00859    getObjectHandle()->registerEventListenerForEvent( eventListener , eventId ) ;
00860 }
00861 
00862 void SimulatedObject::unregisterEventListener( EventListener & eventListener ) 
00863 {
00864   std::list <EventListener *>::iterator listener = find(_listOfEventListeners.begin(), _listOfEventListeners.end(), & eventListener ) ;
00865   _listOfEventListeners.erase( listener ) ;
00866    //this member function is only called by the destructor of the event listeners 
00867    OMASSERT( getObjectHandle() ); 
00868 
00869    getObjectHandle()->unregisterEventListener( eventListener ) ;
00870 }
00871 

logo OpenMask

Documentation generated on Mon Jun 9 11:45:57 2008

Generated with doxygen by Dimitri van Heesch ,   1997-2007