]> SALOME platform Git repositories - modules/superv.git/blob - src/GraphExecutor/DataFlowExecutor_OutNode.cxx
Salome HOME
02c28bb360fa2f83870f2876bfa258baeb977ac2
[modules/superv.git] / src / GraphExecutor / DataFlowExecutor_OutNode.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  SUPERV GraphExecutor : contains classes that permit execution of graphs and particularly the execution automaton
23 //  File   : DataFlowBase_OutNode.cxx
24 //  Author : Jean Rahuel, CEA
25 //  Module : SUPERV
26 //  $Header:
27 //
28 using namespace std;
29
30 #include "DataFlowExecutor_OutNode.hxx"
31
32 #include "DataFlowEditor_DataFlow.hxx"
33 #include "DataFlowExecutor_DataFlow.hxx"
34
35 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
36 //extern Engines_Container_i * _TheContainer ;
37
38 extern GraphExecutor::FiniteStateMachine * theAutomaton ;
39
40 // static const char *ComponentName = "SalomeSuperVisionComponent" ;
41
42 extern int _ArgC ;
43 extern char ** _ArgV ;
44
45 GraphExecutor::OutNode::OutNode() {
46 //               Graph() {
47   // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
48   //_SuperVisionContainer = _TheContainer ;
49   //_MainThreadId = SuperVisionContainer()->MainThreadId() ;
50   _Valid = false ;
51   _Executable = false ;
52   _Done = false ;
53   _NodeAborted = false ;
54   _Threads = 0 ;
55   _ControlState = SUPERV::VoidState ;
56   _State = GraphExecutor::UnKnownState ;
57   _PyInitialized = false ;
58   _run_func_thread = 0;
59   pthread_mutex_init( &_MutexWait , NULL ) ;
60   pthread_mutex_init( &_PyMutexWait , NULL ) ;
61   if ( pthread_cond_init( &_EventWait , NULL ) ) {
62     perror("pthread_cond_init( &_EventWait , NULL )") ;
63     exit( 0 ) ;
64   }
65   if ( pthread_cond_init( &_JoinWait , NULL ) ) {
66     perror("pthread_cond_init( &_JoinWait , NULL )") ;
67     exit( 0 ) ;
68   }
69 }
70
71 GraphExecutor::OutNode::OutNode( CORBA::ORB_ptr ORB, 
72                                  SALOME_NamingService* ptrNamingService ,
73                                  const char *DataFlowName ,
74                                  const char * DebugFileName ,
75                                  const SUPERV::KindOfNode aKindOfNode ) {
76   Set_prof_debug( ORB , DebugFileName ) ;
77   cdebug_in << "GraphExecutor::OutNode::OutNode(" << DataFlowName << " , " << DebugFileName
78             << " , " << aKindOfNode << ")" << endl;
79   if ( aKindOfNode == SUPERV::DataFlowGraph ) {
80     _StreamGraph = NULL ;
81     _Graph = new GraphBase::Graph( ORB , ptrNamingService , DataFlowName , aKindOfNode ,
82                                    _prof_debug , _fdebug ) ;
83   }
84   else if ( aKindOfNode == SUPERV::DataStreamGraph ) {
85     _StreamGraph = new GraphBase::StreamGraph( ORB , ptrNamingService , DataFlowName , aKindOfNode ,
86                                                _prof_debug , _fdebug ) ;
87     _Graph = _StreamGraph ;
88   }
89   // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
90   //_SuperVisionContainer = _TheContainer ;
91   //_MainThreadId = SuperVisionContainer()->MainThreadId() ;
92   _Valid = false ;
93   _Executable = false ;
94   _Done = false ;
95   _NodeAborted = false ;
96   _Threads = 0 ;
97   _ControlState = SUPERV::VoidState ;
98   _State = GraphExecutor::UnKnownState ;
99   _PyInitialized = false ;
100   _run_func_thread = 0;
101   _Orb = CORBA::ORB::_duplicate( ORB ) ;
102   pthread_mutex_init( &_MutexWait , NULL ) ;
103   pthread_mutex_init( &_PyMutexWait , NULL ) ;
104   if ( pthread_cond_init( &_EventWait , NULL ) ) {
105     perror("pthread_cond_init( &_EventWait , NULL )") ;
106     exit( 0 ) ;
107   }
108   if ( pthread_cond_init( &_JoinWait , NULL ) ) {
109     perror("pthread_cond_init( &_JoinWait , NULL )") ;
110     exit( 0 ) ;
111   }
112   cdebug_out << "GraphExecutor::OutNode::OutNode" << endl;
113 }
114
115 GraphExecutor::OutNode::OutNode( CORBA::ORB_ptr ORB, 
116                                  SALOME_NamingService* ptrNamingService ,
117                                  const SALOME_ModuleCatalog::Service& DataFlowService ,
118                                  const char *DataFlowComponentName ,
119                                  const char *DataFlowInterfaceName ,
120                                  const char *DataFlowName ,
121                                  const SUPERV::KindOfNode DataFlowkind ,
122                                  const SUPERV::SDate DataFlowFirstCreation ,
123                                  const SUPERV::SDate DataFlowLastModification ,
124                                  const char * DataFlowEditorRelease ,
125                                  const char * DataFlowAuthor ,
126                                  const char * DataFlowComputer ,
127                                  const char * DataFlowComment ,
128                                  const char * DebugFileName ) {
129   Set_prof_debug( ORB , DebugFileName ) ;
130
131   cdebug_in << "GraphExecutor::OutNode::OutNode(" << DataFlowComponentName << " , "
132             << DataFlowInterfaceName << " , " << DataFlowName << " , " << DataFlowkind << ")"
133             << " , ... , " << DebugFileName << ")" << endl;
134   if ( DataFlowkind == SUPERV::DataFlowGraph ) {
135     _StreamGraph = NULL ;
136     _Graph = new GraphBase::Graph( ORB , ptrNamingService , DataFlowService , DataFlowComponentName ,
137                                    DataFlowInterfaceName , DataFlowName , DataFlowkind ,
138                                    DataFlowFirstCreation , DataFlowLastModification  ,
139                                    DataFlowEditorRelease , DataFlowAuthor ,
140                                    DataFlowComputer , DataFlowComment ,
141                                    _prof_debug , _fdebug ) ;
142 //                                   DataFlowComputer , DataFlowComment , DebugFileName ) ;
143   }
144   else if ( DataFlowkind == SUPERV::DataStreamGraph ) {
145     _StreamGraph = new GraphBase::StreamGraph( ORB , ptrNamingService , DataFlowService , DataFlowComponentName ,
146                                                DataFlowInterfaceName , DataFlowName , DataFlowkind ,
147                                                DataFlowFirstCreation , DataFlowLastModification  ,
148                                                DataFlowEditorRelease , DataFlowAuthor ,
149                                                DataFlowComputer , DataFlowComment ,
150                                                _prof_debug , _fdebug ) ;
151     _Graph = _StreamGraph ;
152   }
153   // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
154   //_SuperVisionContainer = _TheContainer ;
155   //_MainThreadId = SuperVisionContainer()->MainThreadId() ;
156   _Valid = false ;
157   _Executable = false ;
158   _Done = false ;
159   _NodeAborted = false ;
160   _Threads = 0 ;
161   _ControlState = SUPERV::VoidState ;
162   _State = GraphExecutor::UnKnownState ;
163   _PyInitialized = false ;
164   _run_func_thread = 0;
165   _Orb = CORBA::ORB::_duplicate( ORB ) ;
166   pthread_mutex_init( &_MutexWait , NULL ) ;
167   pthread_mutex_init( &_PyMutexWait , NULL ) ;
168   if ( pthread_cond_init( &_EventWait , NULL ) ) {
169     perror("pthread_cond_init( &_EventWait , NULL )") ;
170     exit( 0 ) ;
171   }
172   if ( pthread_cond_init( &_JoinWait , NULL ) ) {
173     perror("pthread_cond_init( &_JoinWait , NULL )") ;
174     exit( 0 ) ;
175   }
176 }
177
178 GraphExecutor::OutNode::~OutNode() {
179 }
180
181 void GraphExecutor::OutNode::Set_prof_debug( CORBA::ORB_ptr ORB ,
182                                              const char * DebugFileName ) {
183   _Graph_prof_debug = 0 ;
184   if ( DebugFileName ) {
185     _fdebug = new ofstream( DebugFileName );
186     SetDebug( ORB , &_Graph_prof_debug , _fdebug ) ;
187     MESSAGE( endl << "Trace redirected to file " << DebugFileName << endl)
188   }
189 }
190
191 bool GraphExecutor::OutNode::LoadDataFlow(const GraphBase::SGraph * aDataFlow ) {
192   bool RetVal = false ;
193   cdebug_in << "GraphExecutor::OutNode::LoadDataFlow " << (*aDataFlow).Info.theName << endl ;
194   RetVal = LoadInfo( (*aDataFlow).Info ) ;
195   if ( Graph()->ServiceName() != NULL ) {
196 //    MESSAGE( "GraphExecutor::OutNode::LoadDataFlow" );
197     if ( RetVal ) {
198       RetVal = LoadNodes( (*aDataFlow).Nodes ) ;
199       if ( RetVal ) {
200         RetVal = LoadLinks( (*aDataFlow).Links ) ;
201         if ( RetVal ) {
202           IsValid() ;
203           RetVal = LoadDatas( (*aDataFlow).Datas ) ;
204 //          IsExecutable() ;
205           if ( !RetVal) {
206             cdebug << "GraphExecutor::OutNode::LoadDataFlow LoadDatas Error."
207                    << endl ;
208           }
209         }
210         else {
211           cdebug << "GraphExecutor::OutNode::LoadDataFlow LoadLinks Error."
212                << endl ;
213         }
214       }
215       else {
216         cdebug << "GraphExecutor::OutNode::LoadDataFlow LoadNodes Error."
217              << endl ;
218       }
219     }
220     else {
221       cdebug << "GraphExecutor::OutNode::LoadDataFlow LoadInfo Error."
222            << endl ;
223     }
224   }
225   else {
226     cdebug << "GraphExecutor::OutNode::LoadDataFlow ServiceName Error." << endl ;
227   }
228   cdebug_out << "GraphExecutor::OutNode::LoadDataFlow " << Name() << " RetVal "
229              << RetVal << endl ;
230   return RetVal ;
231 }
232
233 #if 0
234 bool GraphExecutor::OutNode::LoadXml( const char* myFileName ) {
235   bool RetVal = false ;
236   GraphBase::ListOfSGraphs aListOfDataFlows ;
237   if ( Graph()->LoadXml( _Orb , myFileName , aListOfDataFlows ) ) {
238     RetVal = LoadDataFlow( aListOfDataFlows ) ;
239 //    if ( aConstructor && RetVal )
240 //      RetVal = Name( aDataFlow.Info.theName.c_str() ) ;
241   }
242   return RetVal ;
243 }
244 #endif
245
246 bool GraphExecutor::OutNode::LoadInfo(const GraphBase::SNode &aDataFlowInfo ) {
247   cdebug << "GraphExecutor::OutNode::LoadInfo _StreamGraph " << _StreamGraph << endl ;
248 //  ComponentName( aDataFlowInfo.theComponentName.c_str()  ) ;
249   Graph()->Name( aDataFlowInfo.theName.c_str()  ) ;
250   Graph()->Kind( aDataFlowInfo.theKind ) ;
251   if ( Graph()->IsDataStreamNode() && aDataFlowInfo.theKind == SUPERV::DataStreamGraph ) {
252     StreamGraph()->SetStreamParams( aDataFlowInfo.theTimeout , aDataFlowInfo.theDataStreamTrace , aDataFlowInfo.theDeltaTime ) ;
253   }
254   Graph()->DataService( _Orb , aDataFlowInfo.theService , _prof_debug , _fdebug ) ;
255   Graph()->FirstCreation( aDataFlowInfo.theFirstCreation ) ;
256   Graph()->LastModification( aDataFlowInfo.theLastModification ) ;
257   Graph()->EditorRelease( aDataFlowInfo.theEditorRelease.c_str()  ) ;
258   Graph()->Author( aDataFlowInfo.theAuthor.c_str()   ) ;
259 //  Graph()->Computer( aDataFlowInfo.theContainer.c_str()  ) ;
260   Graph()->Comment( aDataFlowInfo.theComment.c_str()  ) ;
261 // Not in OutNode/DataFlow but in InNode/DataFlow_in_an_other_DataFlow
262 //  Graph()->Coordinates( aDataFlowInfo.theX , aDataFlowInfo.theY ) ;
263   return true ;
264 }
265
266 bool GraphExecutor::OutNode::LoadNodes(const GraphBase::ListOfSNodes &aListOfNodes ) {
267   bool RetVal = true ;
268   GraphExecutor::InNode * anInNode ;
269   cdebug << "GraphExecutor::OutNode::LoadNodes" << endl ;
270   int i ;
271   for ( i = 0 ; i < (int ) aListOfNodes.size() ; i++ ) {
272     GraphBase::SNode aNode = aListOfNodes[ i ] ;
273     anInNode = AddNode( aNode.theService ,
274                         aNode.theListOfFuncName ,
275                         aNode.theListOfPythonFunctions ,
276                         aNode.theComponentName.c_str() ,
277                         aNode.theInterfaceName.c_str()  , aNode.theName.c_str() ,
278                         aNode.theKind ,
279                         aNode.theFirstCreation , aNode.theLastModification ,
280                         aNode.theEditorRelease.c_str() ,
281                         aNode.theAuthor.c_str()  , aNode.theContainer.c_str() ,
282                         aNode.theComment.c_str() ,
283                         aNode.theCoords.theX , aNode.theCoords.theY ) ;
284     if ( !anInNode ) {
285       RetVal = false ;
286       break ;
287     }
288     if ( anInNode->IsOneOfInLineNodes() || anInNode->IsMacroNode() ) {
289       anInNode->GraphExecutor::InNode::InLineNode()->DefPortsOfNode(
290                                 _Orb , aNode.theService , anInNode->NamePtr() ,
291                                 anInNode->Kind() ,
292                                 _prof_debug , _fdebug ) ;
293     }
294 //    GraphBase::ComputingNode * theNode = Graph()->GetChangeGraphNode( aNode.theName.c_str() ) ;
295
296     unsigned int j ;
297     for ( j = 0 ; j < aNode.theListOfInDataStreams.size() ; j++ ) {
298       GraphBase::InPort * anInPort ;
299       anInPort = anInNode->ComputingNode()->AddInDataStreamPort( aNode.theListOfInDataStreams[ j ].theDataStreamParameter.Parametername ,
300                                                                  aNode.theListOfInDataStreams[ j ].theDataStreamParameter.Parametertype ,
301                                                                  aNode.theListOfInDataStreams[ j ].theDataStreamParameter.Parameterdependency ,
302                                                                  SUPERV::DataStreamParameter ) ;
303       ((GraphBase::InDataStreamPort * ) anInPort)->SetParams( aNode.theListOfInDataStreams[ j ].theKindOfSchema ,
304                                                               aNode.theListOfInDataStreams[ j ].theKindOfInterpolation ,
305                                                               aNode.theListOfInDataStreams[ j ].theKindOfExtrapolation ) ;
306     }
307     for ( j = 0 ; j < aNode.theListOfOutDataStreams.size() ; j++ ) {
308       GraphBase::OutPort * anOutPort ;
309       anOutPort = anInNode->ComputingNode()->AddOutDataStreamPort( aNode.theListOfOutDataStreams[ j ].theDataStreamParameter.Parametername ,
310                                                                    aNode.theListOfOutDataStreams[ j ].theDataStreamParameter.Parametertype ,
311                                                                    aNode.theListOfOutDataStreams[ j ].theDataStreamParameter.Parameterdependency ,
312                                                                    SUPERV::DataStreamParameter ) ;
313       ((GraphBase::OutDataStreamPort * ) anOutPort)->NumberOfValues( aNode.theListOfOutDataStreams[ j ].theNumberOfValues ) ;
314     }
315
316
317 //    theNode->DataStreamInPortsNumber( aNode.theDataStreamInArgsNumber ) ;
318 //    theNode->DataStreamOutPortsNumber( aNode.theDataStreamOutArgsNumber ) ;
319 //    cdebug << "GraphExecutor::OutNode::LoadNodes " << anInNode->Name()
320 //           << " InPortsSize " << theNode->GetNodeInPortsSize()
321 //           << " OutPortsSize " << theNode->GetNodeOutPortsSize()
322 //           << " DataStreamInPortsNumber " << aNode.theDataStreamInArgsNumber
323 //           << " DataStreamOutPortsNumber " << aNode.theDataStreamOutArgsNumber
324 //           << endl ;
325 //    int j ;
326 //    if ( aNode.theDataStreamInArgsNumber ) { // -1 because of Gates
327 //      for ( j = theNode->GetNodeInPortsSize() - aNode.theDataStreamInArgsNumber - 1 ; j < theNode->GetNodeInPortsSize() - 1 ; j++ ) {
328 //        cdebug << "GraphExecutor::OutNode::LoadNodes " << anInNode->Name()
329 //               << " InPort " << theNode->GetChangeNodeInPort( j )->PortName()
330 //               << " Kind( DataStreamParameter)" << endl ;
331 //        theNode->GetChangeNodeInPort( j )->Kind( SUPERV::DataStreamParameter ) ;
332 //      }
333 //    }
334 //    if ( aNode.theDataStreamOutArgsNumber ) {
335 //      int withgate = 1 ;
336 //      if ( theNode->IsLoopNode() || theNode->IsEndLoopNode() ) {
337 //        withgate = 0 ;
338 //      }
339 //      for ( j = theNode->GetNodeOutPortsSize() - aNode.theDataStreamOutArgsNumber - withgate ; j < theNode->GetNodeOutPortsSize() - withgate ; j++ ) {
340 //        cdebug << "GraphExecutor::OutNode::LoadNodes " << anInNode->Name()
341 //               << " OutPort " << theNode->GetChangeNodeOutPort( j )->PortName()
342 //               << " Kind( DataStreamParameter)" << endl ;
343 //        theNode->GetChangeNodeOutPort( j )->Kind( SUPERV::DataStreamParameter ) ;
344 //      }
345 //    }
346   }
347   for ( i = 0 ; i < (int ) aListOfNodes.size() ; i++ ) {
348     GraphBase::SNode aNode = aListOfNodes[ i ] ;
349     anInNode = (GraphExecutor::InNode * ) Graph()->GetChangeGraphNode( aNode.theName.c_str() )->GetInNode() ;
350     cdebug << "GraphExecutor::OutNode::LoadNodes " << anInNode->Name() << " IsOneOfGOTONodes "
351            << anInNode->IsOneOfGOTONodes() << " " << aNode.theCoupledNode.c_str() << endl ;
352     if ( anInNode->IsOneOfGOTONodes() && strlen( aNode.theCoupledNode.c_str() ) ) {
353       GraphBase::GOTONode * aCoupledNode ;
354       aCoupledNode = (GraphBase::GOTONode * ) Graph()->GetGraphNode( aNode.theName.c_str() ) ;
355       GraphBase::GOTONode * aCoupledCoupledNode ;
356       aCoupledCoupledNode = (GraphBase::GOTONode * ) Graph()->GetChangeGraphNode( aNode.theCoupledNode.c_str() ) ;
357       if ( aCoupledCoupledNode != NULL ) {
358         aCoupledNode->CoupledNode( aCoupledCoupledNode ) ;
359       }
360       else {
361         aCoupledNode->CoupledNodeName( aNode.theCoupledNode.c_str() ) ;
362       }
363     }
364   }
365   return RetVal ;
366 }
367
368 bool GraphExecutor::OutNode::LoadLinks(const GraphBase::ListOfSLinks &aListOfLinks ) {
369   bool RetVal = true ;
370   cdebug << "GraphExecutor::OutNode::LoadLinks " << aListOfLinks.size()
371          << endl ;
372   int i ;
373   for ( i = 0 ; i < (int ) aListOfLinks.size() ; i++ ) {
374     GraphBase::SLink aLink = aListOfLinks[ i ] ;
375     RetVal = Graph()->AddLink( aLink.FromNodeName.c_str() ,
376                                aLink.FromServiceParameterName.c_str() ,
377                                aLink.ToNodeName.c_str() ,
378                                aLink.ToServiceParameterName.c_str() ) ;
379 //                               , aLink.aLinkValue ) ;
380     if ( !RetVal )
381       break ;
382   }
383   return RetVal ;
384 }
385
386 bool GraphExecutor::OutNode::LoadDatas(const GraphBase::ListOfSLinks &aListOfDatas ) {
387   bool RetVal = true ;
388   cdebug << "GraphExecutor::OutNode::LoadDatas " << aListOfDatas.size()
389          << endl ;
390   int i ;
391   for ( i = 0 ; i < (int ) aListOfDatas.size() ; i++ ) {
392     GraphBase::SLink aLink = aListOfDatas[ i ] ;
393     if ( !strcmp( aLink.FromNodeName.c_str() , Name() ) )
394       RetVal = Graph()->AddInputData( aLink.ToNodeName.c_str() ,
395                                       aLink.ToServiceParameterName.c_str() ,
396                                       aLink.aLinkValue ) ;
397 //    else if ( !strcmp( aLink.ToNodeName.c_str() , Name() ) ) {
398 //      RetVal = Graph()->AddOutputData( aLink.FromNodeName.c_str() ,
399 //                                       aLink.FromServiceParameterName.c_str() ) ;
400 //      Graph()->AddLink( aLink.FromNodeName.c_str() , (GraphBase::ComputingNode *) this ) ;
401 //    }
402     else {
403       cdebug << "GraphExecutor::OutNode::LoadDatas Error " << aLink.FromNodeName
404            << " and " << aLink.ToNodeName << " differents from " << Name()
405            << endl ;
406       RetVal = false ;
407     }
408     if ( !RetVal )
409       break ;
410   }
411   return RetVal ;
412 }
413
414 GraphExecutor::InNode *GraphExecutor::OutNode::AddNode(
415                       const SALOME_ModuleCatalog::Service& NodeService ,
416                       GraphBase::ListOfFuncName aFuncName ,
417                       GraphBase::ListOfPythonFunctions aPythonFunction ,
418                       const char * NodeComponentName ,
419                       const char * NodeInterfaceName ,
420                       const char * NodeName ,
421                       const SUPERV::KindOfNode NodeKindOfNode ,
422                       const SUPERV::SDate NodeFirstCreation ,
423                       const SUPERV::SDate NodeLastModification  ,
424                       const char * NodeEditorRelease ,
425                       const char * NodeAuthor ,
426                       const char * NodeComputer ,
427                       const char * NodeComment ,
428                       const int NodeX ,
429                       const int NodeY ) {
430   cdebug_in << "GraphExecutor::OutNode::AddNode(" << NodeComponentName << " , "
431             << NodeName << ")" << endl;
432   GraphExecutor::InNode *Nd = NULL ;
433   Nd = new GraphExecutor::InNode( _Orb, Graph()->NamingService() , NodeService ,
434                                   NodeComponentName , NodeInterfaceName ,
435                                   NodeName , NodeKindOfNode ,
436                                   aFuncName , aPythonFunction ,
437                                   NodeFirstCreation , NodeLastModification ,
438                                   NodeEditorRelease , NodeAuthor ,
439                                   NodeComputer , NodeComment , false , NodeX , NodeY ,
440                                   _prof_debug , _fdebug ) ;
441   Graph()->AddNode( Nd->ComputingNode() ) ;
442   cdebug_out << "GraphExecutor::OutNode::AddNode" << endl;
443   return Nd ;
444 }
445
446
447 bool GraphExecutor::OutNode::Valid() {
448   cdebug_in << "GraphExecutor::OutNode::Valid" << endl;
449   bool RetVal = true ;
450   Graph()->ReSetMessages() ; // ==> Only one set of errors messages ...
451   if ( _Valid )
452     return true ;
453
454   _Executable = false ;
455
456   if ( !Graph()->CreateService() ) {
457     cdebug_out << "This DataFlow has invalid type(s)." << endl ;
458     RetVal = false ;
459   }
460   int SubStreamGraphsNumber = 0 ;
461   if ( !Graph()->Sort( SubStreamGraphsNumber ) ) {
462     cdebug_out << "This DataFlow is not valid." << endl ;
463     RetVal = false ;
464   }
465   if ( Graph()->IsDataStreamNode() ) {
466     StreamGraph()->SubStreamGraphsNumber( SubStreamGraphsNumber ) ;
467   }
468   Graph()->InLineServices() ;
469
470   if ( !Graph()->ValidLoops() ) {
471     cdebug_out << "This DataFlow have not valid Loops." << endl ;
472     RetVal = false ;
473   }
474   if ( !Graph()->ValidSwitchs() ) {
475     cdebug_out << "This DataFlow have not valid Switchs." << endl ;
476     RetVal = false ;
477   }
478
479 //  CreateService() ;
480
481   Graph()->ComputingNodes() ;
482   if ( RetVal ) {
483     _Valid = true ;
484   }
485
486   cdebug_out << "GraphExecutor::OutNode::Valid " << _Valid << " RetVal " << RetVal << endl;
487   return RetVal ;
488 }
489
490 bool GraphExecutor::OutNode::Executable() {
491   cdebug_in << "GraphExecutor::OutNode::Executable" << endl;
492   bool RetVal = true ;
493   if ( !IsValid() ) {
494     RetVal = false ;
495   }
496
497   if ( Graph()->DataServerNodes() ) {
498     if ( RetVal ) {
499       _Executable = true ;
500     }
501   }
502   else {
503     cdebug << "This DataFlow is not executable." << endl ;
504     _Executable = false ;
505     RetVal = false ;
506   }
507
508   cdebug_out << "GraphExecutor::OutNode::Executable " << _Executable << endl;
509   return _Executable ;
510 }
511
512 bool GraphExecutor::OutNode::Run( const bool AndSuspend ) {
513   bool RetVal = false ;
514   cdebug_in << pthread_self() << "GraphExecutor::OutNode::Run( AndSuspend " << AndSuspend
515             << " ) State " << theAutomaton->StateName( State() ) << endl;
516
517   if ( Executable() ) {
518     _ControlState = SUPERV::VoidState ;
519     _SuspendedThreads = 0 ;
520     Graph()->ThreadNo( pthread_self() ) ;
521     Done( false ) ;
522     _JustStarted = true ;
523     RetVal = true ;
524     int i ;
525     bool again = true ;
526     for ( i = 0 ; i < Graph()->GraphNodesSize() ; i++ ) {
527       GraphExecutor::InNode * anInNode = (GraphExecutor::InNode *) Graph()->GraphNodes( i )->GetInNode() ;
528       anInNode->OutNode( this ) ;
529 // Initialize in python interpretor the python functions
530       if ( !PyInitialized() && anInNode->IsOneOfInLineNodes() ) {
531         if ( !Py_IsInitialized() ) {
532 //          PyEval_InitThreads() ;
533 //          Py_Initialize() ;
534           // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL  
535           //                  1 line below uncommented
536           PySys_SetArgv( _ArgC , _ArgV ) ;
537         }
538         anInNode->InitPython() ;
539         PyInitialized( true ) ;
540       }
541       anInNode->InitialState() ;
542       if ( anInNode->IsOneOfInLineNodes() ) {
543         //JR with Python2.3.4, we may get an error because of a normal previous error
544         bool sts = anInNode->InitPythonFunctions( false ) ;
545         if ( again && !sts ) {
546           sts = anInNode->InitPythonFunctions( false ) ;
547           again = false ;
548         }
549       }
550     }
551 // One more time because inline nodes may share one definition of the same function
552     for ( i = 0 ; i < Graph()->GraphNodesSize() ; i++ ) {
553       GraphExecutor::InNode * anInNode = (GraphExecutor::InNode *) Graph()->GraphNodes( i )->GetInNode() ;
554       if ( anInNode->IsOneOfInLineNodes() ) {
555         if ( !anInNode->InitPythonFunctions( true ) ) {
556           anInNode->State( GraphExecutor::ErroredState ) ;
557 // PAL 8515
558 //JR 24.03.2005 debug : State and done flags were missing
559           State( GraphExecutor::ErroredState ) ;
560           Done( true ) ;
561           cdebug_out << "GraphExecutor::OutNode::Run InitPythonFunctions ERROR "
562                      << anInNode->Name() << endl ;
563           RetVal = false ;
564         }
565       }
566     }
567     /* asv : 29.09.04 : commented out because it does not do anything useful 
568     for ( i = 0 ; i < Graph()->GraphNodesSize() ; i++ ) {
569       GraphExecutor::InNode * anInNode = (GraphExecutor::InNode *) Graph()->GraphNodes( i )->GetInNode() ;
570       if ( RetVal && anInNode->IsMacroNode() ) {
571         GraphBase::Graph * aMacroGraph = anInNode->GraphMacroNode() ;
572         cdebug << "GraphExecutor::OutNode::Run " << anInNode << " MacroNode " << aMacroGraph
573                << aMacroGraph->Name() << endl ;
574         SUPERV::Graph_var iobject = anInNode->GraphMacroNode()->MacroObject() ;
575         if ( CORBA::is_nil( iobject ) ) {
576           cdebug << "GraphExecutor::OutNode::Run MacroObject() is a nil object. Error" << endl ;
577           RetVal = false ;
578         }
579         else {
580           cdebug << "GraphExecutor::OutNode::Run MacroObject() is NOT a nil object." << endl ;
581         }
582       }
583     }
584     */
585     //JR Debug : if there was an error in initialisation of python functions,
586     //           we must not try to execute the graph.
587     if ( RetVal ) {
588     if ( Graph()->GraphMacroLevel() != 0 ) {
589       cdebug << "GraphExecutor::OutNode::Run Execution starting GraphExecutor::Action_DataOk_RunService Node "
590              << Name() << " GraphMacroLevel " << Graph()->GraphMacroLevel() << endl ;
591       PushEvent( NULL , GraphExecutor::NoDataReadyEvent ,
592                  GraphExecutor::DataUndefState ) ; 
593     }
594     else {
595       cdebug << "Execution starting GraphExecutor::Action_DataOk_RunService Node "
596              << Name() << " GraphMacroLevel " << Graph()->GraphMacroLevel() << endl ;
597       PushEvent( NULL , GraphExecutor::ReadyEvent ,
598                  GraphExecutor::DataReadyState ) ; 
599       State( GraphExecutor::DataReadyState ) ;
600
601       cdebug << pthread_self() << " GraphExecutor::OutNode::Run Start of the " << Graph()->HeadNodesSize()
602              << " HeadNodes" << endl ;
603       for ( i = 0 ; i < Graph()->HeadNodesSize() ; i++ ) {
604         GraphExecutor::InNode * anInNode = (GraphExecutor::InNode *) Graph()->HeadNodes( i )->GetInNode() ;
605         cdebug << pthread_self() <<  "GraphExecutor::OutNode::Run Start of HeadNode " << i << " "
606                << anInNode->Name() << endl ;
607         if ( anInNode->State() != GraphExecutor::DataReadyState ) {
608 // PAL 8515
609 //JR 24.03.2005 debug : State and done flags were missing
610           State( GraphExecutor::ErroredState ) ;
611           Done( true ) ;
612           cdebug << "GraphExecutor::OutNode::Run inconsistency State of Node "
613                  << anInNode->Name() << " : " << anInNode->State() << endl ;
614           cdebug_out << "GraphExecutor::OutNode::Run State ERROR" << endl ;
615           return false ;
616         }
617 //        PushEvent( anInNode , GraphExecutor::ReadyEvent ,
618 //                   GraphExecutor::DataReadyState ) ; 
619 // We say that we have to create a thread for that HeadNode 'anInNode'
620         if ( !anInNode->IsMacroNode() ) {
621           anInNode->CreateNewThread( true ) ;
622           IncrCreatedThreads() ;
623         }
624         anInNode->DataFromNode( Name() ) ;
625 // AndSuspend == true <==> Start()
626         if ( AndSuspend ) {
627           anInNode->State( GraphExecutor::DataWaitingState ) ;
628           anInNode->ControlState( SUPERV::ToSuspendStartState ) ;
629 // We send SomeDataReadyEvent
630 // It is a HeadNode ==> AllDataReadyEvent ==> InNode::executeAction() ==> pthread_create
631           if ( !anInNode->SendEvent( GraphExecutor::SomeDataReadyEvent ) ) {
632 // PAL 8515
633 //JR 24.03.2005 debug : State and done flags were missing
634             State( GraphExecutor::ErroredState ) ;
635             Done( true ) ;
636             cdebug << "InNode::SendEvent( SomeDataReadyEvent ) ERROR Node "
637                    << anInNode->Name() << endl ;
638             cdebug_out << "GraphExecutor::OutNode::Run SendEvent ERROR" << endl ;
639             return false ;
640           }
641 // ==> We wait for SuspendedState
642           anInNode->SuspendedWait() ;
643         }
644 // AndSuspend == false <==> Run()
645         else if ( anInNode->IsMacroNode() ) {
646           if ( !anInNode->SendEvent( GraphExecutor::SomeDataReadyEvent ) ) {
647 // PAL 8515
648 //JR 24.03.2005 debug : State and done flags were missing
649             State( GraphExecutor::ErroredState ) ;
650             Done( true ) ;
651             cdebug << "InNode::SendEvent( SomeDataReadyEvent ) ERROR Node "
652                    << anInNode->Name() << endl ;
653             cdebug_out << "GraphExecutor::OutNode::Run SendEvent ERROR" << endl ;
654             return false ;
655           }
656 //JR 15.04.2005 Debug PAL8624 RetroConception :
657           anInNode->CreateNewThread( true ) ;
658           IncrCreatedThreads() ;
659           if ( anInNode->HasAllDataReady() ) {
660             if ( !anInNode->SendEvent( GraphExecutor::AllDataReadyEvent ) ) { // ==> Ready to execute
661               cdebug <<  "GraphExecutor::OutNode::Run SendEvent(AllDataReadyEvent) ERROR "
662                      << " Node " << anInNode->Name() << endl ;
663               cdebug_out << "GraphExecutor::OutNode::Run SendEvent ERROR" << endl ;
664               return false ;
665             }
666           }
667           else {
668             State( GraphExecutor::ErroredState ) ;
669             Done( true ) ;
670             cdebug <<  "GraphExecutor::OutNode::Run HasAllDataReady " << anInNode->HasAllDataReady()
671                    << " Node " << anInNode->Name() << endl ;
672             cdebug_out << "GraphExecutor::OutNode::Run HasAllDataReady ERROR" << endl ;
673             return false ;
674           }
675           cdebug << pthread_self() << " GraphExecutor::OutNode::Run " << anInNode->Name() << "->RunningWait"
676                  << endl ;
677 // ==> We wait for RunningState
678           anInNode->RunningWait() ;
679           cdebug << pthread_self() << " GraphExecutor::OutNode::Run " << anInNode->Name() << "->RunningWaited"
680                  << endl ;
681         }
682         else if ( !anInNode->IsMacroNode() ) {
683 // It is a HeadNode ==> AllDataReadyEvent ==> InNode::executeAction() ==> pthread_create
684           if ( !anInNode->SendEvent( GraphExecutor::ExecuteEvent ) ) {
685 // PAL 8515
686 //JR 24.03.2005 debug : State and done flags were missing
687             State( GraphExecutor::ErroredState ) ;
688             Done( true ) ;
689             cdebug << "InNode::SendEvent( ExecuteEvent ) ERROR Node "
690                    << anInNode->Name() << endl ;
691             cdebug_out << "GraphExecutor::OutNode::Run SendEvent ERROR" << endl ;
692             return false ;
693           }
694 // ==> We wait for RunningState
695           anInNode->RunningWait() ;
696         }
697       }
698
699       if ( AndSuspend ) {
700         PushEvent( NULL , GraphExecutor::SuspendedReadyEvent ,
701                    GraphExecutor::SuspendedReadyState ) ; 
702       }
703       else {
704         PushEvent( NULL , GraphExecutor::ExecutingEvent ,
705                    GraphExecutor::ExecutingState ) ; 
706         if (AutomatonState() == GraphExecutor::DataReadyState) {
707             State( GraphExecutor::ExecutingState ) ;
708         };
709       }
710 //      ostringstream astr ;
711 //      astr << "Graph " << Name() << " is running" ;
712 //      Graph()->ObjImpl()->sendMessage( NOTIF_STEP, astr.str().c_str() ) ;
713       RetVal = true ;
714     }
715     //JR Debug
716     }
717     else {
718       State( GraphExecutor::ErroredState ) ;
719       Done( true ) ;
720       if ( IsDone() && Graph()->GraphMacroLevel() == 0 ) {
721         cdebug << " ==> theAutomaton->Executed() " << endl ;
722         theAutomaton->Executed() ;
723       }
724       else {
725         cdebug << " NO theAutomaton->Executed() " << endl ;
726       }
727     }
728   }
729   else {
730 // PAL 8515
731 //JR 24.03.2005 debug : State and done flags were missing
732     State( GraphExecutor::ErroredState ) ;
733     Done( true ) ;
734 //JR 17.06.2005 debug call to executed was missing ===>
735 //              _GraphExecutingNumber field of FiniteStateMachine was not decremented ===>
736 //              register of python functions was de-activated ==>
737 //              two graphs execution with two different python functions with the same name
738 //              give aborted state for the second one
739 //Example : GraphLoopSwitchOfSwitchNOTValid, GraphLoopSwitchs and GraphLoopSwitchsBranches
740     if ( IsDone() && Graph()->GraphMacroLevel() == 0 ) {
741       cdebug << " ==> theAutomaton->Executed() " << endl ;
742       theAutomaton->Executed() ;
743     }
744     else {
745       cdebug << " NO theAutomaton->Executed() " << endl ;
746     }
747     cdebug_out << "GraphExecutor::OutNode::Run Not Executable ERROR " << endl ;
748     return false ;
749   }
750
751   cdebug_out << pthread_self() << "GraphExecutor::OutNode::Run " << Name()
752              << " State " << theAutomaton->StateName( State() ) << " GraphMacroLevel "
753              << Graph()->GraphMacroLevel() << " RetVal " << RetVal << endl ;
754   return RetVal ;
755 }
756
757 void GraphExecutor::OutNode::CheckAllDone() {
758   int j ;
759   cdebug_in << "GraphExecutor::OutNode::CheckAllDone " << endl;
760   GraphExecutor::AutomatonState OutNodeState = GraphExecutor::SuccessedState ;
761   GraphExecutor::AutomatonState InNodeState ;
762   bool AllDone = true ;
763   if ( !Done() ) {
764     for ( j = 0 ; j < Graph()->QueueNodesSize() ; j++ ) {
765       InNodeState = ( (GraphExecutor::InNode * ) Graph()->QueueNodes( j )->GetInNode() )->State() ;
766       cdebug << j << ". "
767              << ( (GraphExecutor::InNode * ) Graph()->QueueNodes( j )->GetInNode() )->Name()
768              << " " << theAutomaton->StateName( InNodeState ) << endl ;
769       if ( InNodeState != GraphExecutor::SuccessedState &&
770            InNodeState != GraphExecutor::ErroredState &&
771            InNodeState != GraphExecutor::DataWaitingState ) {
772         if ( !IsNodeAborted() ) {
773           AllDone = false ;
774         }
775       }
776       if ( InNodeState != GraphExecutor::SuccessedState &&
777            InNodeState != GraphExecutor::DataWaitingState &&
778            InNodeState != GraphExecutor::DataReadyState ) {
779         OutNodeState = InNodeState ;
780       }
781     }
782     if ( AllDone ) {
783       if( _Threads == 0 && _SuspendedThreads == 0 ) {
784         if ( OutNodeState != GraphExecutor::ErroredState ) {
785           OutNodeState = GraphExecutor::SuccessedState ;
786         }
787       }
788       else {
789         AllDone = false ;
790       }
791     }
792     if ( AllDone ) {
793       int alivenodes = 0 ;
794       for ( j = 0 ; j < Graph()->GraphNodesSize()  ; j++ ) {
795         GraphExecutor::InNode * aNode ;
796         aNode = (GraphExecutor::InNode * ) Graph()->GraphNodes( j )->GetInNode() ;
797         SUPERV::GraphState aState = AutomatonGraphState( aNode->State() ) ;
798         cdebug << "GraphExecutor::OutNode::CheckAllDone " << aNode->Name() << " "
799                << theAutomaton->StateName( aNode->State() ) << " CreateNewThread " << aNode->CreateNewThread()
800                << endl ;
801         if ( aState == SUPERV::ErrorState ||
802              aState == SUPERV::SuspendErroredState ||
803              aState == SUPERV::KillState ||
804              aState == SUPERV::StopState ) {
805           OutNodeState = aNode->State() ;
806           State( OutNodeState ) ;
807         }
808         else if ( aState == SUPERV::ReadyState ||
809                   aState == SUPERV::SuspendReadyState ||
810                   aState == SUPERV::RunningState ||
811                   aState == SUPERV::SuspendDoneState ||
812                   aState == SUPERV::SuspendErroredState ||
813                   aState == SUPERV::SuspendState ) {
814           if ( !IsNodeAborted() ) {
815             alivenodes += 1 ;
816           }
817         }
818         aNode->SuspendedAction() ;
819         aNode->DoneAction() ;
820       }
821 // PushEvent AFTER State and _Done ! ...
822       if ( alivenodes == 0 ) {
823         State( OutNodeState ) ;
824         Done( true ) ;
825         _JustStarted = false ;
826       }
827       cdebug << Name() << " alivenodes " << alivenodes << " OutNodeState " << OutNodeState
828              << endl ;
829       PushEvent( NULL , GraphExecutor::EndExecuteEvent ,
830                  OutNodeState ) ;
831 //      Py_Finalize() ;
832 //      PyInitialized( false ) ;
833     }
834   }
835   if ( IsDone() ) {
836     MESSAGE("================================================================================") ;
837     MESSAGE( Name() << " IS DONE : " <<  theAutomaton->StateName( AutomatonState() ) << " EventQSize "
838              << EventQSize() ) ;
839     MESSAGE("================================================================================") ;
840     cdebug << "================================================================================" << endl ;
841     cdebug << Name() << " IS DONE : " <<  theAutomaton->StateName( AutomatonState() ) << " EventQSize "
842              << EventQSize() << endl  ;
843     cdebug << "================================================================================" << endl ;
844
845     if ( Graph()->GraphMacroLevel() != 0 ) {
846 //JR NPAL14110 09.02.2007 : That must be done only at the end of SubGraph because we may
847 //                          have an output of the SubGraph in a Loop !...
848 //                          So that code must be done in CheckAllDone instead of OutParametersSet
849       cdebug << "CheckAllDone ToNodeName " << Name() << " CoupledNode "
850              << Graph()->CoupledNodeName() << Graph()->CoupledNode()
851              << endl ;
852       cdebug << "CheckAllDone GraphExecutor " << Graph()->CoupledNode()->GraphEditor()->Executor() << endl ;
853       const GraphBase::InPort * anInPort ;
854       GraphBase::OutPort * anOutPort ;
855       int i ;
856       for ( i = 0 ; i <  Graph()->GetNodeOutDataNodePortsSize() ; i++ ) {
857          anInPort = Graph()->GetNodeOutDataNodePort( i ) ;
858          if ( !anInPort->IsGate() ) {
859            anOutPort = anInPort->GetOutPort() ;
860            Graph()->CoupledNode()->GraphEditor()->Executor()->OutputOfAny( Graph()->CoupledNodeName() ,
861                                                                            anInPort->PortName() ,
862                                                                            anOutPort->Value() ) ;
863            cdebug << "CheckAllDone OutputOfAny( " << Graph()->CoupledNodeName() << " , "
864                   << anInPort->PortName() << " , value ) done" << endl ;
865          }
866       }
867     }
868
869 //PAL8520
870 //JR 23.02.2005, Debug : siftEvents() must be inside the pthread_mutex_lock
871     if ( pthread_mutex_lock( &_MutexWait ) ) {
872       perror("EventLoop pthread_mutex_lock ") ;
873       exit( 0 ) ;
874     }
875     // asv : fix for 6798 and other bugs: "sift" the event queue after execution
876     siftEvents();
877     if ( pthread_mutex_unlock( &_MutexWait ) ) {
878       perror("PushEvent pthread_mutex_unlock ") ;
879       exit( 0 ) ;
880     }
881
882     // asv : 25.10.04 : calling Editing() to indicate stop of execution
883     Graph()->GraphEditor()->Editing();
884   }
885   cdebug_out << "GraphExecutor::OutNode::CheckAllDone " << IsDone() << " this " << this << " "
886              << Name() << " GraphAutomatonState "
887              << theAutomaton->StateName( AutomatonState() )
888              << " State " << State() << " Threads " << _Threads << " SuspendedThreads "
889              << _SuspendedThreads << " EventQSize " << EventQSize() << endl ;
890 }
891
892 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
893 //Engines_Container_i * GraphExecutor::OutNode::SuperVisionContainer() {
894 //  return _SuperVisionContainer ;
895 //}
896
897 //const pthread_t GraphExecutor::OutNode::MainThreadId() const {
898 //  return _MainThreadId ;
899 //}
900
901 void GraphExecutor::OutNode::PThreadLock( pthread_mutex_t * aMutex , char * errmsg ) {
902 //  if ( strcmp( errmsg , "EventLoop" ) && strcmp( errmsg , "EventW" ) ) {
903 //    cdebug << "GraphExecutor::OutNode::PThreadLock " << pthread_self() << " " << aMutex << " "
904 //           << errmsg << endl ;
905 //  }
906   if ( pthread_mutex_lock( aMutex ) ) {
907     perror( errmsg ) ;
908     exit( 0 ) ;
909   }
910 //  if ( strcmp( errmsg , "EventLoop" ) && strcmp( errmsg , "EventW" ) ) {
911 //    cdebug << "GraphExecutor::OutNode::PThreadLocked " << pthread_self() << " " << aMutex << " "
912 //           << errmsg << endl ;
913 //  }
914 }
915
916 void GraphExecutor::OutNode::PThreadUnLock( pthread_mutex_t * aMutex , char * errmsg ) {
917 //  if ( strcmp( errmsg , "EventLoop" ) && strcmp( errmsg , "EventW" ) ) {
918 //    cdebug << " GraphExecutor::OutNode::PThreadUnLock " << pthread_self() << " " << aMutex << " "
919 //           << errmsg << endl ;
920 //  }
921   if ( pthread_mutex_unlock( aMutex ) ) {
922     perror( errmsg ) ;
923     exit( 0 ) ;
924   }
925 }
926
927 void GraphExecutor::OutNode::PyThreadLock() {
928 //  cout << " GraphExecutor::OutNode::PyThreadLock " << pthread_self() << endl ;
929   if ( pthread_mutex_lock( &_PyMutexWait ) ) {
930     perror( "GraphExecutor::OutNode::PyThreadLock" ) ;
931     exit( 0 ) ;
932   }
933   theAutomaton->PyLock() ;
934 //  cout << " GraphExecutor::OutNode::PyThreadLocked " << pthread_self() << endl ;
935 }
936
937 void GraphExecutor::OutNode::PyThreadUnLock() {
938 //  cout << " GraphExecutor::OutNode::PyThreadUnLock " << pthread_self() << endl ;
939   if ( pthread_mutex_unlock( &_PyMutexWait ) ) {
940     perror( "GraphExecutor::OutNode::PyThreadUnLock" ) ;
941     exit( 0 ) ;
942   }
943   theAutomaton->PyUnLock() ;
944 //  cout << " GraphExecutor::OutNode::PyThreadUnLocked " << pthread_self() << endl ;
945 }
946
947 void GraphExecutor::OutNode::NewThread() {
948   if ( pthread_mutex_lock( &_MutexWait ) ) {
949     perror("pthread_mutex_lock _NewThread") ;
950     exit( 0 ) ;
951   }
952   _Threads += 1 ;
953 //  cdebug << "NewThread : " << _Threads << " running threads "
954 //         << _SuspendedThreads << " suspended threads"
955 //         << endl ;
956   if ( pthread_mutex_unlock( &_MutexWait ) ) {
957     perror("pthread_mutex_unlock _NewThread") ;
958     exit( 0 ) ;
959   }
960 }
961
962 void GraphExecutor::OutNode::ExitThread( pthread_t ThreadNumber ) {
963   if ( pthread_mutex_lock( &_MutexWait ) ) {
964     perror("pthread_mutex_lock _ExitThread") ;
965     exit( 0 ) ;
966   }
967   if ( ThreadNumber == 0 ) {
968     ThreadNumber = pthread_self() ;
969   }
970   _Threads -= 1 ;
971 //  cdebug << pthread_self() << " ExitThread( " << ThreadNumber << " ) " << _Threads << " running threads "
972 //         << _SuspendedThreads << " suspended threads"
973 //         << endl ;
974   theAutomaton->JoinThread( ThreadNumber ) ;
975   if ( pthread_cond_signal( &_JoinWait ) ) {
976     perror("ExitThread pthread_cond_signal ") ;
977   }
978   if ( pthread_mutex_unlock( &_MutexWait ) ) {
979     perror("pthread_mutex_unlock _ExitThread") ;
980     exit( 0 ) ;
981   }
982   if ( _Threads == 0 && _SuspendedThreads == 0 ) {
983     CheckAllDone() ;
984     cdebug << pthread_self() << " ExitThread( " << ThreadNumber << " ) " << _Threads
985            << " running threads " << _SuspendedThreads << " suspended threads IsDone() "
986            << IsDone() ;
987     _run_func_thread = 0 ; // mkr : IPAL14711
988     if ( IsDone() && Graph()->GraphMacroLevel() == 0 ) {
989       cdebug << " ==> theAutomaton->Executed() " << endl ;
990       theAutomaton->Executed() ;
991     }
992     else {
993       cdebug << " NO theAutomaton->Executed() " << endl ;
994     }
995   }
996 }
997 void GraphExecutor::OutNode::JoinedWait() {
998   if ( pthread_mutex_lock( &_MutexWait ) ) {
999     perror("pthread_mutex_lock JoinedWait") ;
1000     exit( 0 ) ;
1001   }
1002   while ( _Threads ) {
1003     if ( pthread_cond_wait( &_JoinWait , &_MutexWait ) ) {
1004       perror("JoinedWait pthread_cond_wait ") ;
1005     }
1006   }
1007   if ( pthread_mutex_unlock( &_MutexWait ) ) {
1008     perror("pthread_mutex_unlock JoinedWait") ;
1009     exit( 0 ) ;
1010   }
1011 }
1012
1013 void GraphExecutor::OutNode::SuspendThread() {
1014   if ( pthread_mutex_lock( &_MutexWait ) ) {
1015     perror("pthread_mutex_lock _SuspendThread") ;
1016     exit( 0 ) ;
1017   }
1018   _SuspendedThreads += 1 ;
1019   cdebug << "SuspendThread : " << _Threads << " running threads "
1020          << _SuspendedThreads << " suspended threads"
1021          << endl ;
1022   if ( pthread_mutex_unlock( &_MutexWait ) ) {
1023     perror("pthread_mutex_unlock _SuspendThread") ;
1024     exit( 0 ) ;
1025   }
1026   if ( IsSuspended() ) {
1027     PushEvent( NULL , GraphExecutor::SuspendEvent , GraphExecutor::SuspendedState ) ;
1028   }
1029 }
1030 void GraphExecutor::OutNode::ResumeThread() {
1031   if ( pthread_mutex_lock( &_MutexWait ) ) {
1032     perror("pthread_mutex_lock _ResumeThread") ;
1033     exit( 0 ) ;
1034   }
1035   _SuspendedThreads -= 1 ;
1036   cdebug << "ResumeThread : " << _Threads << " running threads "
1037          << _SuspendedThreads << " suspended threads"
1038          << endl ;
1039   if ( pthread_mutex_unlock( &_MutexWait ) ) {
1040     perror("pthread_mutex_unlock _ResumeThread") ;
1041     exit( 0 ) ;
1042   }
1043 }
1044
1045 long GraphExecutor::OutNode::Thread( const char * aNodeName ) {
1046   long RetVal = 0 ;
1047   GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode() ;
1048   if ( anInNode ) {
1049     RetVal = anInNode->ThreadNo() ;
1050   }
1051   return RetVal ;
1052 }
1053
1054 SUPERV::GraphEvent GraphExecutor::OutNode::AutomatonGraphEvent(GraphExecutor::NodeEvent anEvent ) {
1055   SUPERV::GraphEvent aGraphEvent ;
1056   switch ( anEvent ) {
1057   case GraphExecutor::UndefinedEvent : {
1058     aGraphEvent = SUPERV::UndefinedEvent ;
1059     break ;
1060   }
1061   case GraphExecutor::NewThreadEvent : {
1062     aGraphEvent = SUPERV::NewThreadEvent ;
1063     break ;
1064   }
1065   case GraphExecutor::SuspendEvent : {
1066     aGraphEvent = SUPERV::SuspendEvent ;
1067     break ;
1068   }
1069   case GraphExecutor::ResumeEvent : {
1070     aGraphEvent = SUPERV::ResumeEvent ;
1071     break ;
1072   }
1073   case GraphExecutor::KillEvent : {
1074     aGraphEvent = SUPERV::KillEvent ;
1075     break ;
1076   }
1077   case GraphExecutor::StopEvent : {
1078     aGraphEvent = SUPERV::StopEvent ;
1079     break ;
1080   }
1081   case GraphExecutor::ExecuteEvent : {
1082     aGraphEvent = SUPERV::RunningEvent ;
1083     break ;
1084   }
1085   case GraphExecutor::SuccessEvent : {
1086     aGraphEvent = SUPERV::DoneEvent ;
1087     break ;
1088   }
1089   case GraphExecutor::ErrorEvent : {
1090     aGraphEvent = SUPERV::ErroredEvent ;
1091     break ;
1092   }
1093   case GraphExecutor::NoDataReadyEvent : {
1094     aGraphEvent = SUPERV::WaitingEvent ;
1095     break ;
1096   }
1097   case GraphExecutor::SomeDataReadyEvent : {
1098     aGraphEvent = SUPERV::WaitingEvent ;
1099     break ;
1100   }
1101   case GraphExecutor::NotAllDataReadyEvent : {
1102     aGraphEvent = SUPERV::WaitingEvent ;
1103     break ;
1104   }
1105   case GraphExecutor::AllDataReadyEvent : {
1106     aGraphEvent = SUPERV::ReadyEvent ;
1107     break ;
1108   }
1109   case GraphExecutor::ReadyEvent : {
1110     aGraphEvent = SUPERV::ReadyEvent ;
1111     break ;
1112   }
1113   case GraphExecutor::SuspendedReadyEvent : {
1114     aGraphEvent = SUPERV::SuspendEvent ;
1115     break ;
1116   }
1117   case GraphExecutor::ResumedReadyEvent : {
1118     aGraphEvent = SUPERV::ResumeEvent ;
1119     break ;
1120   }
1121   case GraphExecutor::KilledReadyEvent : {
1122     aGraphEvent = SUPERV::KillEvent ;
1123     break ;
1124   }
1125   case GraphExecutor::StoppedReadyEvent : {
1126     aGraphEvent = SUPERV::StopEvent ;
1127     break ;
1128   }
1129   case GraphExecutor::ExecutingEvent : {
1130     aGraphEvent = SUPERV::RunningEvent ;
1131     break ;
1132   }
1133   case GraphExecutor::SuspendedExecutingEvent : {
1134     aGraphEvent = SUPERV::SuspendEvent ;
1135     break ;
1136   }
1137   case GraphExecutor::ResumedExecutingEvent : {
1138     aGraphEvent = SUPERV::ResumeEvent ;
1139     break ;
1140   }
1141   case GraphExecutor::KilledExecutingEvent : {
1142     aGraphEvent = SUPERV::KillEvent ;
1143     break ;
1144   }
1145   case GraphExecutor::StoppedExecutingEvent : {
1146     aGraphEvent = SUPERV::StopEvent ;
1147     break ;
1148   }
1149   case GraphExecutor::SuccessedExecutingEvent : {
1150     aGraphEvent = SUPERV::DoneEvent ;
1151     break ;
1152   }
1153   case GraphExecutor::ErroredExecutingEvent : {
1154     aGraphEvent = SUPERV:: ErroredEvent;
1155     break ;
1156   }
1157   case GraphExecutor::SuspendedSuccessedEvent : {
1158     aGraphEvent = SUPERV::SuspendEvent ;
1159     break ;
1160   }
1161   case GraphExecutor::SuspendedErroredEvent : {
1162     aGraphEvent = SUPERV::SuspendEvent ;
1163     break ;
1164   }
1165   case GraphExecutor::ResumedSuccessedEvent : {
1166     aGraphEvent = SUPERV::ResumeEvent ;
1167     break ;
1168   }
1169   case GraphExecutor::ResumedErroredEvent : {
1170     aGraphEvent = SUPERV::ResumeEvent ;
1171     break ;
1172   }
1173   case GraphExecutor::KilledEvent : {
1174     aGraphEvent = SUPERV::KillEvent ;
1175     break ;
1176   }
1177   case GraphExecutor::StoppedEvent : {
1178     aGraphEvent = SUPERV::StopEvent ;
1179     break ;
1180   }
1181   case GraphExecutor::EndExecuteEvent : {
1182     aGraphEvent = SUPERV::DoneEvent ;
1183     break ;
1184   }
1185   default : {
1186     cdebug << " GraphExecutor::OutNode::AutomatonGraphEvent Error Undefined Event : "
1187            << anEvent << endl ;
1188     aGraphEvent = SUPERV::UndefinedEvent ;
1189   }
1190   }
1191   return aGraphEvent ;
1192 }
1193
1194 SUPERV::GraphState GraphExecutor::OutNode::AutomatonGraphState(GraphExecutor::AutomatonState aState ) {
1195   SUPERV::GraphState aGraphState ;
1196   switch ( aState ) {
1197   case GraphExecutor::UnKnownState : {
1198     aGraphState = SUPERV::UndefinedState ;
1199     break ;
1200   }
1201   case GraphExecutor::DataUndefState : {
1202     aGraphState = SUPERV::UndefinedState ;
1203     break ;
1204   }
1205   case GraphExecutor::DataWaitingState : {
1206     aGraphState = SUPERV::WaitingState ;
1207     break ;
1208   }
1209   case GraphExecutor::DataReadyState : {
1210     aGraphState = SUPERV::ReadyState ;
1211     break ;
1212   }
1213   case GraphExecutor::SuspendedReadyState : {
1214     aGraphState = SUPERV::SuspendReadyState ;
1215     break ;
1216   }
1217   case GraphExecutor::ResumedReadyState : {
1218     aGraphState = SUPERV::ReadyState ;
1219     break ;
1220   }
1221   case GraphExecutor::KilledReadyState : {
1222     aGraphState = SUPERV::KillState ;
1223     break ;
1224   }
1225   case GraphExecutor::StoppedReadyState : {
1226     aGraphState = SUPERV::StopState ;
1227     break ;
1228   }
1229   case GraphExecutor::ExecutingState : {
1230     aGraphState = SUPERV::RunningState ;
1231     break ;
1232   }
1233   case GraphExecutor::SuspendedExecutingState : {
1234     aGraphState = SUPERV::SuspendState ;
1235     break ;
1236   }
1237   case GraphExecutor::ResumedExecutingState : {
1238     aGraphState = SUPERV::RunningState ;
1239     break ;
1240   }
1241   case GraphExecutor::KilledExecutingState : {
1242     aGraphState = SUPERV::KillState ;
1243     break ;
1244   }
1245   case GraphExecutor::StoppedExecutingState : {
1246     aGraphState = SUPERV::StopState ;
1247     break ;
1248   }
1249   case GraphExecutor::SuccessedExecutingState : {
1250     aGraphState = SUPERV::DoneState ;
1251     break ;
1252   }
1253   case GraphExecutor::ErroredExecutingState : {
1254     aGraphState = SUPERV::ErrorState ;
1255     break ;
1256   }
1257   case GraphExecutor::SuspendedSuccessedState : {
1258     aGraphState = SUPERV::SuspendDoneState ;
1259     break ;
1260   }
1261   case GraphExecutor::SuspendedErroredState : {
1262     aGraphState = SUPERV::SuspendErroredState ;
1263     break ;
1264   }
1265   case GraphExecutor::ResumedSuccessedState : {
1266     aGraphState = SUPERV::DoneState ;
1267     break ;
1268   }
1269   case GraphExecutor::ResumedErroredState : {
1270     aGraphState = SUPERV::ErrorState ;
1271     break ;
1272   }
1273   case GraphExecutor::KilledSuccessedState : {
1274     aGraphState = SUPERV::KillState ;
1275     break ;
1276   }
1277   case GraphExecutor::KilledErroredState : {
1278     aGraphState = SUPERV::KillState ;
1279     break ;
1280   }
1281   case GraphExecutor::StoppedSuccessedState : {
1282     aGraphState = SUPERV::StopState ;
1283     break ;
1284   }
1285   case GraphExecutor::StoppedErroredState : {
1286     aGraphState = SUPERV::StopState ;
1287     break ;
1288   }
1289   case GraphExecutor::SuccessedState : {
1290     aGraphState = SUPERV::DoneState ;
1291     break ;
1292   }
1293   case GraphExecutor::ErroredState : {
1294     aGraphState = SUPERV::ErrorState ;
1295     break ;
1296   }
1297   case GraphExecutor::SuspendedState : {
1298     aGraphState = SUPERV::SuspendState ;
1299     break ;
1300   }
1301   case GraphExecutor::KilledState : {
1302     aGraphState = SUPERV::KillState ;
1303     break ;
1304   }
1305   case GraphExecutor::StoppedState : {
1306     aGraphState = SUPERV::StopState ;
1307     break ;
1308   }
1309   case GraphExecutor::LoadingState : {
1310     aGraphState = SUPERV::LoadingState ;
1311     break;
1312   }
1313   default : {
1314     cdebug << " GraphExecutor::OutNode::AutomatonGraphState Error Undefined State : "
1315            << aGraphState << endl ;
1316     aGraphState = SUPERV::UndefinedState ;
1317   }
1318   }
1319   return aGraphState ;
1320 }
1321
1322 //JR 07.04.2005, Debug :  access to _EventNodes must be inside the pthread_mutex_lock
1323 int GraphExecutor::OutNode::GetListSize() {
1324   if ( pthread_mutex_lock( &_MutexWait ) ) {
1325     perror("PushEvent pthread_mutex_lock ") ;
1326     exit( 0 ) ;
1327   }
1328   int eventnbr = _EventNodes.size();
1329   if ( pthread_mutex_unlock( &_MutexWait ) ) {
1330     perror("PushEvent pthread_mutex_unlock ") ;
1331     exit( 0 ) ;
1332   }
1333   return eventnbr ;
1334 }
1335
1336 bool GraphExecutor::OutNode::PushEvent( GraphExecutor::InNode * aNode ,
1337                                         GraphExecutor::NodeEvent anEvent ,
1338                                         GraphExecutor::AutomatonState aState ) {
1339 //  cdebug_in << "PushEvent Threads " << Threads() << " SuspendedThreads "
1340 //            << SuspendedThreads() << endl ;
1341   if ( pthread_mutex_lock( &_MutexWait ) ) {
1342     perror("PushEvent pthread_mutex_lock ") ;
1343     exit( 0 ) ;
1344   }
1345   char* aNodeName = aNode ? aNode->Name() : Name() ;
1346   _EventNodes.push_back( aNodeName ) ;
1347   _Events.push_back( anEvent ) ;
1348   _States.push_back( aState ) ;
1349
1350   pthread_cond_broadcast( &_EventWait );
1351 //  cdebug << pthread_self() << " PushEvent Threads " << Threads() << " SuspendedThreads "
1352 //         << SuspendedThreads() << " pthread_cond_broadcast _EventWait " << &_EventWait << endl ;
1353   if ( aNode ) {
1354 //    cdebug << aNode->ThreadNo() << " PushEvent " << aNode->Name() ;
1355 //    cdebug << " " << aNode->Automaton()->EventName( anEvent )
1356 //           << " " << aNode->Automaton()->StateName( aState )
1357 //           << " ControleState "
1358 //           << aNode->Automaton()->ControlStateName( aNode->ControlState() ) ;
1359   }
1360   else {
1361 //    cdebug << "PushEvent " << Name() ;
1362 //    cdebug << " " << theAutomaton->EventName( anEvent ) << " "
1363 //           << theAutomaton->StateName( aState ) ;
1364   }
1365 //  cdebug_out << "PushEvent Threads " << Threads() << " SuspendedThreads "
1366 //             << SuspendedThreads() << endl ;
1367
1368 //PAL8520
1369 //JR 21.02.2005 Debug : old events should be removed at creation time for python-clients
1370   int EventNodesSize = _EventNodes.size() ;
1371   if ( EventNodesSize > Graph()->GraphNodesSize()*70 ) {
1372     siftEvents();
1373 //    cdebug << "OutNode::PushEvent " << _EventNodes.size() << " in queue instead of "
1374 //           << EventNodesSize << endl ;
1375   }
1376
1377   if ( pthread_mutex_unlock( &_MutexWait ) ) {
1378     perror("PushEvent pthread_mutex_unlock ") ;
1379     exit( 0 ) ;
1380   }
1381   return true ;
1382 }
1383
1384 bool GraphExecutor::OutNode::StateWait( SUPERV::GraphState aState ) {
1385   return false ;
1386 }
1387
1388 bool GraphExecutor::OutNode::Event( char ** aNodeName ,
1389                                     SUPERV::GraphEvent & anEvent ,
1390                                     SUPERV::GraphState & aState ,
1391                                     bool WithWait ) {
1392 //PAL8520
1393 //JR 23.02.2005, Debug : siftEvents() must be inside the pthread_mutex_lock
1394   if ( pthread_mutex_lock( &_MutexWait ) ) {
1395     perror("EventLoop pthread_mutex_lock ") ;
1396     exit( 0 ) ;
1397   }
1398   // asv : fixing problem of loops with large number of iterations (>5000)
1399   // experimentally was found that number of non-handled by GUI events = (number_of_nodes x 7)
1400   // or 7 events for each node - so what we want to do here is remove "old" events for nodes
1401   // "on the fly".  Once again it is done in Stop(), Kill(), Done() functions.
1402   int EventNodessize = _EventNodes.size() ;
1403   if ( EventNodessize > Graph()->GraphNodesSize()*7 )
1404     siftEvents();
1405
1406   int ThreadsNumber ;
1407   int SuspendedThreadsNumber ;
1408   bool cdebuginout = false ;
1409   if ( _EventNodes.size() > 0 ) {
1410 //    cdebug_in << "GraphExecutor::OutNode::Event " << _EventNodes.size() << " in queue instead of "
1411 //              << EventNodessize << endl ;
1412     cdebuginout = true ;
1413   }
1414   _JustStarted = false ;
1415   ThreadsNumber = Threads() ;
1416   SuspendedThreadsNumber = SuspendedThreads() ;
1417   bool RetVal = ( ThreadsNumber - SuspendedThreadsNumber) != 0 ||
1418                 _EventNodes.size() > 0 ;
1419   char * NodeName = "" ;
1420   GraphExecutor::NodeEvent theEvent = GraphExecutor::UndefinedEvent ;
1421   GraphExecutor::AutomatonState theState = GraphExecutor::UnKnownState ;
1422   anEvent = SUPERV::UndefinedEvent ;
1423   aState = SUPERV::UndefinedState ;
1424   if ( ( Done() || IsKilled() || IsStopped() ) && _EventNodes.size() == 0 ) {
1425 //    cdebug << "EventLoop Done()/IsKilled()/IsStopped() && _EventNodes.size() == 0" << endl ;
1426     RetVal = false ;
1427   }
1428   else if ( !WithWait && _EventNodes.size() == 0 ) {
1429     anEvent = SUPERV::NoEvent ;
1430     aState = SUPERV::NoState ;
1431     RetVal = true ;
1432   }
1433   else if ( RetVal ) {
1434     while ( !IsSuspended() && _EventNodes.size() == 0 ) {
1435 //      cdebug << "EventLoop pthread_cond_wait _EventWait" << endl ;
1436       pthread_cond_wait( &_EventWait , &_MutexWait );
1437 //      cdebug << "EventLoop pthread_cond_waited _EventWait"
1438 //             << " _EventNodes.size() " << _EventNodes.size() << endl ;
1439     }
1440     if ( _EventNodes.size() ) {
1441       ThreadsNumber = Threads() ;
1442       NodeName = _EventNodes.front() ;
1443       _EventNodes.pop_front() ;
1444       theEvent = _Events.front() ;
1445       anEvent = AutomatonGraphEvent( theEvent ) ;
1446       _Events.pop_front() ;
1447       theState = _States.front() ;
1448       aState = AutomatonGraphState( theState ) ;
1449       _States.pop_front() ;
1450     }
1451   }
1452   *aNodeName = NodeName ;
1453   if ( IsSuspended() && _EventNodes.size() == 0 ) {
1454     RetVal = false ;
1455   }
1456   if ( anEvent != SUPERV::NoEvent ) {
1457 //    cdebug << pthread_self() << "EventLoop "
1458 //           << NodeName << " " << theAutomaton->StateName( theState )
1459 //           << " _EventNodes.size() " << _EventNodes.size()
1460 //           << " Threads " << Threads() << " SuspendedThreads "
1461 //           << SuspendedThreads() << " RetVal " << RetVal << endl ;
1462   }
1463   if ( pthread_mutex_unlock( &_MutexWait ) ) {
1464     perror("EventLoop pthread_mutex_lock ") ;
1465     exit( 0 ) ;
1466   }
1467   if ( cdebuginout ) {
1468 //    cdebug_out << "GraphExecutor::OutNode::Event " << _EventNodes.size() << " in queue"
1469 //               << *aNodeName << " " << anEvent << " " << aState << endl ;
1470   }
1471   return RetVal ;
1472 }
1473
1474 bool GraphExecutor::OutNode::EventW( char ** aNodeName ,
1475                                      SUPERV::GraphEvent & anEvent ,
1476                                      SUPERV::GraphState & aState ) {
1477   bool sts = true ;
1478   char * NodeName ;
1479   aState = SUPERV::UndefinedState ;
1480   while ( sts &&
1481           aState != SUPERV::SuspendReadyState &&
1482           aState != SUPERV::RunningState &&
1483           aState != SUPERV::SuspendDoneState &&
1484           aState != SUPERV::SuspendErroredState ) {
1485     NodeName = Name() ;
1486     while ( sts && !strcmp( NodeName , Name() ) ) {
1487       sts = EventWait( aNodeName , anEvent , aState ) ;
1488       NodeName = *aNodeName ;
1489     }
1490   }
1491   return sts ;
1492 }
1493
1494 bool GraphExecutor::OutNode::EventWait( char ** aNodeName ,
1495                                         SUPERV::GraphEvent & anEvent ,
1496                                         SUPERV::GraphState & aState ) {
1497   if ( pthread_mutex_lock( &_MutexWait ) ) {
1498     perror("EventW pthread_mutex_lock ") ;
1499     exit( 0 ) ;
1500   }
1501   int ThreadsNumber ;
1502   int SuspendedThreadsNumber ;
1503   ThreadsNumber = Threads() ;
1504   SuspendedThreadsNumber = SuspendedThreads() ;
1505   bool RetVal = ( ThreadsNumber - SuspendedThreadsNumber) != 0 ||
1506                 _EventNodes.size() > 0 ;
1507   cdebug << "--> EventW RetVal " << RetVal << endl ;
1508   char * NodeName = "" ;
1509   GraphExecutor::NodeEvent theEvent = GraphExecutor::UndefinedEvent ;
1510   GraphExecutor::AutomatonState theState = GraphExecutor::UnKnownState ;
1511   anEvent = SUPERV::UndefinedEvent ;
1512   aState = SUPERV::UndefinedState ;
1513   if ( IsDone() && _EventNodes.size() == 0 ) {
1514     cdebug << "EventW IsDone() && _EventNodes.size() == 0" << endl ;
1515     RetVal = 0 ;
1516   }
1517   else if ( RetVal ) {
1518     GraphExecutor::InNode * aNode = NULL ;
1519     while ( aNode == NULL && RetVal ) {
1520       NodeName = _EventNodes.front() ;
1521       theEvent = _Events.front() ;
1522       anEvent = AutomatonGraphEvent( theEvent ) ;
1523       theState = _States.front() ;
1524       aState = AutomatonGraphState( theState ) ;
1525
1526       if ( _JustStarted ) {
1527         _JustStarted = false ;
1528       }
1529       else {
1530         _EventNodes.pop_front() ;
1531         _Events.pop_front() ;
1532         _States.pop_front() ;
1533       }
1534
1535       aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( NodeName )->GetInNode()) ;
1536       cdebug << "EventW Previous Node " << NodeName << " ThreadsNumber "
1537              << ThreadsNumber
1538              << " _EventNodes.size() " << _EventNodes.size() << " "
1539              << theAutomaton->StateName( theState )
1540              << " Threads " << Threads() << " SuspendedThreads "
1541              << SuspendedThreads() << endl ;
1542       if ( aNode ) {
1543       }
1544       else if ( IsDone() && _EventNodes.size() == 0 ) {
1545         cdebug << "EventW IsDone() && _EventNodes.size() == 0" << endl ;
1546         RetVal = 0 ;
1547       }
1548       else {
1549         cdebug << "EventW Not InNode " << NodeName
1550                << " _EventNodes.size() " << _EventNodes.size() << endl ;
1551         while ( _EventNodes.size() == 0 ) {
1552           pthread_cond_wait( &_EventWait , &_MutexWait );
1553         }
1554         cdebug << "EventW pthread_cond_waited Not InNode " << NodeName
1555                << " _EventNodes.size() " << _EventNodes.size() << endl ;
1556       }
1557     }
1558
1559     if ( aNode ) {
1560       if ( aState == SUPERV::SuspendState ||
1561            aState == SUPERV::SuspendReadyState ||
1562            aState == SUPERV::SuspendDoneState ||
1563            aState == SUPERV::SuspendErroredState ) {
1564         aNode->ControlState( SUPERV::ToSuspendState ) ;
1565         if ( aNode->IsSuspended() ) {
1566           if ( pthread_mutex_unlock( &_MutexWait ) ) {
1567             perror("EventW pthread_mutex_lock ") ;
1568             exit( 0 ) ;
1569           }
1570           cdebug << "EventW " << aNode->Name() << " ResumeAction" << endl ;
1571           aNode->ResumeAction( GraphExecutor::ToResumeEvent ) ;
1572           cdebug << "EventW " << aNode->Name() << " ResumedAction" << endl ;
1573           if ( pthread_mutex_lock( &_MutexWait ) ) {
1574             perror("EventW pthread_mutex_lock ") ;
1575             exit( 0 ) ;
1576           }
1577         }
1578         else {
1579           cdebug << "EventW inconsistent SuspendState" << endl ;
1580           RetVal = false ; 
1581         }
1582       }
1583       else {
1584         if ( aNode->IsDone() ) {
1585           RetVal = true ;
1586         }
1587         else {
1588           cdebug << "EventW NOT SuspendedState _EventNodes.size() "
1589                  << _EventNodes.size() << endl ;
1590           RetVal = true ;
1591         }
1592       }
1593       if ( RetVal ) {
1594         cdebug << "EventW " << aNode->Name() << " pthread_cond_wait" << endl ;
1595         while ( _EventNodes.size() == 0 ) {
1596           pthread_cond_wait( &_EventWait , &_MutexWait );
1597         }
1598         ThreadsNumber = Threads() ;
1599         NodeName = _EventNodes.front() ;
1600         theEvent = _Events.front() ;
1601         anEvent = AutomatonGraphEvent( theEvent ) ;
1602         theState = _States.front() ;
1603         aState = AutomatonGraphState( theState ) ;
1604       }
1605     }
1606   }
1607   *aNodeName = NodeName ;
1608   cdebug << "<-- EventW RetVal " << RetVal << " " << NodeName
1609          << " Threads " << Threads() << " SuspendedThreads "
1610          << SuspendedThreads()
1611          << " _EventNodes.size() " << _EventNodes.size()
1612          << " " << theAutomaton->EventName( theEvent ) << " "
1613          << theAutomaton->StateName( theState ) << endl ;
1614   if ( pthread_mutex_unlock( &_MutexWait ) ) {
1615     perror("EventW pthread_mutex_lock ") ;
1616     exit( 0 ) ;
1617   }
1618   return RetVal ;
1619 }
1620 long GraphExecutor::OutNode::EventQSize() {
1621   return _EventNodes.size() ;
1622 }
1623
1624 void GraphExecutor::OutNode::EventList() {
1625   if ( pthread_mutex_lock( &_MutexWait ) ) {
1626     perror("EventList pthread_mutex_lock ") ;
1627     exit( 0 ) ;
1628   }
1629   list< char * >::iterator itEventNodes = _EventNodes.begin() ;
1630   list< GraphExecutor::NodeEvent >::iterator itEvents = _Events.begin() ;
1631   list< GraphExecutor::AutomatonState >::iterator itStates = _States.begin() ;
1632   while ( itEventNodes != _EventNodes.end() ) {
1633     cdebug << pthread_self() << "EVENTSTACK "
1634            << *itEventNodes << " " << *itEvents << " "
1635            << theAutomaton->StateName( *itStates )
1636            << " Threads " << Threads() << " SuspendedThreads " << SuspendedThreads() << endl ;
1637     itEventNodes++ ;
1638     itEvents++ ;
1639     itStates++ ;
1640   }
1641   if ( pthread_mutex_unlock( &_MutexWait ) ) {
1642     perror("EventList pthread_mutex_lock ") ;
1643     exit( 0 ) ;
1644   }
1645 }
1646
1647 void GraphExecutor::OutNode::SendSomeDataReady( const char * aNodeName ) {
1648   cdebug_in << "GraphExecutor::OutNode::SendSomeDataReady " << aNodeName << "->SendSomeDataReady( "
1649             << Name() << " )" << endl;
1650   int sts ;
1651   sts = ((GraphExecutor::InNode * ) Graph()->GetChangeGraphNode( aNodeName )->GetInNode())->SendSomeDataReady( Name() ) ;
1652   cdebug_out << "GraphExecutor::OutNode::SendSomeDataReady sts " << sts << endl ;
1653 }
1654
1655 void GraphExecutor::OutNode::State(GraphExecutor::AutomatonState aState ) {
1656 //  cdebug << "GraphExecutor::OutNode::State " << Name() << " "
1657 //         << theAutomaton->StateName( AutomatonGraphState( _State ) ) << " ---> "
1658 //         << theAutomaton->StateName( AutomatonGraphState( aState ) ) << endl ;
1659   _State = aState ;
1660 }
1661
1662 SUPERV::GraphState GraphExecutor::OutNode::State() {
1663 //  cdebug_in << "GraphExecutor::OutNode::State" << endl;
1664 //  cdebug_out << "GraphExecutor::OutNode::State" << endl ;
1665 //  cdebug << "GraphExecutor::OutNode::State GraphState "
1666 //         << theAutomaton->StateName( AutomatonGraphState( _State ) ) << endl ;
1667   return AutomatonGraphState( _State ) ;
1668 }
1669
1670 SUPERV::GraphState GraphExecutor::OutNode::State( const char * NodeName ) {
1671 //  cdebug_in << "GraphExecutor::OutNode::State " << NodeName << endl;
1672   GraphExecutor::AutomatonState aret = GraphExecutor::UnKnownState ;
1673   const GraphBase::ComputingNode * aCNode =  Graph()->GetGraphNode( NodeName ) ;
1674   if ( aCNode ) {
1675     GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *)aCNode->GetInNode() ;
1676     if ( anInNode ) {
1677       aret = anInNode->State() ;
1678       // asv 27.01.05 - fix for PAL7849, return "Loading" state only if internal state is SUPERV::RunningState
1679       if ( ( aret == GraphExecutor::ExecutingState || 
1680              aret == GraphExecutor::ResumedExecutingState ) && 
1681           anInNode->IsLoading() )
1682         aret = GraphExecutor::LoadingState;
1683 //      cdebug << "GraphExecutor::OutNode::State( " << NodeName << " ) "
1684 //             << theAutomaton->StateName( AutomatonGraphState( aret ) ) << endl ;
1685     }
1686   }
1687 //  cdebug_out << "GraphExecutor::OutNode::State" << endl ;
1688   return AutomatonGraphState( aret ) ;
1689 }
1690
1691 SUPERV::GraphState GraphExecutor::OutNode::State( const char * NodeName ,
1692                                                   const char * ServiceParameterName )  {
1693 //  cdebug_in << "GraphExecutor::OutNode::State " << NodeName << " "
1694 //            << ServiceParameterName<< endl;
1695   SUPERV::GraphState aret = Graph()->PortState( NodeName , ServiceParameterName ) ;
1696 //  cdebug_out << "GraphExecutor::OutNode::State" << endl ;
1697   return aret ;
1698 }
1699
1700 GraphExecutor::AutomatonState GraphExecutor::OutNode::AutomatonState() {
1701 //  cdebug_in << "GraphExecutor::OutNode::AutomatonState" << endl;
1702 //  cdebug_out << "GraphExecutor::OutNode::AutomatonState" << endl ;
1703   return _State ;
1704 }
1705
1706 GraphExecutor::AutomatonState GraphExecutor::OutNode::AutomatonState( const char * NodeName ) {
1707 //  cdebug_in << "GraphExecutor::OutNode::AutomatonState " << NodeName << endl;
1708   GraphExecutor::AutomatonState aret = GraphExecutor::UnKnownState ;
1709   const GraphBase::ComputingNode * aNode = Graph()->GetGraphNode( NodeName ) ;
1710   if ( aNode ) {
1711     GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) aNode->GetInNode() ;
1712     if ( anInNode ) {
1713       aret = anInNode->State() ;
1714     }
1715   }
1716 //  cdebug_out << "GraphExecutor::OutNode::AutomatonState" << endl ;
1717   return aret ;
1718 }
1719
1720 SUPERV::ControlState GraphExecutor::OutNode::ControlState() {
1721 //  cdebug_in << "GraphExecutor::OutNode::ControlState" << endl;
1722 //  cdebug_out << "GraphExecutor::OutNode::ControlState" << endl ;
1723   return _ControlState ;
1724 }
1725
1726 SUPERV::ControlState GraphExecutor::OutNode::ControlState( const char * NodeName ) {
1727 //  cdebug_in << "GraphExecutor::OutNode::ControlState " << NodeName << endl;
1728   SUPERV::ControlState aret = SUPERV::VoidState ;
1729   const GraphBase::ComputingNode * aNode = Graph()->GetGraphNode( NodeName ) ;
1730   if ( aNode ) {
1731     GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) aNode->GetInNode() ;
1732     if ( anInNode ) {
1733       aret = anInNode->ControlState() ;
1734     }
1735   }
1736 //  cdebug_out << "GraphExecutor::OutNode::ControlState" << endl ;
1737   return aret ;
1738 }
1739
1740 void GraphExecutor::OutNode::ControlClear() {
1741 //  cdebug_in << "GraphExecutor::OutNode::ControlClear" << endl;
1742 //  cdebug_out << "GraphExecutor::OutNode::ControlClear" << endl ;
1743   _ControlState = SUPERV::VoidState;
1744 }
1745
1746 void GraphExecutor::OutNode::ControlClear( const char * NodeName ) {
1747 //  cdebug_in << "GraphExecutor::OutNode::ControlClear " << NodeName << endl;
1748   const GraphBase::ComputingNode * aNode = Graph()->GetGraphNode( NodeName ) ;
1749   if ( aNode ) {
1750     GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) aNode->GetInNode() ;
1751     if ( anInNode ) {
1752       anInNode->ControlClear() ;
1753     }
1754   }
1755 //  cdebug_out << "GraphExecutor::OutNode::ControlClear" << endl ;
1756 }
1757
1758 bool GraphExecutor::OutNode::IsWaiting() {
1759 //  cdebug_in << "GraphExecutor::OutNode::IsWaiting" << endl;
1760 //  cdebug_out << "GraphExecutor::OutNode::IsWaiting" << endl ;
1761   return !IsDone() ;
1762 }
1763
1764 bool GraphExecutor::OutNode::IsReady() {
1765 //  cdebug_in << "GraphExecutor::OutNode::IsReady" << endl;
1766 //  cdebug_out << "GraphExecutor::OutNode::IsReady" << endl ;
1767   return !IsDone() ;
1768 }
1769
1770 bool GraphExecutor::OutNode::IsRunning() {
1771 //  cdebug_in << "GraphExecutor::OutNode::IsRunning" << endl;
1772 //  cdebug_out << "GraphExecutor::OutNode::IsRunning" << endl ;
1773   return !IsDone() ;
1774 }
1775
1776 bool GraphExecutor::OutNode::IsDone() {
1777 //  cdebug_in << "GraphExecutor::OutNode::IsDone" << endl;
1778 //  cdebug_out << "GraphExecutor::OutNode::IsDone" << endl ;
1779   return ( Done() || IsKilled() || IsStopped() ) ;
1780 }
1781
1782 bool GraphExecutor::OutNode::IsSuspended() {
1783 //  cdebug_in << "GraphExecutor::OutNode::IsSuspended" << endl;
1784   bool aret = false ;
1785 //  if ( _SuspendedThreads == _Threads && _Threads != 0 ) {
1786   if ( AutomatonGraphState( _State ) ==  SUPERV::SuspendState ) {
1787     aret = true ;
1788   }
1789 //  cdebug_out << "GraphExecutor::OutNode::IsSuspended" << endl ;
1790   return aret ;
1791 }
1792
1793 bool GraphExecutor::OutNode::IsKilled() {
1794 //  cdebug_in << "GraphExecutor::OutNode::IsKilled" << endl;
1795   bool aret = false ;
1796   if ( AutomatonGraphState( _State ) == SUPERV::KillState ) {
1797     aret = true ;
1798   }
1799 //  cdebug_out << "GraphExecutor::OutNode::IsKilled" << endl ;
1800   return aret ;
1801 }
1802
1803 bool GraphExecutor::OutNode::IsStopped() {
1804 //  cdebug_in << "GraphExecutor::OutNode::IsStopped" << endl;
1805   bool aret = false ;
1806   if ( AutomatonGraphState( _State ) == SUPERV::StopState ) {
1807     aret = true ;
1808   }
1809 //  cdebug_out << "GraphExecutor::OutNode::IsStopped" << endl ;
1810   return aret ;
1811 }
1812
1813 bool GraphExecutor::OutNode::IsWaiting( const char * NodeName ) {
1814   bool aret = false ;
1815 //  cdebug_in << "GraphExecutor::OutNode::IsWaiting " << NodeName << endl;
1816   GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) Graph()->GetGraphNode( NodeName )->GetInNode() ;
1817   if ( anInNode ) {
1818     aret = anInNode->IsWaiting() ;
1819   }
1820 //  cdebug_out << "GraphExecutor::OutNode::IsWaiting" << endl ;
1821   return aret ;
1822 }
1823
1824 bool GraphExecutor::OutNode::IsReady( const char * NodeName ) {
1825   bool aret = false ;
1826 //  cdebug_in << "GraphExecutor::OutNode::IsReady " << NodeName << endl;
1827   GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) Graph()->GetGraphNode( NodeName )->GetInNode() ;
1828   if ( anInNode ) {
1829     aret = anInNode->IsReady() ;
1830   }
1831 //  cdebug_out << "GraphExecutor::OutNode::IsReady" << endl ;
1832   return aret ;
1833 }
1834
1835 bool GraphExecutor::OutNode::IsRunning( const char * NodeName ) {
1836   bool aret = false ;
1837 //  cdebug_in << "GraphExecutor::OutNode::IsRunning " << NodeName << endl;
1838   GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) Graph()->GetGraphNode( NodeName )->GetInNode() ;
1839   if ( anInNode ) {
1840     aret = anInNode->IsRunning() ;
1841   }
1842 //  cdebug_out << "GraphExecutor::OutNode::IsRunning" << endl ;
1843   return aret ;
1844 }
1845
1846 bool GraphExecutor::OutNode::IsDone( const char * NodeName ) {
1847   bool aret = false ;
1848 //  cdebug_in << "GraphExecutor::OutNode::IsDone " << NodeName << endl;
1849   GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) Graph()->GetGraphNode( NodeName )->GetInNode() ;
1850   if ( anInNode ) {
1851     aret = anInNode->IsDone() ;
1852   }
1853 //  cdebug_out << "GraphExecutor::OutNode::IsDone" << endl ;
1854   return aret ;
1855 }
1856
1857 bool GraphExecutor::OutNode::IsSuspended( const char * NodeName ) {
1858   bool aret = false ;
1859 //  cdebug_in << "GraphExecutor::OutNode::IsSuspended " << NodeName << endl;
1860   GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) Graph()->GetGraphNode( NodeName )->GetInNode() ;
1861   if ( anInNode ) {
1862     aret = anInNode->IsSuspended() ;
1863   }
1864 //  cdebug_out << "GraphExecutor::OutNode::IsSuspended" << endl ;
1865   return aret ;
1866 }
1867
1868 bool GraphExecutor::OutNode::PortDone( const char * NodeName ,
1869                                        const char * ServiceParameterName )  {
1870 //  cdebug_in << "GraphExecutor::OutNode::IsDone " << NodeName << " "
1871 //            << ServiceParameterName<< endl;
1872   bool aret = Graph()->PortDone( NodeName , ServiceParameterName ) ;
1873 //  cdebug_out << "GraphExecutor::OutNode::IsDone" << endl ;
1874   return aret ;
1875 }
1876
1877 bool GraphExecutor::OutNode::ContainerKill() {
1878   bool RetVal = true ;
1879   cdebug_in << "GraphExecutor::OutNode::ContainerKill" << endl;
1880   _ControlState = SUPERV::ToSuspendState ;
1881   int i ;
1882   for ( i = 0 ; i < Graph()->GraphNodesSize() ; i++ ) {
1883     GraphExecutor::InNode * aNode = (GraphExecutor::InNode * ) Graph()->GraphNodes( i )->GetInNode() ;
1884     bool sts = aNode->ContainerKill() ;
1885     if ( sts && aNode->IsKilled() ) {
1886       cdebug << aNode->Name() << " killed" << endl ;
1887     }
1888     else if ( aNode->IsWaiting() || aNode->IsDone() ) {
1889       cdebug << aNode->Name() << " not killed : "
1890              << theAutomaton->StateName( aNode->State() ) << endl ;
1891     }
1892     else {
1893       cdebug << aNode->Name() << " cannot be killed : "
1894              << theAutomaton->StateName( aNode->State() ) << endl ;
1895       RetVal = false ;
1896     }
1897   }
1898   if ( !RetVal || Threads() != 0 ) {
1899     for ( i = 0 ; i < Graph()->GraphNodesSize() ; i++ ) {
1900       GraphExecutor::InNode * aNode = (GraphExecutor::InNode * ) Graph()->GraphNodes( i )->GetInNode() ;
1901       if ( !aNode->IsKilled() && !aNode->IsWaiting() && !aNode->IsDone() ) {
1902         aNode->KilledAction() ;
1903       }
1904     }
1905     RetVal = true ;
1906   }
1907   State( GraphExecutor::KilledState ) ;
1908   cdebug_out << "GraphExecutor::OutNode::ContainerKill" << endl ;
1909   return RetVal ;
1910 }
1911
1912 bool GraphExecutor::OutNode::Suspend() {
1913   int RetVal = 0 ;
1914   cdebug_in << "GraphExecutor::OutNode::Suspend" << endl;
1915 //  _ControlState = SUPERV::ToSuspendState ;
1916   int i ;
1917   for ( i = 0 ; i < Graph()->GraphNodesSize() ; i++ ) {
1918     GraphExecutor::InNode * aNode = (GraphExecutor::InNode * ) Graph()->GraphNodes( i )->GetInNode() ;
1919     bool sts = aNode->Suspend() ;
1920     if ( sts && aNode->IsSuspended() ) {
1921       RetVal += 1 ;
1922       cdebug << aNode->Name() << " Suspended" << endl ;
1923     }
1924     else if ( aNode->IsWaiting() || aNode->IsReady() ) {
1925       RetVal += 1 ;
1926       cdebug << aNode->Name() << " will be Suspended" << endl ;
1927     }
1928     else if ( aNode->IsDone() ) {
1929       cdebug << aNode->Name() << " not Suspended : "
1930              << theAutomaton->StateName( aNode->State() ) << endl ;
1931     }
1932     else {
1933 //      RetVal += 1 ;
1934       cdebug << aNode->Name() << " cannot be Suspended : "
1935              << theAutomaton->StateName( aNode->State() ) << endl ;
1936     }
1937   }
1938   if ( RetVal ) {
1939     State( GraphExecutor::SuspendedState ) ;
1940     MESSAGE("================================================================================") ;
1941     MESSAGE( Name() << " IS SUSPENDED" ) ;
1942     MESSAGE("================================================================================") ;
1943   }
1944   else {
1945     MESSAGE("================================================================================") ;
1946     MESSAGE( Name() << " IS NOT SUSPENDED" ) ;
1947     MESSAGE("================================================================================") ;
1948   }
1949   for ( i = 0 ; i < Graph()->GraphNodesSize() ; i++ ) {
1950     GraphExecutor::InNode * aNode = (GraphExecutor::InNode * ) Graph()->GraphNodes( i )->GetInNode() ;
1951     MESSAGE(aNode->Name() << " " << theAutomaton->StateName( aNode->State() ) ) ;
1952     cdebug << aNode->Name() << " " << theAutomaton->StateName( aNode->State() ) << endl ;
1953   }
1954   cdebug_out << "GraphExecutor::OutNode::Suspend " << theAutomaton->StateName( State() )
1955              << " EventQSize " << EventQSize() << endl ;
1956   return RetVal ;
1957 }
1958
1959 bool GraphExecutor::OutNode::Resume() {
1960   int RetVal = 0 ;
1961   cdebug_in << "GraphExecutor::OutNode::Resume "
1962             << theAutomaton->StateName( State() ) << endl;
1963   if ( IsSuspended() ) {
1964     State( GraphExecutor::ExecutingState ) ;
1965     int i ;
1966     for ( i = 0 ; i < Graph()->GraphNodesSize() ; i++ ) {
1967       GraphExecutor::InNode * aNode = (GraphExecutor::InNode * ) Graph()->GraphNodes( i )->GetInNode() ;
1968       aNode->ControlState( SUPERV::VoidState ) ;
1969       if ( aNode->IsSuspended() ) {
1970         cdebug << aNode->Name() << "->Resume " << theAutomaton->StateName( aNode->State() )
1971                << endl ;
1972         if ( aNode->Resume() ) {
1973           cdebug << aNode->Name() << " Resumed " << theAutomaton->StateName( aNode->State() )
1974                  << endl ;
1975           RetVal += 1 ;
1976         }
1977         else {
1978           cdebug << aNode->Name() << " Resume failed"
1979                  << theAutomaton->StateName( aNode->State() ) << endl ;
1980         }
1981       }
1982       else {
1983         cdebug << aNode->Name() << " " << theAutomaton->StateName( aNode->State() )
1984                << endl ;
1985       }
1986     }
1987   }
1988   else {
1989     cdebug << Name() << " not suspended " << theAutomaton->StateName( State() ) << endl ;
1990   }
1991   if ( RetVal ) {
1992     MESSAGE("================================================================================") ;
1993     MESSAGE( Name() << " IS RESUMED" ) ;
1994     MESSAGE("================================================================================") ;
1995   }
1996   else {
1997     MESSAGE("================================================================================") ;
1998     MESSAGE( Name() << " IS NOT RESUMED" ) ;
1999     MESSAGE("================================================================================") ;
2000   }
2001   int i ;
2002   for ( i = 0 ; i < Graph()->GraphNodesSize() ; i++ ) {
2003     GraphExecutor::InNode * aNode = (GraphExecutor::InNode * ) Graph()->GraphNodes( i )->GetInNode() ;
2004     MESSAGE(aNode->Name() << " " << theAutomaton->StateName( aNode->State() ) ) ;
2005     cdebug << aNode->Name() << " " << theAutomaton->StateName( aNode->State() ) << endl ;
2006   }
2007   cdebug_out << "GraphExecutor::OutNode::Resume" << theAutomaton->StateName( State() ) << " " << RetVal
2008              << " EventQSize " << EventQSize() << endl ;
2009   return RetVal ;
2010 }
2011
2012 bool GraphExecutor::OutNode::Kill() {
2013   bool RetVal = false ;
2014   cdebug_in << "GraphExecutor::OutNode::Kill"
2015             << " EventQSize " << EventQSize() << " State "
2016             << theAutomaton->StateName( State() ) << endl;
2017   if ( IsDone() ) {
2018     cdebug_out << "GraphExecutor::OutNode::Kill " << RetVal
2019                << " Threads " << _Threads << " SuspendedThreads " << _SuspendedThreads
2020                << " EventQSize " << EventQSize() << " State "
2021                << theAutomaton->StateName( State() ) << endl ;
2022     return RetVal ;
2023   }
2024   _ControlState = SUPERV::ToSuspendState ;
2025   int i ;
2026   for ( i = 0 ; i < Graph()->GraphNodesSize() ; i++ ) {
2027     GraphExecutor::InNode * aNode = (GraphExecutor::InNode * ) Graph()->GraphNodes( i )->GetInNode() ;
2028     bool sts = aNode->Kill() ;
2029     if ( sts && Threads() != 0 ) {
2030       if ( !aNode->IsKilled() ) {
2031         cdebug << aNode->Name() << " not killed : "
2032                << theAutomaton->StateName( aNode->State() ) << " " << aNode->Name() << "->"
2033                << "KilledAction()" << endl ;
2034         MESSAGE( aNode->Name() << " not killed : KilledAction()" ) ;
2035         aNode->KilledAction() ;
2036       }
2037       if ( aNode->IsKilled() ) {
2038         cdebug << aNode->Name() << " killed" << endl ;
2039         RetVal = true ;
2040       }
2041     }
2042 //PAL8003
2043 // JR 24.03.2005 Debug : the node may have been killed
2044     else if ( aNode->IsKilled() ) {
2045       cdebug << aNode->Name() << " killed" << endl ;
2046       RetVal = true ;
2047     }
2048     else if ( aNode->IsWaiting() || aNode->IsDone() ) {
2049       cdebug << aNode->Name() << " not killed : "
2050              << theAutomaton->StateName( aNode->State() ) << endl ;
2051     }
2052     else {
2053       cdebug << aNode->Name() << " cannot be killed : "
2054              << theAutomaton->StateName( aNode->State() ) << endl ;
2055     }
2056   }
2057   if ( RetVal ) {
2058     State( GraphExecutor::KilledState ) ;
2059     MESSAGE("================================================================================") ;
2060     MESSAGE( Name() << " IS KILLED" <<  theAutomaton->StateName( AutomatonState() ) << " EventQSize "
2061              << EventQSize() ) ;
2062     MESSAGE("================================================================================") ;
2063   }
2064   else {
2065     MESSAGE("================================================================================") ;
2066     MESSAGE( Name() << " IS NOT KILLED" ) ;
2067     MESSAGE("================================================================================") ;
2068   }
2069   for ( i = 0 ; i < Graph()->GraphNodesSize() ; i++ ) {
2070     GraphExecutor::InNode * aNode = (GraphExecutor::InNode * ) Graph()->GraphNodes( i )->GetInNode() ;
2071     MESSAGE(aNode->Name() << " " << theAutomaton->StateName( aNode->State() ) ) ;
2072     cdebug << aNode->Name() << " " << theAutomaton->StateName( aNode->State() ) << endl ;
2073   }
2074
2075   if ( pthread_mutex_lock( &_MutexWait ) ) {
2076     perror("EventLoop pthread_mutex_lock ") ;
2077     exit( 0 ) ;
2078   }
2079 //PAL8520
2080 //JR 23.02.2005, Debug : siftEvents() must be inside the pthread_mutex_lock
2081   // remove "extra" events from the event queue
2082   siftEvents();
2083   if ( pthread_mutex_unlock( &_MutexWait ) ) {
2084     perror("PushEvent pthread_mutex_unlock ") ;
2085     exit( 0 ) ;
2086   }
2087
2088   // asv : 25.10.04 : calling Editing() to indicate stop of execution
2089   if ( RetVal ) // mkr : IPAL10722 : we have to indicate stop of execution if only execution was successfully killed
2090     Graph()->GraphEditor()->Editing();
2091
2092   cdebug_out << "GraphExecutor::OutNode::Kill " << RetVal
2093              << " Threads " << _Threads << " SuspendedThreads " << _SuspendedThreads
2094              << " EventQSize " << EventQSize() << endl ;
2095
2096   return RetVal ;
2097 }
2098
2099 bool GraphExecutor::OutNode::Stop() {
2100   bool RetVal = false ;
2101   cdebug_in << "GraphExecutor::OutNode::Stop" << endl;
2102   RetVal = Kill() ;
2103   cdebug_out << "GraphExecutor::OutNode::Stop" << endl ;
2104   if ( RetVal ) {
2105     MESSAGE("================================================================================") ;
2106     MESSAGE( Name() << " IS STOPPED" ) ;
2107     MESSAGE("================================================================================") ;
2108   }
2109   return RetVal ;
2110 }
2111
2112 bool GraphExecutor::OutNode::ReadyWait() {
2113   cdebug_in << "GraphExecutor::OutNode::ReadyWait" << endl;
2114   bool aret ;
2115   if ( pthread_mutex_lock( &_MutexWait ) ) {
2116     perror("pthread_mutex_lock _ReadyWait") ;
2117     exit( 0 ) ;
2118   }
2119   aret = IsReady() ;
2120   if ( !aret ) {
2121     Suspend() ;
2122     pthread_cond_wait( &_EventWait , &_MutexWait );
2123     aret = IsReady() ;
2124   }
2125   if ( pthread_mutex_unlock( &_MutexWait ) ) {
2126     perror("pthread_mutex_lock _ReadyWait") ;
2127     exit( 0 ) ;
2128   }
2129   cdebug_out << "GraphExecutor::OutNode::ReadyWait" << endl ;
2130   return aret ;
2131 }
2132
2133 bool GraphExecutor::OutNode::RunningWait() {
2134   cdebug_in << "GraphExecutor::OutNode::RunningWait" << endl;
2135   bool aret ;
2136   if ( pthread_mutex_lock( &_MutexWait ) ) {
2137     perror("pthread_mutex_lock _RunningWait") ;
2138     exit( 0 ) ;
2139   }
2140   aret = IsRunning() ;
2141   if ( !aret ) {
2142     cdebug << "RunningWait pthread_cond_wait _EventWait" << endl;
2143     pthread_cond_wait( &_EventWait , &_MutexWait );
2144     aret = IsRunning() ;
2145     cdebug << "RunningWait pthread_cond_waited _EventWait " << aret << endl;
2146   }
2147   if ( pthread_mutex_unlock( &_MutexWait ) ) {
2148     perror("pthread_mutex_lock _RunningWait") ;
2149     exit( 0 ) ;
2150   }
2151   cdebug_out << "GraphExecutor::OutNode::RunningWait " << aret << endl ;
2152   return aret ;
2153 }
2154
2155 bool GraphExecutor::OutNode::DoneWait() {
2156   cdebug_in << pthread_self() << " GraphExecutor::DoneWait " << this << " " << Name() << endl;
2157   bool aret ;
2158   if ( pthread_mutex_lock( &_MutexWait ) ) {
2159     perror("pthread_mutex_lock _DoneWait") ;
2160     exit( 0 ) ;
2161   }
2162   aret = IsDone() ;
2163   while ( !aret && !IsSuspended() && IsRunning() ) {
2164     cdebug << pthread_self() << " GraphExecutor::DoneWait " << this << " " << Name()
2165            << " DoneWait pthread_cond_wait _EventWait " << &_EventWait << endl;
2166     pthread_cond_wait( &_EventWait , &_MutexWait );
2167     aret = IsDone() ;
2168     cdebug << pthread_self() << " GraphExecutor::DoneWait " << this << " " << Name()
2169            << " DoneWait pthread_cond_waited _EventWaited " << &_EventWait << " : "<< aret << endl;
2170   }
2171   if ( pthread_mutex_unlock( &_MutexWait ) ) {
2172     perror("pthread_mutex_lock _DoneWait") ;
2173     exit( 0 ) ;
2174   }
2175   while ( _run_func_thread || theAutomaton->ThreadsNumber() ) {
2176     // mkr : IPAL14711
2177     // waiting for pthread_join in FiniteStateMachine
2178   }
2179   if ( Done() || IsKilled() ) Graph()->GraphEditor()->Editing(); // mkr : IPAL14572 : calling Editing() to indicate stop of execution
2180   cdebug_out << pthread_self() << " GraphExecutor::DoneWait " << this << " " << Name()
2181              << " " << State() << " : " << aret << endl ;
2182   return aret ;
2183 }
2184
2185 bool GraphExecutor::OutNode::SuspendedWait() {
2186   cdebug_in << "GraphExecutor::OutNode::SuspendedWait" << endl;
2187   bool aret ;
2188   if ( pthread_mutex_lock( &_MutexWait ) ) {
2189     perror("pthread_mutex_lock _SuspendedWait") ;
2190     exit( 0 ) ;
2191   }
2192   aret = IsSuspended() ;
2193   while ( !aret && !IsDone() ) {
2194     pthread_cond_wait( &_EventWait , &_MutexWait );
2195     aret = IsSuspended() ;
2196   }
2197   if ( pthread_mutex_unlock( &_MutexWait ) ) {
2198     perror("pthread_mutex_lock _SuspendedWait") ;
2199     exit( 0 ) ;
2200   }
2201   cdebug_out << "GraphExecutor::OutNode::SuspendedWait" << endl ;
2202   return aret ;
2203 }
2204
2205 bool GraphExecutor::OutNode::ReadyWait( const char * NodeName ) {
2206   bool aret = false ;
2207   cdebug_in << "GraphExecutor::OutNode::ReadyWait " << NodeName << endl;
2208   GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) Graph()->GetGraphNode( NodeName )->GetInNode() ;
2209   if ( anInNode ) {
2210     aret = anInNode->ReadyWait() ;
2211   }
2212   cdebug_out << "GraphExecutor::OutNode::ReadyWait" << endl ;
2213   return aret ;
2214 }
2215
2216 bool GraphExecutor::OutNode::RunningWait( const char * NodeName ) {
2217   bool aret = false ;
2218   cdebug_in << "GraphExecutor::OutNode::RunningWait " << NodeName << endl;
2219   GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) Graph()->GetGraphNode( NodeName )->GetInNode() ;
2220   if ( anInNode ) {
2221     aret = anInNode->RunningWait() ;
2222   }
2223   cdebug_out << "GraphExecutor::OutNode::RunningWait" << endl ;
2224   return aret ;
2225 }
2226
2227 bool GraphExecutor::OutNode::DoneWait( const char * NodeName ) {
2228   bool aret = false ;
2229   cdebug_in << "GraphExecutor::OutNode::DoneWait " << NodeName << endl;
2230   GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) Graph()->GetGraphNode( NodeName )->GetInNode() ;
2231   if ( anInNode ) {
2232     aret = anInNode->DoneWait() ;
2233   }
2234   cdebug_out << "GraphExecutor::OutNode::DoneWait" << endl ;
2235   return aret ;
2236 }
2237
2238 bool GraphExecutor::OutNode::SuspendedWait( const char * NodeName ) {
2239   bool aret = false ;
2240   cdebug_in << "GraphExecutor::OutNode::SuspendedWait " << NodeName << endl;
2241   GraphExecutor::InNode *anInNode = (GraphExecutor::InNode *) Graph()->GetGraphNode( NodeName )->GetInNode() ;
2242   if ( anInNode ) {
2243     aret = anInNode->SuspendedWait() ;
2244   }
2245   cdebug_out << "GraphExecutor::OutNode::SuspendedWait" << endl ;
2246   return aret ;
2247 }
2248
2249 long GraphExecutor::OutNode::LastLevelDone() {
2250   int RetVal = -1 ;
2251   int i , j ;
2252   for ( i = 0 ; i <= Graph()->LevelMax() ; i++ ) {
2253     for ( j = 0 ; j <= Graph()->NodesNumber( i ) ; j++ ) {
2254       GraphBase::ComputingNode * aNode = Graph()->SortedNodes( i , j ) ;
2255       if ( !IsDone( aNode->Name() ) ) {
2256         break ;
2257       }
2258     }
2259     if ( j != Graph()->NodesNumber( i ) + 1 )
2260       break ;
2261     RetVal = i ;
2262   }
2263   return RetVal ;
2264 }
2265
2266
2267 //JR 30.03.2005const CORBA::Any *GraphExecutor::OutNode::GetInData( const char * NodeName ,
2268 const CORBA::Any GraphExecutor::OutNode::GetInData( const char * NodeName ,
2269                                                     const char * ServiceParameterName ) {
2270 //  cdebug_in << "GraphExecutor::OutNode::GetInData " << NodeName << " "
2271 //            << ServiceParameterName << endl ;
2272 //JR 30.03.2005  const CORBA::Any * retdata = Graph()->PortInData( NodeName , ServiceParameterName ) ;
2273   const CORBA::Any retdata = Graph()->PortInData( NodeName , ServiceParameterName ) ;
2274 //  cdebug_out << "GraphExecutor::OutNode::GetInData" << endl ;
2275   return retdata ;
2276 }
2277
2278 //JR 30.03.2005const CORBA::Any *GraphExecutor::OutNode::GetOutData( const char * NodeName ,
2279 const CORBA::Any GraphExecutor::OutNode::GetOutData( const char * NodeName ,
2280                                                       const char * ServiceParameterName ) {
2281 //  cdebug_in << "GraphExecutor::OutNode::GetOutData " << NodeName << " "
2282 //            << ServiceParameterName << endl ;
2283 //JR 30.03.2005  const CORBA::Any * retdata = Graph()->PortOutData( NodeName , ServiceParameterName ) ;
2284   const CORBA::Any retdata = Graph()->PortOutData( NodeName , ServiceParameterName ) ;
2285 //  cdebug_out << "GraphExecutor::OutNode::GetOutData" << endl ;
2286   return retdata ;
2287 }
2288
2289 const long GraphExecutor::OutNode::CpuUsed() {
2290   return Graph()->CpuUsed() ;
2291 }
2292
2293 const long GraphExecutor::OutNode::CpuUsed( const char * aNodeName ) {
2294   GraphBase::ComputingNode * aNode = Graph()->GetChangeGraphNode( aNodeName ) ;
2295   if ( aNode ) {
2296     GraphExecutor::InNode * anInNode = (GraphExecutor::InNode * ) aNode->GetInNode() ;
2297     if ( anInNode ) {
2298       return anInNode->CpuUsed() ;
2299     }
2300   }
2301   return 0 ;
2302 }
2303
2304 // remove equal sets "Node-Event-State" from _EventNodes/_Events/_States queues
2305 void GraphExecutor::OutNode::siftEvents() {
2306   list<char*>::reverse_iterator itNodes = _EventNodes.rbegin();
2307   list<GraphExecutor::NodeEvent>::reverse_iterator itEvents = _Events.rbegin();
2308   list<GraphExecutor::AutomatonState>::reverse_iterator itStates = _States.rbegin();
2309   list<char*> _NewEventNodes;
2310   list<GraphExecutor::NodeEvent> _NewEvents;
2311   list<GraphExecutor::AutomatonState> _NewStates;
2312   for ( ; itNodes != _EventNodes.rend() ; ++itNodes, ++itEvents, ++itStates) {
2313     //cout << "----- aNodeName ==> " << *itNodes;
2314     list<char*>::reverse_iterator itNewNodes = _NewEventNodes.rbegin();
2315     bool found = false;
2316     for ( ; itNewNodes != _NewEventNodes.rend() ; ++itNewNodes ) {
2317       if ( !strcmp( *itNewNodes, *itNodes ) ) {
2318         found = true;
2319         break;
2320       }
2321     }
2322
2323     if ( found ) {
2324       //cout << "   FOUND";
2325     }
2326     else {
2327       _NewEventNodes.push_back( *itNodes );
2328       _NewEvents.push_back( *itEvents );
2329       _NewStates.push_back( *itStates );
2330     }
2331     //cout << endl;
2332   }
2333   _EventNodes = _NewEventNodes;
2334   _Events = _NewEvents;
2335   _States = _NewStates;
2336 }