Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 ReStartedState            GraphExecutor::ReStartedState
82 #define LoadingState              GraphExecutor::LoadingState
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   MESSAGE( "FiniteStateMachine::FiniteStateMachine" ) ;
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[ ReStartedState ]            = "ReStartedState" ;
170   _StateName[ LoadingState ]              = "LoadingState" ;
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[ Errored_ExecutingAction ] = "Errored_ExecutingAction" ;
234   _ActionName[ Successed_SuccessAction ] = "Successed_SuccessAction" ;
235   _ActionName[ Errored_ErrorAction ] = "Errored_ErrorAction" ;
236
237   _ActionName[ Successed_SuspendAction ] = "Successed_SuspendAction" ;
238   _ActionName[ Errored_SuspendAction ] = "Errored_SuspendAction" ;
239   _ActionName[ SuspendedSuccessed_ResumeAction ] = "SuspendedSuccessed_ResumeAction" ;
240   _ActionName[ SuspendedErrored_ResumeAction ] = "SuspendedErrored_ResumeAction" ;
241   _ActionName[ Successed_KillAction ] = "Successed_KillAction" ;
242   _ActionName[ Errored_KillAction ] = "Errored_KillAction" ;
243   _ActionName[ Successed_StopAction ] = "Successed_StopAction" ;
244   _ActionName[ Errored_StopAction ] = "Errored_StopAction" ;
245
246   _ActionName[ SuspendedSuccessed_ReStartAction ] = "SuspendedSuccessed_ReStartAction" ;
247   _ActionName[ SuspendedErrored_ReStartAction ] = "SuspendedErrored_ReStartAction" ;
248   _ActionName[ SuspendedSuccessed_ReStartAndSuspendAction ] = "SuspendedSuccessed_ReStartAndSuspendAction" ;
249   _ActionName[ SuspendedErrored_ReStartAndSuspendAction ] = "SuspendedErrored_ReStartAndSuspendAction" ;
250
251   _GraphStateName[ SUPERV::UndefinedState ] = "UndefinedState" ;
252   _GraphStateName[ SUPERV::NoState ] = "NoState" ;
253   _GraphStateName[ SUPERV::EditingState ] = "EditingState" ;
254   _GraphStateName[ SUPERV::SuspendState ] = "SuspendState" ;
255   _GraphStateName[ SUPERV::WaitingState ] = "WaitingState" ;
256   _GraphStateName[ SUPERV::ReadyState ] = "ReadyState" ;
257   _GraphStateName[ SUPERV::SuspendReadyState ] = "SuspendReadyState" ;
258   _GraphStateName[ SUPERV::RunningState ] = "RunningState" ;
259   _GraphStateName[ SUPERV::DoneState ] = "DoneState" ;
260   _GraphStateName[ SUPERV::ErrorState ] = "ErrorState" ;
261   _GraphStateName[ SUPERV::SuspendDoneState ] = "SuspendDoneState" ;
262   _GraphStateName[ SUPERV::SuspendErroredState ] = "SuspendErroredState" ;
263   _GraphStateName[ SUPERV::KillState ] = "KillState" ;
264   _GraphStateName[ SUPERV::StopState ] = "StopState" ;
265
266   int i , j ;
267 // NewStates = _TransitionTable[ States ] [ Events ]
268 // associated action = _ActionTable[ NewStates ] [ Events ]
269   for ( i = 0 ; i < NumberOfAutomatonStates ; i++ ) {
270     for ( j = 0 ; j < NumberOfEvents ; j++ ) {
271 // id est = SameState and VoidAction
272       _TransitionTable[ i ][ j ] = (GraphExecutor::AutomatonState ) i ;
273       _ActionTable[ i ][ j ] = GraphExecutor::ErrorAction ;
274     }
275   }
276 // OneEvent ===> Change of State and associated Action :
277 //               _TransitionTable[ OldState ][ OneEvent ] gives a NewState
278 //               _Action[ NewState ][ OneEvent ] gives what to do
279
280 // INPUT-DATAS :
281 // NoDataReadyEvent :
282   _TransitionTable[ DataUndefState ][ NoDataReadyEvent ] = DataWaitingState ;
283   _ActionTable[ DataWaitingState ][ NoDataReadyEvent ] = VoidAction ;
284 // SomeDataReadyEvent :
285   _TransitionTable[ DataWaitingState ][ SomeDataReadyEvent ] = DataUndefState ;
286   _ActionTable[ DataUndefState ][ SomeDataReadyEvent ] = DataWaiting_SomeDataReadyAction;
287   _TransitionTable[ DataReadyState ][ SomeDataReadyEvent ] = DataUndefState ;
288   _ActionTable[ DataUndefState ][ SomeDataReadyEvent ] = DataWaiting_SomeDataReadyAction;
289 // NotAllDataReadyEvent :
290   _TransitionTable[ DataUndefState ][ NotAllDataReadyEvent ] = DataWaitingState ;
291   _ActionTable[ DataWaitingState ][ NotAllDataReadyEvent ] = DataUndef_NotAllDataReadyAction;
292 // AllDataReadyEvent
293   _TransitionTable[ DataUndefState ][ AllDataReadyEvent ] = DataReadyState ;
294   _ActionTable[ DataReadyState ][ AllDataReadyEvent ] = DataUndef_AllDataReadyAction ;
295
296 // DATAS-READY-CONTROL :
297 // SuspendEvent
298   _TransitionTable[ DataReadyState ][ SuspendEvent ] = SuspendedReadyState ;
299   _ActionTable[ SuspendedReadyState ][ SuspendEvent ] = DataReady_SuspendAction;
300 // ResumeEvent
301   _TransitionTable[ SuspendedReadyState ][ ToResumeEvent ] = SuspendedReadyToResumeState ;
302   _ActionTable[ SuspendedReadyToResumeState ][ ToResumeEvent ] = VoidAction ;
303
304   _TransitionTable[ SuspendedReadyToResumeState ][ ResumeEvent ] = ResumedReadyState ;
305   _ActionTable[ ResumedReadyState ][ ResumeEvent] = SuspendedReady_ResumeAction ;
306 // KillEvent
307   _TransitionTable[ SuspendedReadyState ][ KillEvent ] = KilledReadyState ;
308 // StopEvent
309   _TransitionTable[ SuspendedReadyState ][ StopEvent ] = StoppedReadyState ;
310 // KillEvent
311   _TransitionTable[ DataReadyState ][ KillEvent ] = KilledReadyState ;
312   _ActionTable[ KilledReadyState ][ KillEvent] = DataReady_KillAction ;
313 // StopEvent
314   _TransitionTable[ DataReadyState ][ StopEvent ] = StoppedReadyState ;
315   _ActionTable[ StoppedReadyState ][ StopEvent] = DataReady_StopAction ;
316
317 // RUN :
318 // ExecuteEvent
319   _TransitionTable[ SuspendedReadyToResumeState ][ ExecuteEvent ] = ExecutingState ;
320 // ExecuteEvent
321   _TransitionTable[ ResumedReadyState ][ ExecuteEvent ] = ExecutingState ;
322 // ExecuteEvent
323   _TransitionTable[ DataReadyState ][ ExecuteEvent ] = ExecutingState ;
324 // ExecuteEvent
325   _TransitionTable[ ReStartedState ][ ExecuteEvent ] = ExecutingState ;
326   _ActionTable[ ExecutingState ][ ExecuteEvent ] = DataReady_ExecuteAction;
327
328 // RUN-CONTROL :
329 // SuspendEvent
330   _TransitionTable[ ExecutingState ][ SuspendEvent ] = SuspendedExecutingState ;
331   _ActionTable[ SuspendedExecutingState ][ SuspendEvent ] = Executing_SuspendAction;
332 // ResumeEvent
333   _TransitionTable[ SuspendedExecutingState ][ ResumeEvent ] = ResumedExecutingState ;
334   _ActionTable[ ResumedExecutingState ][ ResumeEvent] = SuspendedExecuting_ResumeAction;
335 // ExecuteEvent
336   _TransitionTable[ ResumedExecutingState ][ ExecuteEvent ] = ExecutingState ;
337 // KillEvent
338   _TransitionTable[ SuspendedExecutingState ][ KillEvent ] = KilledExecutingState ;
339 // StopEvent
340   _TransitionTable[ SuspendedExecutingState ][ StopEvent ] = StoppedExecutingState ;
341 // KillEvent
342   _TransitionTable[ ExecutingState ][ KillEvent ] = KilledExecutingState ;
343   _ActionTable[ KilledExecutingState ][ KillEvent] = Executing_KillAction;
344   _TransitionTable[ KilledExecutingState ][ KillEvent ] = KilledState ;
345   _ActionTable[ KilledState ][ KillEvent] = VoidAction;
346 // StopEvent
347   _TransitionTable[ ExecutingState ][ StopEvent ] = StoppedExecutingState ;
348   _ActionTable[ StoppedExecutingState ][ StopEvent] = Executing_StopAction;
349   _TransitionTable[ StoppedExecutingState ][ StopEvent ] = StoppedState ;
350   _ActionTable[ StoppedState ][ StopEvent] = VoidAction;
351
352 // DONE :
353 // SuccessEvent
354   _TransitionTable[ ExecutingState ][ SuccessEvent ] = SuccessedExecutingState ;
355   _ActionTable[ SuccessedExecutingState ][ SuccessEvent ] = Executing_SuccessAction;
356 // SuspendedExecutingState : NO ResumeEvent 13-03-2003
357   _TransitionTable[ SuspendedExecutingState ][ SuccessEvent ] = SuccessedExecutingState ;
358   _ActionTable[ SuccessedExecutingState ][ SuccessEvent ] = Executing_SuccessAction;
359 //JR 24.03.2005 : Debug for PAL8176 : abort of GOTONode
360 // ErrorEvent
361   _TransitionTable[ SuccessedState ][ ErrorEvent ] = ErroredExecutingState ;
362 //  _ActionTable[ ErroredExecutingState ][ ErrorEvent ] = Executing_ErrorAction;
363   _ActionTable[ ErroredExecutingState ][ ErrorEvent ] = Errored_ExecutingAction;
364 // ErrorEvent
365   _TransitionTable[ ExecutingState ][ ErrorEvent ] = ErroredExecutingState ;
366 //  _ActionTable[ ErroredExecutingState ][ ErrorEvent ] = Executing_ErrorAction;
367   _ActionTable[ ErroredExecutingState ][ ErrorEvent ] = Errored_ExecutingAction;
368 // SuspendedExecutingState : NO ResumeEvent 13-03-2003
369   _TransitionTable[ SuspendedExecutingState ][ ErrorEvent ] = ErroredExecutingState ;
370 //  _ActionTable[ ErroredExecutingState ][ ErrorEvent ] = Executing_ErrorAction;
371   _ActionTable[ ErroredExecutingState ][ ErrorEvent ] = Errored_ExecutingAction;
372
373 // DONE-CONTROL :
374 // SuccessedExecutingState - SuccessEvent
375   _TransitionTable[ SuccessedExecutingState ][ SuccessEvent ] = SuccessedState ;
376   _ActionTable[ SuccessedState ][ SuccessEvent ] = Successed_SuccessAction ;
377 // ErroredExecutingState - ErrorEvent
378   _TransitionTable[ ErroredExecutingState ][ ErrorEvent ] = ErroredState ;
379   _ActionTable[ ErroredState ][ ErrorEvent ] = Errored_ErrorAction ;
380
381 // SuccessedState - SuspendEvent
382   _TransitionTable[ SuccessedExecutingState ][ SuspendEvent ] = SuspendedSuccessedState ;
383   _ActionTable[ SuspendedSuccessedState ][ SuspendEvent ] = Successed_SuspendAction;
384 // ErroredState - SuspendEvent
385   _TransitionTable[ ErroredExecutingState ][ SuspendEvent ] = SuspendedErroredState ;
386   _ActionTable[ SuspendedErroredState ][ SuspendEvent ] = Errored_SuspendAction;
387
388 // SuccessedState - KillEvent
389   _TransitionTable[ SuccessedExecutingState ][ KillEvent ] = KilledSuccessedState ;
390   _ActionTable[ KilledSuccessedState ][ KillEvent ] = Successed_KillAction;
391 // ErroredState - KillEvent
392   _TransitionTable[ ErroredExecutingState ][ KillEvent ] = KilledErroredState ;
393   _ActionTable[ KilledErroredState ][ KillEvent ] = Errored_KillAction;
394
395 // SuccessedState - StopEvent
396   _TransitionTable[ SuccessedExecutingState ][ StopEvent ] = StoppedSuccessedState ;
397   _ActionTable[ StoppedSuccessedState ][ StopEvent ] = Successed_StopAction;
398 // ErroredState - StopEvent
399   _TransitionTable[ ErroredExecutingState ][ StopEvent ] = StoppedErroredState ;
400   _ActionTable[ StoppedErroredState ][ StopEvent ] = Errored_StopAction;
401
402 // SuspendedSuccessedState - ResumeEvent
403   _TransitionTable[ SuspendedSuccessedState ][ ResumeEvent ] = ResumedSuccessedState ;
404   _ActionTable[ ResumedSuccessedState ][ ResumeEvent ] = SuspendedSuccessed_ResumeAction;
405
406   _TransitionTable[ ResumedSuccessedState ][ ResumedSuccessedEvent ] = SuccessedState ;
407   _ActionTable[ SuccessedState ][ ResumedSuccessedEvent] = Successed_SuccessAction;
408
409 // SuspendedErroredState - ResumeEvent
410   _TransitionTable[ SuspendedErroredState ][ ResumeEvent ] = ResumedErroredState ;
411   _ActionTable[ ResumedErroredState ][ ResumeEvent ] = SuspendedErrored_ResumeAction;
412   _TransitionTable[ ResumedErroredState ][ ResumedErroredEvent ] = ErroredState ;
413   _ActionTable[ ErroredState ][ ResumedErroredEvent ] = Errored_ErrorAction;
414
415 // SuccessedState - KillEvent
416   _TransitionTable[ SuccessedState ][ KillEvent ] = KilledSuccessedState ;
417   _ActionTable[ KilledSuccessedState ][ KillEvent] = Successed_KillAction;
418   _TransitionTable[ KilledSuccessedState ][ KillEvent ] = KilledState ;
419   _ActionTable[ KilledState ][ KillEvent ] = VoidAction;
420 // ErroredState - KillEvent
421   _TransitionTable[ ErroredState ][ KillEvent ] = KilledErroredState ;
422   _ActionTable[ KilledErroredState ][ KillEvent] = Errored_KillAction;
423   _TransitionTable[ KilledErroredState ][ KillEvent ] = KilledState ;
424   _ActionTable[ KilledState ][ KillEvent ] = VoidAction;
425
426 // SuccessedState - StopEvent
427   _TransitionTable[ SuccessedState ][ StopEvent ] = StoppedSuccessedState ;
428   _ActionTable[ StoppedSuccessedState ][ StopEvent] = Successed_StopAction;
429   _TransitionTable[ StoppedSuccessedState ][ StopEvent ] = StoppedState ;
430   _ActionTable[ StoppedState ][ StopEvent ] = VoidAction;
431 // ErroredState - StopEvent
432   _TransitionTable[ ErroredState ][ StopEvent ] = StoppedErroredState ;
433   _ActionTable[ StoppedErroredState ][ StopEvent] = Errored_StopAction;
434   _TransitionTable[ StoppedErroredState ][ StopEvent ] = StoppedState ;
435   _ActionTable[ StoppedState ][ StopEvent ] = VoidAction;
436
437 // ReStartEvent
438   _TransitionTable[ SuspendedSuccessedState ][ ToReStartEvent ] = SuspendedSuccessedToReStartState ;
439   _ActionTable[ SuspendedSuccessedToReStartState ][ ToReStartEvent] = VoidAction ;
440
441   _TransitionTable[ SuspendedSuccessedToReStartState ][ ReStartEvent ] = ReStartedState ;
442   _TransitionTable[ SuspendedSuccessedToReStartState ][ ReStartAndSuspendEvent ] = ReStartedState ;
443 // ReStartEvent
444   _TransitionTable[ SuspendedErroredState ][ ToReStartEvent ] = SuspendedErroredToReStartState ;
445   _ActionTable[ SuspendedErroredToReStartState ][ ToReStartEvent] = VoidAction ;
446
447   _TransitionTable[ SuspendedErroredToReStartState ][ ReStartEvent ] = ReStartedState ;
448   _TransitionTable[ SuspendedErroredToReStartState ][ ReStartAndSuspendEvent ] = ReStartedState ;
449
450   _ActionTable[ ReStartedState ][ ReStartEvent] = SuspendedSuccessed_ReStartAction;
451   _ActionTable[ ReStartedState ][ ReStartAndSuspendEvent] = SuspendedSuccessed_ReStartAndSuspendAction;
452
453   pthread_mutex_init( &_MutexJoinWait , NULL ) ;
454   if ( pthread_cond_init( &_JoinWait , NULL ) ) {
455     perror("pthread_cond_init( &_JoinWait , NULL )") ;
456     exit( 0 ) ;
457   }
458   _JoinThread = true ;
459   if ( pthread_create( &_JoinThreadNo , NULL , start_function , this ) ) {
460     char * msg = "Cannot pthread_create " ;
461     perror( msg ) ;
462   }
463
464 #if omniORB_4_0_5
465   _SuperVisionComponent = SUPERV::SuperG::_nil() ;
466 #endif
467
468   pthread_mutex_init( &_MutexPythonWait , NULL ) ;
469   _MutexPythonLocked = false ;
470   _ExecNumber = 0 ;
471   _GraphExecutingNumber = 0 ;
472   _PyInitModule = false ;
473   _DbgFileNumber = 0 ;
474   MESSAGE( "FiniteStateMachine::FiniteStateMachine _TransitionTable " ) ;
475 }
476
477 void * start_function( void *p ) {
478   GraphExecutor::FiniteStateMachine *anAutomaton = (GraphExecutor::FiniteStateMachine *) p;
479   if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) ) {
480     perror("pthread_setcanceltype ") ;
481     exit(0) ;
482   }
483   if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) ) {
484     perror("pthread_setcancelstate ") ;
485     exit(0) ;
486   }
487   anAutomaton->JoinThread() ;
488   pthread_exit( PTHREAD_CANCELED ) ;
489 }
490
491 int GraphExecutor::FiniteStateMachine::ExecNumber() {
492   PyLock() ;
493   _ExecNumber += 1 ;
494   int RetVal = _ExecNumber ;
495   _GraphExecutingNumber += 1 ;
496   PyUnLock() ;
497   return RetVal ;
498 }
499
500 void GraphExecutor::FiniteStateMachine::Executed() {
501   PyLock() ;
502   _GraphExecutingNumber -= 1 ;
503   if ( _GraphExecutingNumber == 0 ) {
504     map< string , PyObject * >::iterator aPyFunction ;
505     for ( aPyFunction = _MapOfPyFunctions.begin() ; aPyFunction != _MapOfPyFunctions.end() ; aPyFunction++ ) {
506       if ( !strcmp( aPyFunction->first.c_str() , "PyObjRef" ) ||
507            !strcmp( aPyFunction->first.c_str() , "PyObjIor" ) ) {
508         MESSAGE( "GraphExecutor::FiniteStateMachine::Executed " << aPyFunction->first << " keeped ..."
509                  ) ;
510       }
511       else {
512         MESSAGE( "GraphExecutor::FiniteStateMachine::Executed " << aPyFunction->first << " erased ..."
513                ) ;
514         _MapOfPyFunctions.erase( aPyFunction->first ) ;
515       }
516     }
517   }
518   else {
519     MESSAGE( "GraphExecutor::FiniteStateMachine::Executed _GraphExecutingNumber "
520               << _GraphExecutingNumber << " != 0 ==> no erase" ) ;
521   }
522   PyUnLock() ;
523   return ;
524 }
525
526 void GraphExecutor::FiniteStateMachine::PyInitModule( bool aPyInitModule ) {
527   _PyInitModule = aPyInitModule ;
528 }
529
530 bool GraphExecutor::FiniteStateMachine::PyInitModule() {
531   bool InitedModule = _PyInitModule ;
532   _PyInitModule = true ;
533   return InitedModule ;
534 }
535
536 void GraphExecutor::FiniteStateMachine::PyLock() {
537 //  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyLock " << &_MutexPythonWait << endl ;
538   if ( pthread_mutex_lock( &_MutexPythonWait ) ) {
539     perror( "GraphExecutor::FiniteStateMachine::PyLock" ) ;
540     exit( 0 ) ;
541   }
542   _MutexPythonLocked = true ;
543 //  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyLocked " << &_MutexPythonWait << endl ;
544 }
545
546 void GraphExecutor::FiniteStateMachine::PyUnLock() {
547 //  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyUnLock " << &_MutexPythonWait << endl ;
548   if ( pthread_mutex_unlock( &_MutexPythonWait ) ) {
549     perror( "GraphExecutor::FiniteStateMachine::PyUnLock" ) ;
550     exit( 0 ) ;
551   }
552   _MutexPythonLocked = false ;
553 //  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyUnLocked " << &_MutexPythonWait << endl ;
554 }
555
556 void GraphExecutor::FiniteStateMachine::PyLockReset() {
557   if ( _MutexPythonLocked ) {
558     PyUnLock() ;
559   }
560 }
561
562 #define PyFunctionTrace 1
563 PyObject * GraphExecutor::FiniteStateMachine::PyFunction( const char * aPyFuncName ) {
564   
565   PyObject * RetVal = NULL ;
566   bool PyObjRefIor = !strcmp( aPyFuncName , "PyObjRef" ) || !strcmp( aPyFuncName , "PyObjIor" ) ;
567   PyObject * PyFunctionMapped = _MapOfPyFunctions[ aPyFuncName ] ;
568   if ( _GraphExecutingNumber > 1 && !PyObjRefIor ) {
569     RetVal = PyFunctionMapped ;
570 #if PyFunctionTrace
571     MESSAGE( "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' ) --> " ) ;
572     if ( RetVal ) {
573       MESSAGE( RetVal << " ob_refcnt " << RetVal->ob_refcnt ) ;
574     }
575     else {
576       MESSAGE( " NULL" ) ;
577     }
578 #endif
579   }
580   else {
581     RetVal = PyFunctionMapped ;
582 #if PyFunctionTrace
583     MESSAGE( "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' ) --> " ) ;
584     if ( RetVal && PyObjRefIor ) {
585       MESSAGE( RetVal << " " << RetVal->ob_refcnt ) ;
586     }
587     else if ( RetVal ) {
588       MESSAGE( RetVal << " " << RetVal->ob_refcnt ) ;
589     }
590     else {
591       MESSAGE( " NULL" ) ;
592     }
593 #endif
594   }
595   
596   return RetVal ;
597 }
598
599 bool GraphExecutor::FiniteStateMachine::PyFunction( const char * aPyFuncName , PyObject * aPyFunction ) {
600   
601   bool RetVal = false ;
602   if ( _MapOfPyFunctions[ aPyFuncName ] != NULL ) {
603 #if PyFunctionTrace
604     PyObject * aPyFunc = _MapOfPyFunctions[ aPyFuncName ] ;
605     MESSAGE( "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' , aPyFunction "
606              << aPyFunction << " ) ob_refcnt " << aPyFunction->ob_refcnt << " already mapped : "
607              << aPyFunc << " ob_refcnt " << aPyFunc->ob_refcnt ) ;
608 #endif
609   }
610   else {
611     _MapOfPyFunctions[ aPyFuncName ] = aPyFunction ;
612 #if PyFunctionTrace
613     MESSAGE( "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' ) " << aPyFunction
614              << " ) ob_refcnt " << aPyFunction->ob_refcnt << " mapped" ) ;
615 #endif
616     RetVal = true ;
617   }
618   
619   return RetVal ;
620 }
621
622 bool GraphExecutor::FiniteStateMachine::ErasePyFunction( const char * aPyFuncName ) {
623   
624 #if PyFunctionTrace
625   MESSAGE( "GraphExecutor::FiniteStateMachine::ErasePyFunction( '" << aPyFuncName << "' )" ) ;
626 #endif
627   if ( _MapOfPyFunctions[ aPyFuncName ] != NULL ) {
628     _MapOfPyFunctions.erase( aPyFuncName ) ;
629     return true ;
630   }  
631   return false ;
632 }
633
634 /*
635 GraphBase::Graph * GraphExecutor::FiniteStateMachine::MapGraph( const char * aGraphName ) {
636   GraphBase::Graph * aGraph = _MapOfGraphs[ aGraphName ] ;
637   //cout << "MapGraph " << aGraphName << " --> " << aGraph << endl ;
638   return aGraph ;
639 }
640
641 bool GraphExecutor::FiniteStateMachine::MapGraph( GraphBase::Graph * aGraph , const char * aGraphName ) {
642   if ( MapGraph( aGraphName ) ) {
643     return false ;
644   }
645   _MapOfGraphs[ aGraphName ] = aGraph ;
646   return true ;
647 }
648
649 void GraphExecutor::FiniteStateMachine::EraseGraph( const char * aGraphName ) {
650   _MapOfGraphs.erase( aGraphName ) ;
651 }
652
653 bool GraphExecutor::FiniteStateMachine::GraphName( const char * aGraphName ) {
654   return  _MapOfGraphNames[ aGraphName ] ;
655 }
656
657 string GraphExecutor::FiniteStateMachine::GraphInstanceName( const char * aGraphName ) {
658   int GraphInstanceNumber = _MapOfGraphNames[ aGraphName ] ;
659   if ( GraphInstanceNumber ) {
660     _MapOfGraphNames[ aGraphName ] = GraphInstanceNumber + 1 ;
661   }
662   else {
663     GraphInstanceNumber = 0 ;
664     _MapOfGraphNames[ aGraphName ] = GraphInstanceNumber + 1 ;
665   }
666   string theGraphInstanceName = string( aGraphName ) ;
667   if ( GraphInstanceNumber ) {
668     theGraphInstanceName += "_" ;
669     ostringstream astr ;
670 //    astr << GraphInstanceNumber << ends ;
671     astr << GraphInstanceNumber ;
672     theGraphInstanceName += astr.str() ;
673   }
674   //cout << "GraphExecutor::FiniteStateMachine::GraphInstanceName( " << aGraphName << " ) --> "
675     //   << theGraphInstanceName << endl ;
676   return theGraphInstanceName ;
677 }
678 */
679
680 void GraphExecutor::FiniteStateMachine::JoinThread() {
681   if ( pthread_mutex_lock( &_MutexJoinWait ) ) {
682     perror("FiniteStateMachine:JoinThread pthread_mutex_lock ") ;
683     exit( 0 ) ;
684   }
685   while ( _JoinThread ) {
686     while ( _ThreadList.size() == 0) {
687 //      MESSAGE( pthread_self() << " FiniteStateMachine:Join pthread_cond_wait" );
688       if ( pthread_cond_wait( &_JoinWait , &_MutexJoinWait ) ) {
689         perror("FiniteStateMachine:JoinThread pthread_cond_wait ") ;
690       }
691     }
692     pthread_t _Thread = _ThreadList.front() ;
693 //    MESSAGE( pthread_self() << " FiniteStateMachine:JoinThread pthread_cond_waited : " << _Thread )
694 //    MESSAGE( pthread_self() << " FiniteStateMachine:JoinThread pthread_join : " << _Thread );
695     int joinsts = pthread_join( _Thread , NULL ) ;
696     if ( joinsts ) {
697       perror("FiniteStateMachine:JoinThread pthread_join ") ;
698       MESSAGE( pthread_self() << " FiniteStateMachine:JoinThread pthread_join : " << _Thread
699                << " Error" );
700       exit( 0 ) ;
701     }
702     else {
703 //      MESSAGE( pthread_self() << " FiniteStateMachine:JoinThread pthread_joined : " << _Thread );
704       _ThreadList.pop_front() ;
705     }
706   }
707   if ( pthread_mutex_unlock( &_MutexJoinWait ) ) {
708     perror("FiniteStateMachine:JoinThread pthread_mutex_unlock ") ;
709     exit( 0 ) ;
710   }
711 }
712
713 void GraphExecutor::FiniteStateMachine::JoinThread( pthread_t aThread ) {
714   if ( pthread_mutex_lock( &_MutexJoinWait ) ) {
715     perror("Join pthread_mutex_lock ") ;
716     exit( 0 ) ;
717   }
718 //  MESSAGE(pthread_self() << " JoinThread pthread_cond_signal for " << aThread)
719   _ThreadList.push_back( aThread ) ;
720   if ( pthread_cond_signal( &_JoinWait ) ) {
721     perror("Join pthread_cond_signal ") ;
722   }
723 //  MESSAGE( pthread_self() << " JoinThread pthread_cond_signaled for " << aThread)
724
725   if ( pthread_mutex_unlock( &_MutexJoinWait ) ) {
726     perror("Join pthread_mutex_unlock ") ;
727     exit( 0 ) ;
728   }
729 }
730
731 int GraphExecutor::FiniteStateMachine::ThreadsNumber() {
732   if( _ThreadList.begin() == _ThreadList.end() )
733     return 0;
734   else
735     return _ThreadList.size();
736 }
737
738 ostream & operator<< (ostream &f ,const GraphExecutor::AutomatonState & aState ) {
739   switch (aState) {
740   case GraphExecutor::UnKnownState :
741     f << "UnKnownState";
742     break;
743   case GraphExecutor::DataUndefState :
744     f << "DataUndefState";
745     break;
746   case GraphExecutor::DataWaitingState :
747     f << "DataWaitingState";
748     break;
749   case GraphExecutor::DataReadyState :
750     f << "DataReadyState";
751     break;
752   case GraphExecutor::SuspendedReadyState :
753     f << "SuspendedReadyState";
754     break;
755   case GraphExecutor::SuspendedReadyToResumeState :
756     f << "SuspendedReadyToResumeState";
757     break;
758   case GraphExecutor::ResumedReadyState :
759     f << "ResumedReadyState";
760     break;
761   case GraphExecutor::KilledReadyState :
762     f << "KilledReadyState";
763     break;
764   case GraphExecutor::StoppedReadyState :
765     f << "StoppedReadyState";
766     break;
767   case GraphExecutor::ExecutingState :
768     f << "ExecutingState";
769     break;
770   case GraphExecutor::SuspendedExecutingState :
771     f << "SuspendedExecutingState";
772     break;
773   case GraphExecutor::ResumedExecutingState :
774     f << "ResumedExecutingState";
775     break;
776   case GraphExecutor::KilledExecutingState :
777     f << "KilledExecutingState";
778     break;
779   case GraphExecutor::StoppedExecutingState :
780     f << "StoppedExecutingState";
781     break;
782   case GraphExecutor::SuccessedExecutingState :
783     f << "SuccessedExecutingState";
784     break;
785   case GraphExecutor::ErroredExecutingState :
786     f << "ErroredExecutingState";
787     break;
788   case GraphExecutor::SuspendedSuccessedState :
789     f << "SuspendedSuccessedState";
790     break;
791   case GraphExecutor::SuspendedErroredState :
792     f << "SuspendedErroredState";
793     break;
794   case GraphExecutor::ResumedSuccessedState :
795     f << "ResumedSuccessedState";
796     break;
797   case GraphExecutor::ResumedErroredState :
798     f << "ResumedErroredState";
799     break;
800   case GraphExecutor::KilledSuccessedState :
801     f << "KilledSuccessedState";
802     break;
803   case GraphExecutor::KilledErroredState :
804     f << "KilledErroredState";
805     break;
806   case GraphExecutor::StoppedSuccessedState :
807     f << "StoppedSuccessedState";
808     break;
809   case GraphExecutor::StoppedErroredState :
810     f << "StoppedErroredState";
811     break;
812   case GraphExecutor::SuccessedState :
813     f << "SuccessedState";
814     break;
815   case GraphExecutor::ErroredState :
816     f << "ErroredState";
817     break;
818   case GraphExecutor::SuspendedState :
819     f << "SuspendedState";
820     break;
821   case GraphExecutor::KilledState :
822     f << "KilledState";
823     break;
824   case GraphExecutor::StoppedState :
825     f << "StoppedState";
826     break;
827   case GraphExecutor::SuspendedSuccessedToReStartState :
828     f << "SuspendedSuccessedToReStartState";
829     break;
830   case GraphExecutor::SuspendedErroredToReStartState :
831     f << "SuspendedErroredToReStartState";
832     break;
833   case GraphExecutor::ReStartedState :
834     f << "ReStartedState";
835     break;
836   case GraphExecutor::LoadingState :
837     f << "LoadingState";
838     break;
839   default :
840     f << "GraphExecutor::AutomatonState_?";
841     break;
842   }
843
844   return f;
845 }
846