]> SALOME platform Git repositories - modules/yacs.git/blob - src/evalyfx_swig/evalyfx.i
Salome HOME
Deleted deprecated code
[modules/yacs.git] / src / evalyfx_swig / evalyfx.i
1 // Copyright (C) 2015-2016  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   bool setQOfInterestStatus(bool newStatus);
301   bool isQOfInterest() const;
302 private:
303   YACSEvalOutputPort();
304 };
305
306 class YACSEvalVirtualYACSContainer
307 {
308 public:
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;
315 private:
316   YACSEvalVirtualYACSContainer();
317 };
318
319 class YACSEvalResource
320 {
321 public:
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;
327   %extend
328      {
329        std::size_t __len__() const
330        {
331          return self->size();
332        }
333        YACSEvalVirtualYACSContainer *__getitem__(std::size_t i) const
334        {
335          return self->at(i);
336        }
337      }
338 private:
339   YACSEvalResource();
340 };
341
342 class YACSEvalParamsForCluster
343 {
344 public:
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;
358 private:
359   YACSEvalParamsForCluster();
360 };
361
362 class YACSEvalListOfResources
363 {
364 public:
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();
375   %extend
376      {
377        std::size_t __len__() const
378        {
379          return self->size();
380        }
381        YACSEvalResource *__getitem__(std::size_t i) const
382        {
383          return self->at(i);
384        }
385      }
386 private:
387   YACSEvalListOfResources();
388 };
389
390 class YACSEvalSession
391 {
392 public:
393   YACSEvalSession();
394   ~YACSEvalSession();
395   void launch();
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;
402   int getPort() const;
403   std::string getCorbaConfigFileName() const;
404 };
405
406 class YACSEvalExecParams
407 {
408 public:
409   bool getStopASAPAfterErrorStatus() const;
410   void setStopASAPAfterErrorStatus(bool newStatus);
411   bool getFetchRemoteDirForClusterStatus() const;
412   void setFetchRemoteDirForClusterStatus(bool newStatus);
413 private:
414   YACSEvalExecParams();
415 };
416
417 class YACSEvalYFX
418 {
419 public:
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;
425   void unlockAll();
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);
434   %extend
435      {
436        void lockPortsForEvaluation(PyObject *inputsOfInterest, PyObject *outputsOfInterest)
437        {
438          std::vector<YACSEvalOutputPort *> outputsOfInterestCpp;
439          if(PyList_Check(outputsOfInterest))
440            {
441              int size(PyList_Size(outputsOfInterest));
442              for(int i=0;i<size;i++)
443                {
444                  PyObject *obj(PyList_GetItem(outputsOfInterest,i));
445                  void *argp(0);
446                  int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalOutputPort,0|0));
447                  if(!SWIG_IsOK(status))
448                    {
449                      std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalOutputPort instance !";
450                      throw YACS::Exception(oss.str());
451                    }
452                  outputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalOutputPort *>(argp));
453                }
454            }
455          else
456            {
457              PyErr_SetString(PyExc_TypeError,"not a list");
458              return ;
459            }
460          //
461          std::vector< YACSEvalInputPort * > inputsOfInterestCpp;
462          if(PyList_Check(inputsOfInterest))
463            {
464              int size(PyList_Size(inputsOfInterest));
465              for(int i=0;i<size;i++)
466                {
467                  PyObject *obj(PyList_GetItem(inputsOfInterest,i));
468                  void *argp(0);
469                  int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalInputPort,0|0));
470                  if(!SWIG_IsOK(status))
471                    {
472                      std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalInputPort instance !";
473                      throw YACS::Exception(oss.str());
474                    }
475                  inputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalInputPort *>(argp));
476                }
477            }
478          else
479            {
480              PyErr_SetString(PyExc_TypeError,"not a list");
481              return ;
482            }
483          self->lockPortsForEvaluation(inputsOfInterestCpp,outputsOfInterestCpp);
484        }
485
486        PyObject *getResults() const
487        {
488          std::vector<YACSEvalSeqAny *> retCpp(self->getResults());
489          return convertVectOfSeqAny(retCpp);
490        }
491
492        PyObject *getResultsInCaseOfFailure() const
493        {
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));
499          return retPy;
500        }
501
502        PyObject *run(YACSEvalSession *session)
503        {
504          int ret1;
505          bool ret0(self->run(session,ret1));
506          PyObject *ret(PyTuple_New(2));
507          PyObject *ret0Py(ret0?Py_True:Py_False);
508          Py_XINCREF(ret0Py);
509          PyTuple_SetItem(ret,0,ret0Py);
510          PyTuple_SetItem(ret,1,PyInt_FromLong(ret1));
511          return ret;
512        }
513      }
514 private:
515   YACSEvalYFX();
516 };