Salome HOME
Copyright update 2020
[modules/yacs.git] / src / runtime_swig / SALOMERuntime.i
1 // Copyright (C) 2006-2020  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
20 // ----------------------------------------------------------------------------
21 //
22 %define SALOMEDOCSTRING
23 "Implementation of nodes for SALOME platform."
24 %enddef
25
26 %module(directors="1",docstring=SALOMEDOCSTRING) SALOMERuntime
27
28 //work around SWIG bug #1863647
29 #if SWIG_VERSION >= 0x010336
30 #define SwigPyIterator SALOMERuntime_PySwigIterator
31 #else
32 #define PySwigIterator SALOMERuntime_PySwigIterator
33 #endif
34
35 %feature("autodoc", "1");
36
37 %include engtypemaps.i
38
39 #ifdef DOXYGEN_IS_OK
40 %include docruntime.i
41 #endif
42
43 // ----------------------------------------------------------------------------
44
45 %{
46 #include "SalomeContainer.hxx"
47 #include "SalomeHPContainer.hxx"
48 #include "RuntimeSALOME.hxx"
49 #include "SALOMEDispatcher.hxx"
50 #include "SalomeProc.hxx"
51 #include "PythonNode.hxx"
52 #include "PythonPorts.hxx"
53 #include "PresetNode.hxx"
54 #include "PresetPorts.hxx"
55 #include "CORBANode.hxx"
56 #include "CORBAPorts.hxx"
57 #include "StudyNodes.hxx"
58 #include "StudyPorts.hxx"
59 #include "TypeConversions.hxx"
60 #include "TypeCode.hxx"
61 #include "VisitorSaveSalomeSchema.hxx"
62 #include "VisitorSalomeSaveState.hxx"
63 #include "SalomeOptimizerLoop.hxx"
64 #include "DistributedPythonNode.hxx"
65 #include "PyOptimizerAlg.hxx"
66 #include "PyStdout.hxx"
67 #include "ExecutorSwig.hxx"
68 #include <sstream>
69 #include "Catalog.hxx"
70 %}
71
72 // ----------------------------------------------------------------------------
73
74 %init
75 %{
76   // init section
77
78   PyObject* omnipy = PyImport_ImportModule((char*)"_omnipy");
79   if (!omnipy)
80   {
81     PyErr_SetString(PyExc_ImportError,(char*)"Cannot import _omnipy");
82     return NULL;
83   }
84   PyObject* pyapi = PyObject_GetAttrString(omnipy, (char*)"API");
85   api = (omniORBpyAPI*)PyCapsule_GetPointer(pyapi,"_omnipy.API");
86   Py_DECREF(pyapi);
87 %}
88
89 %{
90 static PyObject *convertContainer2(YACS::ENGINE::Container *cont, int owner=0)
91 {
92   if(!cont)
93     return SWIG_NewPointerObj((void*)cont,SWIGTYPE_p_YACS__ENGINE__Container, owner);
94   if(dynamic_cast<YACS::ENGINE::SalomeHPContainer *>(cont))
95     {
96       return SWIG_NewPointerObj((void*)dynamic_cast<YACS::ENGINE::SalomeHPContainer *>(cont),SWIGTYPE_p_YACS__ENGINE__SalomeHPContainer, owner);
97     }
98   if(dynamic_cast<YACS::ENGINE::SalomeContainer *>(cont))
99     {
100       return SWIG_NewPointerObj((void*)dynamic_cast<YACS::ENGINE::SalomeContainer *>(cont),SWIGTYPE_p_YACS__ENGINE__SalomeContainer, owner);
101     }
102   return SWIG_NewPointerObj((void*)cont,SWIGTYPE_p_YACS__ENGINE__Container, owner);
103 }
104 %}
105
106 // ----------------------------------------------------------------------------
107
108 #ifdef SWIGPYTHON
109 %typemap(out) YACS_ORB::Observer_ptr
110 {
111   $result = api->cxxObjRefToPyObjRef($1, 1);
112 }
113
114 %typemap(in) YACS_ORB::Observer_ptr
115 {
116   try
117   {
118      CORBA::Object_ptr obj = api->pyObjRefToCxxObjRef($input,1);
119      $1 = YACS_ORB::Observer::_narrow(obj);
120   }
121   catch (...)
122   {
123      PyErr_SetString(PyExc_RuntimeError, "not a valid CORBA object ptr");
124      return NULL;
125   }
126 }
127
128 %typemap(out) YACS::ENGINE::PyObj * "Py_INCREF($1); $result = $1;";
129
130 #endif
131
132 // ----------------------------------------------------------------------------
133
134 %import "pilot.i"
135
136 %rename(getSALOMERuntime) YACS::ENGINE::getSALOMERuntime; // to suppress a 503 warning
137 %ignore omniORBpyAPI;
138
139 %include <YACSRuntimeSALOMEExport.hxx>
140 %include "SalomeContainer.hxx"
141 %include "SalomeHPContainer.hxx"
142 %include "RuntimeSALOME.hxx"
143 %include "SALOMEDispatcher.hxx"
144 %include "SalomeProc.hxx"
145 %include "PythonNode.hxx"
146 %include "PythonPorts.hxx"
147 %include "XMLPorts.hxx"
148 %include "PresetNode.hxx"
149 %include "PresetPorts.hxx"
150 %include "CORBANode.hxx"
151 %include "CORBAPorts.hxx"
152 %include "StudyNodes.hxx"
153 %include "StudyPorts.hxx"
154 %include "SalomeOptimizerLoop.hxx"
155 %include "DistributedPythonNode.hxx"
156
157 namespace YACS
158 {
159   namespace ENGINE
160   {
161     void schemaSaveState(Proc* proc,
162                          Executor* exec,
163                          const std::string& xmlSchemaFile);
164   }
165 }
166
167
168 %extend YACS::ENGINE::OutputPresetPort
169 {
170   void setDataPy(PyObject *ob)
171   {
172     std::string sss = convertPyObjectXml(self->edGetType(),ob);
173     self->setData(sss);
174   }
175 }
176
177
178 // Define the methods that can (default) or cannot (nodirector) be overloaded in Python
179 %feature("director") YACS::ENGINE::PyOptimizerAlgBase;
180 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::getTCForInProxy;
181 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::getTCForOutProxy;
182 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::initializeProxy;
183 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::startProxy;
184 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::takeDecisionProxy;
185 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::finishProxy;
186 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::hasError;
187 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::getError;
188 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::setError;
189 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::getTCForAlgoInitProxy;
190 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::getTCForAlgoResultProxy;
191 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::getAlgoResultProxy;
192 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::setProc;
193
194 %feature("director") YACS::ENGINE::PyOptimizerAlgASync;
195 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::getTCForInProxy;
196 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::getTCForOutProxy;
197 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::initializeProxy;
198 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::startProxy;
199 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::takeDecisionProxy;
200 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::finishProxy;
201 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::hasError;
202 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::getError;
203 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::setError;
204 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::signalMasterAndWait;
205 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::signalSlaveAndWait;
206 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::start;
207 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::takeDecision;
208 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::run;
209 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::getTCForAlgoInitProxy;
210 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::getTCForAlgoResultProxy;
211 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::getAlgoResultProxy;
212 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::setProc;
213
214 %feature("director:except") {
215     if ($error != NULL) {
216         std::string errorMessage;
217         PyObject* new_stderr = YACS::ENGINE::newPyStdOut(errorMessage);
218         PySys_SetObject((char*)"stderr", new_stderr);
219         PyErr_Print();
220         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
221         Py_DECREF(new_stderr);
222         throw YACS::Exception(errorMessage);
223     }
224 }
225
226 // Warning: as PyOptimizerAlgSync is only a typedef in PyOptimizerAlg.hxx, we have to
227 // directly rename PyOptimizerAlgBase in OptimizerAlgSync. If someday PyOptimizerAlgSync
228 // becomes a real derived class, this will have to be changed.
229 %rename(OptimizerAlgSync) YACS::ENGINE::PyOptimizerAlgBase;
230 %rename(OptimizerAlgASync) YACS::ENGINE::PyOptimizerAlgASync;
231
232 %include "PyOptimizerAlg.hxx"
233
234 %newobject YACS::ENGINE::RuntimeSALOME::createAnyPyObject;
235
236 %extend YACS::ENGINE::RuntimeSALOME
237 {
238   PyObject *createContainer(const std::string& kind="")
239   {
240     YACS::ENGINE::Container *ret(self->createContainer(kind));
241     return convertContainer2(ret,SWIG_POINTER_OWN | 0);
242   }
243
244   Any* createAnyPyObject(PyObject * pyobj)
245   {
246     return convertPyObjectNeutral(self->getBuiltinCatalog()->_typeMap["pyobj"],
247                                   pyobj);
248   }
249 }