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