Salome HOME
MPV: Merge V1_2d
[modules/superv.git] / src / Supervision / Port_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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : Port_Impl.cxx
25 //  Author : Jean Rahuel
26 //  Module : SUPERV
27 //  $Header: 
28
29 using namespace std;
30 #include <stdio.h>
31 #include <fstream>
32 //#include <sstream>
33 #include <string>
34
35 //#include "utilities.h"
36
37 #include "Graph_Impl.hxx"
38 #include "Port_Impl.hxx"
39
40 Port_Impl::Port_Impl( CORBA::ORB_ptr orb ,
41                       PortableServer::POA_ptr poa ,
42                       PortableServer::ObjectId * contId , 
43                       const char *instanceName ,
44                       const char *interfaceName ,
45                       GraphEditor::DataFlow * DataFlowEditor ,
46                       GraphEditor::InNode * DataFlowNode ,
47 //                      const char *ParameterName ,
48                       GraphBase::Port * DataFlowPort ,
49                       bool InputPort ,
50                       const CORBA::Any * anAny ) :
51 //                      const SUPERV::Value_ptr aValue ) :
52   Value_Impl(orb, poa, contId, instanceName, interfaceName,
53 //             DataFlowEditor , DataFlowNode , ParameterName, aValue->ToAny() ,
54              DataFlowEditor , DataFlowNode , DataFlowPort->PortName(), anAny ,
55              false ) {
56 //  MESSAGE("Value_Impl::Value_Impl activate object instanceName("
57 //          << instanceName << ") interfaceName(" << interfaceName << ") --> "
58 //          << hex << (void *) this << dec )
59   _Orb = CORBA::ORB::_duplicate(orb);
60   _Poa = poa ;
61   _ContId = contId ;
62   _thisObj = this ;
63   _id = _poa->activate_object(_thisObj);
64   _DataFlowEditor = DataFlowEditor ;
65   _DataFlowNode = DataFlowNode ;
66 //  _DataFlowPort = _DataFlowNode->GetChangeInPort( ParameterName ) ;
67   _DataFlowPort = DataFlowPort ;
68 //  _InputPort = true ;
69   _InputPort = InputPort ;
70 //  if ( _DataFlowPort == NULL ) {
71 //    _DataFlowPort = _DataFlowNode->GetChangeOutPort( ParameterName ) ;
72 //    _InputPort = false ;
73 //  }
74   InPort( _InputPort ) ;
75 }
76
77 Port_Impl::Port_Impl( CORBA::ORB_ptr orb ,
78                       PortableServer::POA_ptr poa ,
79                       PortableServer::ObjectId * contId , 
80                       const char *instanceName ,
81                       const char *interfaceName ,
82                       GraphEditor::DataFlow * DataFlowEditor ,
83                       GraphEditor::InNode * DataFlowNode ,
84 //                      const char *ParameterName ) :
85                       GraphBase::Port * DataFlowPort ,
86                       bool InputPort ) :
87   Value_Impl(orb, poa, contId, instanceName, interfaceName,
88              DataFlowEditor , DataFlowNode , DataFlowPort->PortName() , false ) {
89 //             DataFlowEditor , DataFlowNode , ParameterName , false ) {
90 //  MESSAGE("Value_Impl::Value_Impl activate object instanceName("
91 //          << instanceName << ") interfaceName(" << interfaceName << ") --> "
92 //          << hex << (void *) this << dec )
93   _Orb = CORBA::ORB::_duplicate(orb);
94   _Poa = poa ;
95   _ContId = contId ;
96   _thisObj = this ;
97   _id = _poa->activate_object(_thisObj);
98   _DataFlowEditor = DataFlowEditor ;
99   _DataFlowNode = DataFlowNode ;
100 //  _DataFlowPort = _DataFlowNode->GetChangeInPort( ParameterName ) ;
101   _DataFlowPort = DataFlowPort ;
102 //  _InputPort = true ;
103   _InputPort = InputPort ;
104 //  if ( _DataFlowPort == NULL ) {
105 //    _DataFlowPort = _DataFlowNode->GetChangeOutPort( ParameterName ) ;
106 //    _InputPort = false ;
107 //  }
108   InPort( _InputPort ) ;
109 }
110
111 Port_Impl::Port_Impl() {
112 }
113
114 Port_Impl::~Port_Impl() {
115   beginService( "Port_Impl::~Port_Impl" );
116   endService( "Port_Impl::~Port_Impl" );
117 }
118
119 void Port_Impl::destroy() {
120   beginService( "Port_Impl::destroy" );
121   bool destroyed = false ;
122   if ( _DataFlowNode->IsOneOfInLineNodes() ) {
123     if ( _InputPort ) {
124       GraphBase::InPort * anInPort = (GraphBase::InPort * ) _DataFlowPort ;
125       if ( anInPort->GetOutPort() ) {
126         anInPort->GetOutPort()->RemoveInPort( anInPort ) ;
127       }
128       _DataFlowNode->DelInPort( _DataFlowPort->PortName() ) ;
129       if ( _DataFlowNode->IsLoopNode() ) {
130         _DataFlowNode->DelOutPort( _DataFlowPort->PortName() ) ;
131         _DataFlowNode->CoupledNode()->DelInPort( _DataFlowPort->PortName() ) ;
132         _DataFlowNode->CoupledNode()->DelOutPort( _DataFlowPort->PortName() ) ;
133       }
134       destroyed = true ;
135     }
136     else if ( !_DataFlowNode->IsLoopNode() ) {
137       _DataFlowNode->DelOutPort( _DataFlowPort->PortName() ) ;
138       destroyed = true ;
139     }
140     if ( destroyed ) {
141       _DataFlowEditor->UnValid() ;
142 //      _DataFlowEditor->IsValid() ;
143     }
144   }
145   if ( destroyed ) {
146     _poa->deactivate_object(*_id) ;
147     CORBA::release(_poa) ;
148     delete(_id) ;
149     _thisObj->_remove_ref();
150   }
151   endService( "Port_Impl::destroy" );
152 }
153
154 bool Port_Impl::Input( const SUPERV::Value_ptr aValue ) {
155   return Input( aValue->ToAny() ) ;
156 }
157
158 bool Port_Impl::Input( const CORBA::Any * anAny ) {
159   beginService( "Port_Impl::Input" );
160   bool RetVal = false ;
161   if ( _InputPort && _DataFlowEditor->IsEditing() ) {
162     RetVal = true ;
163     Value_Impl::Input( anAny ) ;
164     RetVal = _DataFlowEditor->AddInputData( _DataFlowNode->Name() ,
165                                             _DataFlowPort->PortName() ,
166                                             *anAny ) ;
167   }
168   else if ( _DataFlowEditor->IsExecuting() ) {
169     GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
170 //    if ( _DataFlowExecutor && ( _DataFlowExecutor->IsSuspended() ||
171 //         _DataFlowExecutor->IsSuspended( _DataFlowNode->Name() ) ) ) {
172     if ( _DataFlowExecutor ) {
173       RetVal = _DataFlowExecutor->ChangeInputData( _DataFlowNode->Name() ,
174                                                    _DataFlowPort->PortName() ,
175                                                    *anAny ) ;
176     }
177   }
178   if ( !RetVal ) {
179     MESSAGE( "Port_Impl::Input returns false _InputPort " << _InputPort << " IsEditing "
180              << _DataFlowEditor->IsEditing() << " Executor " << _DataFlowEditor->Executor() ) ;
181   }
182   endService( "Port_Impl::Input" );
183   return RetVal ;
184 }
185
186 char * Port_Impl::Name() {
187 //  beginService( "Port_Impl::Name" ) ;
188   const char * RetVal = _DataFlowPort->PortName() ;
189 //  endService( "Port_Impl::Name" ) ;
190   return CORBA::string_dup( RetVal ) ;
191 }
192
193 char * Port_Impl::Type() {
194 //  beginService( "Port_Impl::Type" ) ;
195   const char * RetVal = _DataFlowPort->PortType() ;
196 //  endService( "Port_Impl::Type" ) ;
197   return CORBA::string_dup( RetVal ) ;
198 }
199
200 SUPERV::CNode_ptr Port_Impl::Node() {
201   bool begin = true ;
202   SUPERV::CNode_var iobject = SUPERV::CNode::_nil() ;
203   if ( CORBA::is_nil( _DataFlowNode->ObjRef() ) ) {
204     if ( begin ) {
205       beginService( "Port_Impl::Node" ) ;
206       begin = false ;
207     }
208     CNode_Impl * myNode = new CNode_Impl( _Orb , _Poa , _ContId ,
209                                           instanceName() , interfaceName() ,
210                                           _DataFlowEditor ,
211                                           _DataFlowNode ) ;
212     PortableServer::ObjectId * id = myNode->getId() ;
213     CORBA::Object_var obj = _poa->id_to_reference(*id);
214     SUPERV::CNode_var iobject ;
215     iobject = SUPERV::CNode::_narrow(obj) ;
216     myNode->SetObjRef( SUPERV::CNode::_duplicate( iobject ) ) ;
217   }
218   else {
219     iobject = _DataFlowNode->ObjRef() ;
220   }
221   if ( !begin ) {
222     endService( "Port_Impl::Node" ) ;
223   }
224   return SUPERV::CNode::_duplicate( iobject ) ;
225 }
226
227 SUPERV::ListOfLinks * Port_Impl::Links() {
228   beginService( "Port_Impl::Links" ) ;
229   SUPERV::ListOfLinks_var RetVal = new SUPERV::ListOfLinks ;
230   RetVal = _DataFlowEditor->ObjImpl()->Links( _DataFlowNode->ComputingNode() ,
231                                               _DataFlowPort->PortName() ) ;
232   endService( "Port_Impl::Links" ) ;
233   return ( RetVal._retn() ) ;
234 }
235
236 SUPERV::Link_ptr Port_Impl::Link() {
237   bool begin = true ;
238   SUPERV::Link_var iobject = SUPERV::Link::_nil() ;
239   if ( _DataFlowNode->IsEndSwitchNode() ) {
240     beginService( "Port_Impl::Link" ) ;
241     begin = false ;
242     SUPERV::ListOfLinks_var Links = new SUPERV::ListOfLinks ;
243 //    cout << "Port_Impl::Link " << _DataFlowNode->Name() << " "
244 //         << _DataFlowPort->PortName() << endl ;
245     Links = _DataFlowEditor->ObjImpl()->Links( _DataFlowNode->ComputingNode() ,
246                                                _DataFlowPort->PortName() ) ;
247     if ( Links->length() ) {
248       iobject = Links[ 0 ] ;
249     }
250   }
251   else {
252     char * FromNodeName = NULL ;
253     char * FromServiceParameterName = NULL ;
254     bool status = _DataFlowEditor->GetLink( _DataFlowNode->Name() ,
255                                             _DataFlowPort->PortName() ,
256                                             & FromNodeName ,
257                                             & FromServiceParameterName ) ;
258     if ( status ) {
259 //      cout << "Port_Impl::Link " << FromNodeName << "("
260 //           << FromServiceParameterName << ") --> " << _DataFlowNode->Name()
261 //           << "(" << _DataFlowPort->PortName() << ")" << endl ;
262 //      SUPERV::Port_ptr theOutPort = Port( FromServiceParameterName ) ;
263
264       GraphBase::InPort * anInPort = _DataFlowNode->GetChangeInPort( _DataFlowPort->PortName() ) ;
265       if ( anInPort->GetOutPort() ) {
266         GraphBase::OutPort * anOutPort = anInPort->GetOutPort() ;
267         if ( _DataFlowNode->IsEndLoopNode() && !strcmp( _DataFlowNode->CoupledNode()->Name() ,
268                                                         anOutPort->NodeName() ) ) {
269 //          MESSAGE( "Link " << anOutPort->NodeName() << "("
270 //                  << anOutPort->PortName() << ") --> " << _DataFlowNode->Name() << "("
271 //                  << anInPort->PortName() << ")" << " ignored" ) ;
272         }
273         else if ( CORBA::is_nil( anOutPort->InPortObjRef( anInPort ) ) ) {
274           if ( begin ) {
275             beginService( "Port_Impl::Link" ) ;
276             begin = false ;
277           }
278           Link_Impl * myLink = new Link_Impl( _Orb , _Poa , _ContId ,
279                                     instanceName() , interfaceName() ,
280                                     _DataFlowEditor ,
281                                     _DataFlowNode ,
282                                     _DataFlowPort->PortName() ,
283                                     (GraphEditor::InNode * ) _DataFlowEditor->GetChangeGraphNode( FromNodeName )->GetInNode() ,
284                                     FromServiceParameterName ,
285                                     false ) ;
286           PortableServer::ObjectId * id = myLink->getId() ;
287           CORBA::Object_var obj = _poa->id_to_reference(*id);
288           iobject = SUPERV::Link::_narrow(obj) ;
289           anOutPort->AddInPortObjRef( anInPort , SUPERV::Link::_duplicate( iobject ) ) ;
290         }
291         else {
292           iobject = anOutPort->InPortObjRef( anInPort ) ;
293         }
294       }
295     }
296   }
297   if ( !begin ) {
298     MESSAGE( "Link " << iobject->OutPort()->Node()->Name() << "( " << iobject->OutPort()->Name() << " ) --> "
299              << iobject->InPort()->Node()->Name() << "( " << iobject->InPort()->Name() << " ) " ) ;
300     endService( "Port_Impl::Link" ) ;
301   }
302   return SUPERV::Link::_duplicate( iobject ) ;
303 }
304
305 bool Port_Impl::IsInput() {
306 //  beginService( "Port_Impl::IsInput" ) ;
307   bool RetVal = _InputPort ;
308 //  MESSAGE( Name() << "IsInput() " << _InputPort ) ;
309 //  endService( "Port_Impl::IsInput" ) ;
310   return RetVal ;
311 }
312
313 bool Port_Impl::IsLinked() {
314 //  beginService( "Port_Impl::IsLinked" ) ;
315   bool RetVal = false ;
316   GraphBase::InPort * anInPort = _DataFlowNode->GetChangeInPort( _DataFlowPort->PortName() ) ;
317   GraphBase::OutPort * anOutPort = NULL ;
318   if ( anInPort) {
319     anOutPort = anInPort->GetOutPort() ;
320   }
321   if ( _DataFlowNode->IsEndLoopNode() && anOutPort &&
322        !strcmp( _DataFlowNode->CoupledNode()->Name() , anOutPort->NodeName() ) ) {
323 //    MESSAGE( "Link " << anOutPort->NodeName() << "("
324 //            << anOutPort->PortName() << ") --> " << _DataFlowNode->Name() << "("
325 //            << anInPort->PortName() << ")" << " ignored" ) ;
326   }
327   else {
328     RetVal = _DataFlowNode->IsLinked( _DataFlowPort->PortName() ) ;
329   }
330 //  endService( "Port_Impl::IsLinked" ) ;
331   return RetVal ;
332 }
333
334 SUPERV::KindOfPort Port_Impl::Kind() {
335 //  beginService( "Port_Impl::Kind" ) ;
336   SUPERV::KindOfPort RetVal = _DataFlowPort->Kind() ;
337 //  endService( "Port_Impl::Kind" ) ;
338   return RetVal ;
339 }
340
341 bool Port_Impl::IsParam() {
342 //  beginService( "Port_Impl::IsParam" ) ;
343   bool RetVal = _DataFlowPort->IsParam() ;
344 //  endService( "Port_Impl::IsParam" ) ;
345   return RetVal ;
346 }
347
348 bool Port_Impl::IsGate() {
349 //  beginService( "Port_Impl::IsGate" ) ;
350   bool RetVal = _DataFlowPort->IsGate() ;
351 //  endService( "Port_Impl::IsGate" ) ;
352   return RetVal ;
353 }
354
355 bool Port_Impl::IsInLine() {
356 //  beginService( "Port_Impl::IsInLine" ) ;
357 //  bool RetVal = _DataFlowPort->IsBus() ;
358   bool RetVal = _DataFlowPort->IsInLine() ;
359 //  endService( "Port_Impl::IsInLine" ) ;
360   return RetVal ;
361 }
362
363 bool Port_Impl::IsLoop() {
364 //  beginService( "Port_Impl::IsLoop" ) ;
365   bool RetVal = _DataFlowPort->IsLoop() ;
366 //  endService( "Port_Impl::IsLoop" ) ;
367   return RetVal ;
368 }
369
370 bool Port_Impl::IsSwitch() {
371 //  beginService( "Port_Impl::IsSwitch" ) ;
372   bool RetVal = _DataFlowPort->IsSwitch() ;
373 //  endService( "Port_Impl::IsSwitch" ) ;
374   return RetVal ;
375 }
376
377 bool Port_Impl::IsEndSwitch() {
378 //  beginService( "Port_Impl::IsEndSwitch" ) ;
379   bool RetVal = _DataFlowPort->IsEndSwitch() ;
380 //  endService( "Port_Impl::IsEndSwitch" ) ;
381   return RetVal ;
382 }
383
384 bool Port_Impl::HasInput() {
385 //  beginService( "Port_Impl::" ) ;
386   bool RetVal = false ;
387   if ( _DataFlowNode ) {
388     RetVal = _DataFlowNode->HasInput( _DataFlowPort->PortName() ) ;
389 //    cout << "Port_Impl::HasInput " << RetVal << " NodeName " << _DataFlowPort->NodeName() << " PortName "
390 //         << _DataFlowPort->PortName() << endl ;
391   }
392 //  endService( "Port_Impl::" ) ;
393   return RetVal ;
394 }
395
396 SUPERV::GraphState Port_Impl::State() {
397 //  beginService( "Port_Impl::State" );
398   SUPERV::GraphState RetVal = SUPERV::EditingState ;
399   if ( _DataFlowEditor ) {
400     GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
401     if ( _DataFlowExecutor ) {
402       RetVal = _DataFlowExecutor->State( _DataFlowPort->NodeName() ,
403                                          _DataFlowPort->PortName() ) ;
404     }
405   }
406 //  endService( "Port_Impl::State" );
407   return RetVal ;
408 }
409
410 bool Port_Impl::IsDone() {
411 //  beginService( "Port_Impl::IsDone" );
412   bool RetVal = false ;
413   if ( _DataFlowEditor ) {
414     GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
415     if ( _DataFlowExecutor ) {
416       RetVal = _DataFlowExecutor->IsDone( _DataFlowPort->NodeName() ,
417                                           _DataFlowPort->PortName() ) ;
418     }
419   }
420 //  endService( "Port_Impl::IsDone" );
421   return RetVal ;
422 }