Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/superv.git] / src / GraphExecutor / DataFlowExecutor_FiniteStateMachine.hxx
1 //  SUPERV GraphExecutor : contains classes that permit execution of graphs and particularly the execution automaton
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : DataFlowBase_FiniteStateMachine.hxx
25 //  Author : Jean Rahuel, CEA
26 //  Module : SUPERV
27 //  $Header:
28
29 #ifndef _DATAFLOWEXECUTOR_FINITESTATEMACHINE
30 #define _DATAFLOWEXECUTOR_FINITESTATEMACHINE
31
32 #include <stdlib.h>
33 #include <iostream>
34 #include <unistd.h>
35 #include <stdio.h>
36
37 #include <Python.h>
38
39 #include <string>
40 #include <map>
41
42 #include "DataFlowBase_Base.hxx"
43
44 #include <SALOMEconfig.h>
45 //#include CORBA_SERVER_HEADER(SUPERV)
46 #include <SALOME_LifeCycleCORBA.hxx>
47
48 namespace GraphBase {
49   class Graph ;
50 }
51
52 namespace GraphExecutor {
53
54 //The AutomatonState is an internal state of the automaton of the Executor
55   enum AutomatonState { UnKnownState ,
56                         DataUndefState ,
57                         DataWaitingState ,
58                         DataReadyState ,
59                         SuspendedReadyState,
60                         SuspendedReadyToResumeState ,
61                         ResumedReadyState ,
62                         KilledReadyState ,
63                         StoppedReadyState ,
64                         ExecutingState ,
65                         SuspendedExecutingState ,
66                         ResumedExecutingState ,
67                         KilledExecutingState ,
68                         StoppedExecutingState ,
69                         SuccessedExecutingState ,
70                         ErroredExecutingState ,
71                         SuspendedSuccessedState ,
72                         SuspendedErroredState,
73                         ResumedSuccessedState ,
74                         ResumedErroredState ,
75                              KilledSuccessedState ,
76                         KilledErroredState ,
77                              StoppedSuccessedState ,
78                         StoppedErroredState ,
79                         SuccessedState ,
80                         ErroredState ,
81                         SuspendedState ,
82                         KilledState ,
83                         StoppedState ,
84                         SuspendedSuccessedToReStartState ,
85                         SuspendedErroredToReStartState ,
86                         ReRunnedState ,
87                         ReStartedState ,
88                         NumberOfAutomatonStates } ;
89
90   enum NodeEvent { UndefinedEvent , NewThreadEvent ,
91                      SuspendEvent , ToResumeEvent  , ResumeEvent ,
92                      KillEvent , StopEvent ,
93                      ExecuteEvent , SuccessEvent , ErrorEvent ,
94                      ToReStartEvent , ReStartEvent , ReStartAndSuspendEvent ,
95                      EndExecuteEvent ,
96                    NoDataReadyEvent , SomeDataReadyEvent ,
97                    NotAllDataReadyEvent , AllDataReadyEvent ,
98                      ReadyEvent , SuspendedReadyEvent ,ResumedReadyEvent ,
99                      KilledReadyEvent , StoppedReadyEvent ,
100                    ExecutingEvent , SuspendedExecutingEvent ,
101                    ResumedExecutingEvent , KilledExecutingEvent ,
102                    StoppedExecutingEvent ,
103                      SuccessedExecutingEvent , ErroredExecutingEvent ,
104                      SuspendedSuccessedEvent , SuspendedErroredEvent ,
105                      ResumedSuccessedEvent , ResumedErroredEvent ,
106                      KilledEvent , StoppedEvent ,
107                    ReStartedEvent , ReStartedAndSuspendEvent ,
108                      NumberOfEvents } ;
109
110   enum StateEventAction { ErrorAction ,
111                           VoidAction ,
112                           executeAction , // +- pthread_create
113                           ExecuteAction , // +- pthread_create
114                             DataWaiting_SomeDataReadyAction ,
115                             DataUndef_NotAllDataReadyAction ,
116                             DataUndef_AllDataReadyAction ,
117                           DataReady_SuspendAction ,
118                           SuspendedReady_ResumeAction ,
119                           DataReady_KillAction ,
120                           DataReady_StopAction ,
121                             DataReady_ExecuteAction ,
122                           Executing_SuspendAction ,
123                           SuspendedExecuting_ResumeAction ,
124                           Executing_KillAction ,
125                           Executing_StopAction ,
126                             Executing_SuccessAction ,
127                             Executing_ErrorAction ,
128                           Successed_SuccessAction ,
129                           Errored_ErrorAction ,
130                           Successed_SuspendAction ,
131                           Errored_SuspendAction ,
132                           SuspendedSuccessed_ResumeAction ,
133                           SuspendedErrored_ResumeAction ,
134                           Successed_KillAction ,
135                           Errored_KillAction ,
136                           Successed_StopAction ,
137                           Errored_StopAction ,
138                             SuspendedSuccessed_ReStartAction ,
139                             SuspendedErrored_ReStartAction ,
140                             SuspendedSuccessed_ReStartAndSuspendAction ,
141                             SuspendedErrored_ReStartAndSuspendAction ,
142                           NumberOfActions } ;
143
144   class FiniteStateMachine {
145
146     private :
147
148       char *              _ControlStateName[ SUPERV::NumberOfControlStates ] ;
149       char *              _StateName[ GraphExecutor::NumberOfAutomatonStates ] ;
150       char *              _GraphStateName[ SUPERV::NumberOfGraphStates ] ;
151       char *              _EventName[ GraphExecutor::NumberOfEvents ] ;
152       char *              _ActionName[ NumberOfActions ] ;
153       GraphExecutor::AutomatonState _TransitionTable[ GraphExecutor::NumberOfAutomatonStates ]
154                                                     [ GraphExecutor::NumberOfEvents ] ;
155       GraphExecutor::StateEventAction _ActionTable[ GraphExecutor::NumberOfAutomatonStates ]
156                                                   [ GraphExecutor::NumberOfEvents ] ;
157
158       pthread_mutex_t            _MutexPythonWait ;
159       map< string , PyObject * > _MapOfPyFunctions ;
160       bool                       _PyInitModule ;
161       int                        _ExecNumber ;
162       int                        _GraphExecutingNumber ;
163
164       int                                _DbgFileNumber ;
165
166 // fields and methods that use them are moved to GraphEditor::OutNode class (asv 20.09.04)  
167 //map< string , GraphBase::Graph * > _MapOfGraphs ;
168 //map< string , int >                _MapOfGraphNames ;
169
170       bool              _JoinThread ;
171       pthread_t         _JoinThreadNo ;
172       pthread_mutex_t   _MutexJoinWait ;
173       pthread_cond_t    _JoinWait ;
174       list< pthread_t > _ThreadList ;
175
176     public :
177
178       FiniteStateMachine() ;
179       virtual ~FiniteStateMachine() {} ;
180
181       int ExecNumber() ;
182       void Executed() ;
183       bool PyInitModule() ;
184       void PyLock() ;
185       void PyUnLock() ;
186       bool PyFunction( const char * aPyFuncName , PyObject * aPyFunction ) ;
187       PyObject * PyFunction( const char * aPyFuncName ) ;
188
189
190       string DbgFileNumber() {
191              _DbgFileNumber += 1 ;
192              ostringstream astr ;
193              astr << _DbgFileNumber ;
194              return astr.str() ; } ;
195 /*
196       GraphBase::Graph * MapGraph( const char * aGraphName ) ;
197       bool MapGraph( GraphBase::Graph * aGraph , const char * aGraphName ) ;
198       void EraseGraph( const char * aGraphName ) ;
199       bool GraphName( const char * aGraphName ) ;
200       string GraphInstanceName( const char * aGraphName ) ;
201 */
202
203       void JoinThread() ;
204       void JoinThread( pthread_t aThread ) ;
205
206       const char * ControlStateName( const SUPERV::ControlState & aState ) const {
207             return _ControlStateName[ aState ] ; } ;
208       const char * StateName( const GraphExecutor::AutomatonState & aState ) const {
209             return _StateName[ aState ] ; } ;
210       const char * StateName( const SUPERV::GraphState & aState ) const {
211             return _GraphStateName[ aState ] ; } ;
212       const char * EventName( const GraphExecutor::NodeEvent & anEvent ) const {
213             return _EventName[ anEvent ] ; } ;
214       const char * ActionName( const GraphExecutor::StateEventAction & anAction ) const {
215             return _ActionName[ anAction ] ; } ;
216
217       const GraphExecutor::AutomatonState NextState( const GraphExecutor::AutomatonState & aState ,
218                                                      const GraphExecutor::NodeEvent & anEvent ) const {
219             return _TransitionTable[ aState ][ anEvent ] ; } ;
220
221       const GraphExecutor::StateEventAction NextAction( const GraphExecutor::AutomatonState & aState ,
222                                                         const GraphExecutor::NodeEvent & anEvent ) const {
223             return _ActionTable[ aState ][ anEvent ] ; } ;
224   };
225
226 };
227
228 ostream & operator<< (ostream &f ,const GraphExecutor::AutomatonState & aState ) ;
229
230 #endif