Salome HOME
9b948e61df2d4bc6276a6f3691060ebebe1aa551
[modules/superv.git] / src / Supervision / Value_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   : Value_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 <strstream>
33 //#include <sstream>
34 #include <string>
35
36 //#include "utilities.h"
37
38 #include "Value_Impl.hxx"
39
40 #include "DataFlowExecutor_DataFlow.hxx"
41
42 Value_Impl::Value_Impl( CORBA::ORB_ptr orb ,
43                         PortableServer::POA_ptr poa ,
44                         PortableServer::ObjectId * contId , 
45                         const char *instanceName ,
46                         const char *interfaceName ,
47                         GraphEditor::DataFlow * DataFlowEditor ,
48                         GraphEditor::InNode * DataFlowNode ,
49                         const char *ParameterName ,
50                         const CORBA::Any * anAny ,
51                         const bool activate ) :
52   Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
53   if ( activate ) {
54 //    MESSAGE("Value_Impl::Value_Impl activate object instanceName("
55 //            << instanceName << ") interfaceName(" << interfaceName << ") --> "
56 //            << hex << (void *) this << dec )
57     _thisObj = this ;
58     _id = _poa->activate_object(_thisObj);
59   }
60   _Orb = CORBA::ORB::_duplicate(orb);
61   _Poa = poa ;
62   _ContId = contId ;
63   _DataFlowEditor = DataFlowEditor ;
64   _DataFlowNode = DataFlowNode ;
65   if ( ParameterName != NULL ) {
66     _ParameterName = new char[ strlen( ParameterName ) + 1 ] ;
67     strcpy( _ParameterName , ParameterName ) ;
68     if ( DataFlowNode ) {
69       _NodeName = new char[ strlen( DataFlowNode->Name() ) + 1 ] ;
70       strcpy( _NodeName , DataFlowNode->Name() ) ;
71     }
72     else {
73     _NodeName = NULL ;
74     }
75   }
76   else {
77     _ParameterName = NULL ;
78     _NodeName = NULL ;
79   }
80   _InputValue = false ;
81   _Any = new CORBA::Any( *anAny ) ;
82 }
83
84 Value_Impl::Value_Impl( CORBA::ORB_ptr orb ,
85                         PortableServer::POA_ptr poa ,
86                         PortableServer::ObjectId * contId , 
87                         const char *instanceName ,
88                         const char *interfaceName ,
89                         GraphEditor::DataFlow * DataFlowEditor ,
90                         GraphEditor::InNode * DataFlowNode ,
91                         const char *ParameterName ,
92                         const bool activate ) :
93   Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
94   if ( activate ) {
95 //    MESSAGE("Value_Impl::Value_Impl activate object instanceName("
96 //            << instanceName << ") interfaceName(" << interfaceName << ") --> "
97 //            << hex << (void *) this << dec )
98     _thisObj = this ;
99     _id = _poa->activate_object(_thisObj);
100   }
101   _Orb = CORBA::ORB::_duplicate(orb);
102   _Poa = poa ;
103   _ContId = contId ;
104   _DataFlowEditor = DataFlowEditor ;
105   _DataFlowNode = DataFlowNode ;
106   if ( ParameterName != NULL ) {
107     _ParameterName = new char[ strlen( ParameterName ) + 1 ] ;
108     strcpy( _ParameterName , ParameterName ) ;
109     if ( DataFlowNode ) {
110       _NodeName = new char[ strlen( DataFlowNode->Name() ) + 1 ] ;
111       strcpy( _NodeName , DataFlowNode->Name() ) ;
112     }
113     else {
114     _NodeName = NULL ;
115     }
116   }
117   else {
118     _ParameterName = NULL ;
119     _NodeName = NULL ;
120   }
121   _InputValue = false ;
122   _Any = new CORBA::Any() ;
123 }
124
125 Value_Impl::Value_Impl() {
126 }
127
128 Value_Impl::~Value_Impl() {
129   beginService( "Value_Impl::~Value_Impl" );
130   endService( "Value_Impl::~Value_Impl" );
131 }
132
133 void Value_Impl::destroy() {
134   beginService( "Value_Impl::destroy" );
135   _poa->deactivate_object(*_id) ;
136   CORBA::release(_poa) ;
137   delete(_id) ;
138   _thisObj->_remove_ref();
139   endService( "Value_Impl::destroy" );
140 }
141
142 void Value_Impl::InPort( bool anInput ) {
143   _InputPort = anInput ;
144 }
145
146 bool Value_Impl::Input( const CORBA::Any * anAny ) {
147 //  _DataFlowEditor->AddInputData( _DataFlowNode->Name() ,
148 //                                 _ParameterName ,
149 //                                 *anAny ) ;
150   delete _Any ;
151   _Any = new CORBA::Any( *anAny ) ;
152   _InputValue = true ;
153   return true ;
154 }
155
156 CORBA::Any * Value_Impl::ToAny() {
157   CORBA::Any * anAny ;
158   if ( _DataFlowEditor ) {
159     char * name ;
160     if ( _DataFlowNode ) {
161       name = _DataFlowNode->Name() ;
162     }
163     else {
164       name = _DataFlowEditor->Graph()->Name() ;
165     }
166     if ( _InputPort ) {
167       if ( _DataFlowNode ) {
168         _InputValue = _DataFlowNode->HasInput( _ParameterName ) ;
169       }
170       else {
171         _InputValue = _DataFlowEditor->HasInput( _ParameterName ) ;
172       }
173       GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
174       if ( _DataFlowExecutor ) {
175         anAny = new CORBA::Any( * _DataFlowExecutor->GetInData( name ,
176                                                                 _ParameterName ) ) ;
177       }
178       else if ( _InputValue ) {
179         anAny = new CORBA::Any( * _DataFlowEditor->GetInData( name ,
180                                                               _ParameterName ) ) ;
181       }
182       else {
183         anAny = new CORBA::Any( *_Any ) ;
184       }
185     }
186     else {
187       GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
188       if ( _DataFlowExecutor ) {
189         anAny = new CORBA::Any( * _DataFlowExecutor->GetOutData( name ,
190                                                                 _ParameterName ) ) ;
191       }
192       else {
193         anAny = new CORBA::Any( *_Any ) ;
194       }
195     }
196   }
197   else {
198     anAny = new CORBA::Any( *_Any ) ;
199   }
200   return anAny ;
201 }
202
203 char * Value_Impl::ToString() {
204 //  beginService( "Value_Impl::ToString" );
205   CORBA::Any anAny = *ToAny() ;
206   if ( _DataFlowEditor ) {
207     char * name ;
208     if ( _DataFlowNode ) {
209       name = _DataFlowNode->Name() ;
210     }
211     else {
212       name = _DataFlowEditor->Graph()->Name() ;
213     }
214 //    cout << "Value_Impl::ToString " << name << " " << _ParameterName << " " ;
215     if ( _InputPort ) {
216       if ( _DataFlowNode ) {
217         _InputValue = _DataFlowNode->HasInput( _ParameterName ) ;
218       }
219       else {
220         _InputValue = _DataFlowEditor->HasInput( _ParameterName ) ;
221       }
222       GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
223       if ( _DataFlowExecutor ) {
224 //        cout << "_DataFlowExecutor->GetInData" << endl ;
225         anAny = * _DataFlowExecutor->GetInData( name ,
226                                                 _ParameterName ) ;
227       }
228       else if ( _InputValue ) {
229 //        cout << "_DataFlowEditor->GetInData _DataFlowNode " << _DataFlowNode
230 //             << " _InputValue " << _InputValue << endl ;
231         anAny = * _DataFlowEditor->GetInData( name ,
232                                               _ParameterName ) ;
233       }
234       else {
235         anAny = *_Any ;
236       }
237     }
238     else {
239       GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
240       if ( _DataFlowExecutor ) {
241         anAny = * _DataFlowExecutor->GetOutData( name ,
242                                                  _ParameterName ) ;
243       }
244       else {
245         anAny = *_Any ;
246       }
247     }
248   }
249   ostringstream astr ;
250   const char * retstr ;
251   int startstr = 0 ;
252   switch (anAny.type()->kind()) {
253     case CORBA::tk_string: {
254       anAny >>= retstr;
255 //      MESSAGE( "ToString( string ) '" << retstr << "'" );
256       break ;
257     }
258     case CORBA::tk_long: {
259       long l;
260       anAny >>= l;
261       astr << l << ends ;
262       retstr = astr.str().c_str() ;
263 //      MESSAGE( "ToString( long ) '" << l << " " << retstr << "'" );
264       break ;
265     }
266     case CORBA::tk_double: {
267       double d;
268       anAny >>= d;
269       astr << setw(25) << setprecision(18) << d << ends ;
270       retstr = astr.str().c_str() ;
271       int i = 0 ;
272       while ( i < (int ) strlen( retstr ) && retstr[ i++ ] == ' ' ) {
273         startstr = i ;
274       }
275 //      MESSAGE( "ToString( double ) '" << d << "' '" << retstr << "' '" << &retstr[ startstr ] << "'");
276       break ;
277     }
278     case CORBA::tk_objref: {
279       CORBA::Object_ptr obj ;
280       try {
281         anAny >>= obj ;
282         retstr = _Orb->object_to_string( obj );
283 //        MESSAGE( "ToString( object ) '" << retstr << "'" );
284       }
285       catch ( ... ) {
286         retstr = "object_to_string catched " ;
287       }
288       break ;
289     }
290     default: {
291       retstr = "Unknown CORBA::Any Type" ;
292 //      MESSAGE( retstr );
293       break ;
294     }
295   }
296 //  endService( "Value_Impl::ToString" );
297   return CORBA::string_dup( &retstr[ startstr ] ) ;
298 }
299
300 bool Value_Impl::IsIOR() {
301   CORBA::Any anAny = *ToAny() ;
302   if ( _DataFlowEditor ) {
303     char * name ;
304     if ( _DataFlowNode ) {
305       name = _DataFlowNode->Name() ;
306     }
307     else {
308       name = _DataFlowEditor->Graph()->Name() ;
309     }
310     if ( _InputPort ) {
311       if ( _DataFlowNode ) {
312         _InputValue = _DataFlowNode->HasInput( _ParameterName ) ;
313       }
314       else {
315         _InputValue = _DataFlowEditor->HasInput( _ParameterName ) ;
316       }
317       if ( _InputValue && _DataFlowEditor->IsEditing() ) {
318         anAny = * _DataFlowEditor->GetInData( name ,
319                                               _ParameterName ) ;
320       }
321       else {
322         GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
323         if ( _DataFlowExecutor ) {
324           anAny = * _DataFlowExecutor->GetInData( name ,
325                                                   _ParameterName ) ;
326         }
327       }
328     }
329     else {
330       if ( _InputValue && _DataFlowEditor->IsEditing() ) {
331         anAny = * _DataFlowEditor->GetOutData( name ,
332                                                _ParameterName ) ;
333       }
334       else {
335         GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
336         if ( _DataFlowExecutor ) {
337           anAny = * _DataFlowExecutor->GetOutData( name ,
338                                                    _ParameterName ) ;
339         }
340       }
341     }
342   }
343   return(anAny.type()->kind() == CORBA::tk_objref);
344 }
345
346 char* Value_Impl::ComponentDataType() {
347   GraphExecutor::DataFlow* _DataFlowExecutor = _DataFlowEditor->Executor();
348   const GraphBase::ComputingNode* node = _DataFlowExecutor->Graph()->GetGraphNode(_DataFlowNode->Name());
349   if ( node->IsFactoryNode() ) {
350     Engines::Component_var compo = ((GraphBase::FactoryNode * ) node )->Component();
351     if ( CORBA::is_nil( compo ) ) {
352       return ( strdup( "UnknownComponent" ) ) ;
353     }
354     else {
355       return ( strdup( compo->instanceName() ) ) ;
356     }
357   }
358   return ( strdup ( "UnknownComponent" ) ) ;
359 }