]> SALOME platform Git repositories - modules/superv.git/blob - src/GraphExecutor/DataFlowExecutor_InNode.hxx
Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/superv.git] / src / GraphExecutor / DataFlowExecutor_InNode.hxx
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_InNode.hxx
25 //  Author : Jean Rahuel, CEA
26 //  Module : SUPERV
27 //  $Header:
28
29 #ifndef _DATAFLOWEXECUTOR_INNODE_HXX
30 #define _DATAFLOWEXECUTOR_INNODE_HXX
31
32 #include <stdio.h>
33
34 #include <Python.h>
35
36 #include "DataFlowBase_Graph.hxx"
37 #include "DataFlowBase_FactoryNode.hxx"
38 #include "DataFlowBase_GOTONode.hxx"
39 #include "DataFlowBase_LoopNode.hxx"
40 #include "DataFlowBase_EndOfLoopNode.hxx"
41 #include "DataFlowBase_SwitchNode.hxx"
42 #include "DataFlowBase_EndOfSwitchNode.hxx"
43
44 #include "DataFlowExecutor_FiniteStateMachine.hxx"
45
46 #define MAXSTACKTHREADSIZE 127
47
48 void * run_function(void *p);
49
50 extern GraphExecutor::FiniteStateMachine * theAutomaton ;
51
52 extern "C" PyObject * PyRunMethod( PyObject * dummy , PyObject * args ) ;
53
54 namespace GraphExecutor {
55
56   struct ServicesAnyData {
57     string     Name;
58     CORBA::Any Value;
59   };
60
61   class OutNode ;
62
63 //  class InNode : public GraphBase::Node {
64   class InNode : public GraphBase::Base {
65
66     private:
67
68       GraphBase::ComputingNode       * _ComputingNode ;
69       GraphBase::FactoryNode         * _FactoryNode ;
70       GraphBase::InLineNode          * _InLineNode ;
71       GraphBase::GOTONode            * _GOTONode ;
72       GraphBase::LoopNode            * _LoopNode ;
73       GraphBase::EndOfLoopNode       * _EndOfLoopNode ;
74       GraphBase::SwitchNode          * _SwitchNode ;
75       GraphBase::EndOfSwitchNode     * _EndOfSwitchNode ;
76       GraphBase::Graph               * _GraphMacroNode ;
77
78       PyObject                       * _MyPyRunMethod ;
79
80       bool                             _createNewThread ;
81       bool                             _createNewThreadIf ;
82       int                              _RewindStack ;
83       GraphExecutor::AutomatonState    _OldState ;
84       GraphExecutor::AutomatonState    _currentState ;
85       GraphExecutor::NodeEvent         _CurrentEvent ;
86       SUPERV::ControlState             _ControlState ;
87       GraphExecutor::AutomatonState    _NextState ;
88       GraphExecutor::StateEventAction  _NextAction ;
89       bool                             _PyFuncRunned ;
90     
91       pthread_mutex_t                  _MutexDataWait ;
92       bool                             _DataWait ;
93
94       pthread_mutex_t                  _MutexWait ;
95
96       pthread_cond_t                   _ReadyWait ;
97       pthread_cond_t                   _RunningWait ;
98       pthread_cond_t                   _DoneWait ;
99       pthread_cond_t                   _SuspendedWait ;
100
101       pthread_cond_t                   _SuspendWait ;
102       bool                             _SuspendSync ;
103       pthread_cond_t                   _ResumeWait ;
104       bool                             _ResumeSync ;
105       GraphExecutor::NodeEvent         _aResumeEvent ;
106       GraphExecutor::InNode *          _aReStartNode ;
107       GraphExecutor::NodeEvent         _aReStartEvent ;
108
109       pthread_cond_t                   _KillWait ;
110       bool                             _KillSync ;
111       pthread_cond_t                   _StopWait ;
112
113       pthread_cond_t                   _ThreadStartedWait ;
114       bool                             _ThreadStartedSync ;
115
116       pthread_t                        _Pythread ;
117       long                             _PyCpuUsed ;
118       long                             _PyTotCpuUsed ;
119
120       GraphExecutor::FiniteStateMachine * _Automaton ;
121     
122       CORBA::ORB_ptr                    _Orb;
123
124       GraphExecutor::OutNode          * _OutNode ;
125
126       char                            * _DataFromNode ;
127
128     public:
129
130       InNode() ;
131       InNode( CORBA::ORB_ptr ORB, SALOME_NamingService* ptrNamingService,
132               const SALOME_ModuleCatalog::Service& NodeService ,
133               const char *NodeComponentName ,
134               const char* NodeInterfaceName ,
135               const char *NodeName ,
136               const SUPERV::KindOfNode akind = SUPERV::ComputingNode ,
137               GraphBase::ListOfFuncName aFuncName = GraphBase::ListOfFuncName() ,
138               GraphBase::ListOfPythonFunctions aPythonFunction = GraphBase::ListOfPythonFunctions() ,
139               const SUPERV::SDate NodeFirstCreation = SUPERV::SDate() ,
140               const SUPERV::SDate NodeLastModification = SUPERV::SDate() ,
141               const char * NodeEditorRelease = NULLSTRING ,
142               const char * NodeAuthor = NULLSTRING ,
143               const char * NodeComputer = NULLSTRING ,
144               const char * NodeComment = NULLSTRING ,
145               const bool   GeneratedName = false ,
146               const int NodeX = 0 ,
147               const int NodeY = 0 ,
148               int * Graph_prof_debug = NULL ,
149               ofstream * Graph_fdebug = NULL ) ;
150       virtual ~InNode() ;
151
152       pthread_t ThreadNo() {
153                 return _ComputingNode->ThreadNo() ; } ;
154       void ThreadNo( pthread_t aThread ) {
155            _ComputingNode->ThreadNo ( aThread ) ; } ;
156
157       char * Name() const {
158              return _ComputingNode->Name() ; } ;
159       const char *const * NamePtr() const {
160                           return _ComputingNode->NamePtr() ; } ;
161       SUPERV::KindOfNode Kind() const {
162                          return _ComputingNode->Kind() ; } ;
163       const bool IsComputingNode() const {
164                  return _ComputingNode->IsComputingNode() ; } ;
165       const bool IsFactoryNode() const {
166                  return _ComputingNode->IsFactoryNode() ; } ;
167       const bool IsInLineNode() const {
168                  return _ComputingNode->IsInLineNode() ; } ;
169       const bool IsOneOfInLineNodes() const {
170                  return _ComputingNode->IsOneOfInLineNodes() ; } ;
171       const bool IsOneOfGOTONodes() const {
172                  return _ComputingNode->IsOneOfGOTONodes() ; } ;
173       const bool IsMacroNode() const {
174                  return _ComputingNode->IsMacroNode() ; } ;
175       const bool IsDataFlowNode() const {
176                  return _ComputingNode->IsDataFlowNode() ; } ;
177       const bool IsDataStreamNode() const {
178                  return _ComputingNode->IsDataStreamNode() ; } ;
179       const bool IsLoopNode() const {
180                  return _ComputingNode->IsLoopNode() ; } ;
181       const bool IsEndLoopNode() const {
182                  return _ComputingNode->IsEndLoopNode() ; } ;
183       const bool IsSwitchNode() const {
184                  return _ComputingNode->IsSwitchNode() ; } ;
185       const bool IsEndSwitchNode() const {
186                  return _ComputingNode->IsEndSwitchNode() ; } ;
187       const bool IsGOTONode() const {
188                  return _ComputingNode->IsGOTONode() ; } ;
189       const bool IsHeadNode() const {
190                  return _ComputingNode->IsHeadNode() ; } ;
191       GraphBase::ComputingNode * ComputingNode() {
192                                  return _ComputingNode ; } ;
193       GraphBase::FactoryNode * FactoryNode() {
194                                  return _FactoryNode ; } ;
195       GraphBase::GOTONode * GOTONode() {
196                             if ( _GOTONode )
197                               return _GOTONode ;
198                             if ( _LoopNode )
199                               return _LoopNode ;
200                             if ( _EndOfLoopNode )
201                               return _EndOfLoopNode ;
202                             if ( _SwitchNode )
203                               return _SwitchNode ;
204                             if ( _EndOfSwitchNode )
205                               return _EndOfSwitchNode ;
206                             return NULL ;
207                             } ;
208       GraphBase::InLineNode * InLineNode() {
209                               GraphBase::InLineNode * aNode = GOTONode() ;
210                               if ( aNode == NULL )
211                                 return _InLineNode ;
212                               return aNode ;
213                               } ;
214       GraphBase::LoopNode * LoopNode() {
215                             return _LoopNode ; } ;
216       GraphBase::Graph * GraphMacroNode() {
217                          return _GraphMacroNode ; } ;
218
219       SUPERV::CNode_var ObjRef() const { return _ComputingNode->ObjRef() ; } ;
220       void SetObjRef( SUPERV::CNode_var aNode ) {
221                      _ComputingNode->SetObjRef( aNode ) ; } ;
222
223       CNode_Impl * ObjImpl() const { return _ComputingNode->ObjImpl() ; } ;
224       void SetObjImpl( CNode_Impl * aGraph ) {
225                        _ComputingNode->SetObjImpl( aGraph ) ; } ;
226
227       Engines::Component_var Component() const ;
228       Engines::Container_var Container() const ;
229       void SetContainer(Engines::Container_var aContainer) {
230                         _FactoryNode->SetContainer( aContainer ) ; } ;
231       void SetComponent(Engines::Component_var anObjComponent) {
232                         _FactoryNode->SetComponent( anObjComponent ) ; } ;
233       void ObjInterface( bool k_interface ) {
234                         _ComputingNode->ObjInterface( k_interface ) ; } ;
235       bool ObjInterface() {
236                         return _FactoryNode->ObjInterface() ; } ;
237       char * ComponentName() const { return _FactoryNode->ComponentName() ; } ;
238       char * InterfaceName() const { return _FactoryNode->InterfaceName() ; } ;
239       char * Computer() const { return _FactoryNode->Computer() ; } ;
240       const char * ServiceName() const {
241                    return _ComputingNode->ServiceName() ; } ;
242       const SALOME_ModuleCatalog::ListOfServicesParameter ServiceInParameter() const {
243             return _ComputingNode->ServiceInParameter() ; } ;
244       const SALOME_ModuleCatalog::ListOfServicesParameter ServiceOutParameter() const {
245             return _ComputingNode->ServiceOutParameter() ; } ;
246
247       void CoupledNode( GraphBase::InLineNode * aCoupledNode ) {
248            GOTONode()->CoupledNode( aCoupledNode ) ; } ;
249       GraphBase::InLineNode * CoupledNode() {
250                               return GOTONode()->CoupledNode() ; } ;
251
252       GraphBase::InPort * AddInPort( const char * InputParameterName ,
253                                      const char * InputParameterType ,
254                                      const SUPERV::KindOfPort aKindOfPort ) {
255                           return _ComputingNode->AddInPort( InputParameterName ,
256                                                             InputParameterType ,
257                                                             aKindOfPort ) ; } ;
258       GraphBase::OutPort * AddOutPort( const char * OutputParameterName ,
259                                        const char * OutputParameterType ,
260                                        const SUPERV::KindOfPort aKindOfPort ) {
261                            return _ComputingNode->AddOutPort( OutputParameterName ,
262                                                               OutputParameterType ,
263                                                               aKindOfPort ) ; } ;
264 //      void InOutPort( GraphBase::InPort * InputPort ,
265 //                      GraphBase::OutPort * OutputPort ) {
266 //           return _ComputingNode->InOutPort( InputPort , OutputPort ) ; } ;
267       int LinkedNodesSize() const {
268           return _ComputingNode->LinkedNodesSize() ; } ;
269 //      GraphBase::ComputingNode * LinkedNodes( int i ) const {
270       GraphBase::StreamNode * LinkedNodes( int i ) const {
271                               return _ComputingNode->LinkedNodes( i ) ; } ;
272       const int LinkedInPortsNumber( int i ) const {
273                 return _ComputingNode->LinkedInPortsNumber( i ) ; } ;
274
275       const int GetNodeInPortsSize() const {
276                 return _ComputingNode->GetNodeInPortsSize() ; } ;
277       const GraphBase::InPort *GetNodeInLoop() const {
278                               return _ComputingNode->GetNodeInLoop() ; } ;
279       const GraphBase::InPort *GetNodeInGate() const {
280                               return _ComputingNode->GetNodeInGate() ; } ;
281       const GraphBase::InPort *GetNodeInPort(int i) const {
282                               return _ComputingNode->GetNodeInPort( i ) ; } ;
283       GraphBase::InPort *GetChangeNodeInLoop() const {
284                         return _ComputingNode->GetChangeNodeInLoop() ; } ;
285       GraphBase::InPort *GetChangeNodeInGate() const {
286                         return _ComputingNode->GetChangeNodeInGate() ; } ;
287       GraphBase::InPort *GetChangeNodeInPort(int i) const {
288                         return _ComputingNode->GetChangeNodeInPort( i ) ; } ;
289       const int GetNodeOutPortsSize() const {
290                 return _ComputingNode->GetNodeOutPortsSize() ; } ;
291       const GraphBase::OutPort *GetNodeOutLoop() const {
292                                return _ComputingNode->GetNodeOutLoop() ; } ;
293       const GraphBase::OutPort *GetNodeOutGate() const {
294                                return _ComputingNode->GetNodeOutGate() ; } ;
295       const GraphBase::OutPort *GetNodeOutPort(int i) const {
296                                return _ComputingNode->GetNodeOutPort( i ) ; } ;
297       GraphBase::OutPort *GetChangeNodeOutLoop() const {
298                          return _ComputingNode->GetChangeNodeOutLoop() ; } ;
299       GraphBase::OutPort *GetChangeNodeOutGate() const {
300                          return _ComputingNode->GetChangeNodeOutGate() ; } ;
301       GraphBase::OutPort *GetChangeNodeOutPort(int i) const {
302                          return _ComputingNode->GetChangeNodeOutPort( i ) ; } ;
303
304       const GraphBase::InPort *GetInPort( const char *name ) {
305             return _ComputingNode->GetInPort( name ) ; } ;
306       const GraphBase::OutPort *GetOutPort( const char *name ) {
307             return _ComputingNode->GetOutPort( name ) ; } ;
308       GraphBase::InPort *GetChangeInPort( const char *name ) {
309                         return _ComputingNode->GetChangeInPort( name ) ; } ;
310       GraphBase::OutPort *GetChangeOutPort( const char *name ) {
311                          return _ComputingNode->GetChangeOutPort( name ) ; } ;
312
313       void PyFuncRunned( bool arunned ) {
314            _PyFuncRunned = arunned ; } ;
315       bool PyFuncRunned() {
316            return _PyFuncRunned ; } ;
317
318       bool InitPython() ;
319       PyObject * InitPyDynInvoke( char * PyFuncName ,
320                                   const SUPERV::ListOfStrings * aPythonFunction ,
321                                   bool & Err ) ;
322
323       void LockDataWait() ;
324       void UnLockDataWait() ;
325       bool IsLockedDataWait() { return _DataWait ; } ;
326
327       bool Ping() ;
328       bool ContainerKill() ;
329
330       bool Kill() ;
331       bool KillDone() ;
332       bool Suspend() ;
333       bool SuspendDone() ;
334       bool Resume() ;
335       bool ReStart( const char * AtNodeName , const bool AndSuspend ) ;
336       bool Stop() ;
337
338       void CreateNewThread( bool k_create ) { _createNewThread = k_create ; } ;
339       void CreateNewThreadIf( bool k_create ) { _createNewThreadIf = k_create ; } ;
340       bool CreateNewThread() { return _createNewThread ; } ;
341       bool CreateNewThreadIf() { return _createNewThreadIf ; } ;
342       void NewThread( pthread_t aThread ) ;
343       void ExitThread() ;
344       void RewindStack( int aRewindStack ) { _RewindStack = aRewindStack ; } ;
345       int RewindStack() const { return _RewindStack ; } ;
346
347       GraphExecutor::AutomatonState State() const {
348              return _currentState; };
349       void State(GraphExecutor::AutomatonState aState ) {
350 //           cdebug << "GraphExecutor::InNode::State( "
351 //                  << Automaton()->StateName( _currentState ) << " --> "
352 //                  << Automaton()->StateName( aState ) << " )"  << endl ;
353            _currentState = aState ; } ;
354       SUPERV::ControlState ControlState() const {
355              return _ControlState; };
356       void ControlState(SUPERV::ControlState aControlState ) {
357            _ControlState = aControlState ; } ;
358       void ControlClear() {
359            _ControlState = SUPERV::VoidState ; } ;
360
361       void SetAutomaton() {
362             _Automaton = theAutomaton ; } ;
363       GraphExecutor::FiniteStateMachine * Automaton() const {
364             return _Automaton ; } ;
365
366       bool IsWaiting() ;
367       bool IsReady() ;
368       bool IsRunning() ;
369       bool IsDone() ;
370       bool IsSuspended() ;
371       bool IsKilled() ;
372       bool IsStopped() ;
373
374       bool StateWait( SUPERV::GraphState aState ) ;
375       bool ReadyWait() ;
376       bool RunningWait() ;
377       bool DoneWait() ;
378       bool SuspendedWait() ;
379
380       void InitialState( GraphExecutor::OutNode * theOutNode ) ;
381       bool InitPythonFunctions(bool WithErr ) ;
382       void SetWaitingStates(GraphExecutor::InNode * EndNode ) ;
383
384       int SendEvent(const GraphExecutor::NodeEvent anEvent ) ;
385       void DataFromNode( char * FromNodeName ) {
386            _DataFromNode = FromNodeName ; } ;
387       const char * DataFromNode() const { return _DataFromNode ; } ;
388
389       int ErrorAction();
390       int VoidAction();
391       void ReadyAction() ;
392       void RunningAction() ;
393       void DoneAction() ;
394       void SuspendedAction() ;
395       GraphExecutor::InNode * SuspendAction() ;
396       bool ResumeAction(GraphExecutor::NodeEvent aResumeEvent ) ;
397       bool ReStartAction( GraphExecutor::InNode * aRestartNode ,
398                           GraphExecutor::NodeEvent anEvent ) ;
399       void KillAction() ;
400       void KilledAction() ;
401       void ThreadStartAction() ;
402       void ThreadStartedAction() ;
403       void StopAction() ;
404       void StoppedAction() ;
405       int executeAction() ; // New Thread or Same Thread
406       int ExecuteAction() ;
407
408       int DataWaiting_SomeDataReadyAction() ;
409       int DataUndef_NotAllDataReadyAction() ;
410       int DataUndef_AllDataReadyAction() ;
411       int DataReady_SuspendAction() ;
412       int SuspendedReady_ResumeAction() ;
413       int DataReady_KillAction() ;
414       int DataReady_StopAction() ;
415       int DataReady_ExecuteAction() ;
416       void DynInvoke( Engines::Component_ptr obj,
417                       const char *method, 
418                       ServicesAnyData * inParams, int nInParams,
419                       ServicesAnyData * outParams, int nOutParams) ;
420       void DynInvoke( Engines::Component_ptr obj,
421                       const char *method, 
422                       const char * aGraphName ,
423                       const char * aNodeName );
424       bool PyDynInvoke( PyObject * MyPyRunMethod ,
425                         const char *method , 
426                         ServicesAnyData * inParams , int nInParams ,
427                         ServicesAnyData * inParams , int nOutParams ) ;
428
429       int Executing_SuspendAction() ;
430       int SuspendedExecuting_ResumeAction() ;
431       int Executing_KillAction() ;
432       int Executing_StopAction() ;
433       int Executing_SuccessAction() ;
434       int Executing_ErrorAction() ;
435       int Successed_SuccessAction() ;
436       bool SendSomeDataReady( char * FromNodeName ) ;
437       int Errored_ErrorAction() ;
438       int Successed_SuspendAction() ;
439       int Errored_SuspendAction() ;
440       int SuspendedSuccessed_ResumeAction() ;
441       int SuspendedErrored_ResumeAction() ;
442       int Successed_KillAction() ;
443       int Errored_KillAction() ;
444       int Successed_StopAction() ;
445       int Errored_StopAction() ;
446       int SuspendedSuccessed_ReStartAction() ;
447       int SuspendedErrored_ReStartAction() ;
448       int SuspendedSuccessed_ReStartAndSuspendAction() ;
449       int SuspendedErrored_ReStartAndSuspendAction() ;
450
451       void InParametersSet( bool & Err ,
452                             int  nInParams ,
453                             ServicesAnyData * InParametersList ) ;
454       void InOutParametersSet( int nOutParams ,
455                                ServicesAnyData * OutParametersList ) ;
456       bool OutParametersSet( bool Err , SUPERV::GraphState NewState ,
457                              int nOutParams ,
458                              ServicesAnyData * OutParametersList ) ;
459       void coutbegin() ;
460       void coutexit() ;
461
462       const long CpuUsed( bool tot = false ) ;
463
464       long PyCpuUsed( bool tot = false ) ;
465       void SetPyCpuUsed() ;
466       long PyCpu() ;
467
468       bool PyRunSimpleString( char* thePyString );
469     } ;
470
471 } ;
472
473 #endif
474