Salome HOME
d79e0e41cd8ec0b22eaf8ae40185a403812a9ae2
[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                         ReStartedState ,
87                         LoadingState,
88                         NumberOfAutomatonStates } ;
89
90   enum NodeEvent { UndefinedEvent , 
91                    NewThreadEvent ,        
92                    SuspendEvent , 
93                    ToResumeEvent  , 
94                    ResumeEvent ,
95                    KillEvent , 
96                    StopEvent ,
97                    ExecuteEvent , 
98                    SuccessEvent , 
99                    ErrorEvent ,
100                    ToReStartEvent , 
101                    ReStartEvent , 
102                    ReStartAndSuspendEvent ,
103                    EndExecuteEvent ,
104                    NoDataReadyEvent , 
105                    SomeDataReadyEvent ,
106                    NotAllDataReadyEvent , 
107                    AllDataReadyEvent ,
108                    ReadyEvent , 
109                    SuspendedReadyEvent ,
110                    ResumedReadyEvent ,
111                    KilledReadyEvent , 
112                    StoppedReadyEvent ,
113                    ExecutingEvent , 
114                    SuspendedExecutingEvent ,
115                    ResumedExecutingEvent , 
116                    KilledExecutingEvent ,
117                    StoppedExecutingEvent ,
118                    SuccessedExecutingEvent , 
119                    ErroredExecutingEvent ,
120                    SuspendedSuccessedEvent , 
121                    SuspendedErroredEvent ,
122                    ResumedSuccessedEvent , 
123                    ResumedErroredEvent ,
124                    KilledEvent , 
125                    StoppedEvent ,
126                    ReStartedEvent , 
127                    ReStartedAndSuspendEvent ,
128                    NumberOfEvents } ;
129
130   enum StateEventAction { ErrorAction ,
131                           VoidAction ,
132                           executeAction , // +- pthread_create
133                           ExecuteAction , // +- pthread_create
134                           DataWaiting_SomeDataReadyAction ,
135                           DataUndef_NotAllDataReadyAction ,
136                           DataUndef_AllDataReadyAction ,
137                           DataReady_SuspendAction ,
138                           SuspendedReady_ResumeAction ,
139                           DataReady_KillAction ,
140                           DataReady_StopAction ,
141                           DataReady_ExecuteAction ,
142                           Executing_SuspendAction ,
143                           SuspendedExecuting_ResumeAction ,
144                           Executing_KillAction ,
145                           Executing_StopAction ,
146                           Executing_SuccessAction ,
147                           Executing_ErrorAction ,
148                           Successed_SuccessAction ,
149                           Errored_ErrorAction ,
150                           Successed_SuspendAction ,
151                           Errored_SuspendAction ,
152                           SuspendedSuccessed_ResumeAction ,
153                           SuspendedErrored_ResumeAction ,
154                           Successed_KillAction ,
155                           Errored_KillAction ,
156                           Successed_StopAction ,
157                           Errored_StopAction ,
158                           SuspendedSuccessed_ReStartAction ,
159                           SuspendedErrored_ReStartAction ,
160                           SuspendedSuccessed_ReStartAndSuspendAction ,
161                           SuspendedErrored_ReStartAndSuspendAction ,
162                           NumberOfActions } ;
163
164   class FiniteStateMachine {
165
166     private :
167
168       char *              _ControlStateName[ SUPERV::NumberOfControlStates ] ;
169       char *              _StateName[ GraphExecutor::NumberOfAutomatonStates ] ;
170       char *              _GraphStateName[ SUPERV::NumberOfGraphStates ] ;
171       char *              _EventName[ GraphExecutor::NumberOfEvents ] ;
172       char *              _ActionName[ NumberOfActions ] ;
173       GraphExecutor::AutomatonState _TransitionTable[ GraphExecutor::NumberOfAutomatonStates ]
174                                                     [ GraphExecutor::NumberOfEvents ] ;
175       GraphExecutor::StateEventAction _ActionTable[ GraphExecutor::NumberOfAutomatonStates ]
176                                                   [ GraphExecutor::NumberOfEvents ] ;
177
178       pthread_mutex_t            _MutexPythonWait ;
179       bool                       _MutexPythonLocked ;
180 // Map between FuncName and corresponding Python Object Function
181       map< string , PyObject * > _MapOfPyFunctions ;
182       bool                       _PyInitModule ;
183       int                        _ExecNumber ;
184       int                        _GraphExecutingNumber ;
185
186       int                                _DbgFileNumber ;
187
188 // fields and methods that use them are moved to GraphEditor::OutNode class (asv 20.09.04)  
189 //map< string , GraphBase::Graph * > _MapOfGraphs ;
190 //map< string , int >                _MapOfGraphNames ;
191
192       bool              _JoinThread ;
193       pthread_t         _JoinThreadNo ;
194       pthread_mutex_t   _MutexJoinWait ;
195       pthread_cond_t    _JoinWait ;
196       list< pthread_t > _ThreadList ;
197
198     public :
199
200       FiniteStateMachine() ;
201       virtual ~FiniteStateMachine() {} ;
202
203       int ExecNumber() ;
204       void Executed() ;
205       void PyInitModule(bool) ;
206       bool PyInitModule() ;
207       void PyLock() ;
208       void PyUnLock() ;
209       void PyLockReset() ;
210       bool PyFunction( const char * aPyFuncName , PyObject * aPyFunction ) ;
211       PyObject * PyFunction( const char * aPyFuncName ) ;
212       bool ErasePyFunction( const char * aPyFuncName ) ;
213
214
215       string DbgFileNumber() {
216              _DbgFileNumber += 1 ;
217              ostringstream astr ;
218              astr << _DbgFileNumber ;
219              return astr.str() ; } ;
220 /*
221       GraphBase::Graph * MapGraph( const char * aGraphName ) ;
222       bool MapGraph( GraphBase::Graph * aGraph , const char * aGraphName ) ;
223       void EraseGraph( const char * aGraphName ) ;
224       bool GraphName( const char * aGraphName ) ;
225       string GraphInstanceName( const char * aGraphName ) ;
226 */
227
228       void JoinThread() ;
229       void JoinThread( pthread_t aThread ) ;
230
231       const char * ControlStateName( const SUPERV::ControlState & aState ) const {
232             return _ControlStateName[ aState ] ; } ;
233       const char * StateName( const GraphExecutor::AutomatonState & aState ) const {
234             return _StateName[ aState ] ; } ;
235       const char * StateName( const SUPERV::GraphState & aState ) const {
236             return _GraphStateName[ aState ] ; } ;
237       const char * EventName( const GraphExecutor::NodeEvent & anEvent ) const {
238             return _EventName[ anEvent ] ; } ;
239       const char * ActionName( const GraphExecutor::StateEventAction & anAction ) const {
240             return _ActionName[ anAction ] ; } ;
241
242       const GraphExecutor::AutomatonState NextState( const GraphExecutor::AutomatonState & aState ,
243                                                      const GraphExecutor::NodeEvent & anEvent ) const {
244             return _TransitionTable[ aState ][ anEvent ] ; } ;
245
246       const GraphExecutor::StateEventAction NextAction( const GraphExecutor::AutomatonState & aState ,
247                                                         const GraphExecutor::NodeEvent & anEvent ) const {
248             return _ActionTable[ aState ][ anEvent ] ; } ;
249   };
250
251 };
252
253 ostream & operator<< (ostream &f ,const GraphExecutor::AutomatonState & aState ) ;
254
255 #endif