Salome HOME
NRI : Merge from 1.2c.
[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   endService( "Port_Impl::Input" );
179   return RetVal ;
180 }
181
182 char * Port_Impl::Name() {
183 //  beginService( "Port_Impl::Name" ) ;
184   const char * RetVal = _DataFlowPort->PortName() ;
185 //  endService( "Port_Impl::Name" ) ;
186   return CORBA::string_dup( RetVal ) ;
187 }
188
189 char * Port_Impl::Type() {
190 //  beginService( "Port_Impl::Type" ) ;
191   const char * RetVal = _DataFlowPort->PortType() ;
192 //  endService( "Port_Impl::Type" ) ;
193   return CORBA::string_dup( RetVal ) ;
194 }
195
196 SUPERV::CNode_ptr Port_Impl::Node() {
197   bool begin = true ;
198   SUPERV::CNode_var iobject = SUPERV::CNode::_nil() ;
199   if ( CORBA::is_nil( _DataFlowNode->ObjRef() ) ) {
200     if ( begin ) {
201       beginService( "Port_Impl::Node" ) ;
202       begin = false ;
203     }
204     CNode_Impl * myNode = new CNode_Impl( _Orb , _Poa , _ContId ,
205                                           instanceName() , interfaceName() ,
206                                           _DataFlowEditor ,
207                                           _DataFlowNode ) ;
208     PortableServer::ObjectId * id = myNode->getId() ;
209     CORBA::Object_var obj = _poa->id_to_reference(*id);
210     SUPERV::CNode_var iobject ;
211     iobject = SUPERV::CNode::_narrow(obj) ;
212     myNode->SetObjRef( SUPERV::CNode::_duplicate( iobject ) ) ;
213   }
214   else {
215     iobject = _DataFlowNode->ObjRef() ;
216   }
217   if ( !begin ) {
218     endService( "Port_Impl::Node" ) ;
219   }
220   return SUPERV::CNode::_duplicate( iobject ) ;
221 }
222
223 SUPERV::ListOfLinks * Port_Impl::Links() {
224   beginService( "Port_Impl::Links" ) ;
225   SUPERV::ListOfLinks_var RetVal = new SUPERV::ListOfLinks ;
226   RetVal = _DataFlowEditor->ObjImpl()->Links( _DataFlowNode->ComputingNode() ,
227                                               _DataFlowPort->PortName() ) ;
228   endService( "Port_Impl::Links" ) ;
229   return ( RetVal._retn() ) ;
230 }
231
232 SUPERV::Link_ptr Port_Impl::Link() {
233   bool begin = true ;
234   SUPERV::Link_var iobject = SUPERV::Link::_nil() ;
235   if ( _DataFlowNode->IsEndSwitchNode() ) {
236     beginService( "Port_Impl::Link" ) ;
237     begin = false ;
238     SUPERV::ListOfLinks_var Links = new SUPERV::ListOfLinks ;
239 //    cout << "Port_Impl::Link " << _DataFlowNode->Name() << " "
240 //         << _DataFlowPort->PortName() << endl ;
241     Links = _DataFlowEditor->ObjImpl()->Links(
242                                         _DataFlowNode->ComputingNode() ,
243                                         _DataFlowPort->PortName() ) ;
244     if ( Links->length() ) {
245       iobject = Links[ 0 ] ;
246     }
247   }
248   else {
249     char * FromNodeName = NULL ;
250     char * FromServiceParameterName = NULL ;
251     bool status = _DataFlowEditor->GetLink( _DataFlowNode->Name() ,
252                                             _DataFlowPort->PortName() ,
253                                             & FromNodeName ,
254                                             & FromServiceParameterName ) ;
255     if ( status ) {
256 //      cout << "Port_Impl::Link " << FromNodeName << "("
257 //           << FromServiceParameterName << ") --> " << _DataFlowNode->Name()
258 //           << "(" << _DataFlowPort->PortName() << ")" << endl ;
259 //      SUPERV::Port_ptr theOutPort = Port( FromServiceParameterName ) ;
260
261       GraphBase::InPort * anInPort = _DataFlowNode->GetChangeInPort( _DataFlowPort->PortName() ) ;
262       if ( anInPort->GetOutPort() ) {
263         GraphBase::OutPort * anOutPort = anInPort->GetOutPort() ;
264         if ( _DataFlowNode->IsEndLoopNode() && !strcmp( _DataFlowNode->CoupledNode()->Name() ,
265                                                         anOutPort->NodeName() ) ) {
266 //          MESSAGE( "Link " << anOutPort->NodeName() << "("
267 //                  << anOutPort->PortName() << ") --> " << _DataFlowNode->Name() << "("
268 //                  << anInPort->PortName() << ")" << " ignored" ) ;
269         }
270         else if ( CORBA::is_nil( anOutPort->InPortObjRef( anInPort ) ) ) {
271           if ( begin ) {
272             beginService( "Port_Impl::Link" ) ;
273             begin = false ;
274           }
275           Link_Impl * myLink = new Link_Impl( _Orb , _Poa , _ContId ,
276                                     instanceName() , interfaceName() ,
277                                     _DataFlowEditor ,
278                                     _DataFlowNode ,
279                                     _DataFlowPort->PortName() ,
280                                     (GraphEditor::InNode * ) _DataFlowEditor->GetChangeGraphNode( FromNodeName )->GetInNode() ,
281                                     FromServiceParameterName ,
282                                     false ) ;
283           PortableServer::ObjectId * id = myLink->getId() ;
284           CORBA::Object_var obj = _poa->id_to_reference(*id);
285           iobject = SUPERV::Link::_narrow(obj) ;
286           anOutPort->AddInPortObjRef( anInPort , SUPERV::Link::_duplicate( iobject ) ) ;
287         }
288         else {
289           iobject = anOutPort->InPortObjRef( anInPort ) ;
290         }
291       }
292     }
293   }
294   if ( !begin ) {
295     endService( "Port_Impl::Link" ) ;
296   }
297   return SUPERV::Link::_duplicate( iobject ) ;
298 }
299
300 bool Port_Impl::IsInput() {
301 //  beginService( "Port_Impl::IsInput" ) ;
302   bool RetVal = _InputPort ;
303 //  MESSAGE( Name() << "IsInput() " << _InputPort ) ;
304 //  endService( "Port_Impl::IsInput" ) ;
305   return RetVal ;
306 }
307
308 bool Port_Impl::IsLinked() {
309 //  beginService( "Port_Impl::IsLinked" ) ;
310   bool RetVal = false ;
311   GraphBase::InPort * anInPort = _DataFlowNode->GetChangeInPort( _DataFlowPort->PortName() ) ;
312   GraphBase::OutPort * anOutPort = NULL ;
313   if ( anInPort) {
314     anOutPort = anInPort->GetOutPort() ;
315   }
316   if ( _DataFlowNode->IsEndLoopNode() && anOutPort &&
317        !strcmp( _DataFlowNode->CoupledNode()->Name() , anOutPort->NodeName() ) ) {
318 //    MESSAGE( "Link " << anOutPort->NodeName() << "("
319 //            << anOutPort->PortName() << ") --> " << _DataFlowNode->Name() << "("
320 //            << anInPort->PortName() << ")" << " ignored" ) ;
321   }
322   else {
323     RetVal = _DataFlowNode->IsLinked( _DataFlowPort->PortName() ) ;
324   }
325 //  endService( "Port_Impl::IsLinked" ) ;
326   return RetVal ;
327 }
328
329 SUPERV::KindOfPort Port_Impl::Kind() {
330 //  beginService( "Port_Impl::Kind" ) ;
331   SUPERV::KindOfPort RetVal = _DataFlowPort->Kind() ;
332 //  endService( "Port_Impl::Kind" ) ;
333   return RetVal ;
334 }
335
336 bool Port_Impl::IsParam() {
337 //  beginService( "Port_Impl::IsParam" ) ;
338   bool RetVal = _DataFlowPort->IsParam() ;
339 //  endService( "Port_Impl::IsParam" ) ;
340   return RetVal ;
341 }
342
343 bool Port_Impl::IsGate() {
344 //  beginService( "Port_Impl::IsGate" ) ;
345   bool RetVal = _DataFlowPort->IsGate() ;
346 //  endService( "Port_Impl::IsGate" ) ;
347   return RetVal ;
348 }
349
350 bool Port_Impl::IsInLine() {
351 //  beginService( "Port_Impl::IsInLine" ) ;
352 //  bool RetVal = _DataFlowPort->IsBus() ;
353   bool RetVal = _DataFlowPort->IsInLine() ;
354 //  endService( "Port_Impl::IsInLine" ) ;
355   return RetVal ;
356 }
357
358 bool Port_Impl::IsLoop() {
359 //  beginService( "Port_Impl::IsLoop" ) ;
360   bool RetVal = _DataFlowPort->IsLoop() ;
361 //  endService( "Port_Impl::IsLoop" ) ;
362   return RetVal ;
363 }
364
365 bool Port_Impl::IsSwitch() {
366 //  beginService( "Port_Impl::IsSwitch" ) ;
367   bool RetVal = _DataFlowPort->IsSwitch() ;
368 //  endService( "Port_Impl::IsSwitch" ) ;
369   return RetVal ;
370 }
371
372 bool Port_Impl::IsEndSwitch() {
373 //  beginService( "Port_Impl::IsEndSwitch" ) ;
374   bool RetVal = _DataFlowPort->IsEndSwitch() ;
375 //  endService( "Port_Impl::IsEndSwitch" ) ;
376   return RetVal ;
377 }
378
379 bool Port_Impl::HasInput() {
380 //  beginService( "Port_Impl::" ) ;
381   bool RetVal = _DataFlowNode->HasInput( _DataFlowPort->PortName() ) ;
382 //  endService( "Port_Impl::" ) ;
383   return RetVal ;
384 }
385
386 SUPERV::GraphState Port_Impl::State() {
387 //  beginService( "Port_Impl::State" );
388   SUPERV::GraphState RetVal = SUPERV::EditingState ;
389   if ( _DataFlowEditor ) {
390     GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
391     if ( _DataFlowExecutor ) {
392       RetVal = _DataFlowExecutor->State( _DataFlowPort->NodeName() ,
393                                          _DataFlowPort->PortName() ) ;
394     }
395   }
396 //  endService( "Port_Impl::State" );
397   return RetVal ;
398 }
399
400 bool Port_Impl::IsDone() {
401 //  beginService( "Port_Impl::IsDone" );
402   bool RetVal = false ;
403   if ( _DataFlowEditor ) {
404     GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
405     if ( _DataFlowExecutor ) {
406       RetVal = _DataFlowExecutor->IsDone( _DataFlowPort->NodeName() ,
407                                           _DataFlowPort->PortName() ) ;
408     }
409   }
410 //  endService( "Port_Impl::IsDone" );
411   return RetVal ;
412 }