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 PyObject *convertToPyToInt(const std::vector<unsigned int>& arr)
80 std::size_t sz(arr.size());
81 PyObject *ret(PyList_New(sz));
82 for(std::size_t i=0;i<sz;i++)
83 PyList_SetItem(ret,i,PyInt_FromLong(arr[i]));
87 static void convertPyToDblArr(PyObject *pyLi, std::vector<double>& arr)
89 if(PyList_Check(pyLi))
91 int size=PyList_Size(pyLi);
93 for(int i=0;i<size;i++)
95 PyObject *o=PyList_GetItem(pyLi,i);
98 double val(PyFloat_AS_DOUBLE(o));
102 throw YACS::Exception("list must contain integers only");
105 else if(PyTuple_Check(pyLi))
107 int size=PyTuple_Size(pyLi);
109 for(int i=0;i<size;i++)
111 PyObject *o=PyTuple_GetItem(pyLi,i);
114 double val(PyFloat_AS_DOUBLE(o));
118 throw YACS::Exception("tuple must contain floats only");
123 throw YACS::Exception("convertPyToNewIntArr3 : not a list nor a tuple");
127 static PyObject *convertVectOfSeqAny(const std::vector<YACSEvalSeqAny *>& retCpp)
129 std::size_t sz(retCpp.size());
130 PyObject *ret(PyList_New(sz));
131 for(std::size_t i=0;i<sz;i++)
133 YACSEvalSeqAny *elt(retCpp[i]);
134 YACSEvalSeqAnyDouble *elt1(dynamic_cast<YACSEvalSeqAnyDouble *>(elt));
135 YACSEvalSeqAnyInt *elt2(dynamic_cast<YACSEvalSeqAnyInt *>(elt));
138 std::vector<double> *zeArr(elt1->getInternal());
139 std::size_t sz2(zeArr->size());
140 PyObject *ret2(PyList_New(sz2));
141 for(std::size_t i2=0;i2<sz2;i2++)
142 PyList_SetItem(ret2,i2,PyFloat_FromDouble((*zeArr)[i2]));
143 PyList_SetItem(ret,i,ret2);
147 std::vector<int> *zeArr(elt2->getInternal());
148 std::size_t sz2(zeArr->size());
149 PyObject *ret2(PyList_New(sz2));
150 for(std::size_t i2=0;i2<sz2;i2++)
151 PyList_SetItem(ret2,i2,PyInt_FromLong((*zeArr)[i2]));
152 PyList_SetItem(ret,i,ret2);
155 throw YACS::Exception("wrap of YACSEvalYFX.getResults : unrecognized type !");
162 %types(YACSEvalInputPort,YACSEvalOutputPort);
163 /*%types(YACS::ENGINE::Node *,YACS::ENGINE::Proc *);
164 %types(YACS::ENGINE::InputPort *,YACS::ENGINE::OutputPort *,YACS::ENGINE::InputDataStreamPort *,YACS::ENGINE::OutputDataStreamPort *);
165 %types(YACS::ENGINE::InGate *,YACS::ENGINE::OutGate *,YACS::ENGINE::InPort *,YACS::ENGINE::OutPort *,YACS::ENGINE::Port *);
166 %types(YACS::ENGINE::Container *, YACS::ENGINE::HomogeneousPoolContainer *);*/
170 %newobject YACSEvalYFX::BuildFromFile;
171 %newobject YACSEvalYFX::BuildFromScheme;
173 %typemap(out) std::vector<YACSEvalInputPort *>
175 std::vector<YACSEvalInputPort *>::const_iterator it;
176 $result = PyList_New($1.size());
178 for (it = $1.begin(); it != $1.end(); ++it, ++i)
180 PyList_SetItem($result,i,SWIG_NewPointerObj(SWIG_as_voidptr(*it),SWIGTYPE_p_YACSEvalInputPort, 0 | 0 ));
184 %typemap(out) std::vector<YACSEvalOutputPort *>
186 std::vector<YACSEvalOutputPort *>::const_iterator it;
187 $result = PyList_New($1.size());
189 for (it = $1.begin(); it != $1.end(); ++it, ++i)
191 PyList_SetItem($result,i,SWIG_NewPointerObj(SWIG_as_voidptr(*it),SWIGTYPE_p_YACSEvalOutputPort, 0 | 0 ));
195 %typemap(out) YACSEvalAny *
198 YACSEvalAnyDouble *val0(dynamic_cast<YACSEvalAnyDouble *>($1));
199 YACSEvalAnyInt *val1(dynamic_cast<YACSEvalAnyInt *>($1));
202 $result = PyFloat_FromDouble(val0->toDouble());
207 $result = PyInt_FromLong(val1->toInt());
213 throw YACS::Exception("PyWrap of YACSEvalInputPort::getDefaultValueDefined : unrecognized type !");
217 %typemap(in) const std::list< YACSEvalOutputPort * >&
225 virtual std::string getTypeOfData() const;
230 class YACSEvalInputPort : public YACSEvalPort
233 std::string getName() const;
234 bool hasDefaultValueDefined() const;
235 YACSEvalAny *getDefaultValueDefined() const;
236 bool isRandomVar() const;
237 void declareRandomnessStatus(bool isRandom);
238 bool hasSequenceOfValuesToEval() const;
241 void setDefaultValue(PyObject *parameter)
243 if(parameter==Py_None)
244 self->setDefaultValue(0);
245 else if(PyFloat_Check(parameter))
247 YACSEvalAnyDouble tmp(PyFloat_AsDouble(parameter));
248 self->setDefaultValue(&tmp);
250 else if(PyInt_Check(parameter))
252 YACSEvalAnyInt tmp((int)PyInt_AsLong(parameter));
253 self->setDefaultValue(&tmp);
256 throw YACS::Exception("PyWrap of YACSEvalInputPort::setParameter : unrecognized type !");
259 void setSequenceOfValuesToEval(PyObject *vals)
261 if(!PyList_Check(vals))
263 PyErr_SetString(PyExc_TypeError,"not a list");
266 int size(PyList_Size(vals));
267 YACSEvalSeqAny *valsCpp(0);
270 PyObject *elt0(PyList_GetItem(vals,0));
271 if(PyFloat_Check(elt0))
273 std::vector<double> zeVals;
274 convertPyToDblArr(vals,zeVals);
275 valsCpp=new YACSEvalSeqAnyDouble(zeVals);
277 else if(PyInt_Check(elt0))
279 std::vector<int> zeVals;
280 convertPyToIntArr(vals,zeVals);
281 valsCpp=new YACSEvalSeqAnyInt(zeVals);
284 throw YACS::Exception("YACSEvalInputPort::setSequenceOfValuesToEval : only list[float] and list[int] actualy supported !");
287 valsCpp=YACSEvalSeqAny::BuildEmptyFromType(self->getTypeOfData());
288 self->setSequenceOfValuesToEval(valsCpp);
296 class YACSEvalOutputPort : public YACSEvalPort
299 std::string getName() const;
301 YACSEvalOutputPort();
304 class YACSEvalVirtualYACSContainer
307 std::string getChosenMachine() const;
308 void setWantedMachine(const std::string& machine);
309 std::vector<std::string> listOfPropertyKeys() const;
310 std::string getValueOfKey(const char *key) const;
311 void setProperty(const std::string& key, const std::string &value);
312 std::string getName() const;
314 YACSEvalVirtualYACSContainer();
317 class YACSEvalResource
320 std::vector<std::string> getAllChosenMachines() const;
321 std::vector<std::string> getAllFittingMachines() const;
322 void setWantedMachine(const std::string& machine);
323 std::size_t size() const;
324 YACSEvalVirtualYACSContainer *at(std::size_t i) const;
327 std::size_t __len__() const
331 YACSEvalVirtualYACSContainer *__getitem__(std::size_t i) const
340 class YACSEvalParamsForCluster
343 bool getExclusiveness() const;
344 void setExclusiveness(bool newStatus);
345 std::string getRemoteWorkingDir();
346 void setRemoteWorkingDir(const std::string& remoteWorkingDir);
347 std::string getWCKey() const;
348 void setWCKey(const std::string& wcKey);
349 unsigned int getNbProcs() const;
350 void setNbProcs(unsigned int nbProcs);
351 void checkConsistency() const;
353 YACSEvalParamsForCluster();
356 class YACSEvalListOfResources
359 std::vector<std::string> getAllChosenMachines() const;
360 std::vector<std::string> getAllFittingMachines() const;
361 void setWantedMachine(const std::string& machine);
362 std::size_t size() const;
363 bool isInteractive() const;
364 YACSEvalResource *at(std::size_t i) const;
365 unsigned int getNumberOfProcsDeclared() const;
366 void checkOKForRun() const;
367 YACSEvalParamsForCluster& getAddParamsForCluster();
370 std::size_t __len__() const
374 YACSEvalResource *__getitem__(std::size_t i) const
380 YACSEvalListOfResources();
383 class YACSEvalSession
389 bool isLaunched() const;
390 void checkLaunched() const;
392 std::string getCorbaConfigFileName() const;
395 class YACSEvalExecParams
398 bool getStopASAPAfterErrorStatus() const;
399 void setStopASAPAfterErrorStatus(bool newStatus);
401 YACSEvalExecParams();
407 static YACSEvalYFX *BuildFromFile(const std::string& xmlOfScheme);
408 static YACSEvalYFX *BuildFromScheme(YACS::ENGINE::Proc *schema);
409 YACSEvalExecParams *getParams() const;
410 std::vector<YACSEvalInputPort *> getFreeInputPorts() const;
411 std::vector<YACSEvalOutputPort *> getFreeOutputPorts() const;
413 bool isLocked() const;
414 YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const;
415 YACSEvalListOfResources *giveResources();
416 std::string getErrorDetailsInCaseOfFailure() const;
417 std::string getStatusOfRunStr() const;
418 void setParallelizeStatus(bool newVal);
419 bool getParallelizeStatus() const;
420 //void registerObserver(YACSEvalObserver *observer);
423 void lockPortsForEvaluation(PyObject *inputsOfInterest, PyObject *outputsOfInterest)
425 std::vector<YACSEvalOutputPort *> outputsOfInterestCpp;
426 if(PyList_Check(outputsOfInterest))
428 int size(PyList_Size(outputsOfInterest));
429 for(int i=0;i<size;i++)
431 PyObject *obj(PyList_GetItem(outputsOfInterest,i));
433 int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalOutputPort,0|0));
434 if(!SWIG_IsOK(status))
436 std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalOutputPort instance !";
437 throw YACS::Exception(oss.str());
439 outputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalOutputPort *>(argp));
444 PyErr_SetString(PyExc_TypeError,"not a list");
448 std::vector< YACSEvalInputPort * > inputsOfInterestCpp;
449 if(PyList_Check(inputsOfInterest))
451 int size(PyList_Size(inputsOfInterest));
452 for(int i=0;i<size;i++)
454 PyObject *obj(PyList_GetItem(inputsOfInterest,i));
456 int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalInputPort,0|0));
457 if(!SWIG_IsOK(status))
459 std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalInputPort instance !";
460 throw YACS::Exception(oss.str());
462 inputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalInputPort *>(argp));
467 PyErr_SetString(PyExc_TypeError,"not a list");
470 self->lockPortsForEvaluation(inputsOfInterestCpp,outputsOfInterestCpp);
473 PyObject *getResults() const
475 std::vector<YACSEvalSeqAny *> retCpp(self->getResults());
476 return convertVectOfSeqAny(retCpp);
479 PyObject *getResultsInCaseOfFailure() const
481 std::vector<unsigned int> ret1Cpp;
482 std::vector<YACSEvalSeqAny *> ret0Cpp(self->getResultsInCaseOfFailure(ret1Cpp));
483 PyObject *retPy(PyTuple_New(2));
484 PyTuple_SetItem(retPy,0,convertVectOfSeqAny(ret0Cpp));
485 PyTuple_SetItem(retPy,1,convertToPyToInt(ret1Cpp));
489 PyObject *run(YACSEvalSession *session)
492 bool ret0(self->run(session,ret1));
493 PyObject *ret(PyTuple_New(2));
494 PyObject *ret0Py(ret0?Py_True:Py_False);
496 PyTuple_SetItem(ret,0,ret0Py);
497 PyTuple_SetItem(ret,1,PyInt_FromLong(ret1));