]> SALOME platform Git repositories - modules/yacs.git/blob - src/evalyfx_swig/evalyfx.i
Salome HOME
9e391bcea041c44678b91b1e13a63bcc59106619
[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   %extend
194      {
195        void setDefaultValue(PyObject *parameter)
196        {
197          if(parameter==Py_None)
198            self->setDefaultValue(0);
199          else if(PyFloat_Check(parameter))
200            {
201              YACSEvalAnyDouble tmp(PyFloat_AsDouble(parameter));
202              self->setDefaultValue(&tmp);
203            }
204          else if(PyInt_Check(parameter))
205            {
206              YACSEvalAnyInt tmp((int)PyInt_AsLong(parameter));
207              self->setDefaultValue(&tmp);
208            }
209          else
210            throw YACS::Exception("PyWrap of YACSEvalInputPort::setParameter : unrecognized type !");
211        }
212        
213        void setSequenceOfValuesToEval(PyObject *vals)
214        {
215          if(!PyList_Check(vals))
216            {
217              PyErr_SetString(PyExc_TypeError,"not a list");
218              return ;
219            }
220          int size(PyList_Size(vals));
221          YACSEvalSeqAny *valsCpp(0);
222          if(size>0)
223            {
224              PyObject *elt0(PyList_GetItem(vals,0));
225              if(PyFloat_Check(elt0))
226                {
227                  std::vector<double> zeVals;
228                  convertPyToDblArr(vals,zeVals);
229                  valsCpp=new YACSEvalSeqAnyDouble(zeVals);
230                }
231              else if(PyInt_Check(elt0))
232                {
233                  std::vector<int> zeVals;
234                  convertPyToIntArr(vals,zeVals);
235                  valsCpp=new YACSEvalSeqAnyInt(zeVals);
236                }
237              else
238                throw YACS::Exception("YACSEvalInputPort::setSequenceOfValuesToEval : only list[float] and list[int] actualy supported !");
239            }
240          else
241            valsCpp=YACSEvalSeqAny::BuildEmptyFromType(self->getTypeOfData());
242          self->setSequenceOfValuesToEval(valsCpp);
243          delete valsCpp;
244        }
245
246        PyObject *hasSequenceOfValuesToEval() const
247        {
248          std::size_t ret1;
249          bool ret0(self->hasSequenceOfValuesToEval(ret1));
250          PyObject *ret(PyTuple_New(2));
251          PyObject *ret0Py=ret0?Py_True:Py_False;
252          Py_XINCREF(ret0Py);
253          PyTuple_SetItem(ret,0,ret0Py);
254          PyTuple_SetItem(ret,1,PyInt_FromLong(ret1));
255          return ret;
256        }
257      }
258 private:
259   YACSEvalInputPort();
260 };
261
262 class YACSEvalOutputPort : public YACSEvalPort
263 {
264 public:
265   std::string getName() const;
266 private:
267   YACSEvalOutputPort();
268 };
269
270 class YACSEvalVirtualYACSContainer
271 {
272 public:
273   std::string getChosenMachine() const;
274   void setWantedMachine(const std::string& machine);
275   std::vector<std::string> listOfPropertyKeys() const;
276   std::string getValueOfKey(const char *key) const;
277   void setProperty(const std::string& key, const std::string &value);
278 private:
279   YACSEvalVirtualYACSContainer();
280 };
281
282 class YACSEvalResource
283 {
284 public:
285   std::vector<std::string> getAllChosenMachines() const;
286   std::vector<std::string> getAllFittingMachines() const;
287   void setWantedMachine(const std::string& machine);
288   std::size_t size() const;
289   YACSEvalVirtualYACSContainer *at(std::size_t i) const;
290   %extend
291      {
292        std::size_t __len__() const
293        {
294          return self->size();
295        }
296        YACSEvalVirtualYACSContainer *__getitem__(std::size_t i) const
297        {
298          return self->at(i);
299        }
300      }
301 private:
302   YACSEvalResource();
303 };
304
305 class YACSEvalListOfResources
306 {
307 public:
308   std::vector<std::string> getAllChosenMachines() const;
309   std::vector<std::string> getAllFittingMachines() const;
310   void setWantedMachine(const std::string& machine);
311   std::size_t size() const;
312   bool isInteractive() const;
313   YACSEvalResource *at(std::size_t i) const;
314   unsigned int getNumberOfProcsDeclared() const;
315   %extend
316      {
317        std::size_t __len__() const
318        {
319          return self->size();
320        }
321        YACSEvalResource *__getitem__(std::size_t i) const
322        {
323          return self->at(i);
324        }
325      }
326 private:
327   YACSEvalListOfResources();
328 };
329
330 class YACSEvalSession
331 {
332 public:
333   YACSEvalSession();
334   ~YACSEvalSession();
335   void launch();
336   bool isLaunched() const;
337   void checkLaunched() const;
338   int getPort() const;
339   std::string getCorbaConfigFileName() const;
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   std::vector<YACSEvalInputPort *> getFreeInputPorts() const;
348   std::vector<YACSEvalOutputPort *> getFreeOutputPorts() const;
349   void unlockAll();
350   bool isLocked() const;
351   YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const;
352   YACSEvalListOfResources *giveResources();
353   %extend
354      {
355        void lockPortsForEvaluation(PyObject *outputsOfInterest)
356        {
357          std::vector<YACSEvalOutputPort *> outputsOfInterestCpp;
358          if(PyList_Check(outputsOfInterest))
359            {
360              int size(PyList_Size(outputsOfInterest));
361              for(int i=0;i<size;i++)
362                {
363                  PyObject *obj(PyList_GetItem(outputsOfInterest,i));
364                  void *argp(0);
365                  int status(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_YACSEvalOutputPort,0|0));
366                  if(!SWIG_IsOK(status))
367                    {
368                      std::ostringstream oss; oss << "Input elt #" << i << " in list is not a YACSEvalOutputPort instance !";
369                      throw YACS::Exception(oss.str());
370                    }
371                  outputsOfInterestCpp.push_back(reinterpret_cast<YACSEvalOutputPort *>(argp));
372                }
373            }
374          else
375            {
376              PyErr_SetString(PyExc_TypeError,"not a list");
377              return ;
378            }
379          self->lockPortsForEvaluation(outputsOfInterestCpp);
380        }
381
382        PyObject *getResults() const
383        {
384          std::vector<YACSEvalSeqAny *> retCpp(self->getResults());
385          std::size_t sz(retCpp.size());
386          PyObject *ret(PyList_New(sz));
387          for(std::size_t i=0;i<sz;i++)
388            {
389              YACSEvalSeqAny *elt(retCpp[i]);
390              YACSEvalSeqAnyDouble *elt1(dynamic_cast<YACSEvalSeqAnyDouble *>(elt));
391              YACSEvalSeqAnyInt *elt2(dynamic_cast<YACSEvalSeqAnyInt *>(elt));
392              if(elt1)
393                {
394                  std::vector<double> *zeArr(elt1->getInternal());
395                  std::size_t sz2(zeArr->size());
396                  PyObject *ret2(PyList_New(sz2));
397                  for(std::size_t i2=0;i2<sz2;i2++)
398                    PyList_SetItem(ret2,i2,PyFloat_FromDouble((*zeArr)[i2]));
399                  PyList_SetItem(ret,i,ret2);
400                }
401              else if(elt2)
402                {
403                  std::vector<int> *zeArr(elt2->getInternal());
404                  std::size_t sz2(zeArr->size());
405                  PyObject *ret2(PyList_New(sz2));
406                  for(std::size_t i2=0;i2<sz2;i2++)
407                    PyList_SetItem(ret2,i2,PyInt_FromLong((*zeArr)[i2]));
408                  PyList_SetItem(ret,i,ret2);
409                }
410              else
411                throw YACS::Exception("wrap of YACSEvalYFX.getResults : unrecognized type !");
412              delete elt;
413            }
414          return ret;
415        }
416
417        PyObject *run(YACSEvalSession *session)
418        {
419          int ret1;
420          bool ret0(self->run(session,ret1));
421          PyObject *ret(PyTuple_New(2));
422          PyObject *ret0Py(ret0?Py_True:Py_False);
423          Py_XINCREF(ret0Py);
424          PyTuple_SetItem(ret,0,ret0Py);
425          PyTuple_SetItem(ret,1,PyInt_FromLong(ret1));
426          return ret;
427        }
428      }
429 private:
430   YACSEvalYFX();
431 };