1 // SUPERV GraphExecutor : contains classes that permit execution of graphs and particularly the execution automaton
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : DataFlowBase_InNode.hxx
25 // Author : Jean Rahuel, CEA
29 #ifndef _DATAFLOWEXECUTOR_INNODE_HXX
30 #define _DATAFLOWEXECUTOR_INNODE_HXX
36 #include "SALOME_Container_i.hxx"
38 #include "DataFlowBase_Graph.hxx"
39 #include "DataFlowBase_FactoryNode.hxx"
40 #include "DataFlowBase_GOTONode.hxx"
41 #include "DataFlowBase_LoopNode.hxx"
42 #include "DataFlowBase_EndOfLoopNode.hxx"
43 #include "DataFlowBase_SwitchNode.hxx"
44 #include "DataFlowBase_EndOfSwitchNode.hxx"
46 #include "DataFlowExecutor_FiniteStateMachine.hxx"
48 #define MAXSTACKTHREADSIZE 127
50 void * run_function(void *p);
52 extern GraphExecutor::FiniteStateMachine * theAutomaton ;
54 extern "C" PyObject * PyRunMethod( PyObject * dummy , PyObject * args ) ;
56 namespace GraphExecutor {
58 struct ServicesAnyData {
65 // class InNode : public GraphBase::Node {
66 class InNode : public GraphBase::Base {
70 GraphBase::ComputingNode * _ComputingNode ;
71 GraphBase::FactoryNode * _FactoryNode ;
72 GraphBase::InLineNode * _InLineNode ;
73 GraphBase::GOTONode * _GOTONode ;
74 GraphBase::LoopNode * _LoopNode ;
75 GraphBase::EndOfLoopNode * _EndOfLoopNode ;
76 GraphBase::SwitchNode * _SwitchNode ;
77 GraphBase::EndOfSwitchNode * _EndOfSwitchNode ;
78 GraphBase::Graph * _GraphMacroNode ;
80 PyObject * _MyPyRunMethod ;
82 bool _createNewThread ;
83 bool _createNewThreadIf ;
85 GraphExecutor::AutomatonState _OldState ;
86 GraphExecutor::AutomatonState _currentState ;
87 GraphExecutor::NodeEvent _CurrentEvent ;
88 SUPERV::ControlState _ControlState ;
89 GraphExecutor::AutomatonState _NextState ;
90 GraphExecutor::StateEventAction _NextAction ;
94 pthread_mutex_t _MutexDataWait ;
97 pthread_mutex_t _MutexWait ;
99 pthread_cond_t _ReadyWait ;
100 pthread_cond_t _RunningWait ;
101 pthread_cond_t _DoneWait ;
102 pthread_cond_t _SuspendedWait ;
104 pthread_cond_t _SuspendWait ;
106 pthread_cond_t _ResumeWait ;
108 GraphExecutor::NodeEvent _aResumeEvent ;
109 GraphExecutor::InNode * _aReStartNode ;
110 GraphExecutor::NodeEvent _aReStartEvent ;
112 pthread_cond_t _KillWait ;
114 pthread_cond_t _StopWait ;
116 pthread_cond_t _ThreadStartedWait ;
117 bool _ThreadStartedSync ;
119 pthread_t _Pythread ;
123 GraphExecutor::FiniteStateMachine * _Automaton ;
127 GraphExecutor::OutNode * _OutNode ;
129 char * _DataFromNode ;
135 InNode( CORBA::ORB_ptr ORB, SALOME_NamingService* ptrNamingService,
136 const SALOME_ModuleCatalog::Service& NodeService ,
137 const char *NodeComponentName ,
138 const char* NodeInterfaceName ,
139 const char *NodeName ,
140 const SUPERV::KindOfNode akind = SUPERV::ComputingNode ,
141 GraphBase::ListOfFuncName aFuncName = GraphBase::ListOfFuncName() ,
142 GraphBase::ListOfPythonFunctions aPythonFunction = GraphBase::ListOfPythonFunctions() ,
143 const SUPERV::SDate NodeFirstCreation = SUPERV::SDate() ,
144 const SUPERV::SDate NodeLastModification = SUPERV::SDate() ,
145 const char * NodeEditorRelease = NULLSTRING ,
146 const char * NodeAuthor = NULLSTRING ,
147 const char * NodeComputer = NULLSTRING ,
148 const char * NodeComment = NULLSTRING ,
149 const bool GeneratedName = false ,
150 const int NodeX = 0 ,
151 const int NodeY = 0 ,
152 int * Graph_prof_debug = NULL ,
153 ofstream * Graph_fdebug = NULL ) ;
156 pthread_t ThreadNo() {
157 return _ComputingNode->ThreadNo() ; } ;
158 void ThreadNo( pthread_t aThread ) {
159 _ComputingNode->ThreadNo ( aThread ) ; } ;
161 char * Name() const {
162 return _ComputingNode->Name() ; } ;
163 const char *const * NamePtr() const {
164 return _ComputingNode->NamePtr() ; } ;
165 SUPERV::KindOfNode Kind() const {
166 return _ComputingNode->Kind() ; } ;
167 const bool IsComputingNode() const {
168 return _ComputingNode->IsComputingNode() ; } ;
169 const bool IsFactoryNode() const {
170 return _ComputingNode->IsFactoryNode() ; } ;
171 const bool IsInLineNode() const {
172 return _ComputingNode->IsInLineNode() ; } ;
173 const bool IsOneOfInLineNodes() const {
174 return _ComputingNode->IsOneOfInLineNodes() ; } ;
175 const bool IsOneOfGOTONodes() const {
176 return _ComputingNode->IsOneOfGOTONodes() ; } ;
177 const bool IsMacroNode() const {
178 return _ComputingNode->IsMacroNode() ; } ;
179 const bool IsDataFlowNode() const {
180 return _ComputingNode->IsDataFlowNode() ; } ;
181 const bool IsDataStreamNode() const {
182 return _ComputingNode->IsDataStreamNode() ; } ;
183 const bool IsLoopNode() const {
184 return _ComputingNode->IsLoopNode() ; } ;
185 const bool IsEndLoopNode() const {
186 return _ComputingNode->IsEndLoopNode() ; } ;
187 const bool IsSwitchNode() const {
188 return _ComputingNode->IsSwitchNode() ; } ;
189 const bool IsEndSwitchNode() const {
190 return _ComputingNode->IsEndSwitchNode() ; } ;
191 const bool IsGOTONode() const {
192 return _ComputingNode->IsGOTONode() ; } ;
193 const bool IsHeadNode() const {
194 return _ComputingNode->IsHeadNode() ; } ;
195 GraphBase::ComputingNode * ComputingNode() {
196 return _ComputingNode ; } ;
197 GraphBase::FactoryNode * FactoryNode() {
198 return _FactoryNode ; } ;
199 GraphBase::GOTONode * GOTONode() {
204 if ( _EndOfLoopNode )
205 return _EndOfLoopNode ;
208 if ( _EndOfSwitchNode )
209 return _EndOfSwitchNode ;
212 GraphBase::InLineNode * InLineNode() {
213 GraphBase::InLineNode * aNode = GOTONode() ;
218 GraphBase::LoopNode * LoopNode() {
219 return _LoopNode ; } ;
220 GraphBase::Graph * GraphMacroNode() {
221 return _GraphMacroNode ; } ;
223 SUPERV::CNode_var ObjRef() const { return _ComputingNode->ObjRef() ; } ;
224 void SetObjRef( SUPERV::CNode_var aNode ) {
225 _ComputingNode->SetObjRef( aNode ) ; } ;
227 CNode_Impl * ObjImpl() const { return _ComputingNode->ObjImpl() ; } ;
228 void SetObjImpl( CNode_Impl * aGraph ) {
229 _ComputingNode->SetObjImpl( aGraph ) ; } ;
231 Engines::Component_var Component() const ;
232 Engines::Container_var Container() const ;
233 void SetContainer(Engines::Container_var aContainer) {
234 _FactoryNode->SetContainer( aContainer ) ; } ;
235 void SetComponent(Engines::Component_var anObjComponent) {
236 _FactoryNode->SetComponent( anObjComponent ) ; } ;
237 void ObjInterface( bool k_interface ) {
238 _ComputingNode->ObjInterface( k_interface ) ; } ;
239 bool ObjInterface() {
240 return _FactoryNode->ObjInterface() ; } ;
241 char * ComponentName() const { return _FactoryNode->ComponentName() ; } ;
242 char * InterfaceName() const { return _FactoryNode->InterfaceName() ; } ;
243 char * Computer() const { return _FactoryNode->Computer() ; } ;
244 const char * ServiceName() const {
245 return _ComputingNode->ServiceName() ; } ;
246 const SALOME_ModuleCatalog::ListOfServicesParameter ServiceInParameter() const {
247 return _ComputingNode->ServiceInParameter() ; } ;
248 const SALOME_ModuleCatalog::ListOfServicesParameter ServiceOutParameter() const {
249 return _ComputingNode->ServiceOutParameter() ; } ;
251 void CoupledNode( GraphBase::InLineNode * aCoupledNode ) {
252 GOTONode()->CoupledNode( aCoupledNode ) ; } ;
253 GraphBase::InLineNode * CoupledNode() {
254 return GOTONode()->CoupledNode() ; } ;
256 GraphBase::InPort * AddInPort( const char * InputParameterName ,
257 const char * InputParameterType ,
258 const SUPERV::KindOfPort aKindOfPort ) {
259 return _ComputingNode->AddInPort( InputParameterName ,
262 GraphBase::OutPort * AddOutPort( const char * OutputParameterName ,
263 const char * OutputParameterType ,
264 const SUPERV::KindOfPort aKindOfPort ) {
265 return _ComputingNode->AddOutPort( OutputParameterName ,
266 OutputParameterType ,
268 // void InOutPort( GraphBase::InPort * InputPort ,
269 // GraphBase::OutPort * OutputPort ) {
270 // return _ComputingNode->InOutPort( InputPort , OutputPort ) ; } ;
271 int LinkedNodesSize() const {
272 return _ComputingNode->LinkedNodesSize() ; } ;
273 // GraphBase::ComputingNode * LinkedNodes( int i ) const {
274 GraphBase::StreamNode * LinkedNodes( int i ) const {
275 return _ComputingNode->LinkedNodes( i ) ; } ;
276 const int LinkedInPortsNumber( int i ) const {
277 return _ComputingNode->LinkedInPortsNumber( i ) ; } ;
279 const int GetNodeInPortsSize() const {
280 return _ComputingNode->GetNodeInPortsSize() ; } ;
281 const GraphBase::InPort *GetNodeInLoop() const {
282 return _ComputingNode->GetNodeInLoop() ; } ;
283 const GraphBase::InPort *GetNodeInGate() const {
284 return _ComputingNode->GetNodeInGate() ; } ;
285 const GraphBase::InPort *GetNodeInPort(int i) const {
286 return _ComputingNode->GetNodeInPort( i ) ; } ;
287 GraphBase::InPort *GetChangeNodeInLoop() const {
288 return _ComputingNode->GetChangeNodeInLoop() ; } ;
289 GraphBase::InPort *GetChangeNodeInGate() const {
290 return _ComputingNode->GetChangeNodeInGate() ; } ;
291 GraphBase::InPort *GetChangeNodeInPort(int i) const {
292 return _ComputingNode->GetChangeNodeInPort( i ) ; } ;
293 const int GetNodeOutPortsSize() const {
294 return _ComputingNode->GetNodeOutPortsSize() ; } ;
295 const GraphBase::OutPort *GetNodeOutLoop() const {
296 return _ComputingNode->GetNodeOutLoop() ; } ;
297 const GraphBase::OutPort *GetNodeOutGate() const {
298 return _ComputingNode->GetNodeOutGate() ; } ;
299 const GraphBase::OutPort *GetNodeOutPort(int i) const {
300 return _ComputingNode->GetNodeOutPort( i ) ; } ;
301 GraphBase::OutPort *GetChangeNodeOutLoop() const {
302 return _ComputingNode->GetChangeNodeOutLoop() ; } ;
303 GraphBase::OutPort *GetChangeNodeOutGate() const {
304 return _ComputingNode->GetChangeNodeOutGate() ; } ;
305 GraphBase::OutPort *GetChangeNodeOutPort(int i) const {
306 return _ComputingNode->GetChangeNodeOutPort( i ) ; } ;
308 const GraphBase::InPort *GetInPort( const char *name ) {
309 return _ComputingNode->GetInPort( name ) ; } ;
310 const GraphBase::OutPort *GetOutPort( const char *name ) {
311 return _ComputingNode->GetOutPort( name ) ; } ;
312 GraphBase::InPort *GetChangeInPort( const char *name ) {
313 return _ComputingNode->GetChangeInPort( name ) ; } ;
314 GraphBase::OutPort *GetChangeOutPort( const char *name ) {
315 return _ComputingNode->GetChangeOutPort( name ) ; } ;
317 void PyFuncRunned( bool arunned ) {
318 _PyFuncRunned = arunned ; } ;
319 bool PyFuncRunned() {
320 return _PyFuncRunned ; } ;
322 void OutNode( GraphExecutor::OutNode * theOutNode ) {
323 _OutNode = theOutNode ; } ;
326 PyObject * InitPyDynInvoke( char * PyFuncName ,
327 const SUPERV::ListOfStrings * aPythonFunction ,
329 void RemovePyDynInvoke( char * PyFuncName ) ;
331 void LockDataWait() ;
332 void UnLockDataWait() ;
333 bool IsLockedDataWait() { return _DataWait ; } ;
336 bool ContainerKill() ;
345 void CreateNewThread( bool k_create ) { _createNewThread = k_create ; } ;
346 void CreateNewThreadIf( bool k_create ) { _createNewThreadIf = k_create ; } ;
347 bool CreateNewThread() { return _createNewThread ; } ;
348 bool CreateNewThreadIf() { return _createNewThreadIf ; } ;
349 void NewThread( pthread_t aThread ) ;
351 void RewindStack( int aRewindStack ) { _RewindStack = aRewindStack ; } ;
352 int RewindStack() const { return _RewindStack ; } ;
354 GraphExecutor::AutomatonState State() const {
355 return _currentState; };
356 void State(GraphExecutor::AutomatonState aState ) {
357 // cdebug << "GraphExecutor::InNode::State( "
358 // << Automaton()->StateName( _currentState ) << " --> "
359 // << Automaton()->StateName( aState ) << " )" << endl ;
360 _currentState = aState ; } ;
361 SUPERV::ControlState ControlState() const {
362 return _ControlState; };
363 void ControlState(SUPERV::ControlState aControlState ) {
364 _ControlState = aControlState ; } ;
365 void ControlClear() {
366 _ControlState = SUPERV::VoidState ; } ;
368 void SetAutomaton() {
369 _Automaton = theAutomaton ; } ;
370 GraphExecutor::FiniteStateMachine * Automaton() const {
371 return _Automaton ; } ;
380 void IsLoading( bool Loading );
381 bool IsLoading() { return _Loading ; } ;
383 bool StateWait( SUPERV::GraphState aState ) ;
387 bool SuspendedWait() ;
389 void InitialState() ;
390 bool InitPythonFunctions(bool WithErr ) ;
391 void SetWaitingStates(GraphExecutor::InNode * EndNode ) ;
393 int SendEvent(const GraphExecutor::NodeEvent anEvent ) ;
394 void DataFromNode( char * FromNodeName ) {
395 _DataFromNode = FromNodeName ; } ;
396 const char * DataFromNode() const { return _DataFromNode ; } ;
401 void RunningAction() ;
403 void SuspendedAction() ;
404 GraphExecutor::InNode * SuspendAction() ;
405 bool ResumeAction(GraphExecutor::NodeEvent aResumeEvent ) ;
406 bool ReStartAction( GraphExecutor::InNode * aRestartNode ,
407 GraphExecutor::NodeEvent anEvent ) ;
409 void KilledAction() ;
410 void ThreadStartAction() ;
411 void ThreadStartedAction() ;
413 void StoppedAction() ;
414 int executeAction() ; // New Thread or Same Thread
415 int ExecuteAction() ;
417 int DataWaiting_SomeDataReadyAction() ;
418 int DataUndef_NotAllDataReadyAction() ;
419 int DataUndef_AllDataReadyAction() ;
420 int DataReady_SuspendAction() ;
421 int SuspendedReady_ResumeAction() ;
422 int DataReady_KillAction() ;
423 int DataReady_StopAction() ;
424 int DataReady_ExecuteAction() ;
425 void DynInvoke( Engines::Component_ptr obj,
427 ServicesAnyData * inParams, int nInParams,
428 ServicesAnyData * outParams, int nOutParams) ;
429 void DynInvoke( Engines::Component_ptr obj,
431 const char * aGraphName ,
432 const char * aNodeName );
433 bool PyDynInvoke( PyObject * MyPyRunMethod ,
435 ServicesAnyData * inParams , int nInParams ,
436 ServicesAnyData * inParams , int nOutParams ) ;
438 int Executing_SuspendAction() ;
439 int SuspendedExecuting_ResumeAction() ;
440 int Executing_KillAction() ;
441 int Executing_StopAction() ;
442 int Executing_SuccessAction() ;
443 int Executing_ErrorAction() ;
444 int Successed_SuccessAction() ;
445 bool SendSomeDataReady( char * FromNodeName ) ;
446 int Errored_ErrorAction() ;
447 int Successed_SuspendAction() ;
448 int Errored_SuspendAction() ;
449 int SuspendedSuccessed_ResumeAction() ;
450 int SuspendedErrored_ResumeAction() ;
451 int Successed_KillAction() ;
452 int Errored_KillAction() ;
453 int Successed_StopAction() ;
454 int Errored_StopAction() ;
455 int SuspendedSuccessed_ReStartAction() ;
456 int SuspendedErrored_ReStartAction() ;
457 int SuspendedSuccessed_ReStartAndSuspendAction() ;
458 int SuspendedErrored_ReStartAndSuspendAction() ;
460 void InParametersSet( bool & Err ,
462 ServicesAnyData * InParametersList ) ;
463 void InOutParametersSet( int nOutParams ,
464 ServicesAnyData * OutParametersList ) ;
465 bool OutParametersSet( bool Err , SUPERV::GraphState NewState ,
467 ServicesAnyData * OutParametersList ) ;
471 const long CpuUsed( bool tot = false ) ;
473 long PyCpuUsed( bool tot = false ) ;
474 void SetPyCpuUsed() ;
477 bool PyRunSimpleString( char* thePyString );
478 PyObject * PyEvalCallObject( PyObject * MyPyRunMethod ,
479 PyObject * ArgsList ) ;