1 // Copyright (C) 2012-2015 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 %define EVALYFXDOCSTRING
22 "Module to evaluate Y=f(X) easily."
25 %module(docstring=EVALYFXDOCSTRING) evalyfx
27 %feature("autodoc", "1");
29 %include "engtypemaps.i"
32 #include "YACSEvalYFX.hxx"
33 #include "YACSEvalPort.hxx"
34 #include "YACSEvalSeqAny.hxx"
35 #include "YACSEvalResource.hxx"
36 #include "YACSEvalSession.hxx"
38 static void convertPyToIntArr(PyObject *pyLi, std::vector<int>& arr)
40 if(PyList_Check(pyLi))
42 int size=PyList_Size(pyLi);
44 for(int i=0;i<size;i++)
46 PyObject *o=PyList_GetItem(pyLi,i);
49 int val=(int)PyInt_AS_LONG(o);
53 throw YACS::Exception("list must contain integers only");
56 else if(PyTuple_Check(pyLi))
58 int size=PyTuple_Size(pyLi);
60 for(int i=0;i<size;i++)
62 PyObject *o=PyTuple_GetItem(pyLi,i);
65 int val=(int)PyInt_AS_LONG(o);
69 throw YACS::Exception("tuple must contain integers only");
74 throw YACS::Exception("convertPyToIntArr : not a list nor a tuple");
78 static void convertPyToDblArr(PyObject *pyLi, std::vector<double>& arr)
80 if(PyList_Check(pyLi))
82 int size=PyList_Size(pyLi);
84 for(int i=0;i<size;i++)
86 PyObject *o=PyList_GetItem(pyLi,i);
89 double val(PyFloat_AS_DOUBLE(o));
93 throw YACS::Exception("list must contain integers only");
96 else if(PyTuple_Check(pyLi))
98 int size=PyTuple_Size(pyLi);
100 for(int i=0;i<size;i++)
102 PyObject *o=PyTuple_GetItem(pyLi,i);
105 double val(PyFloat_AS_DOUBLE(o));
109 throw YACS::Exception("tuple must contain floats only");
114 throw YACS::Exception("convertPyToNewIntArr3 : not a list nor a tuple");
119 %types(YACSEvalInputPort,YACSEvalOutputPort);
120 /*%types(YACS::ENGINE::Node *,YACS::ENGINE::Proc *);
121 %types(YACS::ENGINE::InputPort *,YACS::ENGINE::OutputPort *,YACS::ENGINE::InputDataStreamPort *,YACS::ENGINE::OutputDataStreamPort *);
122 %types(YACS::ENGINE::InGate *,YACS::ENGINE::OutGate *,YACS::ENGINE::InPort *,YACS::ENGINE::OutPort *,YACS::ENGINE::Port *);
123 %types(YACS::ENGINE::Container *, YACS::ENGINE::HomogeneousPoolContainer *);*/
127 %newobject YACSEvalYFX::BuildFromFile;
128 %newobject YACSEvalYFX::BuildFromScheme;
130 %typemap(out) std::vector<YACSEvalInputPort *>
132 std::vector<YACSEvalInputPort *>::const_iterator it;
133 $result = PyList_New($1.size());
135 for (it = $1.begin(); it != $1.end(); ++it, ++i)
137 PyList_SetItem($result,i,SWIG_NewPointerObj(SWIG_as_voidptr(*it),SWIGTYPE_p_YACSEvalInputPort, 0 | 0 ));
141 %typemap(out) std::vector<YACSEvalOutputPort *>
143 std::vector<YACSEvalOutputPort *>::const_iterator it;
144 $result = PyList_New($1.size());
146 for (it = $1.begin(); it != $1.end(); ++it, ++i)
148 PyList_SetItem($result,i,SWIG_NewPointerObj(SWIG_as_voidptr(*it),SWIGTYPE_p_YACSEvalOutputPort, 0 | 0 ));
152 %typemap(out) YACSEvalAny *
155 YACSEvalAnyDouble *val0(dynamic_cast<YACSEvalAnyDouble *>($1));
156 YACSEvalAnyInt *val1(dynamic_cast<YACSEvalAnyInt *>($1));
159 $result = PyFloat_FromDouble(val0->toDouble());
164 $result = PyInt_FromLong(val1->toInt());
170 throw YACS::Exception("PyWrap of YACSEvalInputPort::getDefaultValueDefined : unrecognized type !");
174 %typemap(in) const std::list< YACSEvalOutputPort * >&
182 virtual std::string getTypeOfData() const;
187 class YACSEvalInputPort : public YACSEvalPort
190 std::string getName() const;
191 bool hasDefaultValueDefined() const;
192 YACSEvalAny *getDefaultValueDefined() const;
193 bool isRandomVar() const;
194 void declareRandomnessStatus(bool isRandom);
195 bool hasSequenceOfValuesToEval() const;
198 void setDefaultValue(PyObject *parameter)
200 if(parameter==Py_None)
201 self->setDefaultValue(0);
202 else if(PyFloat_Check(parameter))
204 YACSEvalAnyDouble tmp(PyFloat_AsDouble(parameter));
205 self->setDefaultValue(&tmp);
207 else if(PyInt_Check(parameter))
209 YACSEvalAnyInt tmp((int)PyInt_AsLong(parameter));
210 self->setDefaultValue(&tmp);
213 throw YACS::Exception("PyWrap of YACSEvalInputPort::setParameter : unrecognized type !");
216 void setSequenceOfValuesToEval(PyObject *vals)
218 if(!PyList_Check(vals))
220 PyErr_SetString(PyExc_TypeError,"not a list");
223 int size(PyList_Size(vals));
224 YACSEvalSeqAny *valsCpp(0);
227 PyObject *elt0(PyList_GetItem(vals,0));
228 if(PyFloat_Check(elt0))
230 std::vector<double> zeVals;
231 convertPyToDblArr(vals,zeVals);
232 valsCpp=new YACSEvalSeqAnyDouble(zeVals);
234 else if(PyInt_Check(elt0))
236 std::vector<int> zeVals;
237 convertPyToIntArr(vals,zeVals);
238 valsCpp=new YACSEvalSeqAnyInt(zeVals);
241 throw YACS::Exception("YACSEvalInputPort::setSequenceOfValuesToEval : only list[float] and list[int] actualy supported !");
244 valsCpp=YACSEvalSeqAny::BuildEmptyFromType(self->getTypeOfData());
245 self->setSequenceOfValuesToEval(valsCpp);
253 class YACSEvalOutputPort : public YACSEvalPort
256 std::string getName() const;
258 YACSEvalOutputPort();
261 class YACSEvalVirtualYACSContainer
264 std::string getChosenMachine() const;
265 void setWantedMachine(const std::string& machine);
266 std::vector<std::string> listOfPropertyKeys() const;
267 std::string getValueOfKey(const char *key) const;
268 void setProperty(const std::string& key, const std::string &value);
270 YACSEvalVirtualYACSContainer();
273 class YACSEvalResource
276 std::vector<std::string> getAllChosenMachines() const;
277 std::vector<std::string> getAllFittingMachines() const;
278 void setWantedMachine(const std::string& machine);
279 std::size_t size() const;
280 YACSEvalVirtualYACSContainer *at(std::size_t i) const;
283 std::size_t __len__() const
287 YACSEvalVirtualYACSContainer *__getitem__(std::size_t i) const
296 class YACSEvalListOfResources
299 std::vector<std::string> getAllChosenMachines() const;
300 std::vector<std::string> getAllFittingMachines() const;
301 void setWantedMachine(const std::string& machine);
302 std::size_t size() const;
303 bool isInteractive() const;
304 YACSEvalResource *at(std::size_t i) const;
305 unsigned int getNumberOfProcsDeclared() const;
308 std::size_t __len__() const
312 YACSEvalResource *__getitem__(std::size_t i) const
318 YACSEvalListOfResources();
321 class YACSEvalSession
327 bool isLaunched() const;
328 void checkLaunched() const;
330 std::string getCorbaConfigFileName() const;
336 static YACSEvalYFX *BuildFromFile(const std::string& xmlOfScheme);
337 static YACSEvalYFX *BuildFromScheme(YACS::ENGINE::Proc *schema);
338 std::vector<YACSEvalInputPort *> getFreeInputPorts() const;
339 std::vector<YACSEvalOutputPort *> getFreeOutputPorts() const;
341 bool isLocked() const;
342 YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const;
343 YACSEvalListOfResources *giveResources();
346 void lockPortsForEvaluation(PyObject *inputsOfInterest, PyObject *outputsOfInterest)
348 std::vector<YACSEvalOutputPort *> outputsOfInterestCpp;
349 if(PyList_Check(outputsOfInterest))
351 int size(PyList_Size(outputsOfInterest));
352 for(int i=0;i<size;i++)
354 PyObject *obj(PyList_GetItem(outputsOfInterest,i));
356 int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalOutputPort,0|0));
357 if(!SWIG_IsOK(status))
359 std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalOutputPort instance !";
360 throw YACS::Exception(oss.str());
362 outputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalOutputPort *>(argp));
367 PyErr_SetString(PyExc_TypeError,"not a list");
371 std::vector< YACSEvalInputPort * > inputsOfInterestCpp;
372 if(PyList_Check(inputsOfInterest))
374 int size(PyList_Size(inputsOfInterest));
375 for(int i=0;i<size;i++)
377 PyObject *obj(PyList_GetItem(inputsOfInterest,i));
379 int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalInputPort,0|0));
380 if(!SWIG_IsOK(status))
382 std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalInputPort instance !";
383 throw YACS::Exception(oss.str());
385 inputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalInputPort *>(argp));
390 PyErr_SetString(PyExc_TypeError,"not a list");
393 self->lockPortsForEvaluation(inputsOfInterestCpp,outputsOfInterestCpp);
396 PyObject *getResults() const
398 std::vector<YACSEvalSeqAny *> retCpp(self->getResults());
399 std::size_t sz(retCpp.size());
400 PyObject *ret(PyList_New(sz));
401 for(std::size_t i=0;i<sz;i++)
403 YACSEvalSeqAny *elt(retCpp[i]);
404 YACSEvalSeqAnyDouble *elt1(dynamic_cast<YACSEvalSeqAnyDouble *>(elt));
405 YACSEvalSeqAnyInt *elt2(dynamic_cast<YACSEvalSeqAnyInt *>(elt));
408 std::vector<double> *zeArr(elt1->getInternal());
409 std::size_t sz2(zeArr->size());
410 PyObject *ret2(PyList_New(sz2));
411 for(std::size_t i2=0;i2<sz2;i2++)
412 PyList_SetItem(ret2,i2,PyFloat_FromDouble((*zeArr)[i2]));
413 PyList_SetItem(ret,i,ret2);
417 std::vector<int> *zeArr(elt2->getInternal());
418 std::size_t sz2(zeArr->size());
419 PyObject *ret2(PyList_New(sz2));
420 for(std::size_t i2=0;i2<sz2;i2++)
421 PyList_SetItem(ret2,i2,PyInt_FromLong((*zeArr)[i2]));
422 PyList_SetItem(ret,i,ret2);
425 throw YACS::Exception("wrap of YACSEvalYFX.getResults : unrecognized type !");
431 PyObject *run(YACSEvalSession *session)
434 bool ret0(self->run(session,ret1));
435 PyObject *ret(PyTuple_New(2));
436 PyObject *ret0Py(ret0?Py_True:Py_False);
438 PyTuple_SetItem(ret,0,ret0Py);
439 PyTuple_SetItem(ret,1,PyInt_FromLong(ret1));