Salome HOME
Successful first launch of scheme on cluster.
[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 getLocalWorkingDir();
348   void setLocalWorkingDir(const std::string& localWorkingDir);
349   std::string getWCKey() const;
350   void setWCKey(const std::string& wcKey);
351   unsigned int getNbProcs() const;
352   void setNbProcs(unsigned int nbProcs);
353   void setMaxDuration(const std::string& maxDuration);
354   std::string getMaxDuration() const;
355   void checkConsistency() const;
356 private:
357   YACSEvalParamsForCluster();
358 };
359
360 class YACSEvalListOfResources
361 {
362 public:
363   std::vector<std::string> getAllChosenMachines() const;
364   std::vector<std::string> getAllFittingMachines() const;
365   void setWantedMachine(const std::string& machine);
366   std::size_t size() const;
367   bool isInteractive() const;
368   YACSEvalResource *at(std::size_t i) const;
369   unsigned int getNumberOfProcsDeclared() const;
370   void checkOKForRun() const;
371   YACSEvalParamsForCluster& getAddParamsForCluster();
372   %extend
373      {
374        std::size_t __len__() const
375        {
376          return self->size();
377        }
378        YACSEvalResource *__getitem__(std::size_t i) const
379        {
380          return self->at(i);
381        }
382      }
383 private:
384   YACSEvalListOfResources();
385 };
386
387 class YACSEvalSession
388 {
389 public:
390   YACSEvalSession();
391   ~YACSEvalSession();
392   void launch();
393   bool isLaunched() const;
394   void checkLaunched() const;
395   int getPort() const;
396   std::string getCorbaConfigFileName() const;
397 };
398
399 class YACSEvalExecParams
400 {
401 public:
402   bool getStopASAPAfterErrorStatus() const;
403   void setStopASAPAfterErrorStatus(bool newStatus);
404 private:
405   YACSEvalExecParams();
406 };
407
408 class YACSEvalYFX
409 {
410 public:
411   static YACSEvalYFX *BuildFromFile(const std::string& xmlOfScheme);
412   static YACSEvalYFX *BuildFromScheme(YACS::ENGINE::Proc *schema);
413   YACSEvalExecParams *getParams() const;
414   std::vector<YACSEvalInputPort *> getFreeInputPorts() const;
415   std::vector<YACSEvalOutputPort *> getFreeOutputPorts() const;
416   void unlockAll();
417   bool isLocked() const;
418   YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const;
419   YACSEvalListOfResources *giveResources();
420   std::string getErrorDetailsInCaseOfFailure() const;
421   std::string getStatusOfRunStr() const;
422   void setParallelizeStatus(bool newVal);
423   bool getParallelizeStatus() const;
424   //void registerObserver(YACSEvalObserver *observer);
425   %extend
426      {
427        void lockPortsForEvaluation(PyObject *inputsOfInterest, PyObject *outputsOfInterest)
428        {
429          std::vector<YACSEvalOutputPort *> outputsOfInterestCpp;
430          if(PyList_Check(outputsOfInterest))
431            {
432              int size(PyList_Size(outputsOfInterest));
433              for(int i=0;i<size;i++)
434                {
435                  PyObject *obj(PyList_GetItem(outputsOfInterest,i));
436                  void *argp(0);
437                  int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalOutputPort,0|0));
438                  if(!SWIG_IsOK(status))
439                    {
440                      std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalOutputPort instance !";
441                      throw YACS::Exception(oss.str());
442                    }
443                  outputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalOutputPort *>(argp));
444                }
445            }
446          else
447            {
448              PyErr_SetString(PyExc_TypeError,"not a list");
449              return ;
450            }
451          //
452          std::vector< YACSEvalInputPort * > inputsOfInterestCpp;
453          if(PyList_Check(inputsOfInterest))
454            {
455              int size(PyList_Size(inputsOfInterest));
456              for(int i=0;i<size;i++)
457                {
458                  PyObject *obj(PyList_GetItem(inputsOfInterest,i));
459                  void *argp(0);
460                  int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalInputPort,0|0));
461                  if(!SWIG_IsOK(status))
462                    {
463                      std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalInputPort instance !";
464                      throw YACS::Exception(oss.str());
465                    }
466                  inputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalInputPort *>(argp));
467                }
468            }
469          else
470            {
471              PyErr_SetString(PyExc_TypeError,"not a list");
472              return ;
473            }
474          self->lockPortsForEvaluation(inputsOfInterestCpp,outputsOfInterestCpp);
475        }
476
477        PyObject *getResults() const
478        {
479          std::vector<YACSEvalSeqAny *> retCpp(self->getResults());
480          return convertVectOfSeqAny(retCpp);
481        }
482
483        PyObject *getResultsInCaseOfFailure() const
484        {
485          std::vector<unsigned int> ret1Cpp;
486          std::vector<YACSEvalSeqAny *> ret0Cpp(self->getResultsInCaseOfFailure(ret1Cpp));
487          PyObject *retPy(PyTuple_New(2));
488          PyTuple_SetItem(retPy,0,convertVectOfSeqAny(ret0Cpp));
489          PyTuple_SetItem(retPy,1,convertToPyToInt(ret1Cpp));
490          return retPy;
491        }
492
493        PyObject *run(YACSEvalSession *session)
494        {
495          int ret1;
496          bool ret0(self->run(session,ret1));
497          PyObject *ret(PyTuple_New(2));
498          PyObject *ret0Py(ret0?Py_True:Py_False);
499          Py_XINCREF(ret0Py);
500          PyTuple_SetItem(ret,0,ret0Py);
501          PyTuple_SetItem(ret,1,PyInt_FromLong(ret1));
502          return ret;
503        }
504      }
505 private:
506   YACSEvalYFX();
507 };