1 // Copyright (C) 2015-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 %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;
300 bool setQOfInterestStatus(bool newStatus);
301 bool isQOfInterest() const;
303 YACSEvalOutputPort();
306 class YACSEvalVirtualYACSContainer
309 std::string getChosenMachine() const;
310 void setWantedMachine(const std::string& machine);
311 std::vector<std::string> listOfPropertyKeys() const;
312 std::string getValueOfKey(const char *key) const;
313 void setProperty(const std::string& key, const std::string &value);
314 std::string getName() const;
316 YACSEvalVirtualYACSContainer();
319 class YACSEvalResource
322 std::vector<std::string> getAllChosenMachines() const;
323 std::vector<std::string> getAllFittingMachines() const;
324 void setWantedMachine(const std::string& machine);
325 std::size_t size() const;
326 YACSEvalVirtualYACSContainer *at(std::size_t i) const;
329 std::size_t __len__() const
333 YACSEvalVirtualYACSContainer *__getitem__(std::size_t i) const
342 class YACSEvalParamsForCluster
345 bool getExclusiveness() const;
346 void setExclusiveness(bool newStatus);
347 std::string getRemoteWorkingDir();
348 void setRemoteWorkingDir(const std::string& remoteWorkingDir);
349 std::string getLocalWorkingDir();
350 void setLocalWorkingDir(const std::string& localWorkingDir);
351 std::string getWCKey() const;
352 void setWCKey(const std::string& wcKey);
353 unsigned int getNbProcs() const;
354 void setNbProcs(unsigned int nbProcs);
355 void setMaxDuration(const std::string& maxDuration);
356 std::string getMaxDuration() const;
357 void checkConsistency() const;
359 YACSEvalParamsForCluster();
362 class YACSEvalListOfResources
365 std::vector<std::string> getAllChosenMachines() const;
366 std::vector<std::string> getAllFittingMachines() const;
367 void setWantedMachine(const std::string& machine);
368 std::size_t size() const;
369 bool isInteractive() const;
370 bool isMachineInteractive(const std::string& machine) const;
371 YACSEvalResource *at(std::size_t i) const;
372 unsigned int getNumberOfProcsDeclared() const;
373 void checkOKForRun() const;
374 YACSEvalParamsForCluster& getAddParamsForCluster();
377 std::size_t __len__() const
381 YACSEvalResource *__getitem__(std::size_t i) const
387 YACSEvalListOfResources();
390 class YACSEvalSession
396 bool isLaunched() const;
397 bool isAttached() const;
398 bool isAlreadyPyThreadSaved() const;
399 bool getForcedPyThreadSavedStatus() const;
400 void setForcedPyThreadSavedStatus(bool status);
401 void checkLaunched() const;
403 std::string getCorbaConfigFileName() const;
406 class YACSEvalExecParams
409 bool getStopASAPAfterErrorStatus() const;
410 void setStopASAPAfterErrorStatus(bool newStatus);
411 bool getFetchRemoteDirForClusterStatus() const;
412 void setFetchRemoteDirForClusterStatus(bool newStatus);
414 YACSEvalExecParams();
420 static YACSEvalYFX *BuildFromFile(const std::string& xmlOfScheme);
421 static YACSEvalYFX *BuildFromScheme(YACS::ENGINE::Proc *schema);
422 YACSEvalExecParams *getParams() const;
423 std::vector<YACSEvalInputPort *> getFreeInputPorts() const;
424 std::vector<YACSEvalOutputPort *> getFreeOutputPorts() const;
426 bool isLocked() const;
427 YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const;
428 YACSEvalListOfResources *giveResources();
429 std::string getErrorDetailsInCaseOfFailure() const;
430 std::string getStatusOfRunStr() const;
431 void setParallelizeStatus(bool newVal);
432 bool getParallelizeStatus() const;
433 //void registerObserver(YACSEvalObserver *observer);
436 void lockPortsForEvaluation(PyObject *inputsOfInterest, PyObject *outputsOfInterest)
438 std::vector<YACSEvalOutputPort *> outputsOfInterestCpp;
439 if(PyList_Check(outputsOfInterest))
441 int size(PyList_Size(outputsOfInterest));
442 for(int i=0;i<size;i++)
444 PyObject *obj(PyList_GetItem(outputsOfInterest,i));
446 int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalOutputPort,0|0));
447 if(!SWIG_IsOK(status))
449 std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalOutputPort instance !";
450 throw YACS::Exception(oss.str());
452 outputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalOutputPort *>(argp));
457 PyErr_SetString(PyExc_TypeError,"not a list");
461 std::vector< YACSEvalInputPort * > inputsOfInterestCpp;
462 if(PyList_Check(inputsOfInterest))
464 int size(PyList_Size(inputsOfInterest));
465 for(int i=0;i<size;i++)
467 PyObject *obj(PyList_GetItem(inputsOfInterest,i));
469 int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalInputPort,0|0));
470 if(!SWIG_IsOK(status))
472 std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalInputPort instance !";
473 throw YACS::Exception(oss.str());
475 inputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalInputPort *>(argp));
480 PyErr_SetString(PyExc_TypeError,"not a list");
483 self->lockPortsForEvaluation(inputsOfInterestCpp,outputsOfInterestCpp);
486 PyObject *getResults() const
488 std::vector<YACSEvalSeqAny *> retCpp(self->getResults());
489 return convertVectOfSeqAny(retCpp);
492 PyObject *getResultsInCaseOfFailure() const
494 std::vector<unsigned int> ret1Cpp;
495 std::vector<YACSEvalSeqAny *> ret0Cpp(self->getResultsInCaseOfFailure(ret1Cpp));
496 PyObject *retPy(PyTuple_New(2));
497 PyTuple_SetItem(retPy,0,convertVectOfSeqAny(ret0Cpp));
498 PyTuple_SetItem(retPy,1,convertToPyToInt(ret1Cpp));
502 PyObject *run(YACSEvalSession *session)
505 bool ret0(self->run(session,ret1));
506 PyObject *ret(PyTuple_New(2));
507 PyObject *ret0Py(ret0?Py_True:Py_False);
509 PyTuple_SetItem(ret,0,ret0Py);
510 PyTuple_SetItem(ret,1,PyInt_FromLong(ret1));