Salome HOME
Addition of notification mechanism 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 void convertPyToDblArr(PyObject *pyLi, std::vector<double>& arr)
79 {
80   if(PyList_Check(pyLi))
81     {
82       int size=PyList_Size(pyLi);
83       arr.resize(size);
84       for(int i=0;i<size;i++)
85         {
86           PyObject *o=PyList_GetItem(pyLi,i);
87           if(PyFloat_Check(o))
88             {
89               double val(PyFloat_AS_DOUBLE(o));
90               arr[i]=val;
91             }
92           else
93             throw YACS::Exception("list must contain integers only");
94         }
95     }
96   else if(PyTuple_Check(pyLi))
97     {
98       int size=PyTuple_Size(pyLi);
99       arr.resize(size);
100       for(int i=0;i<size;i++)
101         {
102           PyObject *o=PyTuple_GetItem(pyLi,i);
103           if(PyFloat_Check(o))
104             {
105               double val(PyFloat_AS_DOUBLE(o));
106               arr[i]=val;
107             }
108           else
109             throw YACS::Exception("tuple must contain floats only");
110         }
111     }
112   else
113     {
114       throw YACS::Exception("convertPyToNewIntArr3 : not a list nor a tuple");
115     }
116 }
117 %}
118
119 %types(YACSEvalInputPort,YACSEvalOutputPort);
120 /*%types(YACS::ENGINE::Node *,YACS::ENGINE::Proc *);
121 %types(YACS::ENGINE::InputPort *,YACS::ENGINE::OutputPort *,YACS::ENGINE::InputDataStreamPort *,YACS::ENGINE::OutputDataStreamPort *);
122 %types(YACS::ENGINE::InGate *,YACS::ENGINE::OutGate *,YACS::ENGINE::InPort *,YACS::ENGINE::OutPort *,YACS::ENGINE::Port *);
123 %types(YACS::ENGINE::Container *, YACS::ENGINE::HomogeneousPoolContainer *);*/
124
125 %import "loader.i"
126
127 %newobject YACSEvalYFX::BuildFromFile;
128 %newobject YACSEvalYFX::BuildFromScheme;
129
130 %typemap(out) std::vector<YACSEvalInputPort *>
131 {
132   std::vector<YACSEvalInputPort *>::const_iterator it;
133   $result = PyList_New($1.size());
134   int i = 0;
135   for (it = $1.begin(); it != $1.end(); ++it, ++i)
136     {
137       PyList_SetItem($result,i,SWIG_NewPointerObj(SWIG_as_voidptr(*it),SWIGTYPE_p_YACSEvalInputPort, 0 | 0 ));
138     }
139 }
140
141 %typemap(out) std::vector<YACSEvalOutputPort *>
142 {
143   std::vector<YACSEvalOutputPort *>::const_iterator it;
144   $result = PyList_New($1.size());
145   int i = 0;
146   for (it = $1.begin(); it != $1.end(); ++it, ++i)
147     {
148       PyList_SetItem($result,i,SWIG_NewPointerObj(SWIG_as_voidptr(*it),SWIGTYPE_p_YACSEvalOutputPort, 0 | 0 ));
149     }
150 }
151
152 %typemap(out) YACSEvalAny *
153 {
154   $result = 0;
155   YACSEvalAnyDouble *val0(dynamic_cast<YACSEvalAnyDouble *>($1));
156   YACSEvalAnyInt *val1(dynamic_cast<YACSEvalAnyInt *>($1));
157   if(val0)
158     {
159       $result = PyFloat_FromDouble(val0->toDouble());
160       delete $1;
161     }
162   else if(val1)
163     {
164       $result = PyInt_FromLong(val1->toInt());
165       delete $1;
166     }
167   else
168     {
169       delete $1;
170       throw YACS::Exception("PyWrap of YACSEvalInputPort::getDefaultValueDefined : unrecognized type !");
171     }
172 }
173
174 %typemap(in) const std::list< YACSEvalOutputPort * >& 
175 {
176   
177 }
178
179 class YACSEvalPort
180 {
181 public:
182   virtual std::string getTypeOfData() const;
183 private:
184   YACSEvalPort();
185 };
186
187 class YACSEvalInputPort : public YACSEvalPort
188 {
189 public:
190   std::string getName() const;
191   bool hasDefaultValueDefined() const;
192   YACSEvalAny *getDefaultValueDefined() const;
193   bool isRandomVar() const;
194   void declareRandomnessStatus(bool isRandom);
195   bool hasSequenceOfValuesToEval() const;
196   %extend
197      {
198        void setDefaultValue(PyObject *parameter)
199        {
200          if(parameter==Py_None)
201            self->setDefaultValue(0);
202          else if(PyFloat_Check(parameter))
203            {
204              YACSEvalAnyDouble tmp(PyFloat_AsDouble(parameter));
205              self->setDefaultValue(&tmp);
206            }
207          else if(PyInt_Check(parameter))
208            {
209              YACSEvalAnyInt tmp((int)PyInt_AsLong(parameter));
210              self->setDefaultValue(&tmp);
211            }
212          else
213            throw YACS::Exception("PyWrap of YACSEvalInputPort::setParameter : unrecognized type !");
214        }
215        
216        void setSequenceOfValuesToEval(PyObject *vals)
217        {
218          if(!PyList_Check(vals))
219            {
220              PyErr_SetString(PyExc_TypeError,"not a list");
221              return ;
222            }
223          int size(PyList_Size(vals));
224          YACSEvalSeqAny *valsCpp(0);
225          if(size>0)
226            {
227              PyObject *elt0(PyList_GetItem(vals,0));
228              if(PyFloat_Check(elt0))
229                {
230                  std::vector<double> zeVals;
231                  convertPyToDblArr(vals,zeVals);
232                  valsCpp=new YACSEvalSeqAnyDouble(zeVals);
233                }
234              else if(PyInt_Check(elt0))
235                {
236                  std::vector<int> zeVals;
237                  convertPyToIntArr(vals,zeVals);
238                  valsCpp=new YACSEvalSeqAnyInt(zeVals);
239                }
240              else
241                throw YACS::Exception("YACSEvalInputPort::setSequenceOfValuesToEval : only list[float] and list[int] actualy supported !");
242            }
243          else
244            valsCpp=YACSEvalSeqAny::BuildEmptyFromType(self->getTypeOfData());
245          self->setSequenceOfValuesToEval(valsCpp);
246          delete valsCpp;
247        }
248      }
249 private:
250   YACSEvalInputPort();
251 };
252
253 class YACSEvalOutputPort : public YACSEvalPort
254 {
255 public:
256   std::string getName() const;
257 private:
258   YACSEvalOutputPort();
259 };
260
261 class YACSEvalVirtualYACSContainer
262 {
263 public:
264   std::string getChosenMachine() const;
265   void setWantedMachine(const std::string& machine);
266   std::vector<std::string> listOfPropertyKeys() const;
267   std::string getValueOfKey(const char *key) const;
268   void setProperty(const std::string& key, const std::string &value);
269 private:
270   YACSEvalVirtualYACSContainer();
271 };
272
273 class YACSEvalResource
274 {
275 public:
276   std::vector<std::string> getAllChosenMachines() const;
277   std::vector<std::string> getAllFittingMachines() const;
278   void setWantedMachine(const std::string& machine);
279   std::size_t size() const;
280   YACSEvalVirtualYACSContainer *at(std::size_t i) const;
281   %extend
282      {
283        std::size_t __len__() const
284        {
285          return self->size();
286        }
287        YACSEvalVirtualYACSContainer *__getitem__(std::size_t i) const
288        {
289          return self->at(i);
290        }
291      }
292 private:
293   YACSEvalResource();
294 };
295
296 class YACSEvalListOfResources
297 {
298 public:
299   std::vector<std::string> getAllChosenMachines() const;
300   std::vector<std::string> getAllFittingMachines() const;
301   void setWantedMachine(const std::string& machine);
302   std::size_t size() const;
303   bool isInteractive() const;
304   YACSEvalResource *at(std::size_t i) const;
305   unsigned int getNumberOfProcsDeclared() const;
306   %extend
307      {
308        std::size_t __len__() const
309        {
310          return self->size();
311        }
312        YACSEvalResource *__getitem__(std::size_t i) const
313        {
314          return self->at(i);
315        }
316      }
317 private:
318   YACSEvalListOfResources();
319 };
320
321 class YACSEvalSession
322 {
323 public:
324   YACSEvalSession();
325   ~YACSEvalSession();
326   void launch();
327   bool isLaunched() const;
328   void checkLaunched() const;
329   int getPort() const;
330   std::string getCorbaConfigFileName() const;
331 };
332
333 class YACSEvalExecParams
334 {
335 public:
336   bool getStopASAPAfterErrorStatus() const;
337   void setStopASAPAfterErrorStatus(bool newStatus);
338 private:
339   YACSEvalExecParams();
340 };
341
342 class YACSEvalYFX
343 {
344 public:
345   static YACSEvalYFX *BuildFromFile(const std::string& xmlOfScheme);
346   static YACSEvalYFX *BuildFromScheme(YACS::ENGINE::Proc *schema);
347   YACSEvalExecParams *getParams() const;
348   std::vector<YACSEvalInputPort *> getFreeInputPorts() const;
349   std::vector<YACSEvalOutputPort *> getFreeOutputPorts() const;
350   void unlockAll();
351   bool isLocked() const;
352   YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const;
353   YACSEvalListOfResources *giveResources();
354   //void registerObserver(YACSEvalObserver *observer);
355   %extend
356      {
357        void lockPortsForEvaluation(PyObject *inputsOfInterest, PyObject *outputsOfInterest)
358        {
359          std::vector<YACSEvalOutputPort *> outputsOfInterestCpp;
360          if(PyList_Check(outputsOfInterest))
361            {
362              int size(PyList_Size(outputsOfInterest));
363              for(int i=0;i<size;i++)
364                {
365                  PyObject *obj(PyList_GetItem(outputsOfInterest,i));
366                  void *argp(0);
367                  int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalOutputPort,0|0));
368                  if(!SWIG_IsOK(status))
369                    {
370                      std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalOutputPort instance !";
371                      throw YACS::Exception(oss.str());
372                    }
373                  outputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalOutputPort *>(argp));
374                }
375            }
376          else
377            {
378              PyErr_SetString(PyExc_TypeError,"not a list");
379              return ;
380            }
381          //
382          std::vector< YACSEvalInputPort * > inputsOfInterestCpp;
383          if(PyList_Check(inputsOfInterest))
384            {
385              int size(PyList_Size(inputsOfInterest));
386              for(int i=0;i<size;i++)
387                {
388                  PyObject *obj(PyList_GetItem(inputsOfInterest,i));
389                  void *argp(0);
390                  int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalInputPort,0|0));
391                  if(!SWIG_IsOK(status))
392                    {
393                      std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalInputPort instance !";
394                      throw YACS::Exception(oss.str());
395                    }
396                  inputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalInputPort *>(argp));
397                }
398            }
399          else
400            {
401              PyErr_SetString(PyExc_TypeError,"not a list");
402              return ;
403            }
404          self->lockPortsForEvaluation(inputsOfInterestCpp,outputsOfInterestCpp);
405        }
406
407        PyObject *getResults() const
408        {
409          std::vector<YACSEvalSeqAny *> retCpp(self->getResults());
410          std::size_t sz(retCpp.size());
411          PyObject *ret(PyList_New(sz));
412          for(std::size_t i=0;i<sz;i++)
413            {
414              YACSEvalSeqAny *elt(retCpp[i]);
415              YACSEvalSeqAnyDouble *elt1(dynamic_cast<YACSEvalSeqAnyDouble *>(elt));
416              YACSEvalSeqAnyInt *elt2(dynamic_cast<YACSEvalSeqAnyInt *>(elt));
417              if(elt1)
418                {
419                  std::vector<double> *zeArr(elt1->getInternal());
420                  std::size_t sz2(zeArr->size());
421                  PyObject *ret2(PyList_New(sz2));
422                  for(std::size_t i2=0;i2<sz2;i2++)
423                    PyList_SetItem(ret2,i2,PyFloat_FromDouble((*zeArr)[i2]));
424                  PyList_SetItem(ret,i,ret2);
425                }
426              else if(elt2)
427                {
428                  std::vector<int> *zeArr(elt2->getInternal());
429                  std::size_t sz2(zeArr->size());
430                  PyObject *ret2(PyList_New(sz2));
431                  for(std::size_t i2=0;i2<sz2;i2++)
432                    PyList_SetItem(ret2,i2,PyInt_FromLong((*zeArr)[i2]));
433                  PyList_SetItem(ret,i,ret2);
434                }
435              else
436                throw YACS::Exception("wrap of YACSEvalYFX.getResults : unrecognized type !");
437              delete elt;
438            }
439          return ret;
440        }
441
442        PyObject *run(YACSEvalSession *session)
443        {
444          int ret1;
445          bool ret0(self->run(session,ret1));
446          PyObject *ret(PyTuple_New(2));
447          PyObject *ret0Py(ret0?Py_True:Py_False);
448          Py_XINCREF(ret0Py);
449          PyTuple_SetItem(ret,0,ret0Py);
450          PyTuple_SetItem(ret,1,PyInt_FromLong(ret1));
451          return ret;
452        }
453      }
454 private:
455   YACSEvalYFX();
456 };