Salome HOME
Python execution management : Lock/Unlock
[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" << endl ;
122   bool RetVal = false ;
123   cdebug << pthread_self() << "GraphExecutor::DataFlow::::InputOfAny " << Graph()->Name() << "("
124          << ToServiceParameterName << ")" << endl ;
125   if ( Graph()->GraphMacroLevel() ) {
126     GraphBase::OutPort * anOutPort ;
127     anOutPort = Graph()->GetChangeInDataNodePort( ToServiceParameterName ) ;
128     cdebug << pthread_self() << "GraphExecutor::DataFlow::InputOfAny " << Graph()->Name()
129            << " " << State() << " " << ToServiceParameterName << " " << anOutPort->State()
130            << " Done : " << anOutPort->Done() << endl ;
131     RetVal = AddInputData( Graph()->Name() , ToServiceParameterName , aValue ) ;
132     anOutPort->State(  SUPERV::ReadyState ) ;
133 // There is only one port :
134     anOutPort->ChangeInPorts( 0 )->State( SUPERV::ReadyState ) ;
135     anOutPort->Done( true ) ;
136 // There is only one inport of a Node in an ReversedOutport of a graph :
137     GraphExecutor::InNode * anInNode = (GraphExecutor::InNode * ) Graph()->GetChangeGraphNode( anOutPort->InPorts( 0 )->NodeName() )->GetInNode() ;
138     cdebug << pthread_self() << "GraphExecutor::DataFlow::InputOfAny : " << anInNode->Name()
139            << "->SendSomeDataReady( " << Graph()->Name() << " ) for Port " << anOutPort->InPorts( 0 )->PortName()
140            << endl ;
141     if ( SomeDataReady ) {
142       MESSAGE( "GraphExecutor::InputOfAny " << Graph()->Name() << " SendSomeDataReady --> " << anInNode->Name()
143                << " " << anInNode->State() << " " << anOutPort->InPorts( 0 )->PortName() ) ;
144       int sts = anInNode->SendSomeDataReady( Graph()->Name() ) ;
145       cdebug << "GraphExecutor::DataFlow::InputOfAny " << anInNode->Name()
146              << "->SendSomeDataReady( " << Graph()->Name() << " ) sts " << sts << endl ;
147       if ( sts && anInNode->IsReady() ) {
148         cdebug << pthread_self() << "/" << anInNode->ThreadNo() << "GraphExecutor::DataFlow::InputOfAny : "
149                << anInNode->Name() << "->SendEvent( GraphExecutor::ExecuteEvent ) "
150                << " " << anInNode->Name() << "->IsReady() " << anInNode->IsReady() << " State "
151                << anInNode->State() << endl ;
152         if ( anInNode->IsLockedDataWait() ) {
153           cdebug << pthread_self() << "/" << anInNode->ThreadNo() << "GraphExecutor::DataFlow::InputOfAny : "
154                  << anInNode->Name() << " IsLockedDataWait() ==> UnLockDataWait" << endl ;
155           anInNode->UnLockDataWait() ;
156         }
157         anInNode->ThreadNo( 0 ) ;
158         anInNode->CreateNewThread( true ) ;
159         anInNode->SendEvent( GraphExecutor::ExecuteEvent ) ;
160 //        State( GraphExecutor::ExecutingState ) ;
161       }
162     }
163   }
164   else {
165     cdebug << pthread_self() << "GraphExecutor::DataFlow::InputOfAny GraphMacroLevel "
166            << Graph()->GraphMacroLevel() << " ERROR" << endl ;
167     MESSAGE( "GraphExecutor::DataFlow::InputOfAny GraphMacroLevel " << Graph()->GraphMacroLevel()
168              << " ERROR" ) ;
169   }
170   cdebug_out << pthread_self() << " GraphExecutor::DataFlow::InputOfAny " << RetVal << endl ;
171   return RetVal ;
172 }
173
174 bool GraphExecutor::DataFlow::OutputOfAny( const char * aNodeName ,
175                                            const char * ToServiceParameterName ,
176                                            const CORBA::Any & aValue ) {
177   cdebug_in << pthread_self() << "/" << ThreadNo() << "GraphExecutor::DataFlow::OutputOfAny( " << aNodeName
178             << " , " << ToServiceParameterName
179             << " , aValue ) from " << Graph()->Name() << endl ;
180   bool RetVal = false ;
181   GraphBase::Graph * aMacroNode = (GraphBase::Graph * ) Graph()->GetChangeGraphNode( aNodeName ) ;
182 //  GraphExecutor::InNode * anInNode = (GraphExecutor::InNode * ) aMacroGraph->GetInNode() ;
183   RetVal = Graph()->AddOutputData( aNodeName , ToServiceParameterName , aValue ) ;
184   GraphBase::OutPort * anOutPort = aMacroNode->GetChangeOutPort( ToServiceParameterName ) ;
185   int i ;
186 // Loop over Inports linked to that OutPort of the MacroNode
187   for ( i = 0 ; i < anOutPort->InPortsSize() ; i++ ) {
188     const char * ToNodeName = anOutPort->ChangeInPorts( i )->NodeName() ;
189     const char * ToParameterName = anOutPort->ChangeInPorts( i )->PortName() ;
190     GraphBase::ComputingNode * aComputingNode = Graph()->GetChangeGraphNode( ToNodeName ) ;
191     if ( strcmp( ToNodeName , Graph()->Name() ) ) {
192       GraphExecutor::InNode * aLinkedNode = (GraphExecutor::InNode * ) aComputingNode->GetInNode() ;
193       cdebug << pthread_self() << "/" << aLinkedNode->ThreadNo()
194              << "GraphExecutor::DataFlow::OutputOfAny to Node "
195              << ToNodeName << "(" << ToParameterName << ") from MacroNode " << aNodeName << endl ;
196       int sts ;
197       if ( aLinkedNode->IsLockedDataWait() ) {
198         cdebug << "GraphExecutor::DataFlow::OutputOfAny " << aLinkedNode->Name()
199                << " IsLockedDataWait --> UnLockDataWait" << endl ;
200       }
201       sts = aLinkedNode->SendSomeDataReady( (char * ) aNodeName ) ;
202       cdebug << pthread_self() << "/" << aLinkedNode->ThreadNo()
203              << "GraphExecutor::DataFlow::OutputOfAny " << aLinkedNode->Name()
204              << "->SendSomeDataReady( " << aNodeName << " ) sts " << sts << " " << aLinkedNode->State() << endl ;
205       if ( sts ) {
206         if ( aLinkedNode->State() == GraphExecutor::DataReadyState ) {
207           aLinkedNode->CreateNewThreadIf( true ) ;
208           aLinkedNode->UnLockDataWait() ;
209 //          aLinkedNode->DataUndef_AllDataReadyAction() ;
210           aLinkedNode->SendEvent( GraphExecutor::ExecuteEvent ) ;
211         }
212       }
213     }
214     else if ( Graph()->GraphMacroLevel() != 0 ) {
215       cdebug << pthread_self() << "/" << Graph()->CoupledNode()->ThreadNo()
216              << "GraphExecutor::DataFlow::OutputOfAny to that graph "
217              << ToNodeName << "(" << ToParameterName << ") " << State() << " " << Graph()->CoupledNodeName ()
218              << " sended recursively to the MacroNode coupled to that graph" << endl ;
219       Graph()->CoupledNode()->GraphEditor()->Executor()->OutputOfAny( Graph()->CoupledNodeName() ,
220                                                             ToParameterName ,
221                                                             *anOutPort->Value() ) ;
222     }
223     else {
224       cdebug << "GraphExecutor::DataFlow::OutputOfAny to Graph " << ToNodeName
225              << "(" << ToParameterName << ") ignored" << endl ;
226     }
227   }
228   cdebug_out << pthread_self() << "/" << ThreadNo() << " GraphExecutor::DataFlow::OutputOfAny " << RetVal
229              << endl ;
230   return RetVal ;
231 }
232
233 bool GraphExecutor::DataFlow::Kill() {
234   cdebug_in << "GraphExecutor::DataFlow::Kill()" << endl;
235   bool RetVal = GraphExecutor::OutNode::Kill() ;
236   cdebug_out << "GraphExecutor::DataFlow::Kill() " << RetVal << endl;
237   return RetVal ;
238 }
239
240 bool GraphExecutor::DataFlow::Kill( const char *aNodeName ) {
241 //  cdebug_in << "GraphExecutor::DataFlow::Kill( " << aNodeName << " )"<< endl;
242   bool RetVal = false ;
243   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
244   if ( aNode ) {
245     RetVal = aNode->Kill() ;
246   }
247 //  cdebug_out << "GraphExecutor::DataFlow::Kill" << endl;
248   return RetVal ;
249 }
250
251 bool GraphExecutor::DataFlow::KillDone( const char *aNodeName ) {
252 //  cdebug_in << "GraphExecutor::DataFlow::KillDone( " << aNodeName << " )"<< endl;
253   bool RetVal = false ;
254   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
255   if ( aNode ) {
256     RetVal = aNode->KillDone() ;
257   }
258 //  cdebug_out << "GraphExecutor::DataFlow::KillDone" << endl;
259   return RetVal ;
260 }
261
262 bool GraphExecutor::DataFlow::Suspend() {
263   cdebug_in << "GraphExecutor::DataFlow::Suspend()" << endl;
264   bool RetVal = GraphExecutor::OutNode::Suspend() ;
265   cdebug_out << "GraphExecutor::DataFlow::Suspend()" << RetVal << endl;
266   return RetVal ;
267 }
268
269 bool GraphExecutor::DataFlow::Suspend( const char *aNodeName ) {
270 //  cdebug_in << "GraphExecutor::DataFlow::Suspend( " << aNodeName << " )"<< endl;
271   bool RetVal = false ;
272   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
273   if ( aNode ) {
274     RetVal = aNode->Suspend() ;
275   }
276 //  cdebug_out << "GraphExecutor::DataFlow::Suspend" << endl;
277   return RetVal ;
278 }
279
280 bool GraphExecutor::DataFlow::SuspendDone() {
281   cdebug_in << "GraphExecutor::DataFlow::SuspendDone()" << endl;
282   bool RetVal = false ;
283   cdebug << "Kill not yet implemented." << endl;
284   cdebug_out << "GraphExecutor::DataFlow::SuspendDone()" << endl;
285   return RetVal ;
286 }
287
288 bool GraphExecutor::DataFlow::SuspendDone( const char *aNodeName ) {
289 //  cdebug_in << "GraphExecutor::DataFlow::SuspendDone( " << aNodeName << " )"<< endl;
290   bool RetVal = false ;
291   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
292   if ( aNode ) {
293     RetVal = aNode->SuspendDone() ;
294   }
295 //  cdebug_out << "GraphExecutor::DataFlow::SuspendDone" << endl;
296   return RetVal ;
297 }
298
299 bool GraphExecutor::DataFlow::Resume() {
300   cdebug_in << "GraphExecutor::DataFlow::Resume()" << endl;
301   bool RetVal = GraphExecutor::OutNode::Resume() ;
302   cdebug_out << "GraphExecutor::DataFlow::Resume()" << RetVal << endl;
303   return RetVal ;
304 }
305
306 bool GraphExecutor::DataFlow::Resume( const char *aNodeName ) {
307 //  cdebug_in << "GraphExecutor::DataFlow::Resume( " << aNodeName << " )"<< endl;
308   bool RetVal = false ;
309   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
310   if ( aNode ) {
311     RetVal = aNode->Resume() ;
312   }
313 //  cdebug_out << "GraphExecutor::DataFlow::Resume" << endl;
314   return RetVal ;
315 }
316
317 bool GraphExecutor::DataFlow::Stop() {
318   cdebug_in << "GraphExecutor::DataFlow::Stop()" << endl;
319   bool RetVal = GraphExecutor::OutNode::Stop() ;
320   cdebug_out << "GraphExecutor::DataFlow::Stop()" << endl;
321   return RetVal ;
322 }
323
324 bool GraphExecutor::DataFlow::Stop( const char *aNodeName ) {
325 //  cdebug_in << "GraphExecutor::DataFlow::Stop( " << aNodeName << " )"<< endl;
326   bool RetVal = false ;
327   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
328   if ( aNode ) {
329     RetVal = aNode->Stop() ;
330   }
331 //  cdebug_out << "GraphExecutor::DataFlow::Stop" << endl;
332   return RetVal ;
333 }
334