Salome HOME
MPV: Merge V1_2d
[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 // States :
49 #define UnKnownState            SUPERV::UnKnownState
50 #define DataUndefState            SUPERV::DataUndefState
51 #define DataWaitingState          SUPERV::DataWaitingState
52 #define DataReadyState          SUPERV::DataReadyState
53 #define SuspendedReadyState     SUPERV::SuspendedReadyState
54 #define SuspendedReadyToResumeState SUPERV::SuspendedReadyToResumeState
55 #define ResumedReadyState       SUPERV::ResumedReadyState
56 #define KilledReadyState        SUPERV::KilledReadyState
57 #define StoppedReadyState       SUPERV::StoppedReadyState
58 #define ExecutingState            SUPERV::ExecutingState
59 #define SuspendedExecutingState   SUPERV::SuspendedExecutingState
60 #define ResumedExecutingState     SUPERV::ResumedExecutingState
61 #define KilledExecutingState      SUPERV::KilledExecutingState
62 #define StoppedExecutingState     SUPERV::StoppedExecutingState
63 #define SuccessedExecutingState   SUPERV::SuccessedExecutingState
64 #define ErroredExecutingState     SUPERV::ErroredExecutingState
65 #define SuspendedSuccessedState SUPERV::SuspendedSuccessedState
66 #define SuspendedErroredState   SUPERV::SuspendedErroredState
67 #define SuspendedSuccessedToReStartState SUPERV::SuspendedSuccessedToReStartState
68 #define SuspendedErroredToReStartState   SUPERV::SuspendedErroredToReStartState
69 #define ResumedSuccessedState   SUPERV::ResumedSuccessedState
70 #define ResumedErroredState     SUPERV::ResumedErroredState
71 #define KilledSuccessedState    SUPERV::KilledSuccessedState
72 #define KilledErroredState      SUPERV::KilledErroredState
73 #define StoppedSuccessedState   SUPERV::StoppedSuccessedState
74 #define StoppedErroredState     SUPERV::StoppedErroredState
75 #define SuccessedState          SUPERV::SuccessedState
76 #define ErroredState            SUPERV::ErroredState
77 #define SuspendedState          SUPERV::SuspendedState
78 #define KilledState             SUPERV::KilledState
79 #define StoppedState            SUPERV::StoppedState
80 #define ReRunnedState             SUPERV::ReRunnedState
81 #define ReStartedState            SUPERV::ReStartedState
82 #define NumberOfAutomatonStates        SUPERV::NumberOfAutomatonStates
83
84 // Events :
85 #define UndefinedEvent        GraphExecutor::UndefinedEvent
86 #define NewThreadEvent        GraphExecutor::NewThreadEvent
87 #define SuspendEvent            GraphExecutor::SuspendEvent
88 #define ToResumeEvent           GraphExecutor::ToResumeEvent
89 #define ResumeEvent             GraphExecutor::ResumeEvent
90 #define KillEvent               GraphExecutor::KillEvent
91 #define StopEvent               GraphExecutor::StopEvent
92 #define ExecuteEvent            GraphExecutor::ExecuteEvent
93 #define SuccessEvent            GraphExecutor::SuccessEvent
94 #define ErrorEvent              GraphExecutor::ErrorEvent
95 #define ReStartEvent            GraphExecutor::ReStartEvent
96 #define ReStartAndSuspendEvent  GraphExecutor::ReStartAndSuspendEvent
97 #define NoDataReadyEvent      GraphExecutor::NoDataReadyEvent
98 #define SomeDataReadyEvent    GraphExecutor::SomeDataReadyEvent
99 #define NotAllDataReadyEvent  GraphExecutor::NotAllDataReadyEvent
100 #define AllDataReadyEvent     GraphExecutor::AllDataReadyEvent
101 #define ReadyEvent              GraphExecutor::ReadyEvent
102 #define SuspendedReadyEvent     GraphExecutor::SuspendedReadyEvent
103 #define ResumedReadyEvent       GraphExecutor::ResumedReadyEvent
104 #define KilledReadyEvent        GraphExecutor::KilledReadyEvent
105 #define StoppedReadyEvent       GraphExecutor::StoppedReadyEvent
106 #define ExecutingEvent            GraphExecutor::ExecutingEvent
107 #define SuspendedExecutingEvent   GraphExecutor::SuspendedExecutingEvent
108 #define ResumedExecutingEvent     GraphExecutor::ResumedExecutingEvent
109 #define KilledExecutingEvent      GraphExecutor::KilledExecutingEvent
110 #define StoppedExecutingEvent     GraphExecutor::StoppedExecutingEvent
111 #define SuccessedExecutingEvent GraphExecutor::SuccessedExecutingEvent
112 #define ErroredExecutingEvent   GraphExecutor::ErroredExecutingEvent
113 #define SuspendedSuccessedEvent   GraphExecutor::SuspendedSuccessedEvent
114 #define SuspendedErroredEvent     GraphExecutor::SuspendedErroredEvent
115 #define ResumedSuccessedEvent     GraphExecutor::ResumedSuccessedEvent
116 #define ResumedErroredEvent       GraphExecutor::ResumedErroredEvent
117 #define KilledEvent               GraphExecutor::KilledEvent
118 #define StoppedEvent              GraphExecutor::StoppedEvent
119 #define ToReStartEvent              GraphExecutor::ToReStartEvent
120 #define ReStartedEvent              GraphExecutor::ReStartedEvent
121 #define ReStartedAndSuspendEvent    GraphExecutor::ReStartedAndSuspendEvent
122 #define EndExecuteEvent           GraphExecutor::EndExecuteEvent
123 #define NumberOfEvents            GraphExecutor::NumberOfEvents
124
125 GraphExecutor::FiniteStateMachine::FiniteStateMachine() {
126 //  cdebug_in << "FiniteStateMachine::FiniteStateMachine" << endl;
127
128   _ControlStateName[ VoidState ]          = "VoidState" ;
129   _ControlStateName[ ToSuspendStartState ]= "ToSuspendStartState" ;
130   _ControlStateName[ ToSuspendState ]     = "ToSuspendState" ;
131   _ControlStateName[ ToSuspendDoneState ] = "ToSuspendDoneState" ;
132   _ControlStateName[ ToKillState ]        = "ToKillState" ;
133   _ControlStateName[ ToKillDoneState ]    = "ToKillDoneState" ;
134   _ControlStateName[ ToStopState ]        = "ToStopState" ;
135
136   _StateName[ UnKnownState ]          = "UnKnownState" ;
137   _StateName[ DataUndefState ]            = "DataUndefState" ;
138   _StateName[ DataWaitingState ]          = "DataWaitingState" ;
139   _StateName[ DataReadyState ]            = "DataReadyState" ;
140   _StateName[ SuspendedReadyState ]     = "SuspendedReadyState" ;
141   _StateName[ SuspendedReadyToResumeState ] = "SuspendedReadyToResumeState" ;
142   _StateName[ ResumedReadyState ]       = "ResumedReadyState" ;
143   _StateName[ KilledReadyState ]        = "KilledReadyState" ;
144   _StateName[ StoppedReadyState ]       = "StoppedReadyState" ;
145   _StateName[ ExecutingState ]            = "ExecutingState" ;
146   _StateName[ SuspendedExecutingState ]   = "SuspendedExecutingState" ;
147   _StateName[ ResumedExecutingState ]     = "ResumedExecutingState" ;
148   _StateName[ KilledExecutingState ]      = "KilledExecutingState" ;
149   _StateName[ StoppedExecutingState ]     = "StoppedExecutingState" ;
150   _StateName[ SuccessedExecutingState ]   = "SuccessedExecutingState" ;
151   _StateName[ ErroredExecutingState ]     = "ErroredExecutingState" ;
152   _StateName[ SuspendedSuccessedState ] = "SuspendedSuccessedState" ;
153   _StateName[ SuspendedErroredState ]   = "SuspendedErroredState" ;
154   _StateName[ SuspendedSuccessedToReStartState ] = "SuspendedSuccessedToReStartState" ;
155   _StateName[ SuspendedErroredToReStartState ]   = "SuspendedErroredToReStartState" ;
156   _StateName[ ResumedSuccessedState ]   = "ResumedSuccessedState" ;
157   _StateName[ ResumedErroredState ]     = "ResumedErroredState" ;
158   _StateName[ KilledSuccessedState ]    = "KilledSuccessedState" ;
159   _StateName[ KilledErroredState ]      = "KilledErroredState" ;
160   _StateName[ StoppedSuccessedState ]   = "StoppedSuccessedState" ;
161   _StateName[ StoppedErroredState ]     = "StoppedErroredState" ;
162   _StateName[ SuccessedState ]          = "SuccessedState" ;
163   _StateName[ ErroredState ]            = "ErroredState" ;
164   _StateName[ SuspendedState ]          = "SuspendedState" ;
165   _StateName[ KilledState ]             = "KilledState" ;
166   _StateName[ StoppedState ]            = "StoppedState" ;
167   _StateName[ ReRunnedState ]             = "ReRunnedState" ;
168   _StateName[ ReStartedState ]            = "ReStartedState" ;
169
170   _EventName[ UndefinedEvent ]          = "UndefinedEvent" ;
171   _EventName[ NewThreadEvent ]          = "NewThreadEvent" ;
172   _EventName[ SuspendEvent ]              = "SuspendEvent" ;
173   _EventName[ ToResumeEvent ]             = "ToResumeEvent" ;
174   _EventName[ ResumeEvent ]               = "ResumeEvent" ;
175   _EventName[ KillEvent ]                 = "KillEvent" ;
176   _EventName[ StopEvent ]                 = "StopEvent" ;
177   _EventName[ ExecuteEvent ]              = "ExecuteEvent" ;
178   _EventName[ SuccessEvent ]              = "SuccessEvent" ;
179   _EventName[ ErrorEvent ]                = "ErrorEvent" ;
180   _EventName[ ToReStartEvent ]            = "ToReStartEvent" ;
181   _EventName[ ReStartEvent ]              = "ReStartEvent" ;
182   _EventName[ ReStartAndSuspendEvent ]    = "ReStartAndSuspendEvent" ;
183   _EventName[ NoDataReadyEvent ]        = "NoDataReadyEvent" ;
184   _EventName[ SomeDataReadyEvent ]      = "SomeDataReadyEvent" ;
185   _EventName[ NotAllDataReadyEvent ]    = "NotAllDataReadyEvent" ;
186   _EventName[ AllDataReadyEvent ]       = "AllDataReadyEvent" ;
187   _EventName[ ReadyEvent ]                = "ReadyEvent" ;
188   _EventName[ SuspendedReadyEvent ]       = "SuspendedReadyEvent" ;
189   _EventName[ ResumedReadyEvent ]         = "ResumedReadyEvent" ;
190   _EventName[ KilledReadyEvent ]          = "KilledReadyEvent" ;
191   _EventName[ StoppedReadyEvent ]         = "StoppedReadyEvent" ;
192   _EventName[ ExecutingEvent ]          = "ExecutingEvent" ;
193   _EventName[ SuspendedExecutingEvent ] = "SuspendedExecutingEvent" ;
194   _EventName[ ResumedExecutingEvent ]   = "ResumedExecutingEvent" ;
195   _EventName[ KilledExecutingEvent ]    = "KilledExecutingEvent" ;
196   _EventName[ StoppedExecutingEvent ]   = "StoppedExecutingEvent" ;
197   _EventName[ SuccessedExecutingEvent ]   = "SuccessedExecutingEvent" ;
198   _EventName[ ErroredExecutingEvent ]     = "ErroredExecutingEvent" ;
199   _EventName[ SuspendedSuccessedEvent ]   = "SuspendedSuccessedEvent" ;
200   _EventName[ SuspendedErroredEvent ]     = "SuspendedErroredEvent" ;
201   _EventName[ ResumedSuccessedEvent ]     = "ResumedSuccessedEvent" ;
202   _EventName[ ResumedErroredEvent ]       = "ResumedErroredEvent" ;
203   _EventName[ KilledEvent ]               = "KilledEvent" ;
204   _EventName[ StoppedEvent ]              = "StoppedEvent" ;
205   _EventName[ ReStartedEvent ]          = "ReStartedEvent" ;
206   _EventName[ ReStartedAndSuspendEvent ]= "ReStartedAndSuspendEvent" ;
207   _EventName[ EndExecuteEvent ]         = "EndExecuteEvent" ;
208
209   _ActionName[ ErrorAction ] = "ErrorAction" ;
210   _ActionName[ VoidAction ] = "VoidAction" ;
211   _ActionName[ executeAction ] = "executeAction" ;
212   _ActionName[ ExecuteAction ] = "ExecuteAction" ;
213   _ActionName[ DataWaiting_SomeDataReadyAction ] = "DataWaiting_SomeDataReadyAction" ;
214   _ActionName[ DataUndef_NotAllDataReadyAction ] = "DataUndef_NotAllDataReadyAction";
215   _ActionName[ DataUndef_AllDataReadyAction ] = "DataUndef_AllDataReadyAction" ;
216
217   _ActionName[ DataReady_SuspendAction ] = "DataReady_SuspendAction" ;
218   _ActionName[ SuspendedReady_ResumeAction ] = "SuspendedReady_ResumeAction" ;
219   _ActionName[ DataReady_KillAction ] = "DataReady_KillAction" ;
220   _ActionName[ DataReady_StopAction ] = "DataReady_StopAction" ;
221
222   _ActionName[ DataReady_ExecuteAction ] = "DataReady_ExecuteAction" ;
223
224   _ActionName[ Executing_SuspendAction ] = "Executing_SuspendAction" ;
225   _ActionName[ SuspendedExecuting_ResumeAction ] = "SuspendedExecuting_ResumeAction" ;
226   _ActionName[ Executing_KillAction ] = "Executing_KillAction" ;
227   _ActionName[ Executing_StopAction ] = "Executing_StopAction" ;
228
229   _ActionName[ Executing_SuccessAction ] = "Executing_SuccessAction" ;
230   _ActionName[ Executing_ErrorAction ] = "Executing_ErrorAction" ;
231   _ActionName[ Successed_SuccessAction ] = "Successed_SuccessAction" ;
232   _ActionName[ Errored_ErrorAction ] = "Errored_ErrorAction" ;
233
234   _ActionName[ Successed_SuspendAction ] = "Successed_SuspendAction" ;
235   _ActionName[ Errored_SuspendAction ] = "Errored_SuspendAction" ;
236   _ActionName[ SuspendedSuccessed_ResumeAction ] = "SuspendedSuccessed_ResumeAction" ;
237   _ActionName[ SuspendedErrored_ResumeAction ] = "SuspendedErrored_ResumeAction" ;
238   _ActionName[ Successed_KillAction ] = "Successed_KillAction" ;
239   _ActionName[ Errored_KillAction ] = "Errored_KillAction" ;
240   _ActionName[ Successed_StopAction ] = "Successed_StopAction" ;
241   _ActionName[ Errored_StopAction ] = "Errored_StopAction" ;
242
243   _ActionName[ SuspendedSuccessed_ReStartAction ] = "SuspendedSuccessed_ReStartAction" ;
244   _ActionName[ SuspendedErrored_ReStartAction ] = "SuspendedErrored_ReStartAction" ;
245   _ActionName[ SuspendedSuccessed_ReStartAndSuspendAction ] = "SuspendedSuccessed_ReStartAndSuspendAction" ;
246   _ActionName[ SuspendedErrored_ReStartAndSuspendAction ] = "SuspendedErrored_ReStartAndSuspendAction" ;
247
248   _GraphStateName[ SUPERV::UndefinedState ] = "UndefinedState" ;
249   _GraphStateName[ SUPERV::NoState ] = "NoState" ;
250   _GraphStateName[ SUPERV::EditingState ] = "EditingState" ;
251   _GraphStateName[ SUPERV::SuspendState ] = "SuspendState" ;
252   _GraphStateName[ SUPERV::WaitingState ] = "WaitingState" ;
253   _GraphStateName[ SUPERV::ReadyState ] = "ReadyState" ;
254   _GraphStateName[ SUPERV::SuspendReadyState ] = "SuspendReadyState" ;
255   _GraphStateName[ SUPERV::RunningState ] = "RunningState" ;
256   _GraphStateName[ SUPERV::DoneState ] = "DoneState" ;
257   _GraphStateName[ SUPERV::ErrorState ] = "ErrorState" ;
258   _GraphStateName[ SUPERV::SuspendDoneState ] = "SuspendDoneState" ;
259   _GraphStateName[ SUPERV::SuspendErroredState ] = "SuspendErroredState" ;
260   _GraphStateName[ SUPERV::KillState ] = "KillState" ;
261   _GraphStateName[ SUPERV::StopState ] = "StopState" ;
262   _GraphStateName[ SUPERV::ReRunState ] = "ReRunState" ;
263   _GraphStateName[ SUPERV::ReStartState ] = "ReStartState" ;
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 ] = (SUPERV::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 // NotAllDataReadyEvent :
287   _TransitionTable[ DataUndefState ][ NotAllDataReadyEvent ] = DataWaitingState ;
288   _ActionTable[ DataWaitingState ][ NotAllDataReadyEvent ] = DataUndef_NotAllDataReadyAction;
289 // AllDataReadyEvent
290   _TransitionTable[ DataUndefState ][ AllDataReadyEvent ] = DataReadyState ;
291   _ActionTable[ DataReadyState ][ AllDataReadyEvent ] = DataUndef_AllDataReadyAction ;
292
293 // DATAS-READY-CONTROL :
294 // SuspendEvent
295   _TransitionTable[ DataReadyState ][ SuspendEvent ] = SuspendedReadyState ;
296   _ActionTable[ SuspendedReadyState ][ SuspendEvent ] = DataReady_SuspendAction;
297 // ResumeEvent
298   _TransitionTable[ SuspendedReadyState ][ ToResumeEvent ] = SuspendedReadyToResumeState ;
299   _ActionTable[ SuspendedReadyToResumeState ][ ToResumeEvent ] = VoidAction ;
300
301   _TransitionTable[ SuspendedReadyToResumeState ][ ResumeEvent ] = ResumedReadyState ;
302   _ActionTable[ ResumedReadyState ][ ResumeEvent] = SuspendedReady_ResumeAction ;
303 // KillEvent
304   _TransitionTable[ SuspendedReadyState ][ KillEvent ] = KilledReadyState ;
305 // StopEvent
306   _TransitionTable[ SuspendedReadyState ][ StopEvent ] = StoppedReadyState ;
307 // KillEvent
308   _TransitionTable[ DataReadyState ][ KillEvent ] = KilledReadyState ;
309   _ActionTable[ KilledReadyState ][ KillEvent] = DataReady_KillAction ;
310 // StopEvent
311   _TransitionTable[ DataReadyState ][ StopEvent ] = StoppedReadyState ;
312   _ActionTable[ StoppedReadyState ][ StopEvent] = DataReady_StopAction ;
313
314 // RUN :
315 // ExecuteEvent
316   _TransitionTable[ SuspendedReadyToResumeState ][ ExecuteEvent ] = ExecutingState ;
317 // ExecuteEvent
318   _TransitionTable[ ResumedReadyState ][ ExecuteEvent ] = ExecutingState ;
319 // ExecuteEvent
320   _TransitionTable[ DataReadyState ][ ExecuteEvent ] = ExecutingState ;
321 // ExecuteEvent
322   _TransitionTable[ ReStartedState ][ ExecuteEvent ] = ExecutingState ;
323   _ActionTable[ ExecutingState ][ ExecuteEvent ] = DataReady_ExecuteAction;
324
325 // RUN-CONTROL :
326 // SuspendEvent
327   _TransitionTable[ ExecutingState ][ SuspendEvent ] = SuspendedExecutingState ;
328   _ActionTable[ SuspendedExecutingState ][ SuspendEvent ] = Executing_SuspendAction;
329 // ResumeEvent
330   _TransitionTable[ SuspendedExecutingState ][ ResumeEvent ] = ResumedExecutingState ;
331   _ActionTable[ ResumedExecutingState ][ ResumeEvent] = SuspendedExecuting_ResumeAction;
332 // ExecuteEvent
333   _TransitionTable[ ResumedExecutingState ][ ExecuteEvent ] = ExecutingState ;
334 // KillEvent
335   _TransitionTable[ SuspendedExecutingState ][ KillEvent ] = KilledExecutingState ;
336 // StopEvent
337   _TransitionTable[ SuspendedExecutingState ][ StopEvent ] = StoppedExecutingState ;
338 // KillEvent
339   _TransitionTable[ ExecutingState ][ KillEvent ] = KilledExecutingState ;
340   _ActionTable[ KilledExecutingState ][ KillEvent] = Executing_KillAction;
341   _TransitionTable[ KilledExecutingState ][ KillEvent ] = KilledState ;
342   _ActionTable[ KilledState ][ KillEvent] = VoidAction;
343 // StopEvent
344   _TransitionTable[ ExecutingState ][ StopEvent ] = StoppedExecutingState ;
345   _ActionTable[ StoppedExecutingState ][ StopEvent] = Executing_StopAction;
346   _TransitionTable[ StoppedExecutingState ][ StopEvent ] = StoppedState ;
347   _ActionTable[ StoppedState ][ StopEvent] = VoidAction;
348
349 // DONE :
350 // SuccessEvent
351   _TransitionTable[ ExecutingState ][ SuccessEvent ] = SuccessedExecutingState ;
352   _ActionTable[ SuccessedExecutingState ][ SuccessEvent ] = Executing_SuccessAction;
353 // SuspendedExecutingState : NO ResumeEvent 13-03-2003
354   _TransitionTable[ SuspendedExecutingState ][ SuccessEvent ] = SuccessedExecutingState ;
355   _ActionTable[ SuccessedExecutingState ][ SuccessEvent ] = Executing_SuccessAction;
356 // ErrorEvent
357   _TransitionTable[ ExecutingState ][ ErrorEvent ] = ErroredExecutingState ;
358   _ActionTable[ ErroredExecutingState ][ ErrorEvent ] = Executing_ErrorAction;
359 // SuspendedExecutingState : NO ResumeEvent 13-03-2003
360   _TransitionTable[ SuspendedExecutingState ][ ErrorEvent ] = ErroredExecutingState ;
361   _ActionTable[ ErroredExecutingState ][ ErrorEvent ] = Executing_ErrorAction;
362
363 // DONE-CONTROL :
364 // SuccessedExecutingState - SuccessEvent
365   _TransitionTable[ SuccessedExecutingState ][ SuccessEvent ] = SuccessedState ;
366   _ActionTable[ SuccessedState ][ SuccessEvent ] = Successed_SuccessAction ;
367 // ErroredExecutingState - ErrorEvent
368   _TransitionTable[ ErroredExecutingState ][ ErrorEvent ] = ErroredState ;
369   _ActionTable[ ErroredState ][ ErrorEvent ] = Errored_ErrorAction ;
370
371 // SuccessedState - SuspendEvent
372   _TransitionTable[ SuccessedExecutingState ][ SuspendEvent ] = SuspendedSuccessedState ;
373   _ActionTable[ SuspendedSuccessedState ][ SuspendEvent ] = Successed_SuspendAction;
374 // ErroredState - SuspendEvent
375   _TransitionTable[ ErroredExecutingState ][ SuspendEvent ] = SuspendedErroredState ;
376   _ActionTable[ SuspendedErroredState ][ SuspendEvent ] = Errored_SuspendAction;
377
378 // SuccessedState - KillEvent
379   _TransitionTable[ SuccessedExecutingState ][ KillEvent ] = KilledSuccessedState ;
380   _ActionTable[ KilledSuccessedState ][ KillEvent ] = Successed_KillAction;
381 // ErroredState - KillEvent
382   _TransitionTable[ ErroredExecutingState ][ KillEvent ] = KilledErroredState ;
383   _ActionTable[ KilledErroredState ][ KillEvent ] = Errored_KillAction;
384
385 // SuccessedState - StopEvent
386   _TransitionTable[ SuccessedExecutingState ][ StopEvent ] = StoppedSuccessedState ;
387   _ActionTable[ StoppedSuccessedState ][ StopEvent ] = Successed_StopAction;
388 // ErroredState - StopEvent
389   _TransitionTable[ ErroredExecutingState ][ StopEvent ] = StoppedErroredState ;
390   _ActionTable[ StoppedErroredState ][ StopEvent ] = Errored_StopAction;
391
392 // SuspendedSuccessedState - ResumeEvent
393   _TransitionTable[ SuspendedSuccessedState ][ ResumeEvent ] = ResumedSuccessedState ;
394   _ActionTable[ ResumedSuccessedState ][ ResumeEvent ] = SuspendedSuccessed_ResumeAction;
395
396   _TransitionTable[ ResumedSuccessedState ][ ResumedSuccessedEvent ] = SuccessedState ;
397   _ActionTable[ SuccessedState ][ ResumedSuccessedEvent] = Successed_SuccessAction;
398
399 // SuspendedErroredState - ResumeEvent
400   _TransitionTable[ SuspendedErroredState ][ ResumeEvent ] = ResumedErroredState ;
401   _ActionTable[ ResumedErroredState ][ ResumeEvent ] = SuspendedErrored_ResumeAction;
402   _TransitionTable[ ResumedErroredState ][ ResumedErroredEvent ] = ErroredState ;
403   _ActionTable[ ErroredState ][ ResumedErroredEvent ] = Errored_ErrorAction;
404
405 // SuccessedState - KillEvent
406   _TransitionTable[ SuccessedState ][ KillEvent ] = KilledSuccessedState ;
407   _ActionTable[ KilledSuccessedState ][ KillEvent] = Successed_KillAction;
408   _TransitionTable[ KilledSuccessedState ][ KillEvent ] = KilledState ;
409   _ActionTable[ KilledState ][ KillEvent ] = VoidAction;
410 // ErroredState - KillEvent
411   _TransitionTable[ ErroredState ][ KillEvent ] = KilledErroredState ;
412   _ActionTable[ KilledErroredState ][ KillEvent] = Errored_KillAction;
413   _TransitionTable[ KilledErroredState ][ KillEvent ] = KilledState ;
414   _ActionTable[ KilledState ][ KillEvent ] = VoidAction;
415
416 // SuccessedState - StopEvent
417   _TransitionTable[ SuccessedState ][ StopEvent ] = StoppedSuccessedState ;
418   _ActionTable[ StoppedSuccessedState ][ StopEvent] = Successed_StopAction;
419   _TransitionTable[ StoppedSuccessedState ][ StopEvent ] = StoppedState ;
420   _ActionTable[ StoppedState ][ StopEvent ] = VoidAction;
421 // ErroredState - StopEvent
422   _TransitionTable[ ErroredState ][ StopEvent ] = StoppedErroredState ;
423   _ActionTable[ StoppedErroredState ][ StopEvent] = Errored_StopAction;
424   _TransitionTable[ StoppedErroredState ][ StopEvent ] = StoppedState ;
425   _ActionTable[ StoppedState ][ StopEvent ] = VoidAction;
426
427 // ReStartEvent
428   _TransitionTable[ SuspendedSuccessedState ][ ToReStartEvent ] = SuspendedSuccessedToReStartState ;
429   _ActionTable[ SuspendedSuccessedToReStartState ][ ToReStartEvent] = VoidAction ;
430
431   _TransitionTable[ SuspendedSuccessedToReStartState ][ ReStartEvent ] = ReStartedState ;
432   _TransitionTable[ SuspendedSuccessedToReStartState ][ ReStartAndSuspendEvent ] = ReStartedState ;
433 // ReStartEvent
434   _TransitionTable[ SuspendedErroredState ][ ToReStartEvent ] = SuspendedErroredToReStartState ;
435   _ActionTable[ SuspendedErroredToReStartState ][ ToReStartEvent] = VoidAction ;
436
437   _TransitionTable[ SuspendedErroredToReStartState ][ ReStartEvent ] = ReStartedState ;
438   _TransitionTable[ SuspendedErroredToReStartState ][ ReStartAndSuspendEvent ] = ReStartedState ;
439
440   _ActionTable[ ReStartedState ][ ReStartEvent] = SuspendedSuccessed_ReStartAction;
441   _ActionTable[ ReStartedState ][ ReStartAndSuspendEvent] = SuspendedSuccessed_ReStartAndSuspendAction;
442
443   pthread_mutex_init( &_MutexJoinWait , NULL ) ;
444   if ( pthread_cond_init( &_JoinWait , NULL ) ) {
445     perror("pthread_cond_init( &_JoinWait , NULL )") ;
446     exit( 0 ) ;
447   }
448   _JoinThread = true ;
449   if ( pthread_create( &_JoinThreadNo , NULL , start_function , this ) ) {
450     char * msg = "Cannot pthread_create " ;
451     perror( msg ) ;
452   }
453
454   pthread_mutex_init( &_MutexPythonWait , NULL ) ;
455   _ExecNumber = 0 ;
456   _GraphExecutingNumber = 0 ;
457   _PyInitModule = false ;
458
459 //  cdebug_out << "FiniteStateMachine::FiniteStateMachine _TransitionTable "
460 //             << endl;
461 }
462
463 void * start_function( void *p ) {
464   GraphExecutor::FiniteStateMachine *anAutomaton = (GraphExecutor::FiniteStateMachine *) p;
465   if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) ) {
466     perror("pthread_setcanceltype ") ;
467     exit(0) ;
468   }
469   if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) ) {
470     perror("pthread_setcancelstate ") ;
471     exit(0) ;
472   }
473   anAutomaton->JoinThread() ;
474   pthread_exit( PTHREAD_CANCELED ) ;
475 }
476
477 int GraphExecutor::FiniteStateMachine::ExecNumber() {
478   PyLock() ;
479   _ExecNumber += 1 ;
480   int RetVal = _ExecNumber ;
481   _GraphExecutingNumber += 1 ;
482   PyUnLock() ;
483   return RetVal ;
484 }
485
486 void GraphExecutor::FiniteStateMachine::Executed() {
487   PyLock() ;
488   _GraphExecutingNumber -= 1 ;
489   if ( _GraphExecutingNumber == 0 ) {
490     map< string , PyObject * >::iterator aPyFunction ;
491     for ( aPyFunction = _MapOfPyFunctions.begin() ; aPyFunction != _MapOfPyFunctions.end() ; aPyFunction++ ) {
492       if ( !strcmp( aPyFunction->first.c_str() , "PyObjRef" ) ||
493            !strcmp( aPyFunction->first.c_str() , "PyObjIor" ) ) {
494         //cout << "GraphExecutor::FiniteStateMachine::Executed " << aPyFunction->first << " keeped ..."<< endl ;
495       }
496       else {
497         //cout << "GraphExecutor::FiniteStateMachine::Executed " << aPyFunction->first << " erased ..."<< endl ;
498         _MapOfPyFunctions.erase( aPyFunction->first ) ;
499       }
500     }
501   }
502   else {
503     //cout << "GraphExecutor::FiniteStateMachine::Executed _GraphExecutingNumber " << _GraphExecutingNumber
504     //     << " != 0 ==> no erase" << endl ;
505   }
506   PyUnLock() ;
507   return ;
508 }
509
510 bool GraphExecutor::FiniteStateMachine::PyInitModule() {
511   bool InitedModule = _PyInitModule ;
512   _PyInitModule = true ;
513   return InitedModule ;
514 }
515
516 void GraphExecutor::FiniteStateMachine::PyLock() {
517 //  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyLock " << &_MutexPythonWait << endl ;
518   if ( pthread_mutex_lock( &_MutexPythonWait ) ) {
519     perror( "GraphExecutor::FiniteStateMachine::PyLock" ) ;
520     exit( 0 ) ;
521   }
522 //  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyLocked " << &_MutexPythonWait << endl ;
523 }
524
525 void GraphExecutor::FiniteStateMachine::PyUnLock() {
526 //  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyUnLock " << &_MutexPythonWait << endl ;
527   if ( pthread_mutex_unlock( &_MutexPythonWait ) ) {
528     perror( "GraphExecutor::FiniteStateMachine::PyUnLock" ) ;
529     exit( 0 ) ;
530   }
531 //  cout << pthread_self() << " GraphExecutor::FiniteStateMachine::PyUnLocked " << &_MutexPythonWait << endl ;
532 }
533
534 PyObject * GraphExecutor::FiniteStateMachine::PyFunction( const char * aPyFuncName ) {
535   
536   PyObject * RetVal = NULL ;
537   bool PyObjRefIor = !strcmp( aPyFuncName , "PyObjRef" ) || !strcmp( aPyFuncName , "PyObjIor" ) ;
538   PyObject * PyFunctionMapped = _MapOfPyFunctions[ aPyFuncName ] ;
539   if ( _GraphExecutingNumber > 1 && !PyObjRefIor ) {
540     RetVal = PyFunctionMapped ;
541     //cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' ) --> " ;
542     if ( RetVal ) {
543       //cout << RetVal << " ob_refcnt " << RetVal->ob_refcnt ;
544     }
545     else {
546       //cout << " NULL" ;
547     }
548     //cout << endl ;
549   }
550   else {
551     RetVal = PyFunctionMapped ;
552     //cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' ) --> " ;
553     if ( RetVal && PyObjRefIor ) {
554       //cout << RetVal << " " << RetVal->ob_refcnt << endl ;
555     }
556     else if ( RetVal ) {
557       //cout << RetVal << " " << RetVal->ob_refcnt << endl ;
558 //      _MapOfPyFunctions.erase( aPyFuncName ) ;
559 //      cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' ) erased --> NULL" << endl ;
560     }
561     else {
562       //cout << " NULL" << endl ;
563     }
564   }
565   
566   return RetVal ;
567 }
568
569 bool GraphExecutor::FiniteStateMachine::PyFunction( const char * aPyFuncName , PyObject * aPyFunction ) {
570   
571   bool RetVal = false ;
572   if ( _MapOfPyFunctions[ aPyFuncName ] != NULL ) {
573     PyObject * aPyFunc = _MapOfPyFunctions[ aPyFuncName ] ;
574     //cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' , " << aPyFunction
575     //     << " ) ob_refcnt " << aPyFunction->ob_refcnt << " already mapped : " << aPyFunc << " ob_refcnt "
576     //     << aPyFunc->ob_refcnt << endl ;
577   }
578   else {
579     _MapOfPyFunctions[ aPyFuncName ] = aPyFunction ;
580     //cout << "GraphExecutor::FiniteStateMachine::PyFunction( '" << aPyFuncName << "' , " << aPyFunction
581     //     << " ) ob_refcnt " << aPyFunction->ob_refcnt << " mapped" << endl ;
582     RetVal = true ;
583   }
584   
585   return RetVal ;
586 }
587
588 void GraphExecutor::FiniteStateMachine::JoinThread() {
589   if ( pthread_mutex_lock( &_MutexJoinWait ) ) {
590     perror("Join pthread_mutex_lock ") ;
591     exit( 0 ) ;
592   }
593   while ( _JoinThread ) {
594     while ( _ThreadList.size() == 0) {
595 //      MESSAGE( pthread_self() << " Join pthread_cond_wait" );
596       if ( pthread_cond_wait( &_JoinWait , &_MutexJoinWait ) ) {
597         perror("Join pthread_cond_wait ") ;
598       }
599     }
600     pthread_t _Thread = _ThreadList.front() ;
601 //    MESSAGE( pthread_self() << " Join pthread_cond_waited : " << _Thread )
602     _ThreadList.pop_front() ;
603 //    MESSAGE( pthread_self() << " Join pthread_join : " << _Thread );
604     int joinsts = pthread_join( _Thread , NULL ) ;
605 //    MESSAGE( pthread_self() << " Join pthread_joined : " << _Thread );
606     if ( joinsts ) {
607       perror("Join pthread_join ") ;
608 //      exit( 0 ) ;
609     }
610   }
611   if ( pthread_mutex_unlock( &_MutexJoinWait ) ) {
612     perror("Join pthread_mutex_unlock ") ;
613     exit( 0 ) ;
614   }
615 }
616
617 void GraphExecutor::FiniteStateMachine::JoinThread( pthread_t aThread ) {
618   if ( pthread_mutex_lock( &_MutexJoinWait ) ) {
619     perror("Join pthread_mutex_lock ") ;
620     exit( 0 ) ;
621   }
622 //  MESSAGE(pthread_self() << " JoinThread pthread_cond_signal for " << aThread)
623   _ThreadList.push_back( aThread ) ;
624   if ( pthread_cond_signal( &_JoinWait ) ) {
625     perror("Join pthread_cond_signal ") ;
626   }
627 //  MESSAGE( pthread_self() << " JoinThread pthread_cond_signaled for " << aThread)
628
629   if ( pthread_mutex_unlock( &_MutexJoinWait ) ) {
630     perror("Join pthread_mutex_unlock ") ;
631     exit( 0 ) ;
632   }
633 }
634