Salome HOME
On the road of cluster management in evalyfx.
[modules/yacs.git] / src / evalyfx_swig / evalyfx.i
1 // Copyright (C) 2012-2015  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (EDF R&D)
20
21 %define EVALYFXDOCSTRING
22 "Module to evaluate Y=f(X) easily."
23 %enddef
24
25 %module(docstring=EVALYFXDOCSTRING) evalyfx
26
27 %feature("autodoc", "1");
28
29 %include "engtypemaps.i"
30
31 %{
32 #include "YACSEvalYFX.hxx"
33 #include "YACSEvalPort.hxx"
34 #include "YACSEvalSeqAny.hxx"
35 #include "YACSEvalResource.hxx"
36 #include "YACSEvalSession.hxx"
37
38 static void convertPyToIntArr(PyObject *pyLi, std::vector<int>& arr)
39 {
40   if(PyList_Check(pyLi))
41     {
42       int size=PyList_Size(pyLi);
43       arr.resize(size);
44       for(int i=0;i<size;i++)
45         {
46           PyObject *o=PyList_GetItem(pyLi,i);
47           if(PyInt_Check(o))
48             {
49               int val=(int)PyInt_AS_LONG(o);
50               arr[i]=val;
51             }
52           else
53             throw YACS::Exception("list must contain integers only");
54         }
55     }
56   else if(PyTuple_Check(pyLi))
57     {
58       int size=PyTuple_Size(pyLi);
59       arr.resize(size);
60       for(int i=0;i<size;i++)
61         {
62           PyObject *o=PyTuple_GetItem(pyLi,i);
63           if(PyInt_Check(o))
64             {
65               int val=(int)PyInt_AS_LONG(o);
66               arr[i]=val;
67             }
68           else
69             throw YACS::Exception("tuple must contain integers only");
70         }
71     }
72   else
73     {
74       throw YACS::Exception("convertPyToIntArr : not a list nor a tuple");
75     }
76 }
77
78 static PyObject *convertToPyToInt(const std::vector<unsigned int>& arr)
79 {
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]));
84   return ret;
85 }
86
87 static void convertPyToDblArr(PyObject *pyLi, std::vector<double>& arr)
88 {
89   if(PyList_Check(pyLi))
90     {
91       int size=PyList_Size(pyLi);
92       arr.resize(size);
93       for(int i=0;i<size;i++)
94         {
95           PyObject *o=PyList_GetItem(pyLi,i);
96           if(PyFloat_Check(o))
97             {
98               double val(PyFloat_AS_DOUBLE(o));
99               arr[i]=val;
100             }
101           else
102             throw YACS::Exception("list must contain integers only");
103         }
104     }
105   else if(PyTuple_Check(pyLi))
106     {
107       int size=PyTuple_Size(pyLi);
108       arr.resize(size);
109       for(int i=0;i<size;i++)
110         {
111           PyObject *o=PyTuple_GetItem(pyLi,i);
112           if(PyFloat_Check(o))
113             {
114               double val(PyFloat_AS_DOUBLE(o));
115               arr[i]=val;
116             }
117           else
118             throw YACS::Exception("tuple must contain floats only");
119         }
120     }
121   else
122     {
123       throw YACS::Exception("convertPyToNewIntArr3 : not a list nor a tuple");
124     }
125 }
126
127 static PyObject *convertVectOfSeqAny(const std::vector<YACSEvalSeqAny *>& retCpp)
128 {
129   std::size_t sz(retCpp.size());
130   PyObject *ret(PyList_New(sz));
131   for(std::size_t i=0;i<sz;i++)
132     {
133       YACSEvalSeqAny *elt(retCpp[i]);
134       YACSEvalSeqAnyDouble *elt1(dynamic_cast<YACSEvalSeqAnyDouble *>(elt));
135       YACSEvalSeqAnyInt *elt2(dynamic_cast<YACSEvalSeqAnyInt *>(elt));
136       if(elt1)
137         {
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);
144         }
145       else if(elt2)
146         {
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);
153         }
154       else
155         throw YACS::Exception("wrap of YACSEvalYFX.getResults : unrecognized type !");
156       delete elt;
157     }
158   return ret;
159 }
160 %}
161
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 *);*/
167
168 %import "loader.i"
169
170 %newobject YACSEvalYFX::BuildFromFile;
171 %newobject YACSEvalYFX::BuildFromScheme;
172
173 %typemap(out) std::vector<YACSEvalInputPort *>
174 {
175   std::vector<YACSEvalInputPort *>::const_iterator it;
176   $result = PyList_New($1.size());
177   int i = 0;
178   for (it = $1.begin(); it != $1.end(); ++it, ++i)
179     {
180       PyList_SetItem($result,i,SWIG_NewPointerObj(SWIG_as_voidptr(*it),SWIGTYPE_p_YACSEvalInputPort, 0 | 0 ));
181     }
182 }
183
184 %typemap(out) std::vector<YACSEvalOutputPort *>
185 {
186   std::vector<YACSEvalOutputPort *>::const_iterator it;
187   $result = PyList_New($1.size());
188   int i = 0;
189   for (it = $1.begin(); it != $1.end(); ++it, ++i)
190     {
191       PyList_SetItem($result,i,SWIG_NewPointerObj(SWIG_as_voidptr(*it),SWIGTYPE_p_YACSEvalOutputPort, 0 | 0 ));
192     }
193 }
194
195 %typemap(out) YACSEvalAny *
196 {
197   $result = 0;
198   YACSEvalAnyDouble *val0(dynamic_cast<YACSEvalAnyDouble *>($1));
199   YACSEvalAnyInt *val1(dynamic_cast<YACSEvalAnyInt *>($1));
200   if(val0)
201     {
202       $result = PyFloat_FromDouble(val0->toDouble());
203       delete $1;
204     }
205   else if(val1)
206     {
207       $result = PyInt_FromLong(val1->toInt());
208       delete $1;
209     }
210   else
211     {
212       delete $1;
213       throw YACS::Exception("PyWrap of YACSEvalInputPort::getDefaultValueDefined : unrecognized type !");
214     }
215 }
216
217 %typemap(in) const std::list< YACSEvalOutputPort * >& 
218 {
219   
220 }
221
222 class YACSEvalPort
223 {
224 public:
225   virtual std::string getTypeOfData() const;
226 private:
227   YACSEvalPort();
228 };
229
230 class YACSEvalInputPort : public YACSEvalPort
231 {
232 public:
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;
239   %extend
240      {
241        void setDefaultValue(PyObject *parameter)
242        {
243          if(parameter==Py_None)
244            self->setDefaultValue(0);
245          else if(PyFloat_Check(parameter))
246            {
247              YACSEvalAnyDouble tmp(PyFloat_AsDouble(parameter));
248              self->setDefaultValue(&tmp);
249            }
250          else if(PyInt_Check(parameter))
251            {
252              YACSEvalAnyInt tmp((int)PyInt_AsLong(parameter));
253              self->setDefaultValue(&tmp);
254            }
255          else
256            throw YACS::Exception("PyWrap of YACSEvalInputPort::setParameter : unrecognized type !");
257        }
258        
259        void setSequenceOfValuesToEval(PyObject *vals)
260        {
261          if(!PyList_Check(vals))
262            {
263              PyErr_SetString(PyExc_TypeError,"not a list");
264              return ;
265            }
266          int size(PyList_Size(vals));
267          YACSEvalSeqAny *valsCpp(0);
268          if(size>0)
269            {
270              PyObject *elt0(PyList_GetItem(vals,0));
271              if(PyFloat_Check(elt0))
272                {
273                  std::vector<double> zeVals;
274                  convertPyToDblArr(vals,zeVals);
275                  valsCpp=new YACSEvalSeqAnyDouble(zeVals);
276                }
277              else if(PyInt_Check(elt0))
278                {
279                  std::vector<int> zeVals;
280                  convertPyToIntArr(vals,zeVals);
281                  valsCpp=new YACSEvalSeqAnyInt(zeVals);
282                }
283              else
284                throw YACS::Exception("YACSEvalInputPort::setSequenceOfValuesToEval : only list[float] and list[int] actualy supported !");
285            }
286          else
287            valsCpp=YACSEvalSeqAny::BuildEmptyFromType(self->getTypeOfData());
288          self->setSequenceOfValuesToEval(valsCpp);
289          delete valsCpp;
290        }
291      }
292 private:
293   YACSEvalInputPort();
294 };
295
296 class YACSEvalOutputPort : public YACSEvalPort
297 {
298 public:
299   std::string getName() const;
300 private:
301   YACSEvalOutputPort();
302 };
303
304 class YACSEvalVirtualYACSContainer
305 {
306 public:
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;
313 private:
314   YACSEvalVirtualYACSContainer();
315 };
316
317 class YACSEvalResource
318 {
319 public:
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;
325   %extend
326      {
327        std::size_t __len__() const
328        {
329          return self->size();
330        }
331        YACSEvalVirtualYACSContainer *__getitem__(std::size_t i) const
332        {
333          return self->at(i);
334        }
335      }
336 private:
337   YACSEvalResource();
338 };
339
340 class YACSEvalParamsForCluster
341 {
342 public:
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;
352 private:
353   YACSEvalParamsForCluster();
354 };
355
356 class YACSEvalListOfResources
357 {
358 public:
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();
368   %extend
369      {
370        std::size_t __len__() const
371        {
372          return self->size();
373        }
374        YACSEvalResource *__getitem__(std::size_t i) const
375        {
376          return self->at(i);
377        }
378      }
379 private:
380   YACSEvalListOfResources();
381 };
382
383 class YACSEvalSession
384 {
385 public:
386   YACSEvalSession();
387   ~YACSEvalSession();
388   void launch();
389   bool isLaunched() const;
390   void checkLaunched() const;
391   int getPort() const;
392   std::string getCorbaConfigFileName() const;
393 };
394
395 class YACSEvalExecParams
396 {
397 public:
398   bool getStopASAPAfterErrorStatus() const;
399   void setStopASAPAfterErrorStatus(bool newStatus);
400 private:
401   YACSEvalExecParams();
402 };
403
404 class YACSEvalYFX
405 {
406 public:
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;
412   void unlockAll();
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);
421   %extend
422      {
423        void lockPortsForEvaluation(PyObject *inputsOfInterest, PyObject *outputsOfInterest)
424        {
425          std::vector<YACSEvalOutputPort *> outputsOfInterestCpp;
426          if(PyList_Check(outputsOfInterest))
427            {
428              int size(PyList_Size(outputsOfInterest));
429              for(int i=0;i<size;i++)
430                {
431                  PyObject *obj(PyList_GetItem(outputsOfInterest,i));
432                  void *argp(0);
433                  int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalOutputPort,0|0));
434                  if(!SWIG_IsOK(status))
435                    {
436                      std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalOutputPort instance !";
437                      throw YACS::Exception(oss.str());
438                    }
439                  outputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalOutputPort *>(argp));
440                }
441            }
442          else
443            {
444              PyErr_SetString(PyExc_TypeError,"not a list");
445              return ;
446            }
447          //
448          std::vector< YACSEvalInputPort * > inputsOfInterestCpp;
449          if(PyList_Check(inputsOfInterest))
450            {
451              int size(PyList_Size(inputsOfInterest));
452              for(int i=0;i<size;i++)
453                {
454                  PyObject *obj(PyList_GetItem(inputsOfInterest,i));
455                  void *argp(0);
456                  int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalInputPort,0|0));
457                  if(!SWIG_IsOK(status))
458                    {
459                      std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalInputPort instance !";
460                      throw YACS::Exception(oss.str());
461                    }
462                  inputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalInputPort *>(argp));
463                }
464            }
465          else
466            {
467              PyErr_SetString(PyExc_TypeError,"not a list");
468              return ;
469            }
470          self->lockPortsForEvaluation(inputsOfInterestCpp,outputsOfInterestCpp);
471        }
472
473        PyObject *getResults() const
474        {
475          std::vector<YACSEvalSeqAny *> retCpp(self->getResults());
476          return convertVectOfSeqAny(retCpp);
477        }
478
479        PyObject *getResultsInCaseOfFailure() const
480        {
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));
486          return retPy;
487        }
488
489        PyObject *run(YACSEvalSession *session)
490        {
491          int ret1;
492          bool ret0(self->run(session,ret1));
493          PyObject *ret(PyTuple_New(2));
494          PyObject *ret0Py(ret0?Py_True:Py_False);
495          Py_XINCREF(ret0Py);
496          PyTuple_SetItem(ret,0,ret0Py);
497          PyTuple_SetItem(ret,1,PyInt_FromLong(ret1));
498          return ret;
499        }
500      }
501 private:
502   YACSEvalYFX();
503 };