]> SALOME platform Git repositories - modules/superv.git/blob - src/GraphExecutor/DataFlowExecutor_FiniteStateMachine.hxx
Salome HOME
DCQ:prepare 2.0.0
[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       map< string , GraphBase::Graph * > _MapOfGraphs ;
166       map< string , int >                _MapOfGraphNames ;
167
168       bool              _JoinThread ;
169       pthread_t         _JoinThreadNo ;
170       pthread_mutex_t   _MutexJoinWait ;
171       pthread_cond_t    _JoinWait ;
172       list< pthread_t > _ThreadList ;
173
174     public :
175
176       FiniteStateMachine() ;
177       virtual ~FiniteStateMachine() {} ;
178
179       int ExecNumber() ;
180       void Executed() ;
181       bool PyInitModule() ;
182       void PyLock() ;
183       void PyUnLock() ;
184       bool PyFunction( const char * aPyFuncName , PyObject * aPyFunction ) ;
185       PyObject * PyFunction( const char * aPyFuncName ) ;
186
187       string DbgFileNumber() {
188              _DbgFileNumber += 1 ;
189              ostringstream astr ;
190              astr << _DbgFileNumber ;
191              return astr.str() ; } ;
192       GraphBase::Graph * MapGraph( const char * aGraphName ) ;
193       bool MapGraph( GraphBase::Graph * aGraph , const char * aGraphName ) ;
194       void EraseGraph( const char * aGraphName ) ;
195       bool GraphName( const char * aGraphName ) ;
196       string GraphInstanceName( const char * aGraphName ) ;
197
198       void JoinThread() ;
199       void JoinThread( pthread_t aThread ) ;
200
201       const char * ControlStateName( const SUPERV::ControlState & aState ) const {
202             return _ControlStateName[ aState ] ; } ;
203       const char * StateName( const GraphExecutor::AutomatonState & aState ) const {
204             return _StateName[ aState ] ; } ;
205       const char * StateName( const SUPERV::GraphState & aState ) const {
206             return _GraphStateName[ aState ] ; } ;
207       const char * EventName( const GraphExecutor::NodeEvent & anEvent ) const {
208             return _EventName[ anEvent ] ; } ;
209       const char * ActionName( const GraphExecutor::StateEventAction & anAction ) const {
210             return _ActionName[ anAction ] ; } ;
211
212       const GraphExecutor::AutomatonState NextState( const GraphExecutor::AutomatonState & aState ,
213                                                      const GraphExecutor::NodeEvent & anEvent ) const {
214             return _TransitionTable[ aState ][ anEvent ] ; } ;
215
216       const GraphExecutor::StateEventAction NextAction( const GraphExecutor::AutomatonState & aState ,
217                                                         const GraphExecutor::NodeEvent & anEvent ) const {
218             return _ActionTable[ aState ][ anEvent ] ; } ;
219   };
220
221 };
222
223 ostream & operator<< (ostream &f ,const GraphExecutor::AutomatonState & aState ) ;
224
225 #endif