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