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