OMKInteractorExtension.cpp

Go to the documentation of this file.
00001 /************************************************************************/
00002 /* This file is part of openMask(c) INRIA, CNRS, Universite de Rennes 1 */
00003 /* 1993-2002, thereinafter the Software                                 */
00004 /*                                                                      */
00005 /* The Software has been developped within the Siames Project.          */
00006 /* INRIA, the University of Rennes 1 and CNRS jointly hold intellectual */
00007 /* property rights                                                      */
00008 /*                                                                      */
00009 /* The Software has been registered with the Agence pour la Protection  */
00010 /* des Programmes (APP) under registration number                       */
00011 /* IDDN.FR.001.510008.00.S.P.2001.000.41200                             */
00012 /*                                                                      */
00013 /* This file may be distributed under the terms of the Q Public License */
00014 /* version 1.0 as defined by Trolltech AS of Norway and appearing in    */
00015 /* the file LICENSE.QPL included in the packaging of this file.         */
00016 /*                                                                      */
00017 /* Licensees holding valid specific licenses issued by INRIA, CNRS or   */
00018 /* Universite Rennes 1 for the software may use this file in            */
00019 /* acordance with that specific license                                 */
00020 /************************************************************************/
00021                 
00022 #include "OMKInteractorExtension.h"
00023 #include "OMKExtensibleSimulatedObject.h"
00024 #include "OMKParametersAccessor.inl"
00025 #include "OMKValuedEventListenerCallBack.h"
00026 #include "OMKEventListenerCallBack.h"
00027 #include "OMKMultipleConfigurationParameter.h"
00028 #include "OMKIAttribute.h"
00029 #include "OMKSessionPrm.h"
00030 #include <vector>
00031 #include <string>
00032 #include <iostream>
00033 
00034 using namespace OMK ;
00035 using namespace OMK::Iii ;
00036 using namespace OMK::Type ;
00037 
00038 //-----------------------------------------------------------------
00039 
00040 // Default constructor
00041 InteractorExtension::InteractorExtension( ExtensibleSimulatedObject* owner, const Name& id, bool registerExtension )
00042 : ExtensionT< ExtensibleSimulatedObject >( owner, id, registerExtension ),
00043   _access( "", 1 ),
00044   _accessibleConnectorsListener( 0 ),
00045   _currentValuesSentAndControlTakenListener( 0 ),
00046   _controlReleasedListener( 0 ),
00047   _controlEndedListener( 0 ),
00048   _controlTakenByListener( 0 ),
00049   _controlReleasedByListener( 0 ),
00050   _currentValuesSentListener( 0 )
00051 {
00052   // To be sure that there is an output all the interactor output are scanned
00053   const ConfigurationParameterDescriptor * node = _owner->getObjectDescriptor().getExtensionsParameters() ;
00054   const ConfigurationParameterDescriptor * extNode = node ? node->getSubDescriptorByName( _id.getCString() ) : 0 ;
00055   const ConfigurationParameterDescriptor * outputNode = extNode ? extNode->getSubDescriptorByName( "Outputs" ) : 0 ;
00056   if( outputNode )
00057   {
00058     int nbItems = outputNode->getNumberOfSubItems() ;
00059     OMTRACEID( OMK_DEBUG_III, "The interactor " <<  OMK::debugMsg( this, _owner ) << " has " << nbItems << " outputs" ) ;
00060     for( int i = 0 ; i < nbItems ; ++i )
00061     { 
00062       // Retrieves parameters sets for each interactor output
00063       Name id( static_cast<const MultipleConfigurationParameter *>( outputNode )->getNameOfSubDescriptor( i ) ) ;
00064       IAttribute* attribute = _owner->getAttribute( id ) ;
00065       if( attribute )
00066       {
00067         // To be sure that there is an output
00068         attribute->createOutput() ;
00069         OMTRACEID( OMK_DEBUG_III, ( attribute->getOutput() ? ":-)" : ":-(" ) << " Create the output for \"" << id << "\" !" ) ; 
00070       }
00071       else
00072       {
00073         OMTRACEID( OMK_DEBUG_III, ":-( Error the interactor " <<  OMK::debugMsg( this, _owner ) << std::endl
00074                  << ">>> Unable to create the output for \"" << id << "\" because this attribute doesn't exist !" ) ; 
00075       }
00076     }
00077   }
00078   else
00079   {
00080     OMTRACEID( OMK_DEBUG_III, ":-( Error the interactor " <<  OMK::debugMsg( this, _owner ) << std::endl
00081               << ">>> Unable to find the output node !" ) ; 
00082   }
00083 }
00084 
00085 //-----------------------------------------------------------------
00086 
00087 // Destructor
00088 InteractorExtension::~InteractorExtension()
00089 {
00090   //Removes event listener
00091   delete _accessibleConnectorsListener ;
00092   delete _currentValuesSentAndControlTakenListener ;
00093   delete _controlReleasedListener ;
00094   delete _controlEndedListener ;
00095   delete _controlTakenByListener ;
00096   delete _controlReleasedByListener ;
00097   delete _currentValuesSentListener ;
00098   
00099   // Destroy each session
00100   closeAllSessions() ;
00101 
00102   // Destroy each connector, which will disconnect its associated input
00103   for ( InteractorOutputsMap::const_iterator i = _interactorOutputs.begin() ; i !=  _interactorOutputs.end() ; ++i )
00104   {
00105     delete i->second ;
00106   }
00107   // Finally clears the map 
00108   _interactorOutputs.clear() ;
00109 }
00110 //-----------------------------------------------------------------
00111 
00112 bool InteractorExtension::loadExtensionParameters( const ConfigurationParameterDescriptor * node ) 
00113 {
00114   bool ok = true ;
00115 
00116   ParametersAccessor::get( node, "Level", _access._level ) ; 
00117   ok = ParametersAccessor::get( node, "Group", _access._groupName ) && ok ; 
00118   OMTRACEID( OMK_DEBUG_III, "The interactor" <<  OMK::debugMsg( this, _owner ) 
00119                          << " is in the group \"" << _access._groupName 
00120                          << "\" and has the level " << _access._level ) ;
00121 
00122   startInteractor() ;
00123 
00124   const ConfigurationParameterDescriptor * outputNode = node->getSubDescriptorByName( "Outputs" ) ;
00125   if( outputNode )
00126   {
00127     int nbItems = outputNode->getNumberOfSubItems() ;
00128     OMTRACEID( OMK_DEBUG_III, "The interactor " <<  OMK::debugMsg( this, _owner ) << " has " << nbItems << " outputs" ) ;
00129     for( int i = 0 ; i < nbItems && ok ; ++i )
00130     { 
00131       // Retrieves parameters sets for each interactor output
00132       Name id( static_cast<const MultipleConfigurationParameter *>( outputNode )->getNameOfSubDescriptor( i ) ) ;
00133       const ConfigurationParameterDescriptor * outputParameters = outputNode->getSubDescriptorByPosition( i ) ;
00134       ok = InteractorOutput::create( id, this, outputParameters ) && ok ;
00135     }
00136   }
00137 
00138   return ok  ;
00139 }
00140 //-----------------------------------------------------------------
00141 
00142 void InteractorExtension::startInteractor() 
00143 {
00144   if( !_accessibleConnectorsListener )
00145   { 
00146     // The event listener is not already created => adds it
00147     _accessibleConnectorsListener = new Type::AccessibleParametersListEventListener
00148       ( *_owner, this, 
00149         &InteractorExtension::processAccessibleParameters, 
00150         EventId::ACCESSIBLE_PARAMETERS ) ;
00151     _currentValuesSentAndControlTakenListener = new ControlTakenEventListener
00152       ( *_owner, this, 
00153          &InteractorExtension::processCurrentValuesSentAndControlTaken, 
00154         EventId::CURRENT_VALUES_SENT_AND_CONTROL_TAKEN ) ;
00155     _controlReleasedListener = new ControlEndedEventListener
00156       (  *_owner, this, 
00157          &InteractorExtension::processControlReleased,
00158          EventId::CONTROL_RELEASED ) ;
00159     _controlEndedListener = new ControlEndedEventListener
00160       ( *_owner, this, 
00161         &InteractorExtension::processControlEnded, 
00162         EventId::CONTROL_ENDED ) ;
00163     _controlTakenByListener = new ControlUpdatedEventListener
00164       ( *_owner, this, 
00165         &InteractorExtension::processControlTakenBy, 
00166         EventId::CONTROL_TAKEN_BY ) ;
00167     _controlReleasedByListener = new ControlUpdatedEventListener
00168       ( *_owner, this, 
00169         &InteractorExtension::processControlReleasedBy, 
00170         EventId::CONTROL_RELEASED_BY ) ;
00171     _currentValuesSentListener = new EventListenerCallBack< InteractorExtension >
00172       ( *_owner, this, 
00173         &InteractorExtension::processCurrentValuesSent, 
00174         EventId::CURRENT_VALUES_SENT ) ;
00175   }
00176 }
00177 //-----------------------------------------------------------------
00178 
00179 void InteractorExtension::startSessionWith( const Name& interactiveObject )
00180 { 
00181   if( !getSessionWith( interactiveObject ) )
00182   {
00183     OMTRACEID( OMK_DEBUG_III, "The interactor " <<  OMK::debugMsg( this, _owner ) << " starts session with \"" <<  interactiveObject << "\"") ;
00184     // Add a new session
00185     SessionPrm* session = createSession( interactiveObject ) ;
00186     if( session )
00187     {
00188       _sessionsPrm.insert( std::pair< const Name, SessionPrm* >( session->getInteractiveObject(), session ) ) ;
00189       // Asks for parameters => init will be call if the interactive object answers with the list of its parameters
00190       (*session).c( getAccessibleParameters ) ;
00191     }
00192   }
00193   else
00194   {
00195     OMTRACEID( OMK_DEBUG_III, "The interactor " <<  OMK::debugMsg( this, _owner )
00196                << " doesn't need to start a session with interactive object \"" <<  interactiveObject << "\", session already exists" ) ;
00197   }
00198 }
00199 
00200 //-----------------------------------------------------------------
00201 SessionPrm* InteractorExtension::createSession( const Name& interactiveObject ) 
00202 {
00203   // return the standard session (can be nul if interactiveObject is not a simulated object)
00204   return SessionPrm::create( this, interactiveObject ) ;
00205 }
00206 
00207 //-----------------------------------------------------------------
00208 void InteractorExtension::closeSessionWith( const Name& interactiveObject )
00209 { 
00210   SessionPrm* session = getSessionWith( interactiveObject ) ;
00211   if( session )
00212   {
00213     OMTRACEID( OMK_DEBUG_III, "The interactor " <<  OMK::debugMsg( this, _owner ) << " stops session with " <<  interactiveObject ) ;
00214     // Releases the interactive object, erases session from map, and destroys it
00215     (*session).c( controlRelease ) ;
00216     _sessionsPrm.erase( interactiveObject ) ;
00217     delete session ;
00218   }
00219 }
00220 
00221 //-----------------------------------------------------------------
00222 void InteractorExtension::closeAllSessions() 
00223 {
00224   for ( SessionsMap::const_iterator i = _sessionsPrm.begin() ; i != _sessionsPrm.end() ; i = _sessionsPrm.begin() )
00225   {
00226     closeSessionWith( i->first ) ;
00227   }
00228 }
00229 
00230 //-----------------------------------------------------------------
00231 SessionPrm* InteractorExtension::getSessionWith( const Name& interactiveObject ) 
00232 {
00233   SessionsMap::const_iterator session = _sessionsPrm.find( interactiveObject ) ;
00234   return ( session != _sessionsPrm.end() ) ? session->second : 0 ;
00235 }
00236 
00237 //-----------------------------------------------------------------
00238 bool InteractorExtension::processAccessibleParameters( Type::AccessibleParametersListEvent *event ) 
00239 {
00240   OMASSERTM( event->eventId == EventId::ACCESSIBLE_PARAMETERS, "The event must be \"" << EventId::ACCESSIBLE_PARAMETERS << "\" but is \"" << event->eventId << "\"" ) ;
00241   SessionPrm* session = getSessionWith( event->sender ) ;
00242   OMTRACEID( OMK_DEBUG_III, "The interactor " <<  OMK::debugMsg( this, _owner ) << " received the connectors list from \"" << event->sender << "\"" << ( session ? "" : " but it is not interacting with it !" ) ) ;
00243   if( session )
00244   {
00245     session->init( _interactorOutputs, event->value ) ;
00246   }
00247   return session != 0 ;
00248 }
00249 
00250 //-----------------------------------------------------------------
00251 bool InteractorExtension::processCurrentValuesSentAndControlTaken( ControlTakenEvent *event ) 
00252 {
00253   OMASSERTM( event->eventId == EventId::CURRENT_VALUES_SENT_AND_CONTROL_TAKEN, "The event must be \"" << EventId::CONTROL_TAKE_OVER_AND_GET_CURRENT_VALUES << "\" but is \"" << event->eventId << "\"" ) ;
00254   const ControlTakenPrm& value = event->value ;
00255   SessionPrm* session = getSessionWith( event->sender ) ;
00256   if( session )
00257   {
00258     for( ControlTakenPrm::const_iterator id = value.begin() ; id !=  value.end() ; ++id )
00259     {
00260       session->addControlledConnector( *id ) ;
00261     }
00262   }
00263   else
00264   {
00265     OMTRACEID( OMK_DEBUG_III, "The interactor " <<  OMK::debugMsg( this, _owner ) << " has no available session with \"" <<  event->sender << "\"" ) ;
00266   }
00267 
00268   return session != 0 ;
00269 }
00270 
00271 //-----------------------------------------------------------------
00272 bool InteractorExtension::processControlReleased( ControlReleasedEvent *event )
00273 {
00274   OMASSERTM( event->eventId == EventId::CONTROL_RELEASED, "The event must be \"" << EventId::CONTROL_RELEASED << "\" and is \"" << event->eventId << "\"" ) ;
00275   OMTRACEID( OMK_DEBUG_III, "The interactor " <<  OMK::debugMsg( this, _owner ) << " has released its control on \"" << event->value << "\"" ) ;
00276   return false ;
00277 }
00278 //-----------------------------------------------------------------
00279 bool InteractorExtension::processControlEnded( ControlEndedEvent *event ) 
00280 {
00281   OMASSERTM( event->eventId == EventId::CONTROL_ENDED, "The event must be \"" << EventId::CONTROL_ENDED << "\" and is \"" << event->eventId << "\"" ) ;
00282   const Name& value = event->value ;
00283   SessionPrm* session = getSessionWith( event->sender ) ;
00284   if( session )
00285   {
00286     session->removeControlledConnector( value ) ;
00287   }
00288   else
00289   {
00290     OMTRACEID( OMK_DEBUG_III, "The interactor " <<  OMK::debugMsg( this, _owner ) << " has no available session with \"" <<  event->sender << "\"" ) ;
00291   }
00292   return session != 0 ;
00293 }
00294 
00295 //-----------------------------------------------------------------
00296 bool InteractorExtension::processCurrentValuesSent( Event* event ) 
00297 {
00298   OMASSERTM( event->eventId == EventId::CURRENT_VALUES_SENT, "The event must be \"" << EventId::CURRENT_VALUES_SENT << "\" but is \"" << event->eventId << "\"" ) ;
00299   return true ; 
00300 }
00301 //-----------------------------------------------------------------
00302 bool InteractorExtension::processControlTakenBy( ControlUpdatedEvent* event ) 
00303 {
00304   OMASSERTM( event->eventId == EventId::CONTROL_TAKEN_BY, "The event must be \"" << EventId::CONTROL_TAKEN_BY << "\" but is \"" << event->eventId << "\"" ) ;
00305   return true ; 
00306 }
00307 //-----------------------------------------------------------------
00308 bool InteractorExtension::processControlReleasedBy( ControlUpdatedEvent* event ) 
00309 {
00310   OMASSERTM( event->eventId == EventId::CONTROL_RELEASED_BY, "The event must be \"" << EventId::CONTROL_RELEASED_BY << "\" but is \"" << event->eventId << "\"" ) ;
00311   return true ; 
00312 }
00313 //-----------------------------------------------------------------
00314 void InteractorExtension::addInteractorOutput( InteractorOutput* interactorOutput ) 
00315 {
00316   _interactorOutputs.insert( std::pair< InteractorOutputsIndex, InteractorOutput* >( InteractorOutputsIndex( interactorOutput->category(), interactorOutput->type() ), interactorOutput ) ) ;
00317 }
00318 //-----------------------------------------------------------------
00319 

logo OpenMask

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

Generated with doxygen by Dimitri van Heesch ,   1997-2007