Salome HOME
2eb3d0df34ddcbfb3ef63dc24ad867d6c4104365
[modules/superv.git] / src / GraphExecutor / DataFlowExecutor_DataFlow.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   : DataFlowExecutor_DataFlow.cxx
25 //  Module : SUPERV
26
27 using namespace std;
28 #include "DataFlowExecutor_DataFlow.hxx"
29 #include "DataFlowEditor_DataFlow.hxx"
30
31 // Implementation de la classe GraphExecutor::Graph
32
33 GraphExecutor::DataFlow::DataFlow() :
34   OutNode() {
35   cdebug_in << "GraphExecutor::DataFlow::DataFlowExecutor()" << endl;
36
37   _theNamingService = NULL ;
38   
39   cdebug_out << "GraphExecutor::DataFlow::DataFlowExecutor()" << endl;
40 }
41
42 GraphExecutor::DataFlow::DataFlow( CORBA::ORB_ptr ORB,
43                                    SALOME_NamingService* ptrNamingService ,
44                                    const char *DataFlowName ,
45                                    const char * DebugFileName ,
46                                    const SUPERV::KindOfNode aKindOfNode ) :
47   OutNode( ORB, ptrNamingService , DataFlowName , DebugFileName , aKindOfNode ) {
48   cdebug_in << "GraphExecutor::DataFlow::DataFlow(" ;
49   if ( DataFlowName ) {
50     cdebug << DataFlowName ;
51   }
52   cdebug << ")" << endl;
53
54   _theNamingService = ptrNamingService ;
55
56   cdebug_out << "GraphExecutor::DataFlow::DataFlow" << endl;
57 }
58
59 GraphExecutor::DataFlow::DataFlow( CORBA::ORB_ptr ORB,
60                                    SALOME_NamingService* ptrNamingService ,
61                                    const SALOME_ModuleCatalog::Service& DataFlowService ,
62                                    const char *DataFlowComponentName ,
63                                    const char *DataFlowInterfaceName ,
64                                    const char *DataFlowName ,
65                                    const SUPERV::KindOfNode DataFlowkind ,
66                                    const SUPERV::SDate DataFlowFirstCreation ,
67                                    const SUPERV::SDate DataFlowLastModification ,
68                                    const char * DataFlowExecutorRelease ,
69                                    const char * DataFlowAuthor ,
70                                    const char * DataFlowComputer ,
71                                    const char * DataFlowComment ,
72                                    const char * DebugFileName ) :
73      OutNode( ORB, ptrNamingService , DataFlowService , DataFlowComponentName ,
74               DataFlowInterfaceName , DataFlowName , DataFlowkind ,
75               DataFlowFirstCreation , DataFlowLastModification  ,
76               DataFlowExecutorRelease , DataFlowAuthor ,
77               DataFlowComputer , DataFlowComment , DebugFileName ) {
78   cdebug_in << "GraphExecutor::DataFlow::DataFlow(" << DataFlowName << ")" << endl;
79
80   _theNamingService = ptrNamingService ;
81
82   cdebug_out << "GraphExecutor::DataFlow::DataFlow" << endl;
83 } ;
84
85 GraphExecutor::DataFlow::~DataFlow() {
86 //  delete _DataFlowNode ;
87 //  delete _DataFlowDatas ;
88 //  delete _GT ;
89 }
90
91 bool GraphExecutor::DataFlow::Ping( const char *aNodeName ) {
92   cdebug_in << "GraphExecutor::DataFlow::Ping" << aNodeName << " )" << endl;
93   bool RetVal = false ;
94   if ( Graph()->GetGraphNode( aNodeName ) )
95     RetVal = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode())->Ping() ;
96   cdebug_out << "GraphExecutor::DataFlow::Ping" << endl;
97   return RetVal ;
98 }
99
100 bool GraphExecutor::DataFlow::ContainerKill() {
101   cdebug_in << "GraphExecutor::DataFlow::ContainerKill()" << endl;
102   bool RetVal = GraphExecutor::OutNode::ContainerKill() ;
103   cdebug_out << "GraphExecutor::DataFlow::ContainerKill()" << endl;
104   return RetVal ;
105 }
106
107 bool GraphExecutor::DataFlow::ContainerKill( const char *aNodeName ) {
108 //  cdebug_in << "GraphExecutor::DataFlow::ContainerKill( " << aNodeName << " )"<< endl;
109   bool RetVal = false ;
110   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
111   if ( aNode ) {
112     RetVal = aNode->ContainerKill() ;
113   }
114 //  cdebug_out << "GraphExecutor::DataFlow::ContainerKill" << endl;
115   return RetVal ;
116 }
117
118 bool GraphExecutor::DataFlow::InputOfAny( const char * ToServiceParameterName ,
119                                           const CORBA::Any & aValue ,
120                                           bool SomeDataReady ) {
121   cdebug_in <<"GraphExecutor::DataFlow::InputOfAny( " << ToServiceParameterName
122             << " SomeDataReady " << SomeDataReady << " )" << endl ;
123   bool RetVal = false ;
124   cdebug << pthread_self() << "GraphExecutor::DataFlow::InputOfAny " << Name() << "( "
125          << ToServiceParameterName << " , aValue , SomeDataReady " << SomeDataReady << " ) "
126          << endl ;
127   if ( Graph()->GraphMacroLevel() ) {
128     GraphBase::OutPort * anOutPort ;
129     anOutPort = Graph()->GetChangeInDataNodePort( ToServiceParameterName ) ;
130     cdebug << pthread_self() << "GraphExecutor::DataFlow::InputOfAny " << Name()
131            << " " << State() << " " << ToServiceParameterName << " " << anOutPort->PortState()
132            << " Done : " << anOutPort->PortDone() << endl ;
133     RetVal = AddInputData( Name() , ToServiceParameterName , aValue ) ;
134     anOutPort->PortState(  SUPERV::ReadyState ) ;
135 // There is only one port :
136     anOutPort->ChangeInPorts( 0 )->PortState( SUPERV::ReadyState ) ;
137     anOutPort->PortDone( true ) ;
138 // There is only one inport of a Node in an ReversedOutport of a graph :
139     GraphExecutor::InNode * anInNode = (GraphExecutor::InNode * ) Graph()->GetChangeGraphNode( anOutPort->InPorts( 0 )->NodeName() )->GetInNode() ;
140     cdebug << pthread_self() << "GraphExecutor::DataFlow::InputOfAny : " << anInNode->Name()
141            << "->SendSomeDataReady( " << Name() << " ) for Port "
142            << anOutPort->InPorts( 0 )->PortName() << " SomeDataReady " << SomeDataReady << endl ;
143     if ( SomeDataReady ) {
144       MESSAGE( "GraphExecutor::InputOfAny " << Name() << " SendSomeDataReady --> " << anInNode->Name()
145                << " " << anInNode->State() << " " << anOutPort->InPorts( 0 )->PortName() ) ;
146       int sts = anInNode->SendSomeDataReady( Name() ) ;
147       cdebug << "GraphExecutor::DataFlow::InputOfAny " << anInNode->Name()
148              << "->SendSomeDataReady( " << Name() << " ) sts " << sts << " State "
149              << anInNode->State() << " IsReady " << anInNode->IsReady()
150              << " SomeDataReady " << SomeDataReady << endl ;
151 //JR 15.04.2005 Debug PAL8624 RetroConception :
152       if ( sts && anInNode->HasAllDataReady() ) {
153         cdebug << pthread_self() << "/" << anInNode->ThreadNo()
154                << "GraphExecutor::DataFlow::InputOfAny : "
155                << anInNode->Name() << "->SendEvent( GraphExecutor::ExecuteEvent ) "
156                << " " << anInNode->Name() << "->HasAllDataReady() " << anInNode->HasAllDataReady()
157                << " State " << anInNode->State() << endl ;
158         anInNode->ThreadNo( 0 ) ;
159         anInNode->CreateNewThread( true ) ;
160         if ( !anInNode->SendEvent( GraphExecutor::AllDataReadyEvent ) ) { // ==> Ready to execute
161 //JR 06.05.2005 Debug PAL8624 RetroConception :
162 #if 0
163         if ( res && anInNode->IsReady() ) {
164           cdebug << pthread_self() << "/" << anInNode->ThreadNo() << "GraphExecutor::DataFlow::InputOfAny : "
165                  << anInNode->Name() << "->SendEvent( GraphExecutor::ExecuteEvent ) "
166                  << " " << anInNode->Name() << "->IsReady() " << anInNode->IsReady() << " State "
167                  << anInNode->State() << endl ;
168 //JR 15.04.2005 Debug PAL8624 RetroConception :
169 //          if ( anInNode->IsLockedDataWait() ) {
170 //            cdebug << pthread_self() << "/" << anInNode->ThreadNo() << "GraphExecutor::DataFlow::InputOfAny : "
171 //                   << anInNode->Name() << " IsLockedDataWait() ==> UnLockDataWait" << endl ;
172 //            anInNode->UnLockDataWait() ;
173 //          }
174           anInNode->ThreadNo( 0 ) ;
175           anInNode->CreateNewThread( true ) ;
176           anInNode->SendEvent( GraphExecutor::ExecuteEvent ) ;
177 //          State( GraphExecutor::ExecutingState ) ;
178         }
179         else {
180           RetVal = false ;
181 #endif
182           cdebug << pthread_self() << "/" << anInNode->ThreadNo()
183                  << "GraphExecutor::DataFlow::InputOfAny : NotAllDataReady ERROR : "
184                  << anInNode->Name() << "->SendEvent( GraphExecutor::ExecuteEvent ) "
185                  << " " << anInNode->Name() << "->IsReady() " << anInNode->IsReady() << " State "
186                  << anInNode->State() << endl ;
187         }
188       }
189       else {
190         cdebug << pthread_self() << "/" << anInNode->ThreadNo()
191                << "GraphExecutor::DataFlow::InputOfAny : NotAllDataReady : "
192                << anInNode->Name() << "->SendEvent( GraphExecutor::ExecuteEvent ) "
193                << " " << anInNode->Name() << "->IsReady() " << anInNode->IsReady() << " State "
194                << anInNode->State() << endl ;
195       }
196     }
197     else {
198       cdebug << "GraphExecutor::DataFlow::InputOfAny " << anInNode->Name()
199              << "->SendSomeDataReady( " << Name() << " ) State "
200              << anInNode->State() << " IsReady " << anInNode->IsReady()
201              << " SomeDataReady " << SomeDataReady << endl ;
202     }
203   }
204   else {
205     cdebug << pthread_self() << "GraphExecutor::DataFlow::InputOfAny GraphMacroLevel "
206            << Graph()->GraphMacroLevel() << " ERROR" << endl ;
207     MESSAGE( "GraphExecutor::DataFlow::InputOfAny GraphMacroLevel " << Graph()->GraphMacroLevel()
208              << " ERROR" ) ;
209   }
210   cdebug_out << pthread_self() << " GraphExecutor::DataFlow::InputOfAny " << RetVal << endl ;
211   return RetVal ;
212 }
213
214 bool GraphExecutor::DataFlow::OutputOfAny( const char * aNodeName ,
215                                            const char * ToServiceParameterName ,
216                                            const CORBA::Any & aValue ) {
217   cdebug_in << pthread_self() << "/" << ThreadNo() << "GraphExecutor::DataFlow::OutputOfAny( " << aNodeName
218             << " , " << ToServiceParameterName
219             << " , aValue ) from " << Name() << endl ;
220   bool RetVal = false ;
221   GraphBase::Graph * aMacroNode = (GraphBase::Graph * ) Graph()->GetChangeGraphNode( aNodeName ) ;
222 //  GraphExecutor::InNode * anInNode = (GraphExecutor::InNode * ) aMacroGraph->GetInNode() ;
223   RetVal = Graph()->AddOutputData( aNodeName , ToServiceParameterName , aValue ) ;
224   GraphBase::OutPort * anOutPort = aMacroNode->GetChangeOutPort( ToServiceParameterName ) ;
225   int i ;
226 // Loop over Inports linked to that OutPort of the MacroNode
227   for ( i = 0 ; i < anOutPort->InPortsSize() ; i++ ) {
228     const char * ToNodeName = anOutPort->ChangeInPorts( i )->NodeName() ;
229     const char * ToParameterName = anOutPort->ChangeInPorts( i )->PortName() ;
230     GraphBase::ComputingNode * aComputingNode = Graph()->GetChangeGraphNode( ToNodeName ) ;
231     if ( strcmp( ToNodeName , Name() ) ) {
232       GraphExecutor::InNode * aLinkedNode = (GraphExecutor::InNode * ) aComputingNode->GetInNode() ;
233       cdebug << pthread_self() << "/" << aLinkedNode->ThreadNo()
234              << "GraphExecutor::DataFlow::OutputOfAny to Node "
235              << ToNodeName << "(" << ToParameterName << ") from MacroNode " << aNodeName << endl ;
236       int sts ;
237 //JR 15.04.2005 Debug PAL8624 RetroConception :
238 //      if ( aLinkedNode->IsLockedDataWait() ) {
239 //        cdebug << "GraphExecutor::DataFlow::OutputOfAny " << aLinkedNode->Name()
240 //               << " IsLockedDataWait --> UnLockDataWait" << endl ;
241 //      }
242       sts = aLinkedNode->SendSomeDataReady( (char * ) aNodeName ) ;
243       cdebug << pthread_self() << "/" << aLinkedNode->ThreadNo()
244              << "GraphExecutor::DataFlow::OutputOfAny " << aLinkedNode->Name()
245              << "->SendSomeDataReady( " << aNodeName << " ) sts " << sts << " " << aLinkedNode->State() << endl ;
246       if ( sts ) {
247         if ( aLinkedNode->State() == GraphExecutor::DataReadyState ) {
248 //JR 15.04.2005 Debug PAL8624 RetroConception :
249 //          aLinkedNode->CreateNewThreadIf( true ) ;
250 //          aLinkedNode->UnLockDataWait() ;
251           aLinkedNode->SendEvent( GraphExecutor::ExecuteEvent ) ;
252         }
253       }
254     }
255     else if ( Graph()->GraphMacroLevel() != 0 ) {
256       cdebug << pthread_self() << "/" << Graph()->CoupledNode()->ThreadNo()
257              << "GraphExecutor::DataFlow::OutputOfAny to that graph "
258              << ToNodeName << "(" << ToParameterName << ") " << State() << " " << Graph()->CoupledNodeName ()
259              << " sended recursively to the MacroNode coupled to that graph" << endl ;
260       Graph()->CoupledNode()->GraphEditor()->Executor()->OutputOfAny( Graph()->CoupledNodeName() ,
261                                                             ToParameterName ,
262 //JR 30.03.2005                                                            *anOutPort->Value() ) ;
263                                                             anOutPort->Value() ) ;
264     }
265     else {
266       cdebug << "GraphExecutor::DataFlow::OutputOfAny to Graph " << ToNodeName
267              << "(" << ToParameterName << ") ignored" << endl ;
268     }
269   }
270   cdebug_out << pthread_self() << "/" << ThreadNo() << " GraphExecutor::DataFlow::OutputOfAny " << RetVal
271              << endl ;
272   return RetVal ;
273 }
274
275 bool GraphExecutor::DataFlow::Kill() {
276   cdebug_in << "GraphExecutor::DataFlow::Kill()" << endl;
277   bool RetVal = GraphExecutor::OutNode::Kill() ;
278   cdebug_out << "GraphExecutor::DataFlow::Kill() " << RetVal << endl;
279   return RetVal ;
280 }
281
282 bool GraphExecutor::DataFlow::Kill( const char *aNodeName ) {
283 //  cdebug_in << "GraphExecutor::DataFlow::Kill( " << aNodeName << " )"<< endl;
284   bool RetVal = false ;
285   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
286   if ( aNode ) {
287     RetVal = aNode->Kill() ;
288   }
289 //  cdebug_out << "GraphExecutor::DataFlow::Kill" << endl;
290   return RetVal ;
291 }
292
293 bool GraphExecutor::DataFlow::KillDone( const char *aNodeName ) {
294 //  cdebug_in << "GraphExecutor::DataFlow::KillDone( " << aNodeName << " )"<< endl;
295   bool RetVal = false ;
296   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
297   if ( aNode ) {
298     RetVal = aNode->KillDone() ;
299   }
300 //  cdebug_out << "GraphExecutor::DataFlow::KillDone" << endl;
301   return RetVal ;
302 }
303
304 bool GraphExecutor::DataFlow::Suspend() {
305   cdebug_in << "GraphExecutor::DataFlow::Suspend()" << endl;
306   bool RetVal = GraphExecutor::OutNode::Suspend() ;
307   cdebug_out << "GraphExecutor::DataFlow::Suspend()" << RetVal << endl;
308   return RetVal ;
309 }
310
311 bool GraphExecutor::DataFlow::Suspend( const char *aNodeName ) {
312 //  cdebug_in << "GraphExecutor::DataFlow::Suspend( " << aNodeName << " )"<< endl;
313   bool RetVal = false ;
314   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
315   if ( aNode ) {
316     RetVal = aNode->Suspend() ;
317   }
318 //  cdebug_out << "GraphExecutor::DataFlow::Suspend" << endl;
319   return RetVal ;
320 }
321
322 bool GraphExecutor::DataFlow::SuspendDone() {
323   cdebug_in << "GraphExecutor::DataFlow::SuspendDone()" << endl;
324   bool RetVal = false ;
325   cdebug << "Kill not yet implemented." << endl;
326   cdebug_out << "GraphExecutor::DataFlow::SuspendDone()" << endl;
327   return RetVal ;
328 }
329
330 bool GraphExecutor::DataFlow::SuspendDone( const char *aNodeName ) {
331 //  cdebug_in << "GraphExecutor::DataFlow::SuspendDone( " << aNodeName << " )"<< endl;
332   bool RetVal = false ;
333   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
334   if ( aNode ) {
335     RetVal = aNode->SuspendDone() ;
336   }
337 //  cdebug_out << "GraphExecutor::DataFlow::SuspendDone" << endl;
338   return RetVal ;
339 }
340
341 bool GraphExecutor::DataFlow::Resume() {
342   cdebug_in << "GraphExecutor::DataFlow::Resume()" << endl;
343   bool RetVal = GraphExecutor::OutNode::Resume() ;
344   cdebug_out << "GraphExecutor::DataFlow::Resume()" << RetVal << endl;
345   return RetVal ;
346 }
347
348 bool GraphExecutor::DataFlow::Resume( const char *aNodeName ) {
349 //  cdebug_in << "GraphExecutor::DataFlow::Resume( " << aNodeName << " )"<< endl;
350   bool RetVal = false ;
351   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
352   if ( aNode ) {
353     RetVal = aNode->Resume() ;
354   }
355 //  cdebug_out << "GraphExecutor::DataFlow::Resume" << endl;
356   return RetVal ;
357 }
358
359 bool GraphExecutor::DataFlow::Stop() {
360   cdebug_in << "GraphExecutor::DataFlow::Stop()" << endl;
361   bool RetVal = GraphExecutor::OutNode::Stop() ;
362   cdebug_out << "GraphExecutor::DataFlow::Stop()" << endl;
363   return RetVal ;
364 }
365
366 bool GraphExecutor::DataFlow::Stop( const char *aNodeName ) {
367 //  cdebug_in << "GraphExecutor::DataFlow::Stop( " << aNodeName << " )"<< endl;
368   bool RetVal = false ;
369   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
370   if ( aNode ) {
371     RetVal = aNode->Stop() ;
372   }
373 //  cdebug_out << "GraphExecutor::DataFlow::Stop" << endl;
374   return RetVal ;
375 }
376