1 // Copyright (C) 2006-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "RuntimeSALOME.hxx"
20 #include "SalomePythonComponent.hxx"
21 #include "SalomePythonNode.hxx"
22 #include "PythonNode.hxx"
23 #include "PythonPorts.hxx"
24 #include "CORBANode.hxx"
25 #include "TypeCode.hxx"
30 using namespace YACS::ENGINE;
33 const char SalomePythonNode::PLACEMENT_VAR_NAME_IN_INTERP[]="__container__from__YACS__";
35 SalomePythonNode::SalomePythonNode(const SalomePythonNode& other, ComposedNode *father):ServiceInlineNode(other,father),_pyfunc(0),_context(0)
37 //Not a bug : just because port point of view this is like PythonNode.
38 _implementation = PythonNode::IMPL_NAME;
39 PyGILState_STATE gstate = PyGILState_Ensure();
40 _context=PyDict_New();
41 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
44 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
45 PyGILState_Release(gstate);
46 throw Exception(msg.str());
48 PyGILState_Release(gstate);
51 SalomePythonNode::SalomePythonNode(const std::string& name): ServiceInlineNode(name),_pyfunc(0)
54 //Not a bug : just because port point of view this is like PythonNode.
55 _implementation = PythonNode::IMPL_NAME;
56 PyGILState_STATE gstate = PyGILState_Ensure();
57 _context=PyDict_New();
58 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
61 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
62 PyGILState_Release(gstate);
63 throw Exception(msg.str());
65 PyGILState_Release(gstate);
68 void SalomePythonNode::load()
70 ServiceInlineNode::load();
71 cerr << "---------------SalomePythonNode::load function---------------" << endl;
72 list<OutputPort *>::iterator iter;
73 string value2Export=((SalomePythonComponent*)_component)->getStringValueToExportInInterp();
74 PyObject* ob=PyString_FromString(value2Export.c_str());
75 PyDict_SetItemString(_context,PLACEMENT_VAR_NAME_IN_INTERP,ob);
76 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
78 OutputPyPort *p=(OutputPyPort *)*iter;
79 cerr << "port name: " << p->getName() << endl;
80 cerr << "port kind: " << p->edGetType()->kind() << endl;
82 cerr << _script << endl;
83 PyGILState_STATE gstate = PyGILState_Ensure();
84 PyObject *res=PyRun_String(_script.c_str(),Py_file_input,_context,_context);
88 PyGILState_Release(gstate);
89 throw Exception("Error during execution");
93 _pyfunc=PyDict_GetItemString(_context,_method.c_str());
97 PyGILState_Release(gstate);
98 throw Exception("Error during execution");
100 cerr << "---------------End SalomePythonNode::load function---------------" << endl;
101 PyGILState_Release(gstate);
104 void SalomePythonNode::execute()
106 cerr << "++++++++++++++ SalomePythonNode::execute: " << getName() << " ++++++++++++++++++++" << endl;
109 if(!_pyfunc)throw Exception("SalomePythonNode badly loaded");
110 PyGILState_STATE gstate = PyGILState_Ensure();
112 cerr << "---------------SalomePythonNode::inputs---------------" << endl;
113 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
114 list<InputPort *>::iterator iter2;
115 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
117 InputPyPort *p=(InputPyPort *)*iter2;
118 cerr << "port name: " << p->getName() << endl;
119 cerr << "port kind: " << p->edGetType()->kind() << endl;
121 PyObject_Print(ob,stderr,Py_PRINT_RAW);
123 cerr << "ob refcnt: " << ob->ob_refcnt << endl;
125 PyTuple_SetItem(args,pos,ob);
126 cerr << "ob refcnt: " << ob->ob_refcnt << endl;
129 cerr << "---------------End SalomePythonNode::inputs---------------" << endl;
131 cerr << "----------------SalomePythonNode::calculation---------------" << endl;
132 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
134 PyObject_Print(args,stderr,Py_PRINT_RAW);
136 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
141 PyGILState_Release(gstate);
142 throw Exception("Error during execution");
144 cerr << "----------------End SalomePythonNode::calculation---------------" << endl;
146 cerr << "-----------------SalomePythonNode::outputs-----------------" << endl;
148 if(result == Py_None)
150 else if(PyTuple_Check(result))
151 nres=PyTuple_Size(result);
153 if(getNumberOfOutputPorts() != nres)
156 PyGILState_Release(gstate);
157 throw Exception("Number of output arguments : Mismatch between definition and execution");
161 PyObject_Print(result,stderr,Py_PRINT_RAW);
163 list<OutputPort *>::iterator iter;
166 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
168 OutputPyPort *p=(OutputPyPort *)*iter;
169 cerr << "port name: " << p->getName() << endl;
170 cerr << "port kind: " << p->edGetType()->kind() << endl;
171 cerr << "port pos : " << pos << endl;
172 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
174 cerr << "ob refcnt: " << ob->ob_refcnt << endl;
175 PyObject_Print(ob,stderr,Py_PRINT_RAW);
181 catch(ConversionException)
184 PyGILState_Release(gstate);
187 cerr << "-----------------End SalomePythonNode::outputs-----------------" << endl;
190 PyGILState_Release(gstate);
191 cerr << "++++++++++++++ End SalomePythonNode::execute: " << getName() << " ++++++++++++++++++++" << endl;
194 std::string SalomePythonNode::getKind() const
196 //This not a bug !!! Returns SalomeNode::KIND to be managed by SalomeContainer.
197 return SalomeNode::KIND;
200 Node *SalomePythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
202 return new SalomePythonNode(*this,father);
205 ServiceNode *SalomePythonNode::createNode(const std::string &name)
207 ServiceNode* node=new SalomePythonNode(name);
208 node->setComponent(_component);
212 //! Create a new node of same type with a given name
213 SalomePythonNode* SalomePythonNode::cloneNode(const std::string& name)
215 SalomePythonNode* n=new SalomePythonNode(name);
216 n->setScript(_script);
217 n->setMethod(_method);
218 list<InputPort *>::iterator iter;
219 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
221 InputPyPort *p=(InputPyPort *)*iter;
222 n->edAddInputPort(p->getName(),p->edGetType());
224 list<OutputPort *>::iterator iter2;
225 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
227 OutputPyPort *p=(OutputPyPort *)*iter2;
228 n->edAddOutputPort(p->getName(),p->edGetType());