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::list<YACSEvalInputPort *>
132 std::list<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::list<YACSEvalOutputPort *>
143 std::list<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;
195 void setDefaultValue(PyObject *parameter)
197 if(parameter==Py_None)
198 self->setDefaultValue(0);
199 else if(PyFloat_Check(parameter))
201 YACSEvalAnyDouble tmp(PyFloat_AsDouble(parameter));
202 self->setDefaultValue(&tmp);
204 else if(PyInt_Check(parameter))
206 YACSEvalAnyInt tmp((int)PyInt_AsLong(parameter));
207 self->setDefaultValue(&tmp);
210 throw YACS::Exception("PyWrap of YACSEvalInputPort::setParameter : unrecognized type !");
213 void setSequenceOfValuesToEval(PyObject *vals)
215 if(!PyList_Check(vals))
217 PyErr_SetString(PyExc_TypeError,"not a list");
220 int size(PyList_Size(vals));
221 YACSEvalSeqAny *valsCpp(0);
224 PyObject *elt0(PyList_GetItem(vals,0));
225 if(PyFloat_Check(elt0))
227 std::vector<double> zeVals;
228 convertPyToDblArr(vals,zeVals);
229 valsCpp=new YACSEvalSeqAnyDouble(zeVals);
231 else if(PyInt_Check(elt0))
233 std::vector<int> zeVals;
234 convertPyToIntArr(vals,zeVals);
235 valsCpp=new YACSEvalSeqAnyInt(zeVals);
238 throw YACS::Exception("YACSEvalInputPort::setSequenceOfValuesToEval : only list[float] and list[int] actualy supported !");
241 valsCpp=YACSEvalSeqAny::BuildEmptyFromType(self->getTypeOfData());
242 self->setSequenceOfValuesToEval(valsCpp);
246 PyObject *hasSequenceOfValuesToEval() const
249 bool ret0(self->hasSequenceOfValuesToEval(ret1));
250 PyObject *ret(PyTuple_New(2));
251 PyObject *ret0Py=ret0?Py_True:Py_False;
253 PyTuple_SetItem(ret,0,ret0Py);
254 PyTuple_SetItem(ret,1,PyInt_FromLong(ret1));
262 class YACSEvalOutputPort : public YACSEvalPort
265 std::string getName() const;
267 YACSEvalOutputPort();
270 class YACSEvalVirtualYACSContainer
273 std::string getChosenMachine() const;
274 void setWantedMachine(const std::string& machine);
275 std::vector<std::string> listOfPropertyKeys() const;
276 std::string getValueOfKey(const char *key) const;
277 void setProperty(const std::string& key, const std::string &value);
279 YACSEvalVirtualYACSContainer();
282 class YACSEvalResource
285 std::vector<std::string> getAllChosenMachines() const;
286 std::vector<std::string> getAllFittingMachines() const;
287 void setWantedMachine(const std::string& machine);
288 std::size_t size() const;
289 YACSEvalVirtualYACSContainer *at(std::size_t i) const;
292 std::size_t __len__() const
296 YACSEvalVirtualYACSContainer *__getitem__(std::size_t i) const
305 class YACSEvalListOfResources
308 std::vector<std::string> getAllChosenMachines() const;
309 std::vector<std::string> getAllFittingMachines() const;
310 void setWantedMachine(const std::string& machine);
311 std::size_t size() const;
312 bool isInteractive() const;
313 YACSEvalResource *at(std::size_t i) const;
314 unsigned int getNumberOfProcsDeclared() const;
317 std::size_t __len__() const
321 YACSEvalResource *__getitem__(std::size_t i) const
327 YACSEvalListOfResources();
330 class YACSEvalSession
336 bool isLaunched() const;
337 void checkLaunched() const;
339 std::string getCorbaConfigFileName() const;
345 static YACSEvalYFX *BuildFromFile(const std::string& xmlOfScheme);
346 static YACSEvalYFX *BuildFromScheme(YACS::ENGINE::Proc *schema);
347 std::list<YACSEvalInputPort *> getFreeInputPorts() const;
348 std::list<YACSEvalOutputPort *> getFreeOutputPorts() const;
350 bool isLocked() const;
351 YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const;
352 YACSEvalListOfResources *giveResources();
355 void lockPortsForEvaluation(PyObject *outputsOfInterest)
357 std::list<YACSEvalOutputPort *> outputsOfInterestCpp;
358 if(PyList_Check(outputsOfInterest))
360 int size(PyList_Size(outputsOfInterest));
361 for(int i=0;i<size;i++)
363 PyObject *obj(PyList_GetItem(outputsOfInterest,i));
365 int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalOutputPort,0|0));
366 if(!SWIG_IsOK(status))
368 std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalOutputPort instance !";
369 throw YACS::Exception(oss.str());
371 outputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalOutputPort *>(argp));
376 PyErr_SetString(PyExc_TypeError,"not a list");
379 self->lockPortsForEvaluation(outputsOfInterestCpp);
382 PyObject *getResults() const
384 std::vector<YACSEvalSeqAny *> retCpp(self->getResults());
385 std::size_t sz(retCpp.size());
386 PyObject *ret(PyList_New(sz));
387 for(std::size_t i=0;i<sz;i++)
389 YACSEvalSeqAny *elt(retCpp[i]);
390 YACSEvalSeqAnyDouble *elt1(dynamic_cast<YACSEvalSeqAnyDouble *>(elt));
391 YACSEvalSeqAnyInt *elt2(dynamic_cast<YACSEvalSeqAnyInt *>(elt));
394 std::vector<double> *zeArr(elt1->getInternal());
395 std::size_t sz2(zeArr->size());
396 PyObject *ret2(PyList_New(sz2));
397 for(std::size_t i2=0;i2<sz2;i2++)
398 PyList_SetItem(ret2,i2,PyFloat_FromDouble((*zeArr)[i2]));
399 PyList_SetItem(ret,i,ret2);
403 std::vector<int> *zeArr(elt2->getInternal());
404 std::size_t sz2(zeArr->size());
405 PyObject *ret2(PyList_New(sz2));
406 for(std::size_t i2=0;i2<sz2;i2++)
407 PyList_SetItem(ret2,i2,PyInt_FromLong((*zeArr)[i2]));
408 PyList_SetItem(ret,i,ret2);
411 throw YACS::Exception("wrap of YACSEvalYFX.getResults : unrecognized type !");
417 PyObject *run(YACSEvalSession *session)
420 bool ret0(self->run(session,ret1));
421 PyObject *ret(PyTuple_New(2));
422 PyObject *ret0Py(ret0?Py_True:Py_False);
424 PyTuple_SetItem(ret,0,ret0Py);
425 PyTuple_SetItem(ret,1,PyInt_FromLong(ret1));