1 // Copyright (C) 2006-2016 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, or (at your option) any later version.
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
23 #define private public
24 #define protected public
25 #include <omniORB4/CORBA.h>
26 #include <omniORB4/internal/typecode.h>
27 #include <omniORB4/internal/corbaOrb.h>
30 #include "yacsconfig.h"
31 #include "YACS_version.h"
32 #include "RuntimeSALOME.hxx"
33 #include "SALOMEDispatcher.hxx"
35 #include "TypeCode.hxx"
36 #include "WhileLoop.hxx"
37 #include "ForLoop.hxx"
38 #include "ForEachLoop.hxx"
39 #include "SalomeOptimizerLoop.hxx"
41 #include "InputPort.hxx"
42 #include "OutputPort.hxx"
43 #include "PresetPorts.hxx"
44 #include "InputDataStreamPort.hxx"
45 #include "OutputDataStreamPort.hxx"
47 #include "SalomeProc.hxx"
48 #include "PyStdout.hxx"
50 #include "SessionCataLoader.hxx"
53 #include "CORBAComponent.hxx"
54 #include "SalomeComponent.hxx"
55 #include "SalomeHPComponent.hxx"
56 #include "SalomePythonComponent.hxx"
57 #include "CppComponent.hxx"
59 #include "SalomeContainer.hxx"
60 #include "CppContainer.hxx"
61 #include "SalomeHPContainer.hxx"
64 #include "PythonNode.hxx"
65 #include "CORBANode.hxx"
66 #include "XMLNode.hxx"
67 #include "CppNode.hxx"
68 #include "PresetNode.hxx"
69 #include "OutNode.hxx"
70 #include "StudyNodes.hxx"
71 #include "SalomePythonNode.hxx"
72 #include "DistributedPythonNode.hxx"
75 #include "CORBACORBAConv.hxx"
76 #include "CORBAPythonConv.hxx"
77 #include "CORBAXMLConv.hxx"
78 #include "CORBACppConv.hxx"
79 #include "CORBANeutralConv.hxx"
81 #include "TypeConversions.hxx"
83 #include "PythonCORBAConv.hxx"
84 #include "PythonXMLConv.hxx"
85 #include "PythonCppConv.hxx"
86 #include "PythonNeutralConv.hxx"
87 #include "PythonInitConv.hxx"
90 #include "NeutralCORBAConv.hxx"
91 #include "NeutralPythonConv.hxx"
92 #include "NeutralXMLConv.hxx"
93 #include "NeutralCppConv.hxx"
94 #include "NeutralInitConv.hxx"
97 #include "CppCORBAConv.hxx"
98 #include "CppPythonConv.hxx"
99 #include "CppXMLConv.hxx"
100 #include "CppCppConv.hxx"
101 #include "CppNeutralConv.hxx"
104 #include "XMLCORBAConv.hxx"
105 #include "XMLPythonConv.hxx"
106 #include "XMLCppConv.hxx"
107 #include "XMLNeutralConv.hxx"
109 //Calcium specific ports
110 #include "CalStreamPort.hxx"
113 #include "SALOME_NamingService.hxx"
114 #include "SALOME_LifeCycleCORBA.hxx"
117 #include <libxml/parser.h>
118 #include <omniORB4/CORBA.h>
124 #include "YacsTrace.hxx"
127 using namespace YACS::ENGINE;
129 void RuntimeSALOME::setRuntime(long flags, int argc, char* argv[]) // singleton creation (not thread safe!)
131 if (! Runtime::_singleton)
133 RuntimeSALOME* r=new RuntimeSALOME(flags, argc, argv);
134 Runtime::_singleton = r;
137 DEBTRACE("RuntimeSALOME::setRuntime() done !");
140 RuntimeSALOME* YACS::ENGINE::getSALOMERuntime()
142 YASSERT(RuntimeSALOME::getSingleton());
143 return dynamic_cast< RuntimeSALOME* >(RuntimeSALOME::getSingleton());
147 * Singleton creation, initialize converter map
150 RuntimeSALOME::RuntimeSALOME()
155 void RuntimeSALOME::initBuiltins()
157 //Fill the builtin catalog with nodes specific to the runtime
158 std::map<std::string,TypeCode*>& typeMap=_builtinCatalog->_typeMap;
159 std::map<std::string,Node*>& nodeMap=_builtinCatalog->_nodeMap;
160 std::map<std::string,ComposedNode*>& composednodeMap=_builtinCatalog->_composednodeMap;
161 std::map<std::string,ComponentDefinition*>& componentMap=_builtinCatalog->_componentMap;
162 nodeMap["PyFunction"]=new PyFuncNode("PyFunction");
163 nodeMap["PyScript"]=new PythonNode("PyScript");
164 nodeMap["CORBANode"]=new CORBANode("CORBANode");
165 nodeMap["XmlNode"]=new XmlNode("XmlNode");
166 nodeMap["SalomeNode"]=new SalomeNode("SalomeNode");
167 nodeMap["CppNode"]=new CppNode("CppNode");
168 nodeMap["SalomePythonNode"]=new SalomePythonNode("SalomePythonNode");
169 nodeMap["PresetNode"]=new PresetNode("PresetNode");
170 nodeMap["OutNode"]=new OutNode("OutNode");
171 nodeMap["StudyInNode"]=new StudyInNode("StudyInNode");
172 nodeMap["StudyOutNode"]=new StudyOutNode("StudyOutNode");
173 composednodeMap["OptimizerLoop"]=createOptimizerLoop("OptimizerLoop","","",true);
174 typeMap["dblevec"]= createSequenceTc("dblevec","dblevec",_tc_double);
175 typeMap["intvec"]= createSequenceTc("intvec","intvec",_tc_int);
176 typeMap["stringvec"]= createSequenceTc("stringvec","stringvec",_tc_string);
177 typeMap["boolvec"]= createSequenceTc("boolvec","boolvec",_tc_bool);
178 typeMap["seqdblevec"]= createSequenceTc("seqdblevec","seqdblevec",typeMap["dblevec"]);
179 typeMap["seqintvec"]= createSequenceTc("seqintvec","seqintvec",typeMap["intvec"]);
180 typeMap["seqstringvec"]= createSequenceTc("seqstringvec","seqstringvec",typeMap["stringvec"]);
181 typeMap["seqboolvec"]= createSequenceTc("seqboolvec","seqboolvec",typeMap["boolvec"]);
182 std::list<TypeCodeObjref *> ltc;
183 typeMap["pyobj"]= createInterfaceTc("python:obj:1.0","pyobj",ltc);
184 typeMap["seqpyobj"]= createSequenceTc("seqpyobj","seqpyobj",typeMap["pyobj"]);
185 composednodeMap["Bloc"]=createBloc("Bloc");
186 composednodeMap["Switch"]=createSwitch("Switch");
187 composednodeMap["WhileLoop"]=createWhileLoop("WhileLoop");
188 composednodeMap["ForLoop"]=createForLoop("ForLoop");
189 composednodeMap["ForEachLoop_double"]=createForEachLoop("ForEachLoop_double",Runtime::_tc_double);
190 composednodeMap["ForEachLoop_string"]=createForEachLoop("ForEachLoop_string",Runtime::_tc_string);
191 composednodeMap["ForEachLoop_int"]=createForEachLoop("ForEachLoop_int",Runtime::_tc_int);
192 composednodeMap["ForEachLoop_bool"]=createForEachLoop("ForEachLoop_bool",Runtime::_tc_bool);
193 composednodeMap["ForEachLoop_pyobj"]=createForEachLoop("ForEachLoop_pyobj",typeMap["pyobj"]);;
194 ENGINE::TypeCodeStruct *t = createStructTc("","Engines/dataref");
195 t->addMember("ref",_tc_string);
196 typeMap["dataref"]= t;
199 RuntimeSALOME::RuntimeSALOME(long flags, int argc, char* argv[])
201 // If all flags (apart the IsPyExt flags) are unset, force them to true
202 if ((flags - flags & RuntimeSALOME::IsPyExt) == 0)
203 flags += RuntimeSALOME::UseCorba + RuntimeSALOME::UsePython
204 + RuntimeSALOME::UseCpp + RuntimeSALOME::UseXml;
206 // Salome Nodes implies Corba Nodes
207 if (flags & RuntimeSALOME::UseSalome)
208 flags |= RuntimeSALOME::UseCorba;
210 // Corba Nodes implies Python Nodes
211 if (flags & RuntimeSALOME::UseCorba)
212 flags |= RuntimeSALOME::UsePython;
214 _useCorba = flags & RuntimeSALOME::UseCorba;
215 _usePython = flags & RuntimeSALOME::UsePython;
216 _useCpp = flags & RuntimeSALOME::UseCpp;
217 _useXml = flags & RuntimeSALOME::UseXml;
222 if (_useCpp) _setOfImplementation.insert(CppNode::IMPL_NAME);
223 if (_usePython) _setOfImplementation.insert(PythonNode::IMPL_NAME);
224 if (_useCorba) _setOfImplementation.insert(CORBANode::IMPL_NAME);
225 if (_useXml) _setOfImplementation.insert(XmlNode::IMPL_NAME);
226 init(flags, argc, argv);
229 RuntimeSALOME::~RuntimeSALOME()
231 DEBTRACE("RuntimeSALOME::~RuntimeSALOME");
232 // destroy catalog loader prototypes
233 std::map<std::string, CatalogLoader*>::const_iterator pt;
234 for(pt=_catalogLoaderFactoryMap.begin();pt!=_catalogLoaderFactoryMap.end();pt++)
240 //! CORBA and Python initialization
242 * \param flags contains several bits
243 * bit0 (ispyext) true when method is called from Python
244 * (Python initialization must not be done!)
245 * bit1 (UsePython) true if python nodes are needed
246 * bit1 (UseCorba) true if CORBA nodes are needed
247 * bit1 (UseXml) true if python nodes are needed
248 * bit1 (UseCpp) true if C++ nodes are needed
249 * bit1 (UseSalome) true if Salome nodes are needed
250 * \param argc number of command line arguments (used to initialize the Python interpreter)
251 * \param argv command line arguments (used to initialize the Python interpreter)
255 void RuntimeSALOME::init(long flags, int argc, char* argv[])
257 bool ispyext = flags & RuntimeSALOME::IsPyExt;
260 PortableServer::POA_var root_poa;
261 PortableServer::POAManager_var pman;
262 CORBA::Object_var obj;
263 int nbargs = 0; char **args = 0;
264 _orb = CORBA::ORB_init (nbargs, args);
265 obj = _orb->resolve_initial_references("RootPOA");
266 root_poa = PortableServer::POA::_narrow(obj);
267 pman = root_poa->the_POAManager();
271 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
273 obj = _orb->resolve_initial_references("DynAnyFactory");
274 _dynFactory = DynamicAny::DynAnyFactory::_narrow(obj);
279 DEBTRACE("RuntimeSALOME::init, is python extension = " << ispyext);
281 // Initialize Python interpreter in embedded mode
282 if (!Py_IsInitialized())
284 #if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
287 Py_InitializeEx(0); // do not install signal handlers
289 if (argc > 0 && argv != NULL)
290 PySys_SetArgv(argc, argv);
295 char defaultName[] = "SALOME_YACS_RUNTIME";
296 pyArgv[0] = defaultName;
297 PySys_SetArgv(pyArgc, pyArgv);
299 PyEval_InitThreads(); /* Create (and acquire) the interpreter lock (for threads)*/
300 PyEval_SaveThread(); /* Release the thread state */
301 //here we do not have the Global Interpreter Lock
304 PyObject *mainmod,*pyapi,*res ;
306 PyGILState_STATE gstate;
307 gstate = PyGILState_Ensure(); // acquire the Global Interpreter Lock
309 mainmod = PyImport_AddModule("__main__");
310 globals = PyModule_GetDict(mainmod);
311 /* globals is a borrowed reference */
313 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
315 PyObject *bimod = PyImport_ImportModule("__builtin__");
316 if (bimod == NULL || PyDict_SetItemString(globals, "__builtins__", bimod) != 0)
317 Py_FatalError("can't add __builtins__ to __main__");
321 _bltins = PyEval_GetBuiltins(); /* borrowed ref */
327 _omnipy = PyImport_ImportModule((char*)"_omnipy");
331 PyErr_SetString(PyExc_ImportError, (char*)"Cannot import _omnipy");
334 pyapi = PyObject_GetAttrString(_omnipy, (char*)"API");
339 _api = (omniORBpyAPI*)PyCObject_AsVoidPtr(pyapi);
342 res=PyRun_String("\n"
343 "from math import *\n"
345 "sys.path.insert(0,'.')\n"
346 "from omniORB import CORBA\n"
347 "from omniORB import any\n"
348 "orb = CORBA.ORB_init([], CORBA.ORB_ID)\n"
349 "#print sys.getrefcount(orb)\n"
355 Py_file_input,globals,globals );
363 _pyorb = PyDict_GetItemString(globals,"orb");
364 /* PyDict_GetItemString returns a borrowed reference. There is no need to decref _pyorb */
367 pyany = PyDict_GetItemString(globals,"any");
368 /* PyDict_GetItemString returns a borrowed reference. There is no need to decref pyany */
371 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
375 PyGILState_Release(gstate); // Release the Global Interpreter Lock
379 // initialize the catalogLoaderFactory map with the session one
380 _catalogLoaderFactoryMap["session"]=new SessionCataLoader;
384 void RuntimeSALOME::fini()
388 PyGILState_STATE gstate = PyGILState_Ensure();
390 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
392 PyObject *mainmod, *globals;
393 mainmod = PyImport_AddModule("__main__");
394 globals = PyModule_GetDict(mainmod);
398 res=PyRun_String("orb.destroy()\n"
400 Py_file_input,globals,globals );
406 std::map<std::string,Node*>& nodeMap=_builtinCatalog->_nodeMap;
407 delete nodeMap["PyFunction"];
408 delete nodeMap["PyScript"];
409 delete nodeMap["SalomePythonNode"];
410 nodeMap.erase("PyFunction");
411 nodeMap.erase("PyScript");
412 nodeMap.erase("SalomePythonNode");
416 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
424 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
431 std::string RuntimeSALOME::getVersion() const
433 #ifdef YACS_DEVELOPMENT
434 return CORBA::string_dup(YACS_VERSION_STR"dev");
436 return CORBA::string_dup(YACS_VERSION_STR);
440 Proc* RuntimeSALOME::createProc(const std::string& name)
442 return new SalomeProc(name);
445 TypeCode * RuntimeSALOME::createInterfaceTc(const std::string& id, const std::string& name,
446 std::list<TypeCodeObjref *> ltc)
449 if(id == "") myName = "IDL:" + name + ":1.0";
451 return TypeCode::interfaceTc(myName.c_str(),name.c_str(),ltc);
454 TypeCode * RuntimeSALOME::createSequenceTc(const std::string& id,
455 const std::string& name,
458 return TypeCode::sequenceTc(id.c_str(),name.c_str(),content);
461 TypeCodeStruct * RuntimeSALOME::createStructTc(const std::string& id, const std::string& name)
464 if(id == "") myName = "IDL:" + name + ":1.0";
466 return (TypeCodeStruct *)TypeCode::structTc(myName.c_str(),name.c_str());
469 Bloc* RuntimeSALOME::createBloc(const std::string& name)
471 return new Bloc(name);
474 WhileLoop* RuntimeSALOME::createWhileLoop(const std::string& name)
476 return new WhileLoop(name);
479 ForLoop* RuntimeSALOME::createForLoop(const std::string& name)
481 return new ForLoop(name);
484 OptimizerLoop* RuntimeSALOME::createOptimizerLoop(const std::string& name,const std::string& algLib,const std::string& factoryName,
485 bool algInitOnFile, const std::string& kind, Proc * procForTypes)
487 OptimizerLoop * ol = (kind == "base") ? new OptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes) :
488 new SalomeOptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes);
489 ol->edGetNbOfBranchesPort()->edInit(1);
493 DataNode* RuntimeSALOME::createInDataNode(const std::string& kind,const std::string& name)
498 node = new PresetNode(name);
501 else if(kind == "study" )
503 return new StudyInNode(name);
505 std::string msg="DataNode kind ("+kind+") unknown";
506 throw Exception(msg);
509 DataNode* RuntimeSALOME::createOutDataNode(const std::string& kind,const std::string& name)
513 return new OutNode(name);
515 else if(kind == "study" )
517 return new StudyOutNode(name);
520 std::string msg="OutDataNode kind ("+kind+") unknown";
521 throw Exception(msg);
524 InlineFuncNode* RuntimeSALOME::createFuncNode(const std::string& kind,const std::string& name)
526 InlineFuncNode* node;
527 if(kind == "" || kind == SalomeNode::KIND || kind == PythonNode::KIND)
529 node = new PyFuncNode(name);
532 if(kind == DistributedPythonNode::KIND)
533 return new DistributedPythonNode(name);
534 std::string msg="FuncNode kind ("+kind+") unknown";
535 throw Exception(msg);
538 InlineNode* RuntimeSALOME::createScriptNode(const std::string& kind,const std::string& name)
541 if(kind == "" || kind == SalomeNode::KIND || kind == PythonNode::KIND)
543 node = new PythonNode(name);
546 std::string msg="ScriptNode kind ("+kind+") unknown";
547 throw Exception(msg);
550 ServiceNode* RuntimeSALOME::createRefNode(const std::string& kind,const std::string& name)
553 if(kind == "" || kind == SalomeNode::KIND || kind == CORBANode::KIND)
555 node = new CORBANode(name);
558 else if(kind == XmlNode::KIND)
560 node = new XmlNode(name);
563 std::string msg="RefNode kind ("+kind+") unknown";
564 throw Exception(msg);
567 ServiceNode* RuntimeSALOME::createCompoNode(const std::string& kind,const std::string& name)
570 if(kind == "" || kind == SalomeNode::KIND )
572 node=new SalomeNode(name);
575 else if (kind == CppNode::KIND)
577 node = new CppNode(name);
580 std::string msg="CompoNode kind ("+kind+") unknown";
581 throw Exception(msg);
584 ServiceInlineNode *RuntimeSALOME::createSInlineNode(const std::string& kind, const std::string& name)
586 if(kind == "" || kind == SalomeNode::KIND )
587 return new SalomePythonNode(name);
588 std::string msg="CompoNode kind ("+kind+") unknown";
589 throw Exception(msg);
592 ComponentInstance* RuntimeSALOME::createComponentInstance(const std::string& name,
593 const std::string& kind)
595 ComponentInstance* compo;
596 if(kind == "" || kind == SalomeComponent::KIND)
597 return new SalomeComponent(name);
598 else if(kind == CORBAComponent::KIND)
599 return new CORBAComponent(name);
600 else if(kind == SalomePythonComponent::KIND)
601 return new SalomePythonComponent(name);
602 else if (kind == CppComponent::KIND)
603 return new CppComponent(name);
604 else if (kind == SalomeHPComponent::KIND)
605 return new SalomeHPComponent(name);
606 std::string msg="Component Instance kind ("+kind+") unknown";
607 throw Exception(msg);
610 Container *RuntimeSALOME::createContainer(const std::string& kind)
612 if(kind == "" || kind == SalomeContainer::KIND)
613 return new SalomeContainer;
614 if(kind==SalomeHPContainer::KIND)
615 return new SalomeHPContainer;
616 else if (kind == CppContainer::KIND)
617 return new CppContainer;
618 std::string msg="Container kind ("+kind+") unknown";
619 throw Exception(msg);
622 InputPort * RuntimeSALOME::createInputPort(const std::string& name,
623 const std::string& impl,
627 if(impl == CppNode::IMPL_NAME)
629 return new InputCppPort(name, node, type);
631 else if(impl == PythonNode::IMPL_NAME)
633 return new InputPyPort(name, node, type);
635 else if(impl == CORBANode::IMPL_NAME)
637 return new InputCorbaPort(name, node, type);
639 else if(impl == XmlNode::IMPL_NAME)
641 return new InputXmlPort(name, node, type);
646 msg << "Cannot create " << impl << " InputPort" ;
647 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
648 throw Exception(msg.str());
652 OutputPort * RuntimeSALOME::createOutputPort(const std::string& name,
653 const std::string& impl,
657 if(impl == CppNode::IMPL_NAME)
659 return new OutputCppPort(name, node, type);
661 else if(impl == PythonNode::IMPL_NAME)
663 return new OutputPyPort(name, node, type);
665 else if(impl == CORBANode::IMPL_NAME)
667 return new OutputCorbaPort(name, node, type);
669 else if(impl == XmlNode::IMPL_NAME)
671 return new OutputXmlPort(name, node, type);
676 msg << "Cannot create " << impl << " OutputPort" ;
677 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
678 throw Exception(msg.str());
682 InputDataStreamPort* RuntimeSALOME::createInputDataStreamPort(const std::string& name,
683 Node *node,TypeCode *type)
685 DEBTRACE("createInputDataStreamPort: " << name << " " << type->shortName());
686 if(type->kind() == Objref && std::string(type->shortName(),7) == "CALCIUM")
688 return new InputCalStreamPort(name,node,type);
692 return new InputDataStreamPort(name,node,type);
696 OutputDataStreamPort* RuntimeSALOME::createOutputDataStreamPort(const std::string& name,
697 Node *node,TypeCode *type)
699 DEBTRACE("createOutputDataStreamPort: " << name << " " << type->shortName());
700 if(type->kind() == Objref && std::string(type->shortName(),7) == "CALCIUM")
702 return new OutputCalStreamPort(name,node,type);
706 return new OutputDataStreamPort(name,node,type);
710 //! Main adapter function : adapt an InputPort to be able to connect it to an OutputPort with a possible different implementation
712 * \param source : InputPort to be adapted
713 * \param impl : new implementation (C++, python, CORBA, XML, Neutral)
714 * \param type : data type provided by the InputPort
715 * \param init : indicates if the adapted InputPort will be used for initialization (value true) or not (value false)
717 * \return : adapted InputPort
719 InputPort* RuntimeSALOME::adapt(InputPort* source,
720 const std::string& impl,
721 TypeCode * type,bool init) throw (ConversionException)
723 string imp_source=source->getNode()->getImplementation();
724 if(imp_source == PythonNode::IMPL_NAME)
726 return adapt((InputPyPort*)source,impl,type,init);
728 else if(imp_source == CppNode::IMPL_NAME)
730 return adapt((InputCppPort*)source,impl,type,init);
732 else if(imp_source == CORBANode::IMPL_NAME)
734 return adapt((InputCorbaPort*)source,impl,type,init);
736 else if(imp_source == XmlNode::IMPL_NAME)
738 return adapt((InputXmlPort*)source,impl,type,init);
740 else if(imp_source == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
742 return adaptNeutral(source,impl,type,init);
747 msg << "Cannot adapt " << imp_source << " InputPort to " << impl;
748 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
749 throw ConversionException(msg.str());
753 //! Adapter function for InPropertyPort
755 * \param source : InPropertyPort to be adapted
756 * \param impl : new implementation (C++, python, CORBA, XML, Neutral)
757 * \param type : data type provided by the InPropertyPort
758 * \param init : indicates if the adapted InPropertyPort will be used for initialization (value true) or not (value false)
760 * \return : adapted InputPort
762 InputPort* RuntimeSALOME::adapt(InPropertyPort* source,
763 const std::string& impl,
764 TypeCode * type,bool init) throw (ConversionException)
766 return adaptNeutral((InputPort *)source,impl,type,init);
769 //! Adapt a Neutral input port to a Corba output port
771 * \param inport : Neutral input port to adapt to Corba type type
772 * \param type : output port type
773 * \return an adaptated input port of type InputCorbaPort
775 InputPort* RuntimeSALOME::adaptNeutralToCorba(InputPort* inport,
776 TypeCode * type) throw (ConversionException)
778 // BEWARE : using the generic check
779 if(inport->edGetType()->isAdaptable(type))
781 //the output data is convertible to inport type
782 return new CorbaNeutral(inport);
784 //non convertible type
786 msg << "Cannot connect Corba output port with type: " << type->id() ;
787 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
789 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
791 throw ConversionException(msg.str());
794 //! Adapt a Neutral input port to a Python output port
796 * \param inport : input port to adapt to Python type type
797 * \param type : output port type
798 * \return an adaptated input port of type InputPyPort
800 InputPort* RuntimeSALOME::adaptNeutralToPython(InputPort* inport,
801 TypeCode * type) throw (ConversionException)
803 // BEWARE : using the generic check
804 if(inport->edGetType()->isAdaptable(type))
807 return new PyNeutral(inport);
809 //last chance : an py output that is seq[objref] can be connected to a neutral input objref (P13268)
810 else if(type->kind()==Sequence && type->contentType()->kind()==Objref && inport->edGetType()->kind()==Objref)
812 return new PyNeutral(inport);
814 //non convertible type
816 msg << "Cannot connect Python output port with type: " << type->id() ;
817 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
819 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
821 throw ConversionException(msg.str());
824 //! Adapt a Neutral input port to a Xml output port
826 * \param inport : input port to adapt to Xml type type
827 * \param type : output port type
828 * \return an input port of type InputXmlPort
830 InputPort* RuntimeSALOME::adaptNeutralToXml(InputPort* inport,
831 TypeCode * type) throw (ConversionException)
833 // BEWARE : using the generic check
834 if(inport->edGetType()->isAdaptable(type))
837 return new XmlNeutral(inport);
839 //non convertible type
841 msg << "Cannot connect Xml output port with type: " << type->id() ;
842 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
844 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
846 throw ConversionException(msg.str());
849 //! Adapt a Neutral input port to a C++ output port
851 * \param inport : input port to adapt to C++ type type
852 * \param type : output port type
853 * \return an input port of type InputCppPort
855 InputPort* RuntimeSALOME::adaptNeutralToCpp(InputPort* inport,
856 TypeCode * type) throw (ConversionException)
858 DEBTRACE("RuntimeSALOME::adaptNeutralToCpp(InputPort* inport" );
859 if(isAdaptableNeutralCpp(type,inport->edGetType()))
862 return new CppNeutral(inport);
864 //non convertible type
866 msg << "Cannot connect Cpp output port with type: " << type->id() ;
867 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
869 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
871 throw ConversionException(msg.str());
874 //! Adapt a Neutral input port to connect it to an output port with a given implementation
876 * \param source : Neutral input port to adapt to implementation impl and type type
877 * \param impl : output port implementation (C++, Python, Corba, Xml or Neutral)
878 * \param type : output port supported type
879 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
880 * \return the adaptated port
882 InputPort* RuntimeSALOME::adaptNeutral(InputPort* source,
883 const std::string& impl,
884 TypeCode * type,bool init) throw (ConversionException)
886 if(impl == CppNode::IMPL_NAME)
888 return adaptNeutralToCpp(source,type);
890 else if(impl == PythonNode::IMPL_NAME)
892 return adaptNeutralToPython(source,type);
894 else if(impl == CORBANode::IMPL_NAME)
896 return adaptNeutralToCorba(source,type);
898 else if(impl == XmlNode::IMPL_NAME )
900 return adaptNeutralToXml(source,type);
902 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
905 return new NeutralInit(source);
907 return new ProxyPort(source);
910 msg << "Cannot connect InputPort : unknown implementation " << impl;
911 msg << " (" <<__FILE__ << ":" <<__LINE__ << ")";
912 throw ConversionException(msg.str());
915 //! Adapt a XML input port to connect it to a CORBA output port
917 * \param inport : input port to adapt to CORBA type type
918 * \param type : type supported by output port
919 * \return an adaptator port of type InputCorbaPort
922 InputPort* RuntimeSALOME::adaptXmlToCorba(InputXmlPort* inport,
923 TypeCode * type) throw (ConversionException)
925 if(isAdaptableXmlCorba(type,inport->edGetType()))
927 //output type is convertible to input type
928 return new CorbaXml(inport);
930 //output type is not convertible
932 msg << "Cannot connect Corba output port with type: " << type->id() ;
933 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
935 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
937 throw ConversionException(msg.str());
940 //! Adapt a XML input port to a Python output port
942 * \param inport : input port to adapt to Python type type
943 * \param type : output port type
944 * \return an adaptated input port of type InputPyPort
946 InputPort* RuntimeSALOME::adaptXmlToPython(InputXmlPort* inport,
947 TypeCode * type) throw (ConversionException)
949 if(inport->edGetType()->isAdaptable(type))
951 //the output data is convertible to inport type
952 return new PyXml(inport);
954 //non convertible type
956 msg << "Cannot connect Python output port with type: " << type->id() ;
957 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
959 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
961 throw ConversionException(msg.str());
964 //! Adapt a XML input port to a C++ output port
966 * \param inport : input port to adapt to C++ type type
967 * \param type : output port type
968 * \return an adaptated input port of type InputPyPort
970 InputPort* RuntimeSALOME::adaptXmlToCpp(InputXmlPort* inport,
971 TypeCode * type) throw (ConversionException)
973 DEBTRACE("RuntimeSALOME::adaptXmlToCpp(InputPort* inport" );
974 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
975 if(type->isAdaptable(inport->edGetType()))
977 //the output data is convertible to inport type
978 return new CppXml(inport);
980 //non convertible type
982 msg << "Cannot connect Cpp output port with type: " << type->id() ;
983 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
985 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
987 throw ConversionException(msg.str());
990 //! Adapt a XML input port to a Neutral output port
992 * \param inport : input port to adapt to Neutral type type
993 * \param type : output port type
994 * \return an adaptated input port of type Neutralxxxx
996 InputPort* RuntimeSALOME::adaptXmlToNeutral(InputXmlPort* inport,
997 TypeCode * type) throw (ConversionException)
999 if(inport->edGetType()->isAdaptable(type))
1001 //the output data is convertible to inport type
1002 return new NeutralXml(inport);
1004 //non convertible type
1006 msg << "Cannot connect Xml InputPort to OutputNeutralPort : " ;
1007 msg << "(" <<__FILE__ << ":" <<__LINE__<< ")";
1008 throw ConversionException(msg.str());
1011 //! Adapt a XML input port to a Xml output port
1013 * \param inport : input port to adapt to Xml type type
1014 * \param type : output port type
1015 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1016 * \return an adaptated input port of type Xmlxxxx
1018 InputPort* RuntimeSALOME::adaptXmlToXml(InputXmlPort* inport,
1019 TypeCode * type,bool init) throw (ConversionException)
1022 return new ProxyPort(inport);
1024 if(inport->edGetType()->isAdaptable(type))
1025 return new ProxyPort(inport);
1028 msg << "Cannot connect Xml output port with type: " << type->id() ;
1029 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1031 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1033 throw ConversionException(msg.str());
1036 //! Adapt an Xml input port to an output port which implementation is given by impl
1038 * \param source : input port to adapt to implementation impl and type type
1039 * \param impl : output port implementation (C++, Python or Corba)
1040 * \param type : output port supported type
1041 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1042 * \return the adaptated port
1045 InputPort* RuntimeSALOME::adapt(InputXmlPort* source,
1046 const std::string& impl,
1047 TypeCode * type,bool init) throw (ConversionException)
1049 if(impl == CORBANode::IMPL_NAME)
1051 return adaptXmlToCorba(source,type);
1053 else if(impl == PythonNode::IMPL_NAME)
1055 return adaptXmlToPython(source,type);
1057 else if(impl == CppNode::IMPL_NAME)
1059 return adaptXmlToCpp(source,type);
1061 else if(impl == XmlNode::IMPL_NAME )
1063 return adaptXmlToXml(source,type,init);
1065 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1067 return adaptXmlToNeutral(source,type);
1072 msg << "Cannot connect InputXmlPort to " << impl << " implementation";
1073 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1074 throw ConversionException(msg.str());
1079 //! Adapt a CORBA input port to a CORBA output port
1081 * \param inport : input port to adapt to CORBA outport data type
1082 * \param type : outport data type
1083 * \return an adaptator port of type InputCORBAPort
1085 InputPort* RuntimeSALOME::adaptCorbaToCorba(InputCorbaPort* inport,
1086 TypeCode * type) throw (ConversionException)
1088 if(type->isA(inport->edGetType()))
1090 //types are compatible : no conversion
1091 //outport data type is more specific than inport required type
1092 //so the inport can be used safely
1093 return new ProxyPort(inport);
1095 else if(isAdaptableCorbaCorba(type,inport->edGetType()))
1097 //ouport data can be converted to inport data type
1098 return new CorbaCorba(inport);
1100 //outport data can not be converted
1102 msg << "Cannot connect Corba output port with type: " << type->id() ;
1103 msg << " to CORBA input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1105 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1107 throw ConversionException(msg.str());
1110 //! Adapt a CORBA input port to a Python output port
1112 * \param inport : input port to adapt to Python type type
1113 * \param type : outport data type
1114 * \return an adaptator port of type InputPyPort
1117 InputPort* RuntimeSALOME::adaptCorbaToPython(InputCorbaPort* inport,
1118 TypeCode * type) throw (ConversionException)
1120 if(inport->edGetType()->kind() == Double)
1122 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaDouble(inport);
1124 else if(inport->edGetType()->kind() == Int)
1126 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaInt(inport);
1128 else if(inport->edGetType()->kind() == String)
1130 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaString(inport);
1132 else if(inport->edGetType()->kind() == Bool)
1134 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaBool(inport);
1136 else if(inport->edGetType()->kind() == Objref )
1138 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1140 return new PyCorbaObjref(inport);
1145 msg << "Cannot connect Python output port with type: " << type->id() ;
1146 msg << " to CORBA input port " << inport->getName() << " with incompatible objref type: " << inport->edGetType()->id();
1147 msg << " (" << __FILE__ << ":" <<__LINE__ << ")";
1148 throw ConversionException(msg.str());
1151 else if(inport->edGetType()->kind() == Sequence)
1153 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1155 return new PyCorbaSequence(inport);
1160 msg << "Cannot convert this sequence type " ;
1161 msg << __FILE__ << ":" <<__LINE__;
1162 throw ConversionException(msg.str());
1165 else if(inport->edGetType()->kind() == YACS::ENGINE::Struct)
1167 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1169 return new PyCorbaStruct(inport);
1174 msg << "Cannot convert this struct type " << type->id() << " to " << inport->edGetType()->id();
1175 msg << __FILE__ << ":" <<__LINE__;
1176 throw ConversionException(msg.str());
1179 // Adaptation not possible
1181 msg << "Cannot connect Python output port with type: " << type->id() ;
1182 msg << " to CORBA input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1184 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1186 throw ConversionException(msg.str());
1189 //! Adapt a CORBA input port to connect it to a XML output port
1191 * \param inport : input port to adapt to Xml type type
1192 * \param type : type supported by output port
1193 * \return an adaptator port of type InputXmlPort
1196 InputPort* RuntimeSALOME::adaptCorbaToXml(InputCorbaPort* inport,
1197 TypeCode * type) throw (ConversionException)
1199 // BEWARE : using the generic check
1200 if(inport->edGetType()->isAdaptable(type))
1202 //output type is convertible to input type
1203 return new XmlCorba(inport);
1205 //output type is not convertible
1207 msg << "Cannot connect Xml output port with type: " << type->id() ;
1208 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1210 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1212 throw ConversionException(msg.str());
1215 //! Adapt a CORBA input port to a C++ output port
1217 * \param inport : input port to adapt to C++ type type
1218 * \param type : outport data type
1219 * \return an adaptator port of type InputCPPPort
1222 InputPort* RuntimeSALOME::adaptCorbaToCpp(InputCorbaPort* inport,
1223 TypeCode * type) throw (ConversionException)
1225 DEBTRACE("RuntimeSALOME::adaptCorbaToCpp(InputCorbaPort* inport" );
1226 if(isAdaptableCorbaCpp(type,inport->edGetType()))
1228 //output type is convertible to input type
1229 return new CppCorba(inport);
1231 //output type is not convertible
1233 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1234 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1236 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1238 throw ConversionException(msg.str());
1241 //! Adapt a CORBA input port to a neutral data
1243 * \param inport : InputPort to adapt to Neutral type type
1244 * \param type : outport data type
1245 * \return an adaptator port of type Neutralxxxx
1248 InputPort* RuntimeSALOME::adaptCorbaToNeutral(InputCorbaPort* inport,
1249 TypeCode * type) throw (ConversionException)
1251 if(inport->edGetType()->kind() == Double)
1253 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaDouble(inport);
1255 else if(inport->edGetType()->kind() == Int)
1257 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaInt(inport);
1259 else if(inport->edGetType()->kind() == String)
1261 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaString(inport);
1263 else if(inport->edGetType()->kind() == Bool)
1265 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaBool(inport);
1267 else if(inport->edGetType()->kind() == Objref)
1269 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaObjref(inport);
1271 else if(inport->edGetType()->kind() == Sequence)
1273 if(isAdaptableCorbaNeutral(type,inport->edGetType()))
1274 return new NeutralCorbaSequence(inport);
1278 msg << "Cannot convert this sequence type " ;
1279 msg << __FILE__ << ":" <<__LINE__;
1280 throw ConversionException(msg.str());
1283 else if(inport->edGetType()->kind() == Struct)
1285 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaStruct(inport);
1288 // Adaptation not possible
1290 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1291 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1293 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1295 throw ConversionException(msg.str());
1298 //! Adapt a CORBA input port to an output which implementation and type are given by impl and type
1300 * \param source : input port to adapt to implementation impl and type type
1301 * \param impl : output port implementation (C++, Python or Corba)
1302 * \param type : outport data type
1303 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1304 * \return an adaptator port which type depends on impl
1307 InputPort* RuntimeSALOME::adapt(InputCorbaPort* source,
1308 const std::string& impl,
1309 TypeCode * type,bool init) throw (ConversionException)
1311 if(impl == CppNode::IMPL_NAME)
1313 return adaptCorbaToCpp(source,type);
1315 else if(impl == PythonNode::IMPL_NAME)
1317 return adaptCorbaToPython(source,type);
1319 else if(impl == CORBANode::IMPL_NAME)
1322 return adaptCorbaToCorba(source,type);
1324 return adaptCorbaToCorba(source,type);
1326 else if(impl == XmlNode::IMPL_NAME )
1328 return adaptCorbaToXml(source,type);
1330 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1332 return adaptCorbaToNeutral(source,type);
1337 msg << "Cannot connect InputCorbaPort : unknown implementation " ;
1338 msg << __FILE__ << ":" <<__LINE__;
1339 throw ConversionException(msg.str());
1343 //! Adapt a Python input port to a Python output port
1345 * No need to make conversion or cast.
1346 * Only check, it's possible.
1347 * \param inport : InputPort to adapt to Python type type
1348 * \param type : outport data type
1349 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1350 * \return an adaptator port of type InputPyPort
1353 InputPort* RuntimeSALOME::adaptPythonToPython(InputPyPort* inport,
1354 TypeCode * type,bool init) throw (ConversionException)
1357 return new PyInit(inport);
1359 if(isAdaptablePyObjectPyObject(type,inport->edGetType()))
1361 //output data is convertible to input type
1362 //With python, no need to convert. Conversion will be done automatically
1363 //by the interpreter
1364 return new ProxyPort(inport);
1366 //output data is not convertible to input type
1368 msg << "Cannot connect Python output port with type: " << type->id() ;
1369 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1371 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1373 throw ConversionException(msg.str());
1376 //! Adapt a Python input port to a C++ output port
1378 * \param inport : InputPort to adapt to C++ type type
1379 * \param type : outport data type
1380 * \return an adaptator port of C++ type (InputCppPort)
1383 InputPort* RuntimeSALOME::adaptPythonToCpp(InputPyPort* inport,
1384 TypeCode * type) throw (ConversionException)
1386 DEBTRACE("RuntimeSALOME::adaptPythonToCpp(InputPyPort* inport" );
1387 if(isAdaptablePyObjectCpp(type,inport->edGetType()))
1389 //output type is convertible to input type
1390 return new CppPy(inport);
1392 //output type is not convertible
1394 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1395 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1397 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1399 throw ConversionException(msg.str());
1402 //! Adapt a Python input port to a Neutral data port
1404 * \param inport : InputPort to adapt to Neutral type type
1405 * \param type : outport data type
1406 * \return an adaptator port of Neutral type (Neutralxxxx)
1409 InputPort* RuntimeSALOME::adaptPythonToNeutral(InputPyPort* inport,
1410 TypeCode * type) throw (ConversionException)
1412 if(inport->edGetType()->kind() == Double)
1414 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyDouble(inport);
1416 else if(inport->edGetType()->kind() == Int)
1418 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyInt(inport);
1420 else if(inport->edGetType()->kind() == String)
1422 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyString(inport);
1424 else if(inport->edGetType()->kind() == Bool)
1426 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyBool(inport);
1428 else if(inport->edGetType()->kind() == Objref)
1430 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyObjref(inport);
1432 else if(inport->edGetType()->kind() == Sequence)
1434 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))
1435 return new NeutralPySequence(inport);
1439 msg << "Cannot convert this sequence type " ;
1440 msg << __FILE__ << ":" <<__LINE__;
1441 throw ConversionException(msg.str());
1444 else if(inport->edGetType()->kind() == Struct)
1446 if(isAdaptablePyObjectNeutral(type,inport->edGetType())) return new NeutralPyStruct(inport);
1449 // Adaptation not possible
1451 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1452 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1454 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1456 throw ConversionException(msg.str());
1459 //! Adapt a Python input port to a Corba output port
1461 * Always convert the data
1462 * \param inport : InputPort to adapt to Corba type type
1463 * \param type : outport data type
1464 * \return an adaptator port of Corba type (InputCorbaPort)
1467 InputPort* RuntimeSALOME::adaptPythonToCorba(InputPyPort* inport,
1468 TypeCode * type) throw (ConversionException)
1470 if(inport->edGetType()->kind() == Double)
1472 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyDouble(inport);
1474 else if(inport->edGetType()->kind() == Int)
1476 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyInt(inport);
1478 else if(inport->edGetType()->kind() == String)
1480 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyString(inport);
1482 else if(inport->edGetType()->kind() == Bool)
1484 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyBool(inport);
1486 else if(inport->edGetType()->kind() == Objref)
1488 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1490 return new CorbaPyObjref(inport);
1495 msg << "Cannot connect InputCorbaPort : incompatible objref types " << type->id() << " " << inport->edGetType()->id();
1496 msg << " " << __FILE__ << ":" <<__LINE__;
1497 throw ConversionException(msg.str());
1500 else if(inport->edGetType()->kind() == Sequence)
1502 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1504 return new CorbaPySequence(inport);
1509 msg << "Cannot convert this sequence type " ;
1510 msg << __FILE__ << ":" <<__LINE__;
1511 throw ConversionException(msg.str());
1514 else if(inport->edGetType()->kind() == YACS::ENGINE::Struct)
1516 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1518 return new CorbaPyStruct(inport);
1523 msg << "Cannot convert this struct type " << type->id() << " to " << inport->edGetType()->id();
1524 msg << " " << __FILE__ << ":" <<__LINE__;
1525 throw ConversionException(msg.str());
1528 // Adaptation not possible
1530 msg << "Cannot connect Corba output port with type: " << type->id() ;
1531 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1533 msg << " ("__FILE__ << ":" << __LINE__ << ")";
1535 throw ConversionException(msg.str());
1538 //! Adapt a Python input port to a Xml output port
1540 * \param inport : input port to adapt to Xml type type
1541 * \param type : output port type
1542 * \return an input port of type InputXmlPort
1545 InputPort* RuntimeSALOME::adaptPythonToXml(InputPyPort* inport,
1546 TypeCode * type) throw (ConversionException)
1548 // BEWARE : using the generic check
1549 if(inport->edGetType()->isAdaptable(type))
1552 return new XmlPython(inport);
1554 //non convertible type
1556 msg << "Cannot connect Xml output port with type: " << type->id() ;
1557 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1559 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1561 throw ConversionException(msg.str());
1564 //! Adapt a Python input port to an output port with a given implementation
1566 * \param source : input port to adapt to implementation impl and type type
1567 * \param impl : output port implementation (C++, Python or Corba)
1568 * \param type : output port type
1569 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1570 * \return adaptated input port
1573 InputPort* RuntimeSALOME::adapt(InputPyPort* source,
1574 const std::string& impl,
1575 TypeCode * type,bool init) throw (ConversionException)
1577 if(impl == CppNode::IMPL_NAME)
1579 return adaptPythonToCpp(source,type);
1581 else if(impl == PythonNode::IMPL_NAME)
1583 return adaptPythonToPython(source,type,init);
1585 else if(impl == CORBANode::IMPL_NAME)
1587 return adaptPythonToCorba(source,type);
1589 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1591 return adaptPythonToNeutral(source,type);
1593 else if(impl == XmlNode::IMPL_NAME)
1595 return adaptPythonToXml(source,type);
1600 msg << "Cannot connect InputPyPort : unknown implementation " << impl;
1601 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1602 throw ConversionException(msg.str());
1607 //! Adapt a C++ input port to connect it to a CORBA output port
1609 * \param inport : input port to adapt to CORBA type type
1610 * \param type : type supported by output port
1611 * \return an adaptator port of type InputCorbaPort
1614 InputPort* RuntimeSALOME::adaptCppToCorba(InputCppPort* inport,
1615 TypeCode * type) throw (ConversionException)
1617 DEBTRACE("RuntimeSALOME::adaptCppToCorba(InputCppPort* inport)");
1618 if(isAdaptableCppCorba(type,inport->edGetType()))
1620 //output type is convertible to input type
1621 return new CorbaCpp(inport);
1623 //output type is not convertible
1625 msg << "Cannot connect Corba output port with type: " << type->id() ;
1626 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1628 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1630 throw ConversionException(msg.str());
1633 //! Adapt a C++ input port to a Python output port
1635 * \param inport : input port to adapt to Python type type
1636 * \param type : output port type
1637 * \return an adaptated input port of type InputPyPort
1639 InputPort* RuntimeSALOME::adaptCppToPython(InputCppPort* inport,
1640 TypeCode * type) throw (ConversionException)
1642 DEBTRACE("RuntimeSALOME::adaptCppToPython(InputCppPort* inport)");
1643 if(isAdaptableCppPyObject(type,inport->edGetType()))
1645 //output type is convertible to input type
1646 return new PyCpp(inport);
1648 //output type is not convertible
1650 msg << "Cannot connect Python output port with type: " << type->id() ;
1651 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1653 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1655 throw ConversionException(msg.str());
1658 //! Adapt a C++ input port to a C++ output port
1660 * \param inport : input port to adapt to C++ type type
1661 * \param type : output port type
1662 * \return an adaptated input port of type InputPyPort
1664 InputPort* RuntimeSALOME::adaptCppToCpp(InputCppPort* inport,
1665 TypeCode * type) throw (ConversionException)
1667 DEBTRACE("RuntimeSALOME::adaptCppToCpp(InputPort* inport" );
1668 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1669 if(type->isAdaptable(inport->edGetType()))
1671 //the output data is convertible to inport type
1672 return new CppCpp(inport);
1674 //non convertible type
1676 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1677 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1679 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1681 throw ConversionException(msg.str());
1684 //! Adapt a C++ input port to a Neutral output port
1686 * \param inport : input port to adapt to C++ type type
1687 * \param type : output port type
1688 * \return an adaptated input port of type InputPyPort
1690 InputPort* RuntimeSALOME::adaptCppToNeutral(InputCppPort* inport,
1691 TypeCode * type) throw (ConversionException)
1693 DEBTRACE("RuntimeSALOME::adaptCppToNeutral(InputPort* inport" );
1694 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1695 if(type->isAdaptable(inport->edGetType()))
1697 //the output data is convertible to inport type
1698 return new NeutralCpp(inport);
1700 //non convertible type
1702 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1703 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1705 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1707 throw ConversionException(msg.str());
1710 InputPort* RuntimeSALOME::adaptCppToXml(InputCppPort* inport,
1711 TypeCode * type) throw (ConversionException)
1713 DEBTRACE("RuntimeSALOME::adaptCppToXml(InputCppPort* inport" );
1714 if(isAdaptableCppXml(type,inport->edGetType()))
1717 return new XmlCpp(inport);
1719 //non convertible type
1721 msg << "Cannot connect Xml output port with type: " << type->id() ;
1722 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1724 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1726 throw ConversionException(msg.str());
1729 //! Adapt a C++ input port to connect it to an output port with a given implementation
1731 * \param source : input port to adapt to implementation impl and type type
1732 * \param impl : output port implementation (C++, Python or Corba)
1733 * \param type : output port supported type
1734 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1735 * \return the adaptated port
1738 InputPort* RuntimeSALOME::adapt(InputCppPort* source,
1739 const std::string& impl,
1740 TypeCode * type,bool init) throw (ConversionException)
1742 DEBTRACE("RuntimeSALOME::adapt(InputCppPort* source)");
1743 if(impl == CORBANode::IMPL_NAME)
1745 return adaptCppToCorba(source,type);
1747 else if(impl == PythonNode::IMPL_NAME)
1749 return adaptCppToPython(source,type);
1751 else if(impl == XmlNode::IMPL_NAME)
1753 return adaptCppToXml(source,type);
1755 else if(impl == CppNode::IMPL_NAME)
1757 return adaptCppToCpp(source, type);
1759 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1761 return adaptCppToNeutral(source, type);
1766 msg << "Cannot connect InputCppPort to " << impl << " implementation";
1767 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1768 throw ConversionException(msg.str());
1772 // bool RuntimeSALOME::isCompatible(const OutputPort* outputPort,
1773 // const InputPort* inputPort)
1775 // bool result=true;
1779 CORBA::ORB_ptr RuntimeSALOME::getOrb()
1784 PyObject * RuntimeSALOME::getPyOrb()
1789 PyObject * RuntimeSALOME::getBuiltins()
1794 DynamicAny::DynAnyFactory_ptr RuntimeSALOME::getDynFactory()
1799 PyObject * RuntimeSALOME::get_omnipy()
1804 omniORBpyAPI* RuntimeSALOME::getApi()
1809 void* RuntimeSALOME::convertNeutral(TypeCode * type, Any *data)
1812 return (void *)convertNeutralPyObject(type,data);
1816 return (void *)Py_None;
1820 std::string RuntimeSALOME::convertNeutralAsString(TypeCode * type, Any *data)
1825 // The call to PyGILState_Ensure was moved here because there was also
1826 // a crash when calling convertNeutralPyObject with a sequence of pyobj.
1827 // see also the comment below.
1828 PyGILState_STATE gstate = PyGILState_Ensure();
1829 ob=convertNeutralPyObject(type,data);
1830 std::string s=convertPyObjectToString(ob);
1832 // Note (Renaud Barate, 8 jan 2013): With Python 2.7, this call to Py_DECREF causes a crash
1833 // (SIGSEGV) when ob is a sequence and the call is not protected with the global interpreter
1834 // lock. I thus added the call to PyGILState_Ensure / PyGILState_Release. It worked fine in
1835 // Python 2.6 without this call. If anyone finds the real reason of this bug and another fix,
1836 // feel free to change this code.
1837 //PyGILState_STATE gstate = PyGILState_Ensure();
1839 PyGILState_Release(gstate);
1848 std::string RuntimeSALOME::convertPyObjectToString(PyObject* ob)
1850 return YACS::ENGINE::convertPyObjectToString(ob);
1853 PyObject* RuntimeSALOME::convertStringToPyObject(const std::string& s)
1858 PyGILState_STATE gstate = PyGILState_Ensure();
1859 mainmod = PyImport_AddModule("__main__");
1860 globals = PyModule_GetDict(mainmod);
1861 PyObject* d = PyDict_New();
1862 //PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
1863 ob= PyRun_String( s.c_str(), Py_eval_input, globals, d);
1869 PyObject* new_stderr = newPyStdOut(error);
1870 PySys_SetObject((char *)"stderr", new_stderr);
1872 PySys_SetObject((char *)"stderr", PySys_GetObject((char *)"__stderr__"));
1873 Py_DECREF(new_stderr);
1874 PyGILState_Release(gstate);
1875 throw Exception(error);
1877 PyGILState_Release(gstate);