Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/superv.git] / src / Supervision / Link_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   : Link_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 "StreamLink_Impl.hxx"
38
39 Link_Impl::Link_Impl( CORBA::ORB_ptr orb ,
40                       PortableServer::POA_ptr poa ,
41                       PortableServer::ObjectId * contId , 
42                       const char *instanceName ,
43                       const char *interfaceName ,
44                       GraphEditor::DataFlow * DataFlowEditor ,
45                       GraphEditor::InNode * DataFlowNode ,
46                       const char *InputParameterName ,
47                       GraphEditor::InNode * DataFlowOutNode ,
48                       const char *OutputParameterName ,
49                       const bool Create ,
50                       const bool Get ,
51                       bool & Success ) :
52   Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
53 //  MESSAGE("Link_Impl::Link_Impl activate object instanceName("
54 //          << instanceName << ") interfaceName(" << interfaceName << ") --> "
55 //          << hex << (void *) this << dec )
56 //  beginService( "Link_Impl::Link_Impl" );
57   _Orb = CORBA::ORB::_duplicate(orb);
58   _Poa = poa ;
59   _ContId = contId ;
60   _DataFlowEditor = DataFlowEditor ;
61   _DataFlowNode = DataFlowNode ;
62   _DataFlowNodeName = DataFlowNode->Name() ;
63   _InputParameterName = new char[ strlen( InputParameterName ) + 1 ] ;
64   strcpy( _InputParameterName , InputParameterName ) ;
65   _DataFlowOutNode = DataFlowOutNode ;
66   _DataFlowOutNodeName = DataFlowOutNode->Name() ;
67   _OutputParameterName = new char[ strlen( OutputParameterName ) + 1 ] ;
68   strcpy( _OutputParameterName , OutputParameterName ) ;
69   if ( Create ) {
70     Success = _DataFlowEditor->AddLink( _DataFlowOutNode->Name() ,
71                                         _OutputParameterName , 
72                                         _DataFlowNode->Name() ,
73                                         _InputParameterName ) ;
74   }
75   else if ( Get ) {
76     char * DataFlowOutNodeName = NULL ;
77     char * OutParamName = NULL ;
78     Success = _DataFlowEditor->GetLink( _DataFlowNode->Name() ,
79                                         _InputParameterName , 
80                                         &DataFlowOutNodeName ,
81                                         &OutParamName ) ;
82   }
83   else {
84     Success = false ;
85   }
86   if ( Success ) {
87     _thisObj = this ;
88     _id = _poa->activate_object(_thisObj) ;
89   }
90 //  endService( "Link_Impl::Link_Impl" );
91 }
92
93 Link_Impl::Link_Impl() {
94 }
95
96 Link_Impl::~Link_Impl() {
97   beginService( "Link_Impl::~Link_Impl" );
98   endService( "Link_Impl::~Link_Impl" );
99 }
100
101 void Link_Impl::destroy() {
102   beginService( "Link_Impl::destroy" );
103   Delete() ;
104   _poa->deactivate_object(*_id) ;
105 //  CORBA::release(_poa) ;
106   delete(_id) ;
107 //  _thisObj->_remove_ref();
108   endService( "Link_Impl::destroy" );
109 }
110
111 bool Link_Impl::Delete() {
112 //  beginService( "Link_Impl::Delete" ) ;
113   bool RetVal = _DataFlowEditor->RemoveLink( _DataFlowOutNode->Name() ,
114                                              _OutputParameterName ,
115                                              _DataFlowNode->Name() ,
116                                              _InputParameterName ) ;
117   if ( RetVal ) {
118     DataFlowEditor()->UnValid() ;
119   }
120 //  endService( "Link_Impl::Delete" );
121   return RetVal ;
122 }
123
124 SUPERV::Port_ptr Link_Impl::InPort() {
125 //  beginService( "Link_Impl::InPort" );
126   SUPERV::Port_var iobject = SUPERV::Port::_nil() ;
127   GraphBase::InPort * anInPort = _DataFlowNode->GetChangeInPort( _InputParameterName ) ;
128   if ( anInPort ) {
129     iobject = anInPort->ObjRef() ;
130     if ( CORBA::is_nil( iobject ) ) {
131       if ( !anInPort->IsDataStream() ) {
132         Port_Impl * myPort = new Port_Impl( _Orb , _Poa , _ContId ,
133                                              instanceName() , interfaceName() ,
134                                              _DataFlowEditor ,
135                                              _DataFlowNode ,
136                                              (GraphBase::Port * ) anInPort ,
137                                              true ) ;
138         PortableServer::ObjectId * id = myPort->getId() ;
139         CORBA::Object_var obj = _poa->id_to_reference(*id);
140         iobject = SUPERV::Port::_narrow(obj) ;
141         anInPort->ObjRef( SUPERV::Port::_duplicate( iobject ) ) ;
142       }
143       else {
144         StreamPort_Impl * myStreamPort = new StreamPort_Impl(
145                                        _Orb , _Poa , _ContId ,
146                                        instanceName() , interfaceName() ,
147                                        DataFlowEditor() ,
148                                        DataFlowOutNode() ,
149                                        (GraphBase::Port * ) anInPort ,
150                                        false ) ;
151         PortableServer::ObjectId * id = myStreamPort->getId() ;
152         CORBA::Object_var obj = _poa->id_to_reference(*id);
153         SUPERV::StreamPort_var iStreamobject ;
154         iStreamobject = SUPERV::StreamPort::_narrow(obj) ;
155         anInPort->ObjRef( SUPERV::StreamPort::_duplicate( iStreamobject ) ) ;
156         iobject = SUPERV::Port::_narrow( iStreamobject ) ;
157       }
158     }
159     else {
160       iobject = anInPort->ObjRef() ;
161     }
162   }
163 //  endService( "Link_Impl::InPort" );
164   return SUPERV::Port::_duplicate( iobject ) ;
165 }
166
167 SUPERV::Port_ptr Link_Impl::OutPort() {
168 //  beginService( "Link_Impl::OutPort" );
169   SUPERV::Port_var iobject = SUPERV::Port::_nil() ;
170   GraphBase::OutPort * anOutPort = _DataFlowOutNode->GetChangeOutPort( _OutputParameterName ) ;
171   if ( anOutPort ) {
172     iobject = anOutPort->ObjRef() ;
173     if ( CORBA::is_nil( iobject ) ) {
174       if ( !anOutPort->IsDataStream() ) {
175         Port_Impl * myPort = new Port_Impl( _Orb , _Poa , _ContId ,
176                                              instanceName() , interfaceName() ,
177                                              _DataFlowEditor ,
178                                              _DataFlowOutNode ,
179                                              (GraphBase::Port * ) anOutPort ,
180                                              false ) ;
181         PortableServer::ObjectId * id = myPort->getId() ;
182         CORBA::Object_var obj = _poa->id_to_reference(*id);
183         iobject = SUPERV::Port::_narrow(obj) ;
184         anOutPort->ObjRef( SUPERV::Port::_duplicate( iobject ) ) ;
185       }
186       else {
187         StreamPort_Impl * myStreamPort = new StreamPort_Impl(
188                                        _Orb , _Poa , _ContId ,
189                                        instanceName() , interfaceName() ,
190                                        DataFlowEditor() ,
191                                        DataFlowOutNode() ,
192                                        (GraphBase::Port * ) anOutPort ,
193                                        false ) ;
194         PortableServer::ObjectId * id = myStreamPort->getId() ;
195         CORBA::Object_var obj = _poa->id_to_reference(*id);
196         SUPERV::StreamPort_var iStreamobject ;
197         iStreamobject = SUPERV::StreamPort::_narrow(obj) ;
198         anOutPort->ObjRef( SUPERV::StreamPort::_duplicate( iStreamobject ) ) ;
199         iobject = SUPERV::Port::_narrow( iStreamobject ) ;
200       }
201     }
202     else {
203       iobject = anOutPort->ObjRef() ;
204     }
205   }
206 //  endService( "Link_Impl::OutPort" );
207   return SUPERV::Port::_duplicate( iobject ) ;
208 }
209
210 GraphBase::SLink * Link_Impl::Info() {
211 //  beginService( "Link_Impl::Info" );
212 //  char* FromNodeName ;
213 //  char* FromServiceParameterName ;
214   GraphBase::SLink * RetVal = _DataFlowEditor->GetLink( _DataFlowNode->ComputingNode() ,
215                                                         _InputParameterName ) ;
216 //  endService( "Link_Impl::Info" );
217   return RetVal ;
218 }
219
220 CORBA::Long Link_Impl::CoordsSize() {
221 //  beginService( "Link_Impl::CoordsSize" ) ;
222   CORBA::Long RetVal = _DataFlowEditor->GetLinkCoordSize( _DataFlowOutNode->Name() ,
223                                                    _OutputParameterName ,
224                                                    _DataFlowNode->Name() ,
225                                                    _InputParameterName ) ;
226 //  endService( "Link_Impl::CoordsSize" );
227   return RetVal ;
228 }
229 bool Link_Impl::AddCoord( CORBA::Long index ,
230                           CORBA::Long X ,
231                           CORBA::Long Y ) {
232 //  beginService( "Link_Impl::AddCoord" ) ;
233   bool RetVal = _DataFlowEditor->AddLinkCoord( _DataFlowOutNode->Name() ,
234                                                _OutputParameterName ,
235                                                _DataFlowNode->Name() ,
236                                                _InputParameterName,
237                                                index , X , Y ) ;
238 //  endService( "Link_Impl::AddCoord" );
239   return RetVal ;
240 }
241 bool Link_Impl::ChangeCoord( CORBA::Long index ,
242                              CORBA::Long X ,
243                              CORBA::Long Y ) {
244 //  beginService( "Link_Impl::ChangeCoord" ) ;
245   bool RetVal = _DataFlowEditor->ChangeLinkCoord( _DataFlowOutNode->Name() ,
246                                                   _OutputParameterName ,
247                                                   _DataFlowNode->Name() ,
248                                                   _InputParameterName,
249                                                   index , X , Y ) ;
250 //  endService( "Link_Impl::ChangeCoord" );
251   return RetVal ;
252 }
253 bool Link_Impl::RemoveCoord( CORBA::Long index ) {
254 //  beginService( "Link_Impl::RemoveCoord" ) ;
255   bool RetVal = _DataFlowEditor->RemoveLinkCoord( _DataFlowOutNode->Name() ,
256                                                   _OutputParameterName ,
257                                                   _DataFlowNode->Name() ,
258                                                   _InputParameterName,
259                                                   index ) ;
260 //  endService( "Link_Impl::RemoveCoord" );
261   return RetVal ;
262 }
263 bool Link_Impl::Coords( CORBA::Long index , CORBA::Long & X , CORBA::Long & Y ) {
264 //  beginService( "Link_Impl::Coords" ) ;
265   bool RetVal = _DataFlowEditor->GetLinkCoord( _DataFlowOutNode->Name() ,
266                                                _OutputParameterName ,
267                                                _DataFlowNode->Name() ,
268                                                _InputParameterName,
269                                                index ,  X , Y ) ;
270 //  endService( "Link_Impl::Coords" );
271   return RetVal ;
272 }
273
274 /** 
275  * Return true if InPort's and OutPort's types are compatible
276  * {"string", "boolean", "char", "short", "int", "long", "float", "double", "objref"};
277  * Currently considered compatible ALL types except for objref - they must match exactly
278  * To make sure no other rule of compatibility is used - see Editor::OutNode::IsCompatible().
279  */ 
280 bool Link_Impl::IsValid() {
281   if ( InPort() && OutPort() ) {
282     return DataFlowEditor()->IsCompatible( OutPort()->Type(), InPort()->Type() );
283   }
284   return false;
285 }
286
287 /** 
288  * Return true the ports of this link and theLink are equal.  If you do on the GUI side - it does not
289  * return the correct value..
290  */ 
291 bool Link_Impl::IsEqual( SUPERV::Link_ptr theLink ) {
292   if ( InPort() && OutPort() && !CORBA::is_nil(theLink) ) {
293     return ( InPort() == theLink->InPort() && OutPort() == theLink->OutPort() );
294   }
295   return false;
296 }