]> SALOME platform Git repositories - modules/superv.git/blob - src/GraphExecutor/DataFlowExecutor_FiniteStateMachine.hxx
Salome HOME
MacroNodes and omniORB4 porting
[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 class Graph_Impl ;
49
50 namespace GraphBase {
51   class Graph ;
52 }
53
54 namespace GraphExecutor {
55
56 //The AutomatonState is an internal state of the automaton of the Executor
57   enum AutomatonState { UnKnownState ,
58                         DataUndefState ,
59                         DataWaitingState ,
60                         DataReadyState ,
61                         SuspendedReadyState,
62                         SuspendedReadyToResumeState ,
63                         ResumedReadyState ,
64                         KilledReadyState ,
65                         StoppedReadyState ,
66                         ExecutingState ,
67                         SuspendedExecutingState ,
68                         ResumedExecutingState ,
69                         KilledExecutingState ,
70                         StoppedExecutingState ,
71                         SuccessedExecutingState ,
72                         ErroredExecutingState ,
73                         SuspendedSuccessedState ,
74                         SuspendedErroredState,
75                         ResumedSuccessedState ,
76                         ResumedErroredState ,
77                         KilledSuccessedState ,
78                         KilledErroredState ,
79                         StoppedSuccessedState ,
80                         StoppedErroredState ,
81                         SuccessedState ,
82                         ErroredState ,
83                         SuspendedState ,
84                         KilledState ,
85                         StoppedState ,
86                         SuspendedSuccessedToReStartState ,
87                         SuspendedErroredToReStartState ,
88                         ReStartedState ,
89                         LoadingState,
90                         NumberOfAutomatonStates } ;
91
92   enum NodeEvent { UndefinedEvent , 
93                    NewThreadEvent ,        
94                    SuspendEvent , 
95                    ToResumeEvent  , 
96                    ResumeEvent ,
97                    KillEvent , 
98                    StopEvent ,
99                    ExecuteEvent , 
100                    SuccessEvent , 
101                    ErrorEvent ,
102                    ToReStartEvent , 
103                    ReStartEvent , 
104                    ReStartAndSuspendEvent ,
105                    EndExecuteEvent ,
106                    NoDataReadyEvent , 
107                    SomeDataReadyEvent ,
108                    NotAllDataReadyEvent , 
109                    AllDataReadyEvent ,
110                    ReadyEvent , 
111                    SuspendedReadyEvent ,
112                    ResumedReadyEvent ,
113                    KilledReadyEvent , 
114                    StoppedReadyEvent ,
115                    ExecutingEvent , 
116                    SuspendedExecutingEvent ,
117                    ResumedExecutingEvent , 
118                    KilledExecutingEvent ,
119                    StoppedExecutingEvent ,
120                    SuccessedExecutingEvent , 
121                    ErroredExecutingEvent ,
122                    SuspendedSuccessedEvent , 
123                    SuspendedErroredEvent ,
124                    ResumedSuccessedEvent , 
125                    ResumedErroredEvent ,
126                    KilledEvent , 
127                    StoppedEvent ,
128                    ReStartedEvent , 
129                    ReStartedAndSuspendEvent ,
130                    NumberOfEvents } ;
131
132   enum StateEventAction { ErrorAction ,
133                           VoidAction ,
134                           executeAction , // +- pthread_create
135                           ExecuteAction , // +- pthread_create
136                           DataWaiting_SomeDataReadyAction ,
137                           DataUndef_NotAllDataReadyAction ,
138                           DataUndef_AllDataReadyAction ,
139                           DataReady_SuspendAction ,
140                           SuspendedReady_ResumeAction ,
141                           DataReady_KillAction ,
142                           DataReady_StopAction ,
143                           DataReady_ExecuteAction ,
144                           Executing_SuspendAction ,
145                           SuspendedExecuting_ResumeAction ,
146                           Executing_KillAction ,
147                           Executing_StopAction ,
148                           Executing_SuccessAction ,
149 //                          Executing_ErrorAction ,
150                           Errored_ExecutingAction ,
151                           Successed_SuccessAction ,
152                           Errored_ErrorAction ,
153                           Successed_SuspendAction ,
154                           Errored_SuspendAction ,
155                           SuspendedSuccessed_ResumeAction ,
156                           SuspendedErrored_ResumeAction ,
157                           Successed_KillAction ,
158                           Errored_KillAction ,
159                           Successed_StopAction ,
160                           Errored_StopAction ,
161                           SuspendedSuccessed_ReStartAction ,
162                           SuspendedErrored_ReStartAction ,
163                           SuspendedSuccessed_ReStartAndSuspendAction ,
164                           SuspendedErrored_ReStartAndSuspendAction ,
165                           NumberOfActions } ;
166
167   class FiniteStateMachine {
168
169     private :
170
171       char *              _ControlStateName[ SUPERV::NumberOfControlStates ] ;
172       char *              _StateName[ GraphExecutor::NumberOfAutomatonStates ] ;
173       char *              _GraphStateName[ SUPERV::NumberOfGraphStates ] ;
174       char *              _EventName[ GraphExecutor::NumberOfEvents ] ;
175       char *              _ActionName[ NumberOfActions ] ;
176       GraphExecutor::AutomatonState _TransitionTable[ GraphExecutor::NumberOfAutomatonStates ]
177                                                     [ GraphExecutor::NumberOfEvents ] ;
178       GraphExecutor::StateEventAction _ActionTable[ GraphExecutor::NumberOfAutomatonStates ]
179                                                   [ GraphExecutor::NumberOfEvents ] ;
180
181       pthread_mutex_t            _MutexPythonWait ;
182       bool                       _MutexPythonLocked ;
183 // Map between FuncName and corresponding Python Object Function
184       map< string , PyObject * > _MapOfPyFunctions ;
185       bool                       _PyInitModule ;
186       int                        _ExecNumber ;
187       int                        _GraphExecutingNumber ;
188
189       int                                _DbgFileNumber ;
190
191 // fields and methods that use them are moved to GraphEditor::OutNode class (asv 20.09.04)  
192 //map< string , GraphBase::Graph * > _MapOfGraphs ;
193 //map< string , int >                _MapOfGraphNames ;
194
195       bool              _JoinThread ;
196       pthread_t         _JoinThreadNo ;
197       pthread_mutex_t   _MutexJoinWait ;
198       pthread_cond_t    _JoinWait ;
199       list< pthread_t > _ThreadList ;
200
201       Graph_Impl * _GraphImpl ;
202
203     public :
204
205       FiniteStateMachine() ;
206       virtual ~FiniteStateMachine() {} ;
207
208       int ExecNumber() ;
209       void Executed() ;
210       void PyInitModule(bool) ;
211       bool PyInitModule() ;
212       void PyLock() ;
213       void PyUnLock() ;
214       void PyLockReset() ;
215       bool PyFunction( const char * aPyFuncName , PyObject * aPyFunction ) ;
216       PyObject * PyFunction( const char * aPyFuncName ) ;
217       bool ErasePyFunction( const char * aPyFuncName ) ;
218
219
220       string DbgFileNumber() {
221              _DbgFileNumber += 1 ;
222              ostringstream astr ;
223              astr << _DbgFileNumber ;
224              return astr.str() ; } ;
225 /*
226       GraphBase::Graph * MapGraph( const char * aGraphName ) ;
227       bool MapGraph( GraphBase::Graph * aGraph , const char * aGraphName ) ;
228       void EraseGraph( const char * aGraphName ) ;
229       bool GraphName( const char * aGraphName ) ;
230       string GraphInstanceName( const char * aGraphName ) ;
231 */
232
233       void JoinThread() ;
234       void JoinThread( pthread_t aThread ) ;
235
236       const char * ControlStateName( const SUPERV::ControlState & aState ) const {
237             return _ControlStateName[ aState ] ; } ;
238       const char * StateName( const GraphExecutor::AutomatonState & aState ) const {
239             return _StateName[ aState ] ; } ;
240       const char * StateName( const SUPERV::GraphState & aState ) const {
241             return _GraphStateName[ aState ] ; } ;
242       const char * EventName( const GraphExecutor::NodeEvent & anEvent ) const {
243             return _EventName[ anEvent ] ; } ;
244       const char * ActionName( const GraphExecutor::StateEventAction & anAction ) const {
245             return _ActionName[ anAction ] ; } ;
246
247       const GraphExecutor::AutomatonState NextState( const GraphExecutor::AutomatonState & aState ,
248                                                      const GraphExecutor::NodeEvent & anEvent ) const {
249             return _TransitionTable[ aState ][ anEvent ] ; } ;
250
251       const GraphExecutor::StateEventAction NextAction( const GraphExecutor::AutomatonState & aState ,
252                                                         const GraphExecutor::NodeEvent & anEvent ) const {
253             return _ActionTable[ aState ][ anEvent ] ; } ;
254
255     void GraphImpl( Graph_Impl * aGraphImpl ) {// For GraphMNode ...
256          _GraphImpl = aGraphImpl ; } ;
257     Graph_Impl * GraphImpl() {
258                  return _GraphImpl ; } ;
259   };
260
261 };
262
263 ostream & operator<< (ostream &f ,const GraphExecutor::AutomatonState & aState ) ;
264
265 #endif