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