src/GraphExecutor/DataFlowExecutor_FiniteStateMachine.hxx

Go to the documentation of this file.
00001 //  SUPERV GraphExecutor : contains classes that permit execution of graphs and particularly the execution automaton
00002 //
00003 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
00005 // 
00006 //  This library is free software; you can redistribute it and/or 
00007 //  modify it under the terms of the GNU Lesser General Public 
00008 //  License as published by the Free Software Foundation; either 
00009 //  version 2.1 of the License. 
00010 // 
00011 //  This library is distributed in the hope that it will be useful, 
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
00014 //  Lesser General Public License for more details. 
00015 // 
00016 //  You should have received a copy of the GNU Lesser General Public 
00017 //  License along with this library; if not, write to the Free Software 
00018 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
00019 // 
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //
00023 //
00024 //  File   : DataFlowBase_FiniteStateMachine.hxx
00025 //  Author : Jean Rahuel, CEA
00026 //  Module : SUPERV
00027 //  $Header:
00028 
00029 #ifndef _DATAFLOWEXECUTOR_FINITESTATEMACHINE
00030 #define _DATAFLOWEXECUTOR_FINITESTATEMACHINE
00031 
00032 #include <stdlib.h>
00033 #include <iostream>
00034 #include <unistd.h>
00035 #include <stdio.h>
00036 
00037 #include <Python.h>
00038 
00039 #include <string>
00040 #include <map>
00041 
00042 #include "DataFlowBase_Base.hxx"
00043 
00044 #include <SALOMEconfig.h>
00045 //#include CORBA_SERVER_HEADER(SUPERV)
00046 #include <SALOME_LifeCycleCORBA.hxx>
00047 
00048 class Graph_Impl ;
00049 
00050 namespace GraphBase {
00051   class Graph ;
00052 }
00053 
00054 namespace GraphExecutor {
00055 
00056 //The AutomatonState is an internal state of the automaton of the Executor
00057   enum AutomatonState { UnKnownState ,
00058                         DataUndefState ,
00059                         DataWaitingState ,
00060                         DataReadyState ,
00061                         SuspendedReadyState,
00062                         SuspendedReadyToResumeState ,
00063                         ResumedReadyState ,
00064                         KilledReadyState ,
00065                         StoppedReadyState ,
00066                         ExecutingState ,
00067                         SuspendedExecutingState ,
00068                         ResumedExecutingState ,
00069                         KilledExecutingState ,
00070                         StoppedExecutingState ,
00071                         SuccessedExecutingState ,
00072                         ErroredExecutingState ,
00073                         SuspendedSuccessedState ,
00074                         SuspendedErroredState,
00075                         ResumedSuccessedState ,
00076                         ResumedErroredState ,
00077                   KilledSuccessedState ,
00078                         KilledErroredState ,
00079                   StoppedSuccessedState ,
00080                         StoppedErroredState ,
00081                         SuccessedState ,
00082                         ErroredState ,
00083                         SuspendedState ,
00084                         KilledState ,
00085                         StoppedState ,
00086                         SuspendedSuccessedToReStartState ,
00087                         SuspendedErroredToReStartState ,
00088                         ReStartedState ,
00089                LoadingState,
00090                         NumberOfAutomatonStates } ;
00091 
00092   enum NodeEvent { UndefinedEvent , 
00093                    NewThreadEvent ,        
00094                    SuspendEvent , 
00095                    ToResumeEvent  , 
00096                    ResumeEvent ,
00097                    KillEvent , 
00098                    StopEvent ,
00099                    ExecuteEvent , 
00100                    SuccessEvent , 
00101                    ErrorEvent ,
00102                    ToReStartEvent , 
00103                    ReStartEvent , 
00104                    ReStartAndSuspendEvent ,
00105                    EndExecuteEvent ,
00106                    NoDataReadyEvent , 
00107                    SomeDataReadyEvent ,
00108                    NotAllDataReadyEvent , 
00109                    AllDataReadyEvent ,
00110                    ReadyEvent , 
00111                    SuspendedReadyEvent ,
00112                    ResumedReadyEvent ,
00113                    KilledReadyEvent , 
00114                    StoppedReadyEvent ,
00115                    ExecutingEvent , 
00116                    SuspendedExecutingEvent ,
00117                    ResumedExecutingEvent , 
00118                    KilledExecutingEvent ,
00119                    StoppedExecutingEvent ,
00120                    SuccessedExecutingEvent , 
00121                    ErroredExecutingEvent ,
00122                    SuspendedSuccessedEvent , 
00123                    SuspendedErroredEvent ,
00124                    ResumedSuccessedEvent , 
00125                    ResumedErroredEvent ,
00126                    KilledEvent , 
00127                    StoppedEvent ,
00128                    ReStartedEvent , 
00129                    ReStartedAndSuspendEvent ,
00130                    NumberOfEvents } ;
00131 
00132   enum StateEventAction { ErrorAction ,
00133                           VoidAction ,
00134                           executeAction , // +- pthread_create
00135                           ExecuteAction , // +- pthread_create
00136                           DataWaiting_SomeDataReadyAction ,
00137                           DataUndef_NotAllDataReadyAction ,
00138                           DataUndef_AllDataReadyAction ,
00139                           DataReady_SuspendAction ,
00140                           SuspendedReady_ResumeAction ,
00141                           DataReady_KillAction ,
00142                           DataReady_StopAction ,
00143                           DataReady_ExecuteAction ,
00144                           Executing_SuspendAction ,
00145                           SuspendedExecuting_ResumeAction ,
00146                           Executing_KillAction ,
00147                           Executing_StopAction ,
00148                           Executing_SuccessAction ,
00149 //                          Executing_ErrorAction ,
00150                           Errored_ExecutingAction ,
00151                           Successed_SuccessAction ,
00152                           Errored_ErrorAction ,
00153                           Successed_SuspendAction ,
00154                           Errored_SuspendAction ,
00155                           SuspendedSuccessed_ResumeAction ,
00156                           SuspendedErrored_ResumeAction ,
00157                           Successed_KillAction ,
00158                           Errored_KillAction ,
00159                           Successed_StopAction ,
00160                           Errored_StopAction ,
00161                           SuspendedSuccessed_ReStartAction ,
00162                           SuspendedErrored_ReStartAction ,
00163                           SuspendedSuccessed_ReStartAndSuspendAction ,
00164                           SuspendedErrored_ReStartAndSuspendAction ,
00165                           NumberOfActions } ;
00166 
00167   class FiniteStateMachine {
00168 
00169     private :
00170 
00171       char *              _ControlStateName[ SUPERV::NumberOfControlStates ] ;
00172       char *              _StateName[ GraphExecutor::NumberOfAutomatonStates ] ;
00173       char *              _GraphStateName[ SUPERV::NumberOfGraphStates ] ;
00174       char *              _EventName[ GraphExecutor::NumberOfEvents ] ;
00175       char *              _ActionName[ NumberOfActions ] ;
00176       GraphExecutor::AutomatonState _TransitionTable[ GraphExecutor::NumberOfAutomatonStates ]
00177                                                     [ GraphExecutor::NumberOfEvents ] ;
00178       GraphExecutor::StateEventAction _ActionTable[ GraphExecutor::NumberOfAutomatonStates ]
00179                                                   [ GraphExecutor::NumberOfEvents ] ;
00180 
00181       pthread_mutex_t            _MutexPythonWait ;
00182       bool                       _MutexPythonLocked ;
00183 // Map between FuncName and corresponding Python Object Function
00184       map< string , PyObject * > _MapOfPyFunctions ;
00185       bool                       _PyInitModule ;
00186       int                        _ExecNumber ;
00187       int                        _GraphExecutingNumber ;
00188 
00189       int                                _DbgFileNumber ;
00190 
00191 // fields and methods that use them are moved to GraphEditor::OutNode class (asv 20.09.04)  
00192 //map< string , GraphBase::Graph * > _MapOfGraphs ;
00193 //map< string , int >                _MapOfGraphNames ;
00194 
00195       bool              _JoinThread ;
00196       pthread_t         _JoinThreadNo ;
00197       pthread_mutex_t   _MutexJoinWait ;
00198       pthread_cond_t    _JoinWait ;
00199       list< pthread_t > _ThreadList ;
00200 
00201       Graph_Impl * _GraphImpl ;
00202 
00203     public :
00204 
00205       FiniteStateMachine() ;
00206       virtual ~FiniteStateMachine() {} ;
00207 
00208       int ExecNumber() ;
00209       void Executed() ;
00210       void PyInitModule(bool) ;
00211       bool PyInitModule() ;
00212       void PyLock() ;
00213       void PyUnLock() ;
00214       void PyLockReset() ;
00215       bool PyFunction( const char * aPyFuncName , PyObject * aPyFunction ) ;
00216       PyObject * PyFunction( const char * aPyFuncName ) ;
00217       bool ErasePyFunction( const char * aPyFuncName ) ;
00218 
00219 
00220       string DbgFileNumber() {
00221              _DbgFileNumber += 1 ;
00222              ostringstream astr ;
00223              astr << _DbgFileNumber ;
00224              return astr.str() ; } ;
00225 /*
00226       GraphBase::Graph * MapGraph( const char * aGraphName ) ;
00227       bool MapGraph( GraphBase::Graph * aGraph , const char * aGraphName ) ;
00228       void EraseGraph( const char * aGraphName ) ;
00229       bool GraphName( const char * aGraphName ) ;
00230       string GraphInstanceName( const char * aGraphName ) ;
00231 */
00232 
00233       void JoinThread() ;
00234       void JoinThread( pthread_t aThread ) ;
00235 
00236       const char * ControlStateName( const SUPERV::ControlState & aState ) const {
00237             return _ControlStateName[ aState ] ; } ;
00238       const char * StateName( const GraphExecutor::AutomatonState & aState ) const {
00239             return _StateName[ aState ] ; } ;
00240       const char * StateName( const SUPERV::GraphState & aState ) const {
00241             return _GraphStateName[ aState ] ; } ;
00242       const char * EventName( const GraphExecutor::NodeEvent & anEvent ) const {
00243             return _EventName[ anEvent ] ; } ;
00244       const char * ActionName( const GraphExecutor::StateEventAction & anAction ) const {
00245             return _ActionName[ anAction ] ; } ;
00246 
00247       const GraphExecutor::AutomatonState NextState( const GraphExecutor::AutomatonState & aState ,
00248                                                      const GraphExecutor::NodeEvent & anEvent ) const {
00249             return _TransitionTable[ aState ][ anEvent ] ; } ;
00250 
00251       const GraphExecutor::StateEventAction NextAction( const GraphExecutor::AutomatonState & aState ,
00252                                                         const GraphExecutor::NodeEvent & anEvent ) const {
00253             return _ActionTable[ aState ][ anEvent ] ; } ;
00254 
00255     void GraphImpl( Graph_Impl * aGraphImpl ) {// For GraphMNode ...
00256          _GraphImpl = aGraphImpl ; } ;
00257     Graph_Impl * GraphImpl() {
00258                  return _GraphImpl ; } ;
00259   };
00260 
00261 };
00262 
00263 ostream & operator<< (ostream &f ,const GraphExecutor::AutomatonState & aState ) ;
00264 
00265 #endif