Salome HOME
SMH: 3.0.0 preparation = merged version (POLYWORK + RTVDEBUG01) + adopation for new GUI
[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->PortState()
130            << " Done : " << anOutPort->PortDone() << endl ;
131     RetVal = AddInputData( Graph()->Name() , ToServiceParameterName , aValue ) ;
132     anOutPort->PortState(  SUPERV::ReadyState ) ;
133 // There is only one port :
134     anOutPort->ChangeInPorts( 0 )->PortState( SUPERV::ReadyState ) ;
135     anOutPort->PortDone( 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 //JR 30.03.2005                                                            *anOutPort->Value() ) ;
222                                                             anOutPort->Value() ) ;
223     }
224     else {
225       cdebug << "GraphExecutor::DataFlow::OutputOfAny to Graph " << ToNodeName
226              << "(" << ToParameterName << ") ignored" << endl ;
227     }
228   }
229   cdebug_out << pthread_self() << "/" << ThreadNo() << " GraphExecutor::DataFlow::OutputOfAny " << RetVal
230              << endl ;
231   return RetVal ;
232 }
233
234 bool GraphExecutor::DataFlow::Kill() {
235   cdebug_in << "GraphExecutor::DataFlow::Kill()" << endl;
236   bool RetVal = GraphExecutor::OutNode::Kill() ;
237   cdebug_out << "GraphExecutor::DataFlow::Kill() " << RetVal << endl;
238   return RetVal ;
239 }
240
241 bool GraphExecutor::DataFlow::Kill( const char *aNodeName ) {
242 //  cdebug_in << "GraphExecutor::DataFlow::Kill( " << aNodeName << " )"<< endl;
243   bool RetVal = false ;
244   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
245   if ( aNode ) {
246     RetVal = aNode->Kill() ;
247   }
248 //  cdebug_out << "GraphExecutor::DataFlow::Kill" << endl;
249   return RetVal ;
250 }
251
252 bool GraphExecutor::DataFlow::KillDone( const char *aNodeName ) {
253 //  cdebug_in << "GraphExecutor::DataFlow::KillDone( " << aNodeName << " )"<< endl;
254   bool RetVal = false ;
255   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
256   if ( aNode ) {
257     RetVal = aNode->KillDone() ;
258   }
259 //  cdebug_out << "GraphExecutor::DataFlow::KillDone" << endl;
260   return RetVal ;
261 }
262
263 bool GraphExecutor::DataFlow::Suspend() {
264   cdebug_in << "GraphExecutor::DataFlow::Suspend()" << endl;
265   bool RetVal = GraphExecutor::OutNode::Suspend() ;
266   cdebug_out << "GraphExecutor::DataFlow::Suspend()" << RetVal << endl;
267   return RetVal ;
268 }
269
270 bool GraphExecutor::DataFlow::Suspend( const char *aNodeName ) {
271 //  cdebug_in << "GraphExecutor::DataFlow::Suspend( " << aNodeName << " )"<< endl;
272   bool RetVal = false ;
273   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
274   if ( aNode ) {
275     RetVal = aNode->Suspend() ;
276   }
277 //  cdebug_out << "GraphExecutor::DataFlow::Suspend" << endl;
278   return RetVal ;
279 }
280
281 bool GraphExecutor::DataFlow::SuspendDone() {
282   cdebug_in << "GraphExecutor::DataFlow::SuspendDone()" << endl;
283   bool RetVal = false ;
284   cdebug << "Kill not yet implemented." << endl;
285   cdebug_out << "GraphExecutor::DataFlow::SuspendDone()" << endl;
286   return RetVal ;
287 }
288
289 bool GraphExecutor::DataFlow::SuspendDone( const char *aNodeName ) {
290 //  cdebug_in << "GraphExecutor::DataFlow::SuspendDone( " << aNodeName << " )"<< endl;
291   bool RetVal = false ;
292   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
293   if ( aNode ) {
294     RetVal = aNode->SuspendDone() ;
295   }
296 //  cdebug_out << "GraphExecutor::DataFlow::SuspendDone" << endl;
297   return RetVal ;
298 }
299
300 bool GraphExecutor::DataFlow::Resume() {
301   cdebug_in << "GraphExecutor::DataFlow::Resume()" << endl;
302   bool RetVal = GraphExecutor::OutNode::Resume() ;
303   cdebug_out << "GraphExecutor::DataFlow::Resume()" << RetVal << endl;
304   return RetVal ;
305 }
306
307 bool GraphExecutor::DataFlow::Resume( const char *aNodeName ) {
308 //  cdebug_in << "GraphExecutor::DataFlow::Resume( " << aNodeName << " )"<< endl;
309   bool RetVal = false ;
310   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
311   if ( aNode ) {
312     RetVal = aNode->Resume() ;
313   }
314 //  cdebug_out << "GraphExecutor::DataFlow::Resume" << endl;
315   return RetVal ;
316 }
317
318 bool GraphExecutor::DataFlow::Stop() {
319   cdebug_in << "GraphExecutor::DataFlow::Stop()" << endl;
320   bool RetVal = GraphExecutor::OutNode::Stop() ;
321   cdebug_out << "GraphExecutor::DataFlow::Stop()" << endl;
322   return RetVal ;
323 }
324
325 bool GraphExecutor::DataFlow::Stop( const char *aNodeName ) {
326 //  cdebug_in << "GraphExecutor::DataFlow::Stop( " << aNodeName << " )"<< endl;
327   bool RetVal = false ;
328   GraphExecutor::InNode * aNode = ((GraphExecutor::InNode *) Graph()->GetGraphNode( aNodeName )->GetInNode()) ;
329   if ( aNode ) {
330     RetVal = aNode->Stop() ;
331   }
332 //  cdebug_out << "GraphExecutor::DataFlow::Stop" << endl;
333   return RetVal ;
334 }
335