]> SALOME platform Git repositories - modules/yacs.git/blob - src/evalyfx/YACSEvalPort.cxx
Salome HOME
debug for test case.
[modules/yacs.git] / src / evalyfx / YACSEvalPort.cxx
1 // Copyright (C) 2012-2015  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 // 
19 // Author : Anthony Geay (EDF R&D)
20
21 #include "YACSEvalPort.hxx"
22 #include "YACSEvalSeqAny.hxx"
23 #include "OutputPort.hxx"
24 #include "InputPort.hxx"
25 #include "TypeCode.hxx"
26
27 #include "CORBAPorts.hxx"
28 #include "PythonPorts.hxx"
29 #include "AnyInputPort.hxx"
30
31 #include <limits>
32 #include <sstream>
33 #include <typeinfo>
34
35 const char YACSEvalAnyDouble::TYPE_REPR[]="double";
36
37 const char YACSEvalAnyInt::TYPE_REPR[]="int";
38
39 int YACSEvalAnyDouble::toInt() const
40 {
41   throw YACS::Exception("YACSEvalAnyDouble::toInt : invalid type requested !");
42 }
43
44 double YACSEvalAnyDouble::toDouble() const
45 {
46   return _v;
47 }
48
49 YACSEvalAnyDouble *YACSEvalAnyDouble::deepCpy() const
50 {
51   return new YACSEvalAnyDouble(*this);
52 }
53
54 int YACSEvalAnyInt::toInt() const
55 {
56   return _v;
57 }
58
59 double YACSEvalAnyInt::toDouble() const
60 {
61   throw YACS::Exception("YACSEvalAnyInt::toDouble : invalid type requested !");
62 }
63
64 YACSEvalAnyInt *YACSEvalAnyInt::deepCpy() const
65 {
66   return new YACSEvalAnyInt(*this);
67 }
68
69 bool YACSEvalPort::IsInputPortPublishable(const YACS::ENGINE::InputPort *port)
70 {
71   YACS::ENGINE::TypeCode *tc(port->edGetType());
72   if(!tc)
73     throw YACS::Exception("YACSEvalPort::IsInPortPublishable : null type code !");
74   if(tc->kind()==YACS::ENGINE::Double || tc->kind()==YACS::ENGINE::Int)
75     return true;
76   if(tc->kind()==YACS::ENGINE::String)
77     {
78       if(port->edIsManuallyInitialized())
79         return false;
80     }
81   return true;
82 }
83
84 std::string YACSEvalPort::GetTypeOfData(const YACS::ENGINE::DataPort *port)
85 {
86   YACS::ENGINE::TypeCode *tc(port->edGetType());
87   if(!tc)
88     throw YACS::Exception("YACSEvalPort::GetTypeOfData : null type code !");
89   if(tc->kind()==YACS::ENGINE::Double)
90     return std::string(YACSEvalAnyDouble::TYPE_REPR);
91   if(tc->kind()==YACS::ENGINE::Int)
92     return std::string(YACSEvalAnyInt::TYPE_REPR);
93   throw YACS::Exception("YACSEvalPort::GetTypeOfData : Unrecognized type of data ! Must be int or double for the moment !");
94 }
95
96 YACSEvalInputPort::YACSEvalInputPort(YACS::ENGINE::InputPort *ptr):_ptr(ptr),_mySeq(0),_isRandom(false),_isLocked(false),_undergroundPort(0)
97 {
98   GetTypeOfData(_ptr);
99 }
100
101 std::string YACSEvalInputPort::getName() const
102 {
103   return _ptr->getName();
104 }
105
106 std::string YACSEvalInputPort::getTypeOfData() const
107 {
108   return GetTypeOfData(_ptr);
109 }
110
111 bool YACSEvalInputPort::isOKForLock() const
112 {
113   if(_isLocked)
114     return true;
115   return hasDefaultValueDefined();
116 }
117
118 bool YACSEvalInputPort::hasDefaultValueDefined() const
119 {
120   return _ptr->edIsManuallyInitialized();
121 }
122
123 /*!
124  * The caller has the ownership of the returned instance.
125  */
126 YACSEvalAny *YACSEvalInputPort::getDefaultValueDefined() const
127 {
128   if(!hasDefaultValueDefined())
129     throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : no default value defined !");
130   _ptr->exRestoreInit();
131   YACS::ENGINE::InputPyPort *i0(dynamic_cast<YACS::ENGINE::InputPyPort *>(_ptr));
132   if(i0)
133     {
134       PyObject *data(i0->getPyObj());
135       if(!data)
136         throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : internal error !");
137       if(PyFloat_Check(data))
138         return new YACSEvalAnyDouble(PyFloat_AsDouble(data));
139       if(PyInt_Check(data))
140         return new YACSEvalAnyInt((int)PyInt_AsLong(data));
141       throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : unmanaged types different from int and double for py !");
142     }
143   YACS::ENGINE::AnyInputPort *i1(dynamic_cast<YACS::ENGINE::AnyInputPort *>(_ptr));
144   if(i1)
145     {
146       YACS::ENGINE::Any *data(i1->getValue());
147       return convertFromInternalAnyToExternal(data);
148     }
149   YACS::ENGINE::InputCorbaPort *i2(dynamic_cast<YACS::ENGINE::InputCorbaPort *>(_ptr));
150   if(i2)
151     {
152       CORBA::Any *data(i2->getAny());
153       if(!data)
154         throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : internal error # 3 !");
155       std::string td(getTypeOfData());
156       if(td==YACSEvalAnyDouble::TYPE_REPR)
157         {
158           CORBA::Double v;
159           if((*data)>>=v)
160             return new YACSEvalAnyDouble(v);
161           else
162             throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : internal error # 31 !");
163         }
164       if(td==YACSEvalAnyInt::TYPE_REPR)
165         {
166           CORBA::Long v;
167           if((*data)>>=v)
168             return new YACSEvalAnyInt(v);
169           else
170             throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : internal error # 32 !");
171         }
172     }
173   std::ostringstream oss;
174   oss << "YACSEvalInputPort::getDefaultValueDefined : Please contact anthony.geay@edf.fr with -> \"" << typeid(*_ptr).name() << "\" !";
175   throw YACS::Exception(oss.str());
176 }
177
178 /*!
179  * This method does not steal the ownership of \a parameter (a deep copy is performed).
180  */
181 void YACSEvalInputPort::setDefaultValue(const YACSEvalAny *parameter)
182 {
183   checkForNonConstMethod();
184   if(parameter)
185     {
186       if(parameter->getTypeOfData()!=getTypeOfData())
187         {
188           std::ostringstream oss; oss << "YACSEvalInputPort::setParameter : Type of data mismatch ! Expecting \"" << getTypeOfData() << "\" having \"" <<  parameter->getTypeOfData() << "\" !";
189           throw YACS::Exception(oss.str());
190         }
191     }
192   //
193   YACS::ENGINE::InputPyPort *i0(dynamic_cast<YACS::ENGINE::InputPyPort *>(_ptr));
194   YACS::ENGINE::AnyInputPort *i1(dynamic_cast<YACS::ENGINE::AnyInputPort *>(_ptr));
195   YACS::ENGINE::InputCorbaPort *i2(dynamic_cast<YACS::ENGINE::InputCorbaPort *>(_ptr));
196   if(parameter)
197     {
198       const YACSEvalAnyDouble *par0(dynamic_cast<const YACSEvalAnyDouble *>(parameter));
199       const YACSEvalAnyInt *par1(dynamic_cast<const YACSEvalAnyInt *>(parameter));
200       if(i0)
201         {
202           if(par0)
203             {
204               PyObject *obj(PyFloat_FromDouble(par0->toDouble()));
205               i0->put(obj);
206               Py_XDECREF(obj);
207             }
208           else if(par1)
209             {
210               PyObject *obj(PyInt_FromLong(par1->toInt()));
211               i0->put(obj);
212               Py_XDECREF(obj);
213             }
214           else
215             throw YACS::Exception("YACSEvalInputPort::setDefaultValueDefined : unmanaged types different from int and double for py !");
216         }
217       else if(i1)
218         {
219           if(par0)
220             {
221               YACS::ENGINE::AtomAny *v(YACS::ENGINE::AtomAny::New(par0->toDouble()));
222               i1->put(v);
223               v->decrRef();
224             }
225           else if(par1)
226             {
227               YACS::ENGINE::AtomAny *v(YACS::ENGINE::AtomAny::New(par1->toInt()));
228               i1->put(v);
229               v->decrRef();
230             }
231           else
232             throw YACS::Exception("YACSEvalInputPort::setDefaultValueDefined : unmanaged types different from int and double for yacsport !");
233         }
234       else if(i2)
235         {
236           CORBA::Any v;
237           if(par0)
238             v<<=par0->toDouble();
239           else if(par1)
240             v<<=(CORBA::Short)par1->toInt();
241           else
242             throw YACS::Exception("YACSEvalInputPort::setDefaultValueDefined : unmanaged types different from int and double for corbaport !");
243           i2->put(&v);
244         }
245       else
246         {
247           std::ostringstream oss;
248           oss << "YACSEvalInputPort::getDefaultValueDefined : Please contact anthony.geay@edf.fr with -> \"" << typeid(*_ptr).name() << "\" !";
249           throw YACS::Exception(oss.str());
250         }
251       _ptr->exSaveInit();
252     }
253   else
254     _ptr->edRemoveManInit();
255 }
256
257 void YACSEvalInputPort::setSequenceOfValuesToEval(const YACSEvalSeqAny *vals)
258 {
259   if(!_isRandom && _isLocked)
260     throw YACS::Exception("YACSEvalInputPort::setSequenceOfValuesToEval : trying to set a sequence on a var not declared as random !");
261   //checkForNonConstMethod not called here because it is a special non const method !
262   if(!vals)
263     throw YACS::Exception("YACSEvalInputPort::setSequenceOfValuesToEval : input is NULL !");
264   if(vals==_mySeq)
265     return ;
266   if(_mySeq)
267     delete _mySeq;
268   _mySeq=vals->copy();
269   _isRandom=true;
270 }
271
272 bool YACSEvalInputPort::hasSequenceOfValuesToEval() const
273 {
274   return (_mySeq!=0);
275 }
276
277 void YACSEvalInputPort::declareRandomnessStatus(bool isRandom)
278 {
279   checkForNonConstMethod();
280   _isRandom=isRandom;
281   if(!_isRandom)
282     _undergroundPort=0;
283 }
284
285 std::size_t YACSEvalInputPort::initializeUndergroundWithSeq() const
286 {
287   if(!_undergroundPort)
288     throw YACS::Exception("YACSEvalInputPort::initializeUndergroundWithSeq : internal error ! this input has not been locked properly ! Need to be locked !");
289   if(!_mySeq)
290     throw YACS::Exception("YACSEvalInputPort::initializeUndergroundWithSeq : this is not set as a sequence !");
291   _mySeq->initialize(_undergroundPort);
292   return _mySeq->size();
293 }
294
295 YACSEvalInputPort::~YACSEvalInputPort()
296 {
297   delete _mySeq;
298 }
299
300 void YACSEvalInputPort::setUndergroundPortToBeSet(YACS::ENGINE::InputPyPort *p) const
301 {
302   if(!_isRandom)
303     throw YACS::Exception("YACSEvalInputPort::setUndergroundPortToBeSet : trying to set a non random var !");
304   _undergroundPort=p;
305 }
306
307 YACSEvalAny *YACSEvalInputPort::convertFromInternalAnyToExternal(YACS::ENGINE::Any *data) const
308 {
309   if(!data)
310     throw YACS::Exception("YACSEvalInputPort::convertFromInternalAnyToExternal : internal error # 2 !");
311   YACS::ENGINE::AtomAny *data2(dynamic_cast<YACS::ENGINE::AtomAny *>(data));
312   if(!data2)
313     throw YACS::Exception("YACSEvalInputPort::convertFromInternalAnyToExternal : internal error # 21 !");
314   std::string td(getTypeOfData());
315   if(td==YACSEvalAnyDouble::TYPE_REPR)
316     return new YACSEvalAnyDouble(data2->getDoubleValue());
317   if(td==YACSEvalAnyInt::TYPE_REPR)
318     return new YACSEvalAnyInt(data2->getIntValue());
319   throw YACS::Exception("YACSEvalInputPort::convertFromInternalAnyToExternal : unmanaged types different from int and double for any !");
320 }
321
322 void YACSEvalInputPort::checkForNonConstMethod() const
323 {
324   if(_isLocked)
325     throw YACS::Exception("YACSEvalInputPort::checkForNonConstMethod : trying to modify a locked input ! To release it call unlockAll on YACSEvalYFX instance owning this !");
326 }
327
328 YACSEvalOutputPort::YACSEvalOutputPort(YACS::ENGINE::OutputPort *ptr):_ptr(ptr)
329 {
330   GetTypeOfData(_ptr);
331 }
332
333 std::string YACSEvalOutputPort::getName() const
334 {
335   return _ptr->getName();
336 }
337
338 std::string YACSEvalOutputPort::getTypeOfData() const
339 {
340   return GetTypeOfData(_ptr);
341 }