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