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