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