Salome HOME
If there is a syntax error in a python function (InLine Node), the states
[modules/superv.git] / src / Supervision / INode_Impl.cxx
1 //  SUPERV Supervision : contains the implementation of interfaces of SuperVision described in SUPERV.idl
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : INode_Impl.cxx
8 //  Author : Jean Rahuel
9 //  Module : SUPERV
10 //  $Header: 
11
12 using namespace std;
13 #include <stdio.h>
14 #include <fstream>
15 //#include <sstream>
16 #include <string>
17
18 //#include "utilities.h"
19
20 #include "LNode_Impl.hxx"
21
22 INode_Impl::INode_Impl( CORBA::ORB_ptr orb ,
23                        PortableServer::POA_ptr poa ,
24                        PortableServer::ObjectId * contId , 
25                        const char *instanceName ,
26                        const char *interfaceName ,
27                        const char *aDataFlowName ) :
28 //  Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
29   CNode_Impl( orb , poa , contId , instanceName , interfaceName , aDataFlowName ) {
30 //  MESSAGE("INode_Impl::INode_Impl activate object instanceName("
31 //          << instanceName << ") interfaceName(" << interfaceName << ") --> "
32 //          << hex << (void *) this << dec )
33 //  _thisObj = this ;
34 //  _id = _poa->activate_object(_thisObj);
35   MESSAGE( "INode_Impl::INode_Impl " << aDataFlowName );
36   beginService( "INode_Impl::INode_Impl" );
37   _Orb = CORBA::ORB::_duplicate(orb);
38   _Poa = poa ;
39   _ContId = contId ;
40   endService( "INode_Impl::INode_Impl" );  
41 }
42
43 INode_Impl::INode_Impl() {
44 }
45
46 INode_Impl::INode_Impl( CORBA::ORB_ptr orb ,
47                         PortableServer::POA_ptr poa ,
48                         PortableServer::ObjectId * contId , 
49                         const char *instanceName ,
50                         const char *interfaceName ,
51                         GraphEditor::DataFlow * aDataFlowEditor ,
52                         const char * FuncName , 
53                         const SUPERV::ListOfStrings & aPythonFunction ,
54                         const SUPERV::KindOfNode NodeKindOfNode ) :
55 //  Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
56   CNode_Impl( orb , poa , contId , instanceName , interfaceName , aDataFlowEditor , SALOME_ModuleCatalog::Service() , FuncName , NodeKindOfNode , FuncName , aPythonFunction ) {
57   beginService( "INode_Impl::INode_Impl" );
58   if ( FuncName ) {
59 //    cout << "INode_Impl::CNode_Impl " << (void *) FuncName << " " << FuncName
60 //         << " " << strlen( FuncName ) << endl ;
61   }
62   if ( NodeKindOfNode == SUPERV::InLineNode ) {
63     MESSAGE( "INode_Impl::INode_Impl " << FuncName << " _poa->activate_object" );
64     _thisObj = this ;
65     _id = _poa->activate_object(_thisObj);
66   }
67   MESSAGE("INode_Impl::INode_Impl " << FuncName << " " << strlen( FuncName ) ) ;
68   _Orb = CORBA::ORB::_duplicate(orb);
69   _Poa = poa ;
70   _ContId = contId ;
71 //  DataFlowNode()->SetPythonFunction( FuncName , aPythonFunction ) ;
72   endService( "INode_Impl::INode_Impl" );  
73 }
74
75 INode_Impl::INode_Impl( CORBA::ORB_ptr orb ,
76                         PortableServer::POA_ptr poa ,
77                         PortableServer::ObjectId * contId , 
78                         const char *instanceName ,
79                         const char *interfaceName ,
80                         GraphEditor::DataFlow * aDataFlowEditor ,
81                         const GraphBase::ListOfFuncName FuncName , 
82                         const GraphBase::ListOfPythonFunctions aPythonFunction ,
83                         const SUPERV::KindOfNode NodeKindOfNode ) :
84 //  Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
85   CNode_Impl( orb , poa , contId , instanceName , interfaceName , aDataFlowEditor , SALOME_ModuleCatalog::Service() , FuncName[0].c_str() , NodeKindOfNode , FuncName , aPythonFunction ) {
86   beginService( "INode_Impl::INode_Impl" );
87   if ( NodeKindOfNode == SUPERV::InLineNode ) {
88     MESSAGE( "INode_Impl::INode_Impl " << FuncName[0] << " _poa->activate_object" );
89     _thisObj = this ;
90     _id = _poa->activate_object(_thisObj);
91   }
92   MESSAGE("INode_Impl::INode_Impl " << FuncName[0].c_str() << " " << strlen( FuncName[0].c_str() ) ) ;
93   _Orb = CORBA::ORB::_duplicate(orb);
94   _Poa = poa ;
95   _ContId = contId ;
96 //  DataFlowNode()->SetPythonFunction( FuncName , aPythonFunction ) ;
97   endService( "INode_Impl::INode_Impl" );  
98 }
99
100 INode_Impl::INode_Impl( CORBA::ORB_ptr orb ,
101                         PortableServer::POA_ptr poa ,
102                         PortableServer::ObjectId * contId , 
103                         const char *instanceName ,
104                         const char *interfaceName ,
105                         GraphEditor::DataFlow * aDataFlowEditor ,
106                         GraphEditor::InNode * aDataFlowNode ) :
107 //  Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
108   CNode_Impl( orb , poa , contId , instanceName , interfaceName , aDataFlowEditor , aDataFlowNode ) {
109   MESSAGE( DataFlowNode()->Name() << " " );
110   beginService( "INode_Impl::INode_Impl" );
111   if ( DataFlowNode()->IsInLineNode() ) {
112     MESSAGE( "INode_Impl::INode_Impl " << DataFlowNode()->PyFuncName()
113              << " _poa->activate_object" );
114     _thisObj = this ;
115     _id = _poa->activate_object(_thisObj);
116   }
117   _Orb = CORBA::ORB::_duplicate(orb);
118   _Poa = poa ;
119   _ContId = contId ;
120   endService( "INode_Impl::INode_Impl" );  
121 }
122
123 INode_Impl::~INode_Impl() {
124   beginService( "INode_Impl::~INode_Impl" );
125   endService( "INode_Impl::~INode_Impl" );
126 }
127
128 void INode_Impl::destroy() {
129   beginService( "INode_Impl::Destroy" );
130   if ( DataFlowEditor()->IsEditing() ) {
131     if ( Delete() ) {
132       _poa->deactivate_object(*_id) ;
133       CORBA::release(_poa) ;
134       delete(_id) ;
135       _thisObj->_remove_ref();
136     }
137   }
138   endService( "INode_Impl::Destroy" );
139 }
140
141 bool INode_Impl::Delete() {
142 //  beginService( "INode_Impl::Delete" );
143   bool RetVal = false ;
144   if ( DataFlowEditor()->IsEditing() ) {
145     RetVal = DataFlowEditor()->RemoveNode( Name() ) ;
146     if ( RetVal )
147       RetVal = DataFlowEditor()->IsValid() ;
148   }
149 //  endService( "INode_Impl::Delete" );
150   return RetVal ;
151 }
152
153 void INode_Impl::SetPyFunction( const char * FuncName ,
154                                 const SUPERV::ListOfStrings & aPyFunction ) {
155   beginService( "INode_Impl::SetPyFunction" );
156   if ( DataFlowEditor()->IsEditing() ) {
157     DataFlowNode()->SetPythonFunction( FuncName , aPyFunction ) ;
158   }
159   endService( "INode_Impl::SetPyFunction" );
160   return ;
161 }
162
163 SUPERV::ListOfStrings * INode_Impl::PyFunction() {
164   beginService( "INode_Impl::PyFunction" );
165   SUPERV::ListOfStrings * aPyFunc = NULL ;
166   if ( DataFlowEditor()->IsEditing() ) {
167     aPyFunc = new SUPERV::ListOfStrings( *DataFlowNode()->PythonFunction() ) ;
168   }
169   else {
170     aPyFunc = new SUPERV::ListOfStrings() ;
171   }
172   endService( "INode_Impl::PyFunction" );
173   return aPyFunc ;
174 }
175
176 char * INode_Impl::PyFuncName() {
177   return CORBA::string_dup( DataFlowNode()->PyFuncName() ) ;
178 }
179
180 SUPERV::Port_ptr INode_Impl::InPort( const char *aParameterName ,
181                                      const char *aParameterType ) {
182   SUPERV::Port_ptr Inobject = SUPERV::Port::_nil() ;
183   SUPERV::Port_ptr Outobject = SUPERV::Port::_nil() ;
184   SUPERV::Port_ptr InEndobject = SUPERV::Port::_nil() ;
185   Port_Impl * myInPort = NULL ;
186   bool InPortCreated = false ;
187   MESSAGE( "CNode_Impl::InPort " << DataFlowNode()->Name() << "->AddInPort( " << aParameterName << " , "
188            << aParameterType << " )" ) ;
189   GraphBase::InPort * anInPort = DataFlowNode()->GetChangeInPort( aParameterName ) ;
190   if ( anInPort == NULL ) {
191     anInPort = DataFlowNode()->AddInPort( aParameterName , aParameterType , SUPERV::InLineParameter ) ;
192     InPortCreated = true ;
193   }
194   if ( anInPort ) {
195     Inobject = anInPort->ObjRef() ;
196     if ( CORBA::is_nil( Inobject ) ) {
197       myInPort = new Port_Impl( _Orb , _Poa , _ContId ,
198                                 instanceName() , interfaceName() ,
199                                 DataFlowEditor() ,
200                                 DataFlowNode() ,
201                                 (GraphBase::Port * ) anInPort ,
202                                 true ) ;
203       if ( myInPort ) {
204         PortableServer::ObjectId * id = myInPort->getId() ;
205         CORBA::Object_var obj = _poa->id_to_reference(*id) ;
206         Inobject = SUPERV::Port::_narrow(obj) ;
207       }      
208     }
209   }
210
211   if ( !CORBA::is_nil( Inobject ) && InPortCreated &&
212        ( DataFlowNode()->IsLoopNode() || DataFlowNode()->IsEndLoopNode() ) ) {
213 //         || DataFlowNode()->IsEndSwitchNode() ) ) {
214     Outobject = OutPort( aParameterName , aParameterType ) ;
215     if ( DataFlowNode()->IsLoopNode() ) {
216       LNode_Impl * myLoopNode = (LNode_Impl * ) this ;
217       InEndobject = myLoopNode->Coupled()->InPort( aParameterName , aParameterType ) ;
218       DataFlowEditor()->AddLink( Outobject->Node()->Name() , Outobject->Name() ,
219                                  InEndobject->Node()->Name() , InEndobject->Name() ) ;
220     }
221   }
222   DataFlowEditor()->UnValid() ;
223   return SUPERV::Port::_duplicate( Inobject ) ;
224 }
225
226 SUPERV::Port_ptr INode_Impl::OutPort( const char *aParameterName ,
227                                       const char *aParameterType ) {
228   Port_Impl * myOutPort = NULL ;
229   SUPERV::Port_ptr Outobject = SUPERV::Port::_nil() ;
230   MESSAGE( "CNode_Impl::OutPort " << DataFlowNode()->Name() << "->AddOutPort( " << aParameterName << " , "
231            << aParameterType << " )" ) ;
232   GraphBase::OutPort * anOutPort = DataFlowNode()->GetChangeOutPort( aParameterName ) ;
233   if ( anOutPort == NULL ) {
234     anOutPort = DataFlowNode()->AddOutPort( aParameterName , aParameterType , SUPERV::InLineParameter ) ;
235   }
236   if ( anOutPort ) {
237     Outobject = anOutPort->ObjRef() ;
238     if ( CORBA::is_nil( Outobject ) ) {
239       const CORBA::Any * anAny = anOutPort->Value() ;
240       myOutPort = new Port_Impl( _Orb , _Poa , _ContId ,
241                                  instanceName() , interfaceName() ,
242                                  DataFlowEditor() ,
243                                  DataFlowNode() ,
244                                  (GraphBase::Port * ) anOutPort ,
245                                  false ,
246                                  anAny ) ;
247       if ( myOutPort ) {
248         PortableServer::ObjectId * id = myOutPort->getId() ;
249         CORBA::Object_var obj = _poa->id_to_reference(*id) ;
250         Outobject = SUPERV::Port::_narrow(obj) ;
251       }
252     }
253   }
254   DataFlowEditor()->UnValid() ;
255   return SUPERV::Port::_duplicate( Outobject );
256 }
257
258 SUPERV::StreamPort_ptr INode_Impl::InStreamPort( const char *aParameterName ,
259                                                  const SALOME_ModuleCatalog::DataStreamType aParameterType ,
260                                                  const SALOME_ModuleCatalog::DataStreamDependency aDependency ) {
261   SUPERV::StreamPort_ptr Inobject = SUPERV::StreamPort::_nil() ;
262   if ( DataFlowEditor()->Graph()->IsDataStreamNode() ) {
263     StreamPort_Impl * myInStreamPort = NULL ;
264     bool InPortCreated = false ;
265     MESSAGE( "INode_Impl::InStreamPort " << DataFlowNode()->Name() << "->AddInPort( " << aParameterName << " , "
266              << aParameterType << " )" ) ;
267     GraphBase::InPort * anInPort = DataFlowNode()->GetChangeInPort( aParameterName ) ;
268     if ( anInPort && !anInPort->IsDataStream() ) {
269       MESSAGE( "INode_Impl::InStreamPort ERROR !IsDataStream " ) ;
270     }
271     else if ( DataFlowEditor()->Graph()->IsDataStreamNode() ) {
272       if ( anInPort == NULL ) {
273         anInPort = DataFlowNode()->ComputingNode()->AddInDataStreamPort( aParameterName , aParameterType , aDependency , SUPERV::DataStreamParameter ) ;
274         InPortCreated = true ;
275       }
276
277       if ( anInPort ) {
278         Inobject = SUPERV::StreamPort::_narrow( anInPort->ObjRef() ) ;
279         if ( CORBA::is_nil( Inobject ) ) {
280           myInStreamPort = new StreamPort_Impl( _Orb , _Poa , _ContId ,
281                                 instanceName() , interfaceName() ,
282                                 DataFlowEditor() ,
283                                 DataFlowNode() ,
284                                 (GraphBase::Port * ) anInPort ,
285                                 true ) ;
286           if ( myInStreamPort ) {
287             PortableServer::ObjectId * id = myInStreamPort->getId() ;
288             CORBA::Object_var obj = _poa->id_to_reference(*id) ;
289             Inobject = SUPERV::StreamPort::_narrow(obj) ;
290             anInPort->ObjRef( SUPERV::StreamPort::_duplicate( Inobject ) ) ;
291           }
292         }
293       }
294     }
295
296 //    if ( !CORBA::is_nil( Inobject ) && InPortCreated &&
297 //         ( DataFlowNode()->IsLoopNode() || DataFlowNode()->IsEndLoopNode() ||
298 //           DataFlowNode()->IsEndSwitchNode() ) ) {
299 //      Outobject = OutPort( aParameterName , aParameterType ) ;
300 //      if ( DataFlowNode()->IsLoopNode() ) {
301 //        LNode_Impl * myLoopNode = (LNode_Impl * ) this ;
302 //        InEndobject = myLoopNode->Coupled()->InPort( aParameterName , aParameterType ) ;
303 //        DataFlowEditor()->AddLink( Outobject->Node()->Name() , Outobject->Name() ,
304 //                                   InEndobject->Node()->Name() , InEndobject->Name() ) ;
305 //      }
306 //    }
307     DataFlowEditor()->UnValid() ;
308   }
309   return SUPERV::StreamPort::_duplicate( Inobject ) ;
310 }
311
312 SUPERV::StreamPort_ptr INode_Impl::OutStreamPort( const char * aParameterName ,
313                                                   const SALOME_ModuleCatalog::DataStreamType aParameterType ,
314                                                   const SALOME_ModuleCatalog::DataStreamDependency aDependency ) {
315   StreamPort_Impl * myOutStreamPort = NULL ;
316   SUPERV::StreamPort_ptr Outobject = SUPERV::StreamPort::_nil() ;
317   if ( DataFlowEditor()->Graph()->IsDataStreamNode() ) {
318     MESSAGE( "INode_Impl::OutStreamPort " << DataFlowNode()->Name() << "->AddOutPort( " << aParameterName << " , "
319              << aParameterType << " )" ) ;
320     GraphBase::OutPort * anOutPort = DataFlowNode()->GetChangeOutPort( aParameterName ) ;
321     if ( anOutPort && !anOutPort->IsDataStream() ) {
322       MESSAGE( "INode_Impl::OutStreamPort ERROR !IsDataStream " ) ;
323     }
324     else if ( DataFlowEditor()->Graph()->IsDataStreamNode() ) {
325       if ( anOutPort == NULL ) {
326         anOutPort = DataFlowNode()->ComputingNode()->AddOutDataStreamPort( aParameterName , aParameterType , aDependency , SUPERV::DataStreamParameter ) ;
327       }
328
329       if ( anOutPort ) {
330         Outobject = SUPERV::StreamPort::_narrow( anOutPort->ObjRef() ) ;
331         if ( CORBA::is_nil( Outobject ) ) {
332           const CORBA::Any * anAny = anOutPort->Value() ;
333           myOutStreamPort = new StreamPort_Impl( _Orb , _Poa , _ContId ,
334                                  instanceName() , interfaceName() ,
335                                  DataFlowEditor() ,
336                                  DataFlowNode() ,
337                                  (GraphBase::Port * ) anOutPort ,
338                                  false ,
339                                  anAny ) ;
340           if ( myOutStreamPort ) {
341             PortableServer::ObjectId * id = myOutStreamPort->getId() ;
342             CORBA::Object_var obj = _poa->id_to_reference(*id) ;
343             Outobject = SUPERV::StreamPort::_narrow(obj) ;
344             anOutPort->ObjRef( SUPERV::StreamPort::_duplicate( Outobject ) ) ;
345           }
346         }
347       }
348     }
349     DataFlowEditor()->UnValid() ;
350   }
351   return SUPERV::StreamPort::_duplicate( Outobject );
352 }
353
354