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