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