Salome HOME
Fix the python API of the savestate feature.
[modules/yacs.git] / src / runtime_swig / SALOMERuntime.i
1 // Copyright (C) 2006-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
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 %}
70
71 // ----------------------------------------------------------------------------
72
73 %init
74 %{
75   // init section
76
77   PyObject* omnipy = PyImport_ImportModule((char*)"_omnipy");
78   if (!omnipy)
79   {
80     PyErr_SetString(PyExc_ImportError,(char*)"Cannot import _omnipy");
81     return;
82   }
83   PyObject* pyapi = PyObject_GetAttrString(omnipy, (char*)"API");
84   api = (omniORBPYAPI*)PyCObject_AsVoidPtr(pyapi);
85   Py_DECREF(pyapi);
86 %}
87
88 // ----------------------------------------------------------------------------
89
90 #ifdef SWIGPYTHON
91 %typemap(out) YACS_ORB::Observer_ptr
92 {
93   $result = api->cxxObjRefToPyObjRef($1, 1);
94 }
95
96 %typemap(in) YACS_ORB::Observer_ptr
97 {
98   try
99   {
100      CORBA::Object_ptr obj = api->pyObjRefToCxxObjRef($input,1);
101      $1 = YACS_ORB::Observer::_narrow(obj);
102   }
103   catch (...)
104   {
105      PyErr_SetString(PyExc_RuntimeError, "not a valid CORBA object ptr");
106      return NULL;
107   }
108 }
109
110 %typemap(out) YACS::ENGINE::PyObj * "Py_INCREF($1); $result = $1;";
111
112 #endif
113
114 // ----------------------------------------------------------------------------
115
116 %import "pilot.i"
117
118 %rename(getSALOMERuntime) YACS::ENGINE::getSALOMERuntime; // to suppress a 503 warning
119 %ignore omniORBpyAPI;
120
121 %include <YACSRuntimeSALOMEExport.hxx>
122 %include "SalomeContainer.hxx"
123 %include "SalomeHPContainer.hxx"
124 %include "RuntimeSALOME.hxx"
125 %include "SALOMEDispatcher.hxx"
126 %include "SalomeProc.hxx"
127 %include "PythonNode.hxx"
128 %include "PythonPorts.hxx"
129 %include "XMLPorts.hxx"
130 %include "PresetNode.hxx"
131 %include "PresetPorts.hxx"
132 %include "CORBANode.hxx"
133 %include "CORBAPorts.hxx"
134 %include "StudyNodes.hxx"
135 %include "StudyPorts.hxx"
136 %include "SalomeOptimizerLoop.hxx"
137 %include "DistributedPythonNode.hxx"
138
139 namespace YACS
140 {
141   namespace ENGINE
142   {
143     void schemaSaveState(Proc* proc,
144                          Executor* exec,
145                          const std::string& xmlSchemaFile);
146   }
147 }
148
149
150 %extend YACS::ENGINE::OutputPresetPort
151 {
152   void setDataPy(PyObject *ob)
153   {
154     std::string sss = convertPyObjectXml(self->edGetType(),ob);
155     self->setData(sss);
156   }
157 }
158
159
160 // Define the methods that can (default) or cannot (nodirector) be overloaded in Python
161 %feature("director") YACS::ENGINE::PyOptimizerAlgBase;
162 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::getTCForInProxy;
163 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::getTCForOutProxy;
164 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::initializeProxy;
165 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::startProxy;
166 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::takeDecisionProxy;
167 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::finishProxy;
168 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::hasError;
169 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::getError;
170 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::setError;
171 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::getTCForAlgoInitProxy;
172 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::getTCForAlgoResultProxy;
173 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::getAlgoResultProxy;
174 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgBase::setProc;
175
176 %feature("director") YACS::ENGINE::PyOptimizerAlgASync;
177 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::getTCForInProxy;
178 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::getTCForOutProxy;
179 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::initializeProxy;
180 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::startProxy;
181 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::takeDecisionProxy;
182 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::finishProxy;
183 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::hasError;
184 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::getError;
185 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::setError;
186 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::signalMasterAndWait;
187 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::signalSlaveAndWait;
188 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::start;
189 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::takeDecision;
190 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::run;
191 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::getTCForAlgoInitProxy;
192 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::getTCForAlgoResultProxy;
193 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::getAlgoResultProxy;
194 %feature("nodirector") YACS::ENGINE::PyOptimizerAlgASync::setProc;
195
196 %feature("director:except") {
197     if ($error != NULL) {
198         std::string errorMessage;
199         PyObject* new_stderr = YACS::ENGINE::newPyStdOut(errorMessage);
200         PySys_SetObject((char*)"stderr", new_stderr);
201         PyErr_Print();
202         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
203         Py_DECREF(new_stderr);
204         throw YACS::Exception(errorMessage);
205     }
206 }
207
208 // Warning: as PyOptimizerAlgSync is only a typedef in PyOptimizerAlg.hxx, we have to
209 // directly rename PyOptimizerAlgBase in OptimizerAlgSync. If someday PyOptimizerAlgSync
210 // becomes a real derived class, this will have to be changed.
211 %rename(OptimizerAlgSync) YACS::ENGINE::PyOptimizerAlgBase;
212 %rename(OptimizerAlgASync) YACS::ENGINE::PyOptimizerAlgASync;
213 %include "PyOptimizerAlg.hxx"