1 // Copyright (C) 2012-2016 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (EDF R&D)
21 #include "YACSEvalPort.hxx"
22 #include "YACSEvalSeqAny.hxx"
23 #include "OutputPort.hxx"
24 #include "InputPort.hxx"
25 #include "TypeCode.hxx"
27 #include "CORBAPorts.hxx"
28 #include "PythonPorts.hxx"
29 #include "AnyInputPort.hxx"
35 const char YACSEvalAnyDouble::TYPE_REPR[]="double";
37 const char YACSEvalAnyInt::TYPE_REPR[]="int";
39 int YACSEvalAnyDouble::toInt() const
41 throw YACS::Exception("YACSEvalAnyDouble::toInt : invalid type requested !");
44 double YACSEvalAnyDouble::toDouble() const
49 YACSEvalAnyDouble *YACSEvalAnyDouble::deepCpy() const
51 return new YACSEvalAnyDouble(*this);
54 int YACSEvalAnyInt::toInt() const
59 double YACSEvalAnyInt::toDouble() const
61 throw YACS::Exception("YACSEvalAnyInt::toDouble : invalid type requested !");
64 YACSEvalAnyInt *YACSEvalAnyInt::deepCpy() const
66 return new YACSEvalAnyInt(*this);
69 bool YACSEvalPort::IsInputPortPublishable(const YACS::ENGINE::InputPort *port)
71 YACS::ENGINE::TypeCode *tc(port->edGetType());
73 throw YACS::Exception("YACSEvalPort::IsInPortPublishable : null type code !");
74 if(tc->kind()==YACS::ENGINE::Double || tc->kind()==YACS::ENGINE::Int)
76 if(tc->kind()==YACS::ENGINE::String)
78 if(port->edIsManuallyInitialized())
84 bool YACSEvalPort::IsOutputPortPublishable(const YACS::ENGINE::OutputPort *port)
86 YACS::ENGINE::TypeCode *tc(port->edGetType());
88 throw YACS::Exception("YACSEvalPort::IsOutputPortPublishable : null type code !");
89 return tc->kind()==YACS::ENGINE::Double || tc->kind()==YACS::ENGINE::Int;
92 std::string YACSEvalPort::GetTypeOfData(const YACS::ENGINE::DataPort *port)
94 YACS::ENGINE::TypeCode *tc(port->edGetType());
96 throw YACS::Exception("YACSEvalPort::GetTypeOfData : null type code !");
97 if(tc->kind()==YACS::ENGINE::Double)
98 return std::string(YACSEvalAnyDouble::TYPE_REPR);
99 if(tc->kind()==YACS::ENGINE::Int)
100 return std::string(YACSEvalAnyInt::TYPE_REPR);
101 throw YACS::Exception("YACSEvalPort::GetTypeOfData : Unrecognized type of data ! Must be int or double for the moment !");
104 YACSEvalInputPort::YACSEvalInputPort(YACS::ENGINE::InputPort *ptr):_ptr(ptr),_mySeq(0),_isRandom(false),_isLocked(false),_undergroundPort(0)
109 std::string YACSEvalInputPort::getName() const
111 return _ptr->getName();
114 std::string YACSEvalInputPort::getTypeOfData() const
116 return GetTypeOfData(_ptr);
119 bool YACSEvalInputPort::isOKForLock() const
123 return hasDefaultValueDefined();
126 bool YACSEvalInputPort::isLocked() const
131 bool YACSEvalInputPort::hasDefaultValueDefined() const
133 return _ptr->edIsManuallyInitialized();
137 * The caller has the ownership of the returned instance.
139 YACSEvalAny *YACSEvalInputPort::getDefaultValueDefined() const
141 if(!hasDefaultValueDefined())
142 throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : no default value defined !");
143 _ptr->exRestoreInit();
144 YACS::ENGINE::InputPyPort *i0(dynamic_cast<YACS::ENGINE::InputPyPort *>(_ptr));
147 PyObject *data(i0->getPyObj());
149 throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : internal error !");
150 if(PyFloat_Check(data))
151 return new YACSEvalAnyDouble(PyFloat_AsDouble(data));
152 if(PyInt_Check(data))
153 return new YACSEvalAnyInt((int)PyInt_AsLong(data));
154 throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : unmanaged types different from int and double for py !");
156 YACS::ENGINE::AnyInputPort *i1(dynamic_cast<YACS::ENGINE::AnyInputPort *>(_ptr));
159 YACS::ENGINE::Any *data(i1->getValue());
160 return convertFromInternalAnyToExternal(data);
162 YACS::ENGINE::InputCorbaPort *i2(dynamic_cast<YACS::ENGINE::InputCorbaPort *>(_ptr));
165 CORBA::Any *data(i2->getAny());
167 throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : internal error # 3 !");
168 std::string td(getTypeOfData());
169 if(td==YACSEvalAnyDouble::TYPE_REPR)
173 return new YACSEvalAnyDouble(v);
175 throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : internal error # 31 !");
177 if(td==YACSEvalAnyInt::TYPE_REPR)
181 return new YACSEvalAnyInt(v);
183 throw YACS::Exception("YACSEvalInputPort::getDefaultValueDefined : internal error # 32 !");
186 std::ostringstream oss;
187 oss << "YACSEvalInputPort::getDefaultValueDefined : Please contact anthony.geay@edf.fr with -> \"" << typeid(*_ptr).name() << "\" !";
188 throw YACS::Exception(oss.str());
192 * This method does not steal the ownership of \a parameter (a deep copy is performed).
194 void YACSEvalInputPort::setDefaultValue(const YACSEvalAny *parameter)
196 checkForNonConstMethod();
199 if(parameter->getTypeOfData()!=getTypeOfData())
201 std::ostringstream oss; oss << "YACSEvalInputPort::setParameter : Type of data mismatch ! Expecting \"" << getTypeOfData() << "\" having \"" << parameter->getTypeOfData() << "\" !";
202 throw YACS::Exception(oss.str());
206 YACS::ENGINE::InputPyPort *i0(dynamic_cast<YACS::ENGINE::InputPyPort *>(_ptr));
207 YACS::ENGINE::AnyInputPort *i1(dynamic_cast<YACS::ENGINE::AnyInputPort *>(_ptr));
208 YACS::ENGINE::InputCorbaPort *i2(dynamic_cast<YACS::ENGINE::InputCorbaPort *>(_ptr));
211 const YACSEvalAnyDouble *par0(dynamic_cast<const YACSEvalAnyDouble *>(parameter));
212 const YACSEvalAnyInt *par1(dynamic_cast<const YACSEvalAnyInt *>(parameter));
217 PyObject *obj(PyFloat_FromDouble(par0->toDouble()));
223 PyObject *obj(PyInt_FromLong(par1->toInt()));
228 throw YACS::Exception("YACSEvalInputPort::setDefaultValueDefined : unmanaged types different from int and double for py !");
234 YACS::ENGINE::AtomAny *v(YACS::ENGINE::AtomAny::New(par0->toDouble()));
240 YACS::ENGINE::AtomAny *v(YACS::ENGINE::AtomAny::New(par1->toInt()));
245 throw YACS::Exception("YACSEvalInputPort::setDefaultValueDefined : unmanaged types different from int and double for yacsport !");
251 v<<=par0->toDouble();
253 v<<=(CORBA::Short)par1->toInt();
255 throw YACS::Exception("YACSEvalInputPort::setDefaultValueDefined : unmanaged types different from int and double for corbaport !");
260 std::ostringstream oss;
261 oss << "YACSEvalInputPort::getDefaultValueDefined : Please contact anthony.geay@edf.fr with -> \"" << typeid(*_ptr).name() << "\" !";
262 throw YACS::Exception(oss.str());
267 _ptr->edRemoveManInit();
270 void YACSEvalInputPort::setSequenceOfValuesToEval(const YACSEvalSeqAny *vals)
272 if(!_isRandom && _isLocked)
273 throw YACS::Exception("YACSEvalInputPort::setSequenceOfValuesToEval : trying to set a sequence on a var not declared as random !");
274 //checkForNonConstMethod not called here because it is a special non const method !
276 throw YACS::Exception("YACSEvalInputPort::setSequenceOfValuesToEval : input is NULL !");
285 bool YACSEvalInputPort::hasSequenceOfValuesToEval() const
290 void YACSEvalInputPort::declareRandomnessStatus(bool isRandom)
292 checkForNonConstMethod();
298 std::size_t YACSEvalInputPort::initializeUndergroundWithSeq() const
300 if(!_undergroundPort)
301 throw YACS::Exception("YACSEvalInputPort::initializeUndergroundWithSeq : internal error ! this input has not been locked properly ! Need to be locked !");
303 throw YACS::Exception("YACSEvalInputPort::initializeUndergroundWithSeq : this is not set as a sequence !");
304 _mySeq->initialize(_undergroundPort);
305 return _mySeq->size();
308 YACSEvalInputPort::~YACSEvalInputPort()
313 void YACSEvalInputPort::setUndergroundPortToBeSet(YACS::ENGINE::InputPyPort *p) const
316 throw YACS::Exception("YACSEvalInputPort::setUndergroundPortToBeSet : trying to set a non random var !");
320 YACSEvalAny *YACSEvalInputPort::convertFromInternalAnyToExternal(YACS::ENGINE::Any *data) const
323 throw YACS::Exception("YACSEvalInputPort::convertFromInternalAnyToExternal : internal error # 2 !");
324 YACS::ENGINE::AtomAny *data2(dynamic_cast<YACS::ENGINE::AtomAny *>(data));
326 throw YACS::Exception("YACSEvalInputPort::convertFromInternalAnyToExternal : internal error # 21 !");
327 std::string td(getTypeOfData());
328 if(td==YACSEvalAnyDouble::TYPE_REPR)
329 return new YACSEvalAnyDouble(data2->getDoubleValue());
330 if(td==YACSEvalAnyInt::TYPE_REPR)
331 return new YACSEvalAnyInt(data2->getIntValue());
332 throw YACS::Exception("YACSEvalInputPort::convertFromInternalAnyToExternal : unmanaged types different from int and double for any !");
335 void YACSEvalInputPort::checkForNonConstMethod() const
338 throw YACS::Exception("YACSEvalInputPort::checkForNonConstMethod : trying to modify a locked input ! To release it call unlockAll on YACSEvalYFX instance owning this !");
341 YACSEvalOutputPort::YACSEvalOutputPort(YACS::ENGINE::OutputPort *ptr):_ptr(ptr),_isQOfInt(false)
346 std::string YACSEvalOutputPort::getName() const
348 return _ptr->getName();
351 std::string YACSEvalOutputPort::getTypeOfData() const
353 return GetTypeOfData(_ptr);