]> SALOME platform Git repositories - modules/superv.git/blob - src/GraphExecutor/DataFlowExecutor_FiniteStateMachine.cxx
Salome HOME
Python execution management : Lock/Unlock
[modules/superv.git] / src / GraphExecutor / DataFlowExecutor_FiniteStateMachine.cxx
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.cxx
25 //  Author : Jean Rahuel, CEA
26 //  Module : SUPERV
27 //  $Header:
28
29 using namespace std;
30 #include <stdlib.h>
31 #include <iostream>
32 #include <unistd.h>
33 #include <stdio.h>
34
35 #include "DataFlowExecutor_FiniteStateMachine.hxx"
36
37 void * start_function( void *p ) ;
38
39 // ControlStates :
40 #define VoidState           SUPERV::VoidState
41 #define ToSuspendStartState SUPERV::ToSuspendStartState
42 #define ToSuspendState      SUPERV::ToSuspendState
43 #define ToSuspendDoneState  SUPERV::ToSuspendDoneState
44 #define ToKillState         SUPERV::ToKillState
45 #define ToKillDoneState     SUPERV::ToKillDoneState
46 #define ToStopState         SUPERV::ToStopState
47
48 #if 0
49 // States :
50 #define UnKnownState            GraphExecutor::UnKnownState
51 #define DataUndefState            GraphExecutor::DataUndefState
52 #define DataWaitingState          GraphExecutor::DataWaitingState
53 #define DataReadyState          GraphExecutor::DataReadyState
54 #define SuspendedReadyState     GraphExecutor::SuspendedReadyState
55 #define SuspendedReadyToResumeState GraphExecutor::SuspendedReadyToResumeState
56 #define ResumedReadyState       GraphExecutor::ResumedReadyState
57 #define KilledReadyState        GraphExecutor::KilledReadyState
58 #define StoppedReadyState       GraphExecutor::StoppedReadyState
59 #define ExecutingState            GraphExecutor::ExecutingState
60 #define SuspendedExecutingState   GraphExecutor::SuspendedExecutingState
61 #define ResumedExecutingState     GraphExecutor::ResumedExecutingState
62 #define KilledExecutingState      GraphExecutor::KilledExecutingState
63 #define StoppedExecutingState     GraphExecutor::StoppedExecutingState
64 #define SuccessedExecutingState   GraphExecutor::SuccessedExecutingState
65 #define ErroredExecutingState     GraphExecutor::ErroredExecutingState
66 #define SuspendedSuccessedState GraphExecutor::SuspendedSuccessedState
67 #define SuspendedErroredState   GraphExecutor::SuspendedErroredState
68 #define SuspendedSuccessedToReStartState GraphExecutor::SuspendedSuccessedToReStartState
69 #define SuspendedErroredToReStartState   GraphExecutor::SuspendedErroredToReStartState
70 #define ResumedSuccessedState   GraphExecutor::ResumedSuccessedState
71 #define ResumedErroredState     GraphExecutor::ResumedErroredState
72 #define KilledSuccessedState    GraphExecutor::KilledSuccessedState
73 #define KilledErroredState      GraphExecutor::KilledErroredState
74 #define StoppedSuccessedState   GraphExecutor::StoppedSuccessedState
75 #define StoppedErroredState     GraphExecutor::StoppedErroredState
76 #define SuccessedState          GraphExecutor::SuccessedState
77 #define ErroredState            GraphExecutor::ErroredState
78 #define SuspendedState          GraphExecutor::SuspendedState
79 #define KilledState             GraphExecutor::KilledState
80 #define StoppedState            GraphExecutor::StoppedState
81 #define ReRunnedState             GraphExecutor::ReRunnedState
82 #define ReStartedState            GraphExecutor::ReStartedState
83 #define NumberOfAutomatonStates        GraphExecutor::NumberOfAutomatonStates
84 #endif
85
86 // Events :
87 #define UndefinedEvent        GraphExecutor::UndefinedEvent
88 #define NewThreadEvent        GraphExecutor::NewThreadEvent
89 #define SuspendEvent            GraphExecutor::SuspendEvent
90 #define ToResumeEvent           GraphExecutor::ToResumeEvent
91 #define ResumeEvent             GraphExecutor::ResumeEvent
92 #define KillEvent               GraphExecutor::KillEvent
93 #define StopEvent               GraphExecutor::StopEvent
94 #define ExecuteEvent            GraphExecutor::ExecuteEvent
95 #define SuccessEvent            GraphExecutor::SuccessEvent
96 #define ErrorEvent              GraphExecutor::ErrorEvent
97 #define ReStartEvent            GraphExecutor::ReStartEvent
98 #define ReStartAndSuspendEvent  GraphExecutor::ReStartAndSuspendEvent
99 #define NoDataReadyEvent      GraphExecutor::NoDataReadyEvent
100 #define SomeDataReadyEvent    GraphExecutor::SomeDataReadyEvent
101 #define NotAllDataReadyEvent  GraphExecutor::NotAllDataReadyEvent
102 #define AllDataReadyEvent     GraphExecutor::AllDataReadyEvent
103 #define ReadyEvent              GraphExecutor::ReadyEvent
104 #define SuspendedReadyEvent     GraphExecutor::SuspendedReadyEvent
105 #define ResumedReadyEvent       GraphExecutor::ResumedReadyEvent
106 #define KilledReadyEvent        GraphExecutor::KilledReadyEvent
107 #define StoppedReadyEvent       GraphExecutor::StoppedReadyEvent
108 #define ExecutingEvent            GraphExecutor::ExecutingEvent
109 #define SuspendedExecutingEvent   GraphExecutor::SuspendedExecutingEvent
110 #define ResumedExecutingEvent     GraphExecutor::ResumedExecutingEvent
111 #define KilledExecutingEvent      GraphExecutor::KilledExecutingEvent
112 #define StoppedExecutingEvent     GraphExecutor::StoppedExecutingEvent
113 #define SuccessedExecutingEvent GraphExecutor::SuccessedExecutingEvent
114 #define ErroredExecutingEvent   GraphExecutor::ErroredExecutingEvent
115 #define SuspendedSuccessedEvent   GraphExecutor::SuspendedSuccessedEvent
116 #define SuspendedErroredEvent     GraphExecutor::SuspendedErroredEvent
117 #define ResumedSuccessedEvent     GraphExecutor::ResumedSuccessedEvent
118 #define ResumedErroredEvent       GraphExecutor::ResumedErroredEvent
119 #define KilledEvent               GraphExecutor::KilledEvent
120 #define StoppedEvent              GraphExecutor::StoppedEvent
121 #define ToReStartEvent              GraphExecutor::ToReStartEvent
122 #define ReStartedEvent              GraphExecutor::ReStartedEvent
123 #define ReStartedAndSuspendEvent    GraphExecutor::ReStartedAndSuspendEvent
124 #define EndExecuteEvent           GraphExecutor::EndExecuteEvent
125 #define NumberOfEvents            GraphExecutor::NumberOfEvents
126
127 GraphExecutor::FiniteStateMachine::FiniteStateMachine() {
128 //  cdebug_in << "FiniteStateMachine::FiniteStateMachine" << endl;
129
130   _ControlStateName[ VoidState ]          = "VoidState" ;
131   _ControlStateName[ ToSuspendStartState ]= "ToSuspendStartState" ;
132   _ControlStateName[ ToSuspendState ]     = "ToSuspendState" ;
133   _ControlStateName[ ToSuspendDoneState ] = "ToSuspendDoneState" ;
134   _ControlStateName[ ToKillState ]        = "ToKillState" ;
135   _ControlStateName[ ToKillDoneState ]    = "ToKillDoneState" ;
136   _ControlStateName[ ToStopState ]        = "ToStopState" ;
137
138   _StateName[ UnKnownState ]          = "UnKnownState" ;
139   _StateName[ DataUndefState ]            = "DataUndefState" ;
140   _StateName[ DataWaitingState ]          = "DataWaitingState" ;
141   _StateName[ DataReadyState ]            = "DataReadyState" ;
142   _StateName[ SuspendedReadyState ]     = "SuspendedReadyState" ;
143   _StateName[ SuspendedReadyToResumeState ] = "SuspendedReadyToResumeState" ;
144   _StateName[ ResumedReadyState ]       = "ResumedReadyState" ;
145   _StateName[ KilledReadyState ]        = "KilledReadyState" ;
146   _StateName[ StoppedReadyState ]       = "StoppedReadyState" ;
147   _StateName[ ExecutingState ]            = "ExecutingState" ;
148   _StateName[ SuspendedExecutingState ]   = "SuspendedExecutingState" ;
149   _StateName[ ResumedExecutingState ]     = "ResumedExecutingState" ;
150   _StateName[ KilledExecutingState ]      = "KilledExecutingState" ;
151   _StateName[ StoppedExecutingState ]     = "StoppedExecutingState" ;
152   _StateName[ SuccessedExecutingState ]   = "SuccessedExecutingState" ;
153   _StateName[ ErroredExecutingState ]     = "ErroredExecutingState" ;
154   _StateName[ SuspendedSuccessedState ] = "SuspendedSuccessedState" ;
155   _StateName[ SuspendedErroredState ]   = "SuspendedErroredState" ;
156   _StateName[ SuspendedSuccessedToReStartState ] = "SuspendedSuccessedToReStartState" ;
157   _StateName[ SuspendedErroredToReStartState ]   = "SuspendedErroredToReStartState" ;
158   _StateName[ ResumedSuccessedState ]   = "ResumedSuccessedState" ;
159   _StateName[ ResumedErroredState ]     = "ResumedErroredState" ;
160   _StateName[ KilledSuccessedState ]    = "KilledSuccessedState" ;
161   _StateName[ KilledErroredState ]      = "KilledErroredState" ;
162   _StateName[ StoppedSuccessedState ]   = "StoppedSuccessedState" ;
163   _StateName[ StoppedErroredState ]     = "StoppedErroredState" ;
164   _StateName[ SuccessedState ]          = "SuccessedState" ;
165   _StateName[ ErroredState ]            = "ErroredState" ;
166   _StateName[ SuspendedState ]          = "SuspendedState" ;
167   _StateName[ KilledState ]             = "KilledState" ;
168   _StateName[ StoppedState ]            = "StoppedState" ;
169   _StateName[ ReRunnedState ]             = "ReRunnedState" ;
170   _StateName[ ReStartedState ]            = "ReStartedState" ;
171
172   _EventName[ UndefinedEvent ]          = "UndefinedEvent" ;
173   _EventName[ NewThreadEvent ]          = "NewThreadEvent" ;
174   _EventName[ SuspendEvent ]              = "SuspendEvent" ;
175   _EventName[ ToResumeEvent ]             = "ToResumeEvent" ;
176   _EventName[ ResumeEvent ]               = "ResumeEvent" ;
177   _EventName[ KillEvent ]                 = "KillEvent" ;
178   _EventName[ StopEvent ]                 = "StopEvent" ;
179   _EventName[ ExecuteEvent ]              = "ExecuteEvent" ;
180   _EventName[ SuccessEvent ]              = "SuccessEvent" ;
181   _EventName[ ErrorEvent ]                = "ErrorEvent" ;
182   _EventName[ ToReStartEvent ]            = "ToReStartEvent" ;
183   _EventName[ ReStartEvent ]              = "ReStartEvent" ;
184   _EventName[ ReStartAndSuspendEvent ]    = "ReStartAndSuspendEvent" ;
185   _EventName[ NoDataReadyEvent ]        = "NoDataReadyEvent" ;
186   _EventName[ SomeDataReadyEvent ]      = "SomeDataReadyEvent" ;
187   _EventName[ NotAllDataReadyEvent ]    = "NotAllDataReadyEvent" ;
188   _EventName[ AllDataReadyEvent ]       = "AllDataReadyEvent" ;
189   _EventName[ ReadyEvent ]                = "ReadyEvent" ;
190   _EventName[ SuspendedReadyEvent ]       = "SuspendedReadyEvent" ;
191   _EventName[ ResumedReadyEvent ]         = "ResumedReadyEvent" ;
192   _EventName[ KilledReadyEvent ]          = "KilledReadyEvent" ;
193   _EventName[ StoppedReadyEvent ]         = "StoppedReadyEvent" ;
194   _EventName[ ExecutingEvent ]          = "ExecutingEvent" ;
195   _EventName[ SuspendedExecutingEvent ] = "SuspendedExecutingEvent" ;
196   _EventName[ ResumedExecutingEvent ]   = "ResumedExecutingEvent" ;
197   _EventName[ KilledExecutingEvent ]    = "KilledExecutingEvent" ;
198   _EventName[ StoppedExecutingEvent ]   = "StoppedExecutingEvent" ;
199   _EventName[ SuccessedExecutingEvent ]   = "SuccessedExecutingEvent" ;
200   _EventName[ ErroredExecutingEvent ]     = "ErroredExecutingEvent" ;
201   _EventName[ SuspendedSuccessedEvent ]   = "SuspendedSuccessedEvent" ;
202   _EventName[ SuspendedErroredEvent ]     = "SuspendedErroredEvent" ;
203   _EventName[ ResumedSuccessedEvent ]     = "ResumedSuccessedEvent" ;
204   _EventName[ ResumedErroredEvent ]       = "ResumedErroredEvent" ;
205   _EventName[ KilledEvent ]               = "KilledEvent" ;
206   _EventName[ StoppedEvent ]              = "StoppedEvent" ;
207   _EventName[ ReStartedEvent ]          = "ReStartedEvent" ;
208   _EventName[ ReStartedAndSuspendEvent ]= "ReStartedAndSuspendEvent" ;
209   _EventName[ EndExecuteEvent ]         = "EndExecuteEvent" ;
210
211   _ActionName[ ErrorAction ] = "ErrorAction" ;
212   _ActionName[ VoidAction ] = "VoidAction" ;
213   _ActionName[ executeAction ] = "executeAction" ;
214   _ActionName[ ExecuteAction ] = "ExecuteAction" ;
215   _ActionName[ DataWaiting_SomeDataReadyAction ] = "DataWaiting_SomeDataReadyAction" ;
216   _ActionName[ DataUndef_NotAllDataReadyAction ] = "DataUndef_NotAllDataReadyAction";
217   _ActionName[ DataUndef_AllDataReadyAction ] = "DataUndef_AllDataReadyAction" ;
218
219   _ActionName[ DataReady_SuspendAction ] = "DataReady_SuspendAction" ;
220   _ActionName[ SuspendedReady_ResumeAction ] = "SuspendedReady_ResumeAction" ;
221   _ActionName[ DataReady_KillAction ] = "DataReady_KillAction" ;
222   _ActionName[ DataReady_StopAction ] = "DataReady_StopAction" ;
223
224   _ActionName[ DataReady_ExecuteAction ] = "DataReady_ExecuteAction" ;
225
226   _ActionName[ Executing_SuspendAction ] = "Executing_SuspendAction" ;
227   _ActionName[ SuspendedExecuting_ResumeAction ] = "SuspendedExecuting_ResumeAction" ;
228   _ActionName[ Executing_KillAction ] = "Executing_KillAction" ;
229   _ActionName[ Executing_StopAction ] = "Executing_StopAction" ;
230
231   _ActionName[ Executing_SuccessAction ] = "Executing_SuccessAction" ;
232   _ActionName[ Executing_ErrorAction ] = "Executing_ErrorAction" ;
233   _ActionName[ Successed_SuccessAction ] = "Successed_SuccessAction" ;
234   _ActionName[ Errored_ErrorAction ] = "Errored_ErrorAction" ;
235
236   _ActionName[ Successed_SuspendAction ] = "Successed_SuspendAction" ;
237   _ActionName[ Errored_SuspendAction ] = "Errored_SuspendAction" ;
238   _ActionName[ SuspendedSuccessed_ResumeAction ] = "SuspendedSuccessed_ResumeAction" ;
239   _ActionName[ SuspendedErrored_ResumeAction ] = "SuspendedErrored_ResumeAction" ;
240   _ActionName[ Successed_KillAction ] = "Successed_KillAction" ;
241   _ActionName[ Errored_KillAction ] = "Errored_KillAction" ;
242   _ActionName[ Successed_StopAction ] = "Successed_StopAction" ;
243   _ActionName[ Errored_StopAction ] = "Errored_StopAction" ;
244
245   _ActionName[ SuspendedSuccessed_ReStartAction ] = "SuspendedSuccessed_ReStartAction" ;
246   _ActionName[ SuspendedErrored_ReStartAction ] = "SuspendedErrored_ReStartAction" ;
247   _ActionName[ SuspendedSuccessed_ReStartAndSuspendAction ] = "SuspendedSuccessed_ReStartAndSuspendAction" ;
248   _ActionName[ SuspendedErrored_ReStartAndSuspendAction ] = "SuspendedErrored_ReStartAndSuspendAction" ;
249
250   _GraphStateName[ SUPERV::UndefinedState ] = "UndefinedState" ;
251   _GraphStateName[ SUPERV::NoState ] = "NoState" ;
252   _GraphStateName[ SUPERV::EditingState ] = "EditingState" ;
253   _GraphStateName[ SUPERV::SuspendState ] = "SuspendState" ;
254   _GraphStateName[ SUPERV::WaitingState ] = "WaitingState" ;
255   _GraphStateName[ SUPERV::ReadyState ] = "ReadyState" ;
256   _GraphStateName[ SUPERV::SuspendReadyState ] = "SuspendReadyState" ;
257   _GraphStateName[ SUPERV::RunningState ] = "RunningState" ;
258   _GraphStateName[ SUPERV::DoneState ] = "DoneState" ;
259   _GraphStateName[ SUPERV::ErrorState ] = "ErrorState" ;
260   _GraphStateName[ SUPERV::SuspendDoneState ] = "SuspendDoneState" ;
261   _GraphStateName[ SUPERV::SuspendErroredState ] = "SuspendErroredState" ;
262   _GraphStateName[ SUPERV::KillState ] = "KillState" ;
263   _GraphStateName[ SUPERV::StopState ] = "StopState" ;
264   _GraphStateName[ SUPERV::ReRunState ] = "ReRunState" ;
265   _GraphStateName[ SUPERV::ReStartState ] = "ReStartState" ;
266
267   int i , j ;
268 // NewStates = _TransitionTable[ States ] [ Events ]
269 // associated action = _ActionTable[ NewStates ] [ Events ]
270   for ( i = 0 ; i < NumberOfAutomatonStates ; i++ ) {
271     for ( j = 0 ; j < NumberOfEvents ; j++ ) {
272 // id est = SameState and VoidAction
273       _TransitionTable[ i ][ j ] = (GraphExecutor::AutomatonState ) i ;
274       _ActionTable[ i ][ j ] = GraphExecutor::ErrorAction ;
275     }
276   }
277 // OneEvent ===> Change of State and associated Action :
278 //               _TransitionTable[ OldState ][ OneEvent ] gives a NewState
279 //               _Action[ NewState ][ OneEvent ] gives what to do
280
281 // INPUT-DATAS :
282 // NoDataReadyEvent :
283   _TransitionTable[ DataUndefState ][ NoDataReadyEvent ] = DataWaitingState ;
284   _ActionTable[ DataWaitingState ][ NoDataReadyEvent ] = VoidAction ;
285 // SomeDataReadyEvent :
286   _TransitionTable[ DataWaitingState ][ SomeDataReadyEvent ] = DataUndefState ;
287   _ActionTable[ DataUndefState ][ SomeDataReadyEvent ] = DataWaiting_SomeDataReadyAction;
288   _TransitionTable[ DataReadyState ][ SomeDataReadyEvent ] = DataUndefState ;
289   _ActionTable[ DataUndefState ][ SomeDataReadyEvent ] = DataWaiting_SomeDataReadyAction;
290 // NotAllDataReadyEvent :
291   _TransitionTable[ DataUndefState ][ NotAllDataReadyEvent ] = DataWaitingState ;
292   _ActionTable[ DataWaitingState ][ NotAllDataReadyEvent ] = DataUndef_NotAllDataReadyAction;
293 // AllDataReadyEvent
294   _TransitionTable[ DataUndefState ][ AllDataReadyEvent ] = DataReadyState ;
295   _ActionTable[ DataReadyState ][ AllDataReadyEvent ] = DataUndef_AllDataReadyAction ;
296
297 // DATAS-READY-CONTROL :
298 // SuspendEvent
299   _TransitionTable[ DataReadyState ][ SuspendEvent ] = SuspendedReadyState ;
300   _ActionTable[ SuspendedReadyState ][ SuspendEvent ] = DataReady_SuspendAction;
301 // ResumeEvent
302   _TransitionTable[ SuspendedReadyState ][ ToResumeEvent ] = SuspendedReadyToResumeState ;
303   _ActionTable[ SuspendedReadyToResumeState ][ ToResumeEvent ] = VoidAction ;
304
305   _TransitionTable[ SuspendedReadyToResumeState ][ ResumeEvent ] = ResumedReadyState ;
306   _ActionTable[ ResumedReadyState ][ ResumeEvent] = SuspendedReady_ResumeAction ;
307 // KillEvent
308   _TransitionTable[ SuspendedReadyState ][ KillEvent ] = KilledReadyState ;
309 // StopEvent
310   _TransitionTable[ SuspendedReadyState ][ StopEvent ] = StoppedReadyState ;
311 // KillEvent
312   _TransitionTable[ DataReadyState ][ KillEvent ] = KilledReadyState ;
313   _ActionTable[ KilledReadyState ][ KillEvent] = DataReady_KillAction ;
314 // StopEvent
315   _TransitionTable[ DataReadyState ][ StopEvent ] = StoppedReadyState ;
316   _ActionTable[ StoppedReadyState ][ StopEvent] = DataReady_StopAction ;
317
318 // RUN :
319 // ExecuteEvent
320   _TransitionTable[ SuspendedReadyToResumeState ][ ExecuteEvent ] = ExecutingState ;
321 // ExecuteEvent
322   _TransitionTable[ ResumedReadyState ][ ExecuteEvent ] = ExecutingState ;
323 // ExecuteEvent
324   _TransitionTable[ DataReadyState ][ ExecuteEvent ] = ExecutingState ;
325 // ExecuteEvent
326   _TransitionTable[ ReStartedState ][ ExecuteEvent ] = ExecutingState ;
327   _ActionTable[ ExecutingState ][ ExecuteEvent ] = DataReady_ExecuteAction;
328
329 // RUN-CONTROL :
330 // SuspendEvent
331   _TransitionTable[ ExecutingState ][ SuspendEvent ] = SuspendedExecutingState ;
332   _ActionTable[ SuspendedExecutingState ][ SuspendEvent ] = Executing_SuspendAction;
333 // ResumeEvent
334   _TransitionTable[ SuspendedExecutingState ][ ResumeEvent ] = ResumedExecutingState ;
335   _ActionTable[ ResumedExecutingState ][ ResumeEvent] = SuspendedExecuting_ResumeAction;
336 // ExecuteEvent
337   _TransitionTable[ ResumedExecutingState ][ ExecuteEvent ] = ExecutingState ;
338 // KillEvent
339   _TransitionTable[ SuspendedExecutingState ][ KillEvent ] = KilledExecutingState ;
340 // StopEvent
341   _TransitionTable[ SuspendedExecutingState ][ StopEvent ] = StoppedExecutingState ;
342 // KillEvent
343   _TransitionTable[ ExecutingState ][ KillEvent ] = KilledExecutingState ;
344   _ActionTable[ KilledExecutingState ][ KillEvent] = Executing_KillAction;
345   _TransitionTable[ KilledExecutingState ][ KillEvent ] = KilledState ;
346   _ActionTable[ KilledState ][ KillEvent] = VoidAction;
347 // StopEvent
348   _TransitionTable[ ExecutingState ][ StopEvent ] = StoppedExecutingState ;
349   _ActionTable[ StoppedExecutingState ][ StopEvent] = Executing_StopAction;
350   _TransitionTable[ StoppedExecutingState ][ StopEvent ] = StoppedState ;
351   _ActionTable[ StoppedState ][ StopEvent] = VoidAction;
352
353 // DONE :
354 // SuccessEvent
355   _TransitionTable[ ExecutingState ][ SuccessEvent ] = SuccessedExecutingState ;
356   _ActionTable[ SuccessedExecutingState ][ SuccessEvent ] = Executing_SuccessAction;
357 // SuspendedExecutingState : NO ResumeEvent 13-03-2003
358   _TransitionTable[ SuspendedExecutingState ][ SuccessEvent ] = SuccessedExecutingState ;
359   _ActionTable[ SuccessedExecutingState ][ SuccessEvent ] = Executing_SuccessAction;
360 // ErrorEvent
361   _TransitionTable[ ExecutingState ][ ErrorEvent ] = ErroredExecutingState ;
362   _ActionTable[ ErroredExecutingState ][ ErrorEvent ] = Executing_ErrorAction;
363 // SuspendedExecutingState : NO ResumeEvent 13-03-2003
364   _TransitionTable[ SuspendedExecutingState ][ ErrorEvent ] = ErroredExecutingState ;
365   _ActionTable[ ErroredExecutingState ][ ErrorEvent ] = Executing_ErrorAction;
366
367 // DONE-CONTROL :
368 // SuccessedExecutingState - SuccessEvent
369   _TransitionTable[ SuccessedExecutingState ][ SuccessEvent ] = SuccessedState ;
370   _ActionTable[ SuccessedState ][ SuccessEvent ] = Successed_SuccessAction ;
371 // ErroredExecutingState - ErrorEvent
372   _TransitionTable[ ErroredExecutingState ][ ErrorEvent ] = ErroredState ;
373   _ActionTable[ ErroredState ][ ErrorEvent ] = Errored_ErrorAction ;
374
375 // SuccessedState - SuspendEvent
376   _TransitionTable[ SuccessedExecutingState ][ SuspendEvent ] = SuspendedSuccessedState ;
377   _ActionTable[ SuspendedSuccessedState ][ SuspendEvent ] = Successed_SuspendAction;
378 // ErroredState - SuspendEvent
379   _TransitionTable[ ErroredExecutingState ][ SuspendEvent ] = SuspendedErroredState ;
380   _ActionTable[ SuspendedErroredState ][ SuspendEvent ] = Errored_SuspendAction;
381
382 // SuccessedState - KillEvent
383   _TransitionTable[ SuccessedExecutingState ][ KillEvent ] = KilledSuccessedState ;
384   _ActionTable[ KilledSuccessedState ][ KillEvent ] = Successed_KillAction;
385 // ErroredState - KillEvent
386   _TransitionTable[ ErroredExecutingState ][ KillEvent ] = KilledErroredState ;
387   _ActionTable[ KilledErroredState ][ KillEvent ] = Errored_KillAction;
388
389 // SuccessedState - StopEvent
390   _TransitionTable[ SuccessedExecutingState ][ StopEvent ] = StoppedSuccessedState ;
391   _ActionTable[ StoppedSuccessedState ][ StopEvent ] = Successed_StopAction;
392 // ErroredState - StopEvent
393   _TransitionTable[ ErroredExecutingState ][ StopEvent ] = StoppedErroredState ;
394   _ActionTable[ StoppedErroredState ][ StopEvent ] = Errored_StopAction;
395
396 // SuspendedSuccessedState - ResumeEvent
397   _TransitionTable[ SuspendedSuccessedState ][ ResumeEvent ] = ResumedSuccessedState ;
398   _ActionTable[ ResumedSuccessedState ][ ResumeEvent ] = SuspendedSuccessed_ResumeAction;
399
400   _TransitionTable[ ResumedSuccessedState ][ ResumedSuccessedEvent ] = SuccessedState ;
401   _ActionTable[ SuccessedState ][ ResumedSuccessedEvent] = Successed_SuccessAction;
402
403 // SuspendedErroredState - ResumeEvent
404   _TransitionTable[ SuspendedErroredState ][ ResumeEvent ] = ResumedErroredState ;
405   _ActionTable[ ResumedErroredState ][ ResumeEvent ] = SuspendedErrored_ResumeAction;
406   _TransitionTable[ ResumedErroredState ][ ResumedErroredEvent ] = ErroredState ;
407   _ActionTable[ ErroredState ][ ResumedErroredEvent ] = Errored_ErrorAction;
408
409 // SuccessedState - KillEvent
410   _TransitionTable[ SuccessedState ][ KillEvent ] = KilledSuccessedState ;
411   _ActionTable[ KilledSuccessedState ][ KillEvent] = Successed_KillAction;
412   _TransitionTable[ KilledSuccessedState ][ KillEvent ] = KilledState ;
413   _ActionTable[ KilledState ][ KillEvent ] = VoidAction;
414 // ErroredState - KillEvent
415   _TransitionTable[ ErroredState ][ KillEvent ] = KilledErroredState ;
416   _ActionTable[ KilledErroredState ][ KillEvent] = Errored_KillAction;
417   _TransitionTable[ KilledErroredState ][ KillEvent ] = KilledState ;
418   _ActionTable[ KilledState ][ KillEvent ] = VoidAction;
419
420 // SuccessedState - StopEvent
421   _TransitionTable[ SuccessedState ][ StopEvent ] = StoppedSuccessedState ;
422   _ActionTable[ StoppedSuccessedState ][ StopEvent] = Successed_StopAction;
423   _TransitionTable[ StoppedSuccessedState ][ StopEvent ] = StoppedState ;
424   _ActionTable[ StoppedState ][ StopEvent ] = VoidAction;
425 // ErroredState - StopEvent
426   _TransitionTable[ ErroredState ][ StopEvent ] = StoppedErroredState ;
427   _ActionTable[ StoppedErroredState ][ StopEvent] = Errored_StopAction;
428   _TransitionTable[ StoppedErroredState ][ StopEvent ] = StoppedState ;
429   _ActionTable[ StoppedState ][ StopEvent ] = VoidAction;
430
431 // ReStartEvent
432   _TransitionTable[ SuspendedSuccessedState ][ ToReStartEvent ] = SuspendedSuccessedToReStartState ;
433   _ActionTable[ SuspendedSuccessedToReStartState ][ ToReStartEvent] = VoidAction ;
434
435   _TransitionTable[ SuspendedSuccessedToReStartState ][ ReStartEvent ] = ReStartedState ;
436   _TransitionTable[ SuspendedSuccessedToReStartState ][ ReStartAndSuspendEvent ] = ReStartedState ;
437 // ReStartEvent
438   _TransitionTable[ SuspendedErroredState ][ ToReStartEvent ] = SuspendedErroredToReStartState ;
439   _ActionTable[ SuspendedErroredToReStartState ][ ToReStartEvent] = VoidAction ;
440
441   _TransitionTable[ SuspendedErroredToReStartState ][ ReStartEvent ] = ReStartedState ;
442   _TransitionTable[ SuspendedErroredToReStartState ][ ReStartAndSuspendEvent ] = ReStartedState ;
443
444   _ActionTable[ ReStartedState ][ ReStartEvent] = SuspendedSuccessed_ReStartAction;
445   _ActionTable[ ReStartedState ][ ReStartAndSuspendEvent] = SuspendedSuccessed_ReStartAndSuspendAction;
446
447   pthread_mutex_init( &_MutexJoinWait , NULL ) ;
448   if ( pthread_cond_init( &_JoinWait , NULL ) ) {
449     perror("pthread_cond_init( &_JoinWait , NULL )") ;
450     exit( 0 ) ;
451   }
452   _JoinThread = true ;
453   if ( pthread_create( &_JoinThreadNo , NULL , start_function , this ) ) {
454     char * msg = "Cannot pthread_create " ;
455     perror( msg ) ;
456   }
457
458   pthread_mutex_init( &_MutexPythonWait , NULL ) ;
459   _MutexPythonLocked = false ;
460   _ExecNumber = 0 ;
461   _GraphExecutingNumber = 0 ;
462   _PyInitModule = false ;
463   _DbgFileNumber = 0 ;
464 //  cdebug_out << "FiniteStateMachine::FiniteStateMachine _TransitionTable "
465 //             << endl;
466 }
467
468 void * start_function( void *p ) {
469   GraphExecutor::FiniteStateMachine *anAutomaton = (GraphExecutor::FiniteStateMachine *) p;
470   if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) ) {
471     perror("pthread_setcanceltype ") ;
472     exit(0) ;
473   }
474   if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) ) {
475     perror("pthread_setcancelstate ") ;
476     exit(0) ;
477   }
478   anAutomaton->JoinThread() ;
479   pthread_exit( PTHREAD_CANCELED ) ;
480 }
481
482 int GraphExecutor::FiniteStateMachine::ExecNumber() {
483   PyLock() ;
484   _ExecNumber += 1 ;
485   int RetVal = _ExecNumber ;
486   _GraphExecutingNumber += 1 ;
487   PyUnLock() ;
488   return RetVal ;
489 }
490
491 void GraphExecutor::FiniteStateMachine::Executed() {
492   PyLock() ;
493   _GraphExecutingNumber -= 1 ;
494   if ( _GraphExecutingNumber == 0 ) {
495     map< string , PyObject * >::iterator aPyFunction ;
496     for ( aPyFunction = _MapOfPyFunctions.begin() ; aPyFunction != _MapOfPyFunctions.end() ; aPyFunction++ ) {
497       if ( !strcmp( aPyFunction->first.c_str() , "PyObjRef" ) ||
498            !strcmp( aPyFunction->first.c_str() , "PyObjIor" ) ) {
499         //cout << "GraphExecutor::FiniteStateMachine::Executed " << aPyFunction->first << " keeped ..."<< endl ;
500       }
501       else {
502         //cout << "GraphExecutor::FiniteStateMachine::Executed " << aPyFunction->first << " erased ..."<< endl ;
503         _MapOfPyFunctions.erase( aPyFunction->first ) ;
504       }
505     }
506   }
507   else {
508     //cout << "GraphExecutor::FiniteStateMachine::Executed _GraphExecutingNumber " << _GraphExecutingNumber
509     //     << " != 0 ==> no erase" << endl ;
510   }
511   PyUnLock() ;
512   return ;
513 }
514
515 void GraphExecutor::FiniteStateMachine::PyInitModule( bool aPyInitModule ) {
516   _PyInitModule = aPyInitModule ;
517 }
518
519 bool GraphExecutor::FiniteStateMachine::PyInitModule() {
520   bool InitedModule = _PyInitModule ;
521   _PyInitModule = true ;
522   return InitedModule ;
523 }
524
525 void GraphExecutor::FiniteStateMachine::PyLock() {
526 //  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyLock " << &_MutexPythonWait << endl ;
527   if ( pthread_mutex_lock( &_MutexPythonWait ) ) {
528     perror( "GraphExecutor::FiniteStateMachine::PyLock" ) ;
529     exit( 0 ) ;
530   }
531   _MutexPythonLocked = true ;
532 //  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyLocked " << &_MutexPythonWait << endl ;
533 }
534
535 void GraphExecutor::FiniteStateMachine::PyUnLock() {
536 //  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyUnLock " << &_MutexPythonWait << endl ;
537   if ( pthread_mutex_unlock( &_MutexPythonWait ) ) {
538     perror( "GraphExecutor::FiniteStateMachine::PyUnLock" ) ;
539     exit( 0 ) ;
540   }
541   _MutexPythonLocked = false ;
542 //  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyUnLocked " << &_MutexPythonWait << endl ;
543 }
544
545 void GraphExecutor::FiniteStateMachine::PyLockReset() {
546   if ( _MutexPythonLocked ) {
547     PyUnLock() ;
548   }
549 }
550
551 PyObject * GraphExecutor::FiniteStateMachine::PyFunction( const char * aPyFuncName ) {
552   
553   PyObject * RetVal = NULL ;
554   bool PyObjRefIor = !strcmp( aPyFuncName , "PyObjRef" ) || !strcmp( aPyFuncName , "PyObjIor" ) ;
555   PyObject * PyFunctionMapped = _MapOfPyFunctions[ aPyFuncName ] ;
556   if ( _GraphExecutingNumber > 1 && !PyObjRefIor ) {
557     RetVal = PyFunctionMapped ;
558     //cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' ) --> " ;
559     if ( RetVal ) {
560       //cout << RetVal << " ob_refcnt " << RetVal->ob_refcnt ;
561     }
562     else {
563       //cout << " NULL" ;
564     }
565     //cout << endl ;
566   }
567   else {
568     RetVal = PyFunctionMapped ;
569     //cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' ) --> " ;
570     if ( RetVal && PyObjRefIor ) {
571       //cout << RetVal << " " << RetVal->ob_refcnt << endl ;
572     }
573     else if ( RetVal ) {
574       //cout << RetVal << " " << RetVal->ob_refcnt << endl ;
575 //      _MapOfPyFunctions.erase( aPyFuncName ) ;
576 //      cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' ) erased --> NULL" << endl ;
577     }
578     else {
579       //cout << " NULL" << endl ;
580     }
581   }
582   
583   return RetVal ;
584 }
585
586 bool GraphExecutor::FiniteStateMachine::PyFunction( const char * aPyFuncName , PyObject * aPyFunction ) {
587   
588   bool RetVal = false ;
589   if ( _MapOfPyFunctions[ aPyFuncName ] != NULL ) {
590     //PyObject * aPyFunc = _MapOfPyFunctions[ aPyFuncName ] ;
591     //cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' , " << aPyFunction
592     //     << " ) ob_refcnt " << aPyFunction->ob_refcnt << " already mapped : " << aPyFunc << " ob_refcnt "
593     //     << aPyFunc->ob_refcnt << endl ;
594   }
595   else {
596     _MapOfPyFunctions[ aPyFuncName ] = aPyFunction ;
597     //cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' , " << aPyFunction
598     //     << " ) ob_refcnt " << aPyFunction->ob_refcnt << " mapped" << endl ;
599     RetVal = true ;
600   }
601   
602   return RetVal ;
603 }
604 /*
605 GraphBase::Graph * GraphExecutor::FiniteStateMachine::MapGraph( const char * aGraphName ) {
606   GraphBase::Graph * aGraph = _MapOfGraphs[ aGraphName ] ;
607   //cout << "MapGraph " << aGraphName << " --> " << aGraph << endl ;
608   return aGraph ;
609 }
610
611 bool GraphExecutor::FiniteStateMachine::MapGraph( GraphBase::Graph * aGraph , const char * aGraphName ) {
612   if ( MapGraph( aGraphName ) ) {
613     return false ;
614   }
615   _MapOfGraphs[ aGraphName ] = aGraph ;
616   return true ;
617 }
618
619 void GraphExecutor::FiniteStateMachine::EraseGraph( const char * aGraphName ) {
620   _MapOfGraphs.erase( aGraphName ) ;
621 }
622
623 bool GraphExecutor::FiniteStateMachine::GraphName( const char * aGraphName ) {
624   return  _MapOfGraphNames[ aGraphName ] ;
625 }
626
627 string GraphExecutor::FiniteStateMachine::GraphInstanceName( const char * aGraphName ) {
628   int GraphInstanceNumber = _MapOfGraphNames[ aGraphName ] ;
629   if ( GraphInstanceNumber ) {
630     _MapOfGraphNames[ aGraphName ] = GraphInstanceNumber + 1 ;
631   }
632   else {
633     GraphInstanceNumber = 0 ;
634     _MapOfGraphNames[ aGraphName ] = GraphInstanceNumber + 1 ;
635   }
636   string theGraphInstanceName = string( aGraphName ) ;
637   if ( GraphInstanceNumber ) {
638     theGraphInstanceName += "_" ;
639     ostringstream astr ;
640 //    astr << GraphInstanceNumber << ends ;
641     astr << GraphInstanceNumber ;
642     theGraphInstanceName += astr.str() ;
643   }
644   //cout << "GraphExecutor::FiniteStateMachine::GraphInstanceName( " << aGraphName << " ) --> "
645     //   << theGraphInstanceName << endl ;
646   return theGraphInstanceName ;
647 }
648 */
649
650 void GraphExecutor::FiniteStateMachine::JoinThread() {
651   if ( pthread_mutex_lock( &_MutexJoinWait ) ) {
652     perror("FiniteStateMachine:JoinThread pthread_mutex_lock ") ;
653     exit( 0 ) ;
654   }
655   while ( _JoinThread ) {
656     while ( _ThreadList.size() == 0) {
657 //      MESSAGE( pthread_self() << " FiniteStateMachine:Join pthread_cond_wait" );
658       if ( pthread_cond_wait( &_JoinWait , &_MutexJoinWait ) ) {
659         perror("FiniteStateMachine:JoinThread pthread_cond_wait ") ;
660       }
661     }
662     pthread_t _Thread = _ThreadList.front() ;
663 //    MESSAGE( pthread_self() << " FiniteStateMachine:JoinThread pthread_cond_waited : " << _Thread )
664 //    MESSAGE( pthread_self() << " FiniteStateMachine:JoinThread pthread_join : " << _Thread );
665     int joinsts = pthread_join( _Thread , NULL ) ;
666     if ( joinsts ) {
667       perror("FiniteStateMachine:JoinThread pthread_join ") ;
668       MESSAGE( pthread_self() << " FiniteStateMachine:JoinThread pthread_join : " << _Thread
669                << " Error" );
670       exit( 0 ) ;
671     }
672     else {
673       MESSAGE( pthread_self() << " FiniteStateMachine:JoinThread pthread_joined : " << _Thread );
674       _ThreadList.pop_front() ;
675     }
676   }
677   if ( pthread_mutex_unlock( &_MutexJoinWait ) ) {
678     perror("FiniteStateMachine:JoinThread pthread_mutex_unlock ") ;
679     exit( 0 ) ;
680   }
681 }
682
683 void GraphExecutor::FiniteStateMachine::JoinThread( pthread_t aThread ) {
684   if ( pthread_mutex_lock( &_MutexJoinWait ) ) {
685     perror("Join pthread_mutex_lock ") ;
686     exit( 0 ) ;
687   }
688 //  MESSAGE(pthread_self() << " JoinThread pthread_cond_signal for " << aThread)
689   _ThreadList.push_back( aThread ) ;
690   if ( pthread_cond_signal( &_JoinWait ) ) {
691     perror("Join pthread_cond_signal ") ;
692   }
693 //  MESSAGE( pthread_self() << " JoinThread pthread_cond_signaled for " << aThread)
694
695   if ( pthread_mutex_unlock( &_MutexJoinWait ) ) {
696     perror("Join pthread_mutex_unlock ") ;
697     exit( 0 ) ;
698   }
699 }
700
701 ostream & operator<< (ostream &f ,const GraphExecutor::AutomatonState & aState ) {
702   switch (aState) {
703   case GraphExecutor::UnKnownState :
704     f << "UnKnownState";
705     break;
706   case GraphExecutor::DataUndefState :
707     f << "DataUndefState";
708     break;
709   case GraphExecutor::DataWaitingState :
710     f << "DataWaitingState";
711     break;
712   case GraphExecutor::DataReadyState :
713     f << "DataReadyState";
714     break;
715   case GraphExecutor::SuspendedReadyState :
716     f << "SuspendedReadyState";
717     break;
718   case GraphExecutor::SuspendedReadyToResumeState :
719     f << "SuspendedReadyToResumeState";
720     break;
721   case GraphExecutor::ResumedReadyState :
722     f << "ResumedReadyState";
723     break;
724   case GraphExecutor::KilledReadyState :
725     f << "KilledReadyState";
726     break;
727   case GraphExecutor::StoppedReadyState :
728     f << "StoppedReadyState";
729     break;
730   case GraphExecutor::ExecutingState :
731     f << "ExecutingState";
732     break;
733   case GraphExecutor::SuspendedExecutingState :
734     f << "SuspendedExecutingState";
735     break;
736   case GraphExecutor::ResumedExecutingState :
737     f << "ResumedExecutingState";
738     break;
739   case GraphExecutor::KilledExecutingState :
740     f << "KilledExecutingState";
741     break;
742   case GraphExecutor::StoppedExecutingState :
743     f << "StoppedExecutingState";
744     break;
745   case GraphExecutor::SuccessedExecutingState :
746     f << "SuccessedExecutingState";
747     break;
748   case GraphExecutor::ErroredExecutingState :
749     f << "ErroredExecutingState";
750     break;
751   case GraphExecutor::SuspendedSuccessedState :
752     f << "SuspendedSuccessedState";
753     break;
754   case GraphExecutor::SuspendedErroredState :
755     f << "SuspendedErroredState";
756     break;
757   case GraphExecutor::ResumedSuccessedState :
758     f << "ResumedSuccessedState";
759     break;
760   case GraphExecutor::ResumedErroredState :
761     f << "ResumedErroredState";
762     break;
763   case GraphExecutor::KilledSuccessedState :
764     f << "KilledSuccessedState";
765     break;
766   case GraphExecutor::KilledErroredState :
767     f << "KilledErroredState";
768     break;
769   case GraphExecutor::StoppedSuccessedState :
770     f << "StoppedSuccessedState";
771     break;
772   case GraphExecutor::StoppedErroredState :
773     f << "StoppedErroredState";
774     break;
775   case GraphExecutor::SuccessedState :
776     f << "SuccessedState";
777     break;
778   case GraphExecutor::ErroredState :
779     f << "ErroredState";
780     break;
781   case GraphExecutor::SuspendedState :
782     f << "SuspendedState";
783     break;
784   case GraphExecutor::KilledState :
785     f << "KilledState";
786     break;
787   case GraphExecutor::StoppedState :
788     f << "StoppedState";
789     break;
790   case GraphExecutor::SuspendedSuccessedToReStartState :
791     f << "SuspendedSuccessedToReStartState";
792     break;
793   case GraphExecutor::SuspendedErroredToReStartState :
794     f << "SuspendedErroredToReStartState";
795     break;
796   case GraphExecutor::ReRunnedState :
797     f << "ReRunnedState";
798     break;
799   case GraphExecutor::ReStartedState :
800     f << "ReStartedState";
801     break;
802   default :
803     f << "GraphExecutor::AutomatonState_?";
804     break;
805   }
806
807   return f;
808 }
809