1 // Copyright (C) 2006-2020 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"
115 #include "SALOME_NamingService.hxx"
116 #include "SALOME_ResourcesManager.hxx"
117 #include "SALOME_ContainerManager.hxx"
118 #include "SALOMEconfig.h"
119 #include CORBA_CLIENT_HEADER(SALOME_ContainerManager)
123 #include <libxml/parser.h>
124 #include <omniORB4/CORBA.h>
130 #include "YacsTrace.hxx"
133 using namespace YACS::ENGINE;
135 void RuntimeSALOME::setRuntime(long flags, int argc, char* argv[]) // singleton creation (not thread safe!)
137 if (! Runtime::_singleton)
139 RuntimeSALOME* r=new RuntimeSALOME(flags, argc, argv);
140 Runtime::_singleton = r;
143 DEBTRACE("RuntimeSALOME::setRuntime() done !");
146 RuntimeSALOME* YACS::ENGINE::getSALOMERuntime()
148 YASSERT(RuntimeSALOME::getSingleton());
149 return dynamic_cast< RuntimeSALOME* >(RuntimeSALOME::getSingleton());
153 * Singleton creation, initialize converter map
156 RuntimeSALOME::RuntimeSALOME()
161 void RuntimeSALOME::initBuiltins()
163 //Fill the builtin catalog with nodes specific to the runtime
164 std::map<std::string,TypeCode*>& typeMap=_builtinCatalog->_typeMap;
165 std::map<std::string,Node*>& nodeMap=_builtinCatalog->_nodeMap;
166 std::map<std::string,ComposedNode*>& composednodeMap=_builtinCatalog->_composednodeMap;
167 std::map<std::string,ComponentDefinition*>& componentMap=_builtinCatalog->_componentMap;
168 nodeMap["PyFunction"]=new PyFuncNode("PyFunction");
169 nodeMap["PyScript"]=new PythonNode("PyScript");
170 nodeMap["CORBANode"]=new CORBANode("CORBANode");
171 nodeMap["XmlNode"]=new XmlNode("XmlNode");
172 nodeMap["SalomeNode"]=new SalomeNode("SalomeNode");
173 nodeMap["CppNode"]=new CppNode("CppNode");
174 nodeMap["SalomePythonNode"]=new SalomePythonNode("SalomePythonNode");
175 nodeMap["PresetNode"]=new PresetNode("PresetNode");
176 nodeMap["OutNode"]=new OutNode("OutNode");
177 nodeMap["StudyInNode"]=new StudyInNode("StudyInNode");
178 nodeMap["StudyOutNode"]=new StudyOutNode("StudyOutNode");
179 composednodeMap["OptimizerLoop"]=createOptimizerLoop("OptimizerLoop","","",true);
180 typeMap["dblevec"]= createSequenceTc("dblevec","dblevec",_tc_double);
181 typeMap["intvec"]= createSequenceTc("intvec","intvec",_tc_int);
182 typeMap["stringvec"]= createSequenceTc("stringvec","stringvec",_tc_string);
183 typeMap["boolvec"]= createSequenceTc("boolvec","boolvec",_tc_bool);
184 typeMap["seqdblevec"]= createSequenceTc("seqdblevec","seqdblevec",typeMap["dblevec"]);
185 typeMap["seqintvec"]= createSequenceTc("seqintvec","seqintvec",typeMap["intvec"]);
186 typeMap["seqstringvec"]= createSequenceTc("seqstringvec","seqstringvec",typeMap["stringvec"]);
187 typeMap["seqboolvec"]= createSequenceTc("seqboolvec","seqboolvec",typeMap["boolvec"]);
188 std::list<TypeCodeObjref *> ltc;
189 typeMap["pyobj"]= createInterfaceTc("python:obj:1.0","pyobj",ltc);
190 typeMap["seqpyobj"]= createSequenceTc("seqpyobj","seqpyobj",typeMap["pyobj"]);
191 composednodeMap["Bloc"]=createBloc("Bloc");
192 composednodeMap["Switch"]=createSwitch("Switch");
193 composednodeMap["WhileLoop"]=createWhileLoop("WhileLoop");
194 composednodeMap["ForLoop"]=createForLoop("ForLoop");
195 composednodeMap["ForEachLoop_double"]=createForEachLoop("ForEachLoop_double",Runtime::_tc_double);
196 composednodeMap["ForEachLoop_string"]=createForEachLoop("ForEachLoop_string",Runtime::_tc_string);
197 composednodeMap["ForEachLoop_int"]=createForEachLoop("ForEachLoop_int",Runtime::_tc_int);
198 composednodeMap["ForEachLoop_bool"]=createForEachLoop("ForEachLoop_bool",Runtime::_tc_bool);
199 composednodeMap["ForEachLoop_pyobj"]=createForEachLoop("ForEachLoop_pyobj",typeMap["pyobj"]);;
200 ENGINE::TypeCodeStruct *t = createStructTc("","Engines/dataref");
201 t->addMember("ref",_tc_string);
202 typeMap["dataref"]= t;
205 RuntimeSALOME::RuntimeSALOME(long flags, int argc, char* argv[])
207 // If all flags (apart the IsPyExt flags) are unset, force them to true
208 if ((flags - flags & RuntimeSALOME::IsPyExt) == 0)
209 flags += RuntimeSALOME::UseCorba + RuntimeSALOME::UsePython
210 + RuntimeSALOME::UseCpp + RuntimeSALOME::UseXml;
212 // Salome Nodes implies Corba Nodes
213 if (flags & RuntimeSALOME::UseSalome)
214 flags |= RuntimeSALOME::UseCorba;
216 // Corba Nodes implies Python Nodes
217 if (flags & RuntimeSALOME::UseCorba)
218 flags |= RuntimeSALOME::UsePython;
220 _useCorba = flags & RuntimeSALOME::UseCorba;
221 _usePython = flags & RuntimeSALOME::UsePython;
222 _useCpp = flags & RuntimeSALOME::UseCpp;
223 _useXml = flags & RuntimeSALOME::UseXml;
228 if (_useCpp) _setOfImplementation.insert(CppNode::IMPL_NAME);
229 if (_usePython) _setOfImplementation.insert(PythonNode::IMPL_NAME);
230 if (_useCorba) _setOfImplementation.insert(CORBANode::IMPL_NAME);
231 if (_useXml) _setOfImplementation.insert(XmlNode::IMPL_NAME);
232 init(flags, argc, argv);
235 RuntimeSALOME::~RuntimeSALOME()
237 DEBTRACE("RuntimeSALOME::~RuntimeSALOME");
238 // destroy catalog loader prototypes
239 std::map<std::string, CatalogLoader*>::const_iterator pt;
240 for(pt=_catalogLoaderFactoryMap.begin();pt!=_catalogLoaderFactoryMap.end();pt++)
246 //! CORBA and Python initialization
248 * \param flags contains several bits
249 * bit0 (ispyext) true when method is called from Python
250 * (Python initialization must not be done!)
251 * bit1 (UsePython) true if python nodes are needed
252 * bit1 (UseCorba) true if CORBA nodes are needed
253 * bit1 (UseXml) true if python nodes are needed
254 * bit1 (UseCpp) true if C++ nodes are needed
255 * bit1 (UseSalome) true if Salome nodes are needed
256 * \param argc number of command line arguments (used to initialize the Python interpreter)
257 * \param argv command line arguments (used to initialize the Python interpreter)
261 void RuntimeSALOME::init(long flags, int argc, char* argv[])
263 bool ispyext = flags & RuntimeSALOME::IsPyExt;
266 PortableServer::POA_var root_poa;
267 PortableServer::POAManager_var pman;
268 CORBA::Object_var obj;
269 int nbargs = 0; char **args = 0;
270 _orb = CORBA::ORB_init (nbargs, args);
271 obj = _orb->resolve_initial_references("RootPOA");
272 root_poa = PortableServer::POA::_narrow(obj);
273 pman = root_poa->the_POAManager();
277 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
279 obj = _orb->resolve_initial_references("DynAnyFactory");
280 _dynFactory = DynamicAny::DynAnyFactory::_narrow(obj);
285 DEBTRACE("RuntimeSALOME::init, is python extension = " << ispyext);
287 // Initialize Python interpreter in embedded mode
288 if (!Py_IsInitialized())
290 #if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
293 Py_InitializeEx(0); // do not install signal handlers
295 if (argc > 0 && argv != NULL)
297 wchar_t **changed_argv = new wchar_t*[argc];
298 for (int i = 0; i < argc; i++)
300 changed_argv[i] = Py_DecodeLocale(argv[i], NULL);
302 PySys_SetArgv(argc, changed_argv);
308 char defaultName[] = "SALOME_YACS_RUNTIME";
309 wchar_t **changed_pyArgv = new wchar_t*[pyArgc];
310 pyArgv[0] = defaultName;
311 for (int i = 0; i < pyArgc; i++)
313 changed_pyArgv[i] = Py_DecodeLocale(pyArgv[i], NULL);
315 PySys_SetArgv(pyArgc, changed_pyArgv);
317 PyEval_InitThreads(); /* Create (and acquire) the interpreter lock (for threads)*/
318 PyEval_SaveThread(); /* Release the thread state */
319 //here we do not have the Global Interpreter Lock
322 PyObject *mainmod,*pyapi,*res ;
324 PyGILState_STATE gstate;
325 gstate = PyGILState_Ensure(); // acquire the Global Interpreter Lock
327 mainmod = PyImport_AddModule("__main__");
328 globals = PyModule_GetDict(mainmod);
329 /* globals is a borrowed reference */
331 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
333 PyObject *bimod = PyImport_ImportModule("builtins");
334 if (bimod == NULL || PyDict_SetItemString(globals, "__builtins__", bimod) != 0)
335 Py_FatalError("can't add __builtins__ to __main__");
339 _bltins = PyEval_GetBuiltins(); /* borrowed ref */
345 _omnipy = PyImport_ImportModule((char*)"_omnipy");
349 PyErr_SetString(PyExc_ImportError, (char*)"Cannot import _omnipy");
352 pyapi = PyObject_GetAttrString(_omnipy, (char*)"API");
357 _api = (omniORBpyAPI*)PyCapsule_GetPointer(pyapi,"_omnipy.API");
360 res=PyRun_String("\n"
361 "from math import *\n"
363 "sys.path.insert(0,'.')\n"
364 "from omniORB import CORBA\n"
365 "from omniORB import any\n"
366 "orb = CORBA.ORB_init([], CORBA.ORB_ID)\n"
367 "#print(sys.getrefcount(orb))\n"
373 Py_file_input,globals,globals );
381 _pyorb = PyDict_GetItemString(globals,"orb");
382 /* PyDict_GetItemString returns a borrowed reference. There is no need to decref _pyorb */
385 pyany = PyDict_GetItemString(globals,"any");
386 /* PyDict_GetItemString returns a borrowed reference. There is no need to decref pyany */
389 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
393 PyGILState_Release(gstate); // Release the Global Interpreter Lock
397 // initialize the catalogLoaderFactory map with the session one
398 _catalogLoaderFactoryMap["session"]=new SessionCataLoader;
402 void RuntimeSALOME::fini()
406 PyGILState_STATE gstate = PyGILState_Ensure();
408 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
410 PyObject *mainmod, *globals;
411 mainmod = PyImport_AddModule("__main__");
412 globals = PyModule_GetDict(mainmod);
416 res=PyRun_String("orb.destroy()\n"
418 Py_file_input,globals,globals );
424 std::map<std::string,Node*>& nodeMap=_builtinCatalog->_nodeMap;
425 delete nodeMap["PyFunction"];
426 delete nodeMap["PyScript"];
427 delete nodeMap["SalomePythonNode"];
428 nodeMap.erase("PyFunction");
429 nodeMap.erase("PyScript");
430 nodeMap.erase("SalomePythonNode");
434 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
442 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
449 std::vector< std::pair<std::string,int> > RuntimeSALOME::getCatalogOfComputeNodes() const
451 CORBA::ORB_ptr orb(getOrb());
452 SALOME_NamingService namingService;
455 namingService.init_orb(orb);
457 catch(SALOME_Exception& e)
459 throw Exception("RuntimeSALOME::getCatalogOfComputeNodes : Unable to contact the SALOME Naming Service");
461 CORBA::Object_var obj(namingService.Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS));
462 if(CORBA::is_nil(obj))
463 throw Exception("RuntimeSALOME::getCatalogOfComputeNodes : Unable to access to the resource manager !");
464 Engines::ResourcesManager_var resManager(Engines::ResourcesManager::_narrow(obj));
465 if(CORBA::is_nil(resManager))
466 throw Exception("RuntimeSALOME::getCatalogOfComputeNodes : Internal error ! The entry attached to the res manager in NS does not have right type !");
467 std::vector< std::pair<std::string,int> > ret;
468 Engines::ResourceParameters params;
470 params.hostname = "";
474 params.cpu_clock = 0;
476 params.nb_proc_per_node = 0;
478 params.can_launch_batch_jobs = false;
479 params.can_run_containers = true;
480 params.componentList.length(0);
483 Engines::ResourceList_var resourceList;
484 resourceList = resManager->GetFittingResources(params);
485 ret.reserve(resourceList->length());
486 for(int i = 0; i<resourceList->length(); i++)
488 const char* resource_name = resourceList[i];
489 std::string std_resource_name = resource_name;
490 Engines::ResourceDefinition_var resource_definition
491 = resManager->GetResourceDefinition(resource_name);
492 int nb_cores = resource_definition->nb_node *
493 resource_definition->nb_proc_per_node;
494 ret.push_back(std::pair<std::string,int>(resource_name, nb_cores));
497 catch(SALOME::SALOME_Exception& e)
500 message=e.details.text.in();
501 throw Exception(message);
507 std::string RuntimeSALOME::getVersion() const
509 #ifdef YACS_DEVELOPMENT
510 return CORBA::string_dup(YACS_VERSION_STR"dev");
512 return CORBA::string_dup(YACS_VERSION_STR);
516 Proc* RuntimeSALOME::createProc(const std::string& name)
518 return new SalomeProc(name);
521 TypeCode * RuntimeSALOME::createInterfaceTc(const std::string& id, const std::string& name,
522 std::list<TypeCodeObjref *> ltc)
525 if(id == "") myName = "IDL:" + name + ":1.0";
527 return TypeCode::interfaceTc(myName.c_str(),name.c_str(),ltc);
530 TypeCode * RuntimeSALOME::createSequenceTc(const std::string& id,
531 const std::string& name,
534 return TypeCode::sequenceTc(id.c_str(),name.c_str(),content);
537 TypeCodeStruct * RuntimeSALOME::createStructTc(const std::string& id, const std::string& name)
540 if(id == "") myName = "IDL:" + name + ":1.0";
542 return (TypeCodeStruct *)TypeCode::structTc(myName.c_str(),name.c_str());
545 Bloc* RuntimeSALOME::createBloc(const std::string& name)
547 return new Bloc(name);
550 WhileLoop* RuntimeSALOME::createWhileLoop(const std::string& name)
552 return new WhileLoop(name);
555 ForLoop* RuntimeSALOME::createForLoop(const std::string& name)
557 return new ForLoop(name);
560 OptimizerLoop* RuntimeSALOME::createOptimizerLoop(const std::string& name,const std::string& algLib,const std::string& factoryName,
561 bool algInitOnFile, const std::string& kind, Proc * procForTypes)
563 OptimizerLoop * ol = (kind == "base") ? new OptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes) :
564 new SalomeOptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes);
565 ol->edGetNbOfBranchesPort()->edInit(1);
569 DataNode* RuntimeSALOME::createInDataNode(const std::string& kind,const std::string& name)
574 node = new PresetNode(name);
577 else if(kind == "study" )
579 return new StudyInNode(name);
581 std::string msg="DataNode kind ("+kind+") unknown";
582 throw Exception(msg);
585 DataNode* RuntimeSALOME::createOutDataNode(const std::string& kind,const std::string& name)
589 return new OutNode(name);
591 else if(kind == "study" )
593 return new StudyOutNode(name);
596 std::string msg="OutDataNode kind ("+kind+") unknown";
597 throw Exception(msg);
600 InlineFuncNode* RuntimeSALOME::createFuncNode(const std::string& kind,const std::string& name)
602 InlineFuncNode* node;
603 if(kind == "" || kind == SalomeNode::KIND || kind == PythonNode::KIND)
605 node = new PyFuncNode(name);
608 if(kind == DistributedPythonNode::KIND)
609 return new DistributedPythonNode(name);
610 std::string msg="FuncNode kind ("+kind+") unknown";
611 throw Exception(msg);
614 InlineNode* RuntimeSALOME::createScriptNode(const std::string& kind,const std::string& name)
617 if(kind == "" || kind == SalomeNode::KIND || kind == PythonNode::KIND)
619 node = new PythonNode(name);
622 std::string msg="ScriptNode kind ("+kind+") unknown";
623 throw Exception(msg);
626 ServiceNode* RuntimeSALOME::createRefNode(const std::string& kind,const std::string& name)
629 if(kind == "" || kind == SalomeNode::KIND || kind == CORBANode::KIND)
631 node = new CORBANode(name);
634 else if(kind == XmlNode::KIND)
636 node = new XmlNode(name);
639 std::string msg="RefNode kind ("+kind+") unknown";
640 throw Exception(msg);
643 ServiceNode* RuntimeSALOME::createCompoNode(const std::string& kind,const std::string& name)
646 if(kind == "" || kind == SalomeNode::KIND )
648 node=new SalomeNode(name);
651 else if (kind == CppNode::KIND)
653 node = new CppNode(name);
656 std::string msg="CompoNode kind ("+kind+") unknown";
657 throw Exception(msg);
660 ServiceInlineNode *RuntimeSALOME::createSInlineNode(const std::string& kind, const std::string& name)
662 if(kind == "" || kind == SalomeNode::KIND )
663 return new SalomePythonNode(name);
664 std::string msg="CompoNode kind ("+kind+") unknown";
665 throw Exception(msg);
668 ComponentInstance* RuntimeSALOME::createComponentInstance(const std::string& name,
669 const std::string& kind)
671 ComponentInstance* compo;
672 if(kind == "" || kind == SalomeComponent::KIND)
673 return new SalomeComponent(name);
674 else if(kind == CORBAComponent::KIND)
675 return new CORBAComponent(name);
676 else if(kind == SalomePythonComponent::KIND)
677 return new SalomePythonComponent(name);
678 else if (kind == CppComponent::KIND)
679 return new CppComponent(name);
680 else if (kind == SalomeHPComponent::KIND)
681 return new SalomeHPComponent(name);
682 std::string msg="Component Instance kind ("+kind+") unknown";
683 throw Exception(msg);
686 Container *RuntimeSALOME::createContainer(const std::string& kind)
688 if(kind == "" || kind == SalomeContainer::KIND)
689 return new SalomeContainer;
690 if(kind==SalomeHPContainer::KIND)
691 return new SalomeHPContainer;
692 else if (kind == CppContainer::KIND)
693 return new CppContainer;
694 std::string msg="Container kind ("+kind+") unknown";
695 throw Exception(msg);
698 InputPort * RuntimeSALOME::createInputPort(const std::string& name,
699 const std::string& impl,
703 if(impl == CppNode::IMPL_NAME)
705 return new InputCppPort(name, node, type);
707 else if(impl == PythonNode::IMPL_NAME)
709 return new InputPyPort(name, node, type);
711 else if(impl == CORBANode::IMPL_NAME)
713 return new InputCorbaPort(name, node, type);
715 else if(impl == XmlNode::IMPL_NAME)
717 return new InputXmlPort(name, node, type);
722 msg << "Cannot create " << impl << " InputPort" ;
723 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
724 throw Exception(msg.str());
728 OutputPort * RuntimeSALOME::createOutputPort(const std::string& name,
729 const std::string& impl,
733 if(impl == CppNode::IMPL_NAME)
735 return new OutputCppPort(name, node, type);
737 else if(impl == PythonNode::IMPL_NAME)
739 return new OutputPyPort(name, node, type);
741 else if(impl == CORBANode::IMPL_NAME)
743 return new OutputCorbaPort(name, node, type);
745 else if(impl == XmlNode::IMPL_NAME)
747 return new OutputXmlPort(name, node, type);
752 msg << "Cannot create " << impl << " OutputPort" ;
753 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
754 throw Exception(msg.str());
758 InputDataStreamPort* RuntimeSALOME::createInputDataStreamPort(const std::string& name,
759 Node *node,TypeCode *type)
761 DEBTRACE("createInputDataStreamPort: " << name << " " << type->shortName());
762 if(type->kind() == Objref && std::string(type->shortName(),7) == "CALCIUM")
764 return new InputCalStreamPort(name,node,type);
768 return new InputDataStreamPort(name,node,type);
772 OutputDataStreamPort* RuntimeSALOME::createOutputDataStreamPort(const std::string& name,
773 Node *node,TypeCode *type)
775 DEBTRACE("createOutputDataStreamPort: " << name << " " << type->shortName());
776 if(type->kind() == Objref && std::string(type->shortName(),7) == "CALCIUM")
778 return new OutputCalStreamPort(name,node,type);
782 return new OutputDataStreamPort(name,node,type);
786 //! Main adapter function : adapt an InputPort to be able to connect it to an OutputPort with a possible different implementation
788 * \param source : InputPort to be adapted
789 * \param impl : new implementation (C++, python, CORBA, XML, Neutral)
790 * \param type : data type provided by the InputPort
791 * \param init : indicates if the adapted InputPort will be used for initialization (value true) or not (value false)
793 * \return : adapted InputPort
795 InputPort* RuntimeSALOME::adapt(InputPort* source,
796 const std::string& impl,
797 TypeCode * type,bool init)
799 string imp_source=source->getNode()->getImplementation();
800 if(imp_source == PythonNode::IMPL_NAME)
802 return adapt((InputPyPort*)source,impl,type,init);
804 else if(imp_source == CppNode::IMPL_NAME)
806 return adapt((InputCppPort*)source,impl,type,init);
808 else if(imp_source == CORBANode::IMPL_NAME)
810 return adapt((InputCorbaPort*)source,impl,type,init);
812 else if(imp_source == XmlNode::IMPL_NAME)
814 return adapt((InputXmlPort*)source,impl,type,init);
816 else if(imp_source == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
818 return adaptNeutral(source,impl,type,init);
823 msg << "Cannot adapt " << imp_source << " InputPort to " << impl;
824 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
825 throw ConversionException(msg.str());
829 //! Adapter function for InPropertyPort
831 * \param source : InPropertyPort to be adapted
832 * \param impl : new implementation (C++, python, CORBA, XML, Neutral)
833 * \param type : data type provided by the InPropertyPort
834 * \param init : indicates if the adapted InPropertyPort will be used for initialization (value true) or not (value false)
836 * \return : adapted InputPort
838 InputPort* RuntimeSALOME::adapt(InPropertyPort* source,
839 const std::string& impl,
840 TypeCode * type,bool init)
842 return adaptNeutral((InputPort *)source,impl,type,init);
845 //! Adapt a Neutral input port to a Corba output port
847 * \param inport : Neutral input port to adapt to Corba type type
848 * \param type : output port type
849 * \return an adaptated input port of type InputCorbaPort
851 InputPort* RuntimeSALOME::adaptNeutralToCorba(InputPort* inport,
854 // BEWARE : using the generic check
855 if(inport->edGetType()->isAdaptable(type))
857 //the output data is convertible to inport type
858 return new CorbaNeutral(inport);
860 //non convertible type
862 msg << "Cannot connect Corba output port with type: " << type->id() ;
863 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
865 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
867 throw ConversionException(msg.str());
870 //! Adapt a Neutral input port to a Python output port
872 * \param inport : input port to adapt to Python type type
873 * \param type : output port type
874 * \return an adaptated input port of type InputPyPort
876 InputPort* RuntimeSALOME::adaptNeutralToPython(InputPort* inport,
879 // BEWARE : using the generic check
880 if(inport->edGetType()->isAdaptable(type))
883 return new PyNeutral(inport);
885 //last chance : an py output that is seq[objref] can be connected to a neutral input objref (P13268)
886 else if(type->kind()==Sequence && type->contentType()->kind()==Objref && inport->edGetType()->kind()==Objref)
888 return new PyNeutral(inport);
890 //non convertible type
892 msg << "Cannot connect Python output port with type: " << type->id() ;
893 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
895 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
897 throw ConversionException(msg.str());
900 //! Adapt a Neutral input port to a Xml output port
902 * \param inport : input port to adapt to Xml type type
903 * \param type : output port type
904 * \return an input port of type InputXmlPort
906 InputPort* RuntimeSALOME::adaptNeutralToXml(InputPort* inport,
909 // BEWARE : using the generic check
910 if(inport->edGetType()->isAdaptable(type))
913 return new XmlNeutral(inport);
915 //non convertible type
917 msg << "Cannot connect Xml output port with type: " << type->id() ;
918 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
920 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
922 throw ConversionException(msg.str());
925 //! Adapt a Neutral input port to a C++ output port
927 * \param inport : input port to adapt to C++ type type
928 * \param type : output port type
929 * \return an input port of type InputCppPort
931 InputPort* RuntimeSALOME::adaptNeutralToCpp(InputPort* inport,
934 DEBTRACE("RuntimeSALOME::adaptNeutralToCpp(InputPort* inport" );
935 if(isAdaptableNeutralCpp(type,inport->edGetType()))
938 return new CppNeutral(inport);
940 //non convertible type
942 msg << "Cannot connect Cpp output port with type: " << type->id() ;
943 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
945 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
947 throw ConversionException(msg.str());
950 //! Adapt a Neutral input port to connect it to an output port with a given implementation
952 * \param source : Neutral input port to adapt to implementation impl and type type
953 * \param impl : output port implementation (C++, Python, Corba, Xml or Neutral)
954 * \param type : output port supported type
955 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
956 * \return the adaptated port
958 InputPort* RuntimeSALOME::adaptNeutral(InputPort* source,
959 const std::string& impl,
960 TypeCode * type,bool init)
962 if(impl == CppNode::IMPL_NAME)
964 return adaptNeutralToCpp(source,type);
966 else if(impl == PythonNode::IMPL_NAME)
968 return adaptNeutralToPython(source,type);
970 else if(impl == CORBANode::IMPL_NAME)
972 return adaptNeutralToCorba(source,type);
974 else if(impl == XmlNode::IMPL_NAME )
976 return adaptNeutralToXml(source,type);
978 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
981 return new NeutralInit(source);
983 return new ProxyPort(source);
986 msg << "Cannot connect InputPort : unknown implementation " << impl;
987 msg << " (" <<__FILE__ << ":" <<__LINE__ << ")";
988 throw ConversionException(msg.str());
991 //! Adapt a XML input port to connect it to a CORBA output port
993 * \param inport : input port to adapt to CORBA type type
994 * \param type : type supported by output port
995 * \return an adaptator port of type InputCorbaPort
998 InputPort* RuntimeSALOME::adaptXmlToCorba(InputXmlPort* inport,
1001 if(isAdaptableXmlCorba(type,inport->edGetType()))
1003 //output type is convertible to input type
1004 return new CorbaXml(inport);
1006 //output type is not convertible
1008 msg << "Cannot connect Corba output port with type: " << type->id() ;
1009 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1011 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1013 throw ConversionException(msg.str());
1016 //! Adapt a XML input port to a Python output port
1018 * \param inport : input port to adapt to Python type type
1019 * \param type : output port type
1020 * \return an adaptated input port of type InputPyPort
1022 InputPort* RuntimeSALOME::adaptXmlToPython(InputXmlPort* inport,
1025 if(inport->edGetType()->isAdaptable(type))
1027 //the output data is convertible to inport type
1028 return new PyXml(inport);
1030 //non convertible type
1032 msg << "Cannot connect Python output port with type: " << type->id() ;
1033 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1035 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1037 throw ConversionException(msg.str());
1040 //! Adapt a XML input port to a C++ output port
1042 * \param inport : input port to adapt to C++ type type
1043 * \param type : output port type
1044 * \return an adaptated input port of type InputPyPort
1046 InputPort* RuntimeSALOME::adaptXmlToCpp(InputXmlPort* inport,
1049 DEBTRACE("RuntimeSALOME::adaptXmlToCpp(InputPort* inport" );
1050 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1051 if(type->isAdaptable(inport->edGetType()))
1053 //the output data is convertible to inport type
1054 return new CppXml(inport);
1056 //non convertible type
1058 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1059 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1061 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1063 throw ConversionException(msg.str());
1066 //! Adapt a XML input port to a Neutral output port
1068 * \param inport : input port to adapt to Neutral type type
1069 * \param type : output port type
1070 * \return an adaptated input port of type Neutralxxxx
1072 InputPort* RuntimeSALOME::adaptXmlToNeutral(InputXmlPort* inport,
1075 if(inport->edGetType()->isAdaptable(type))
1077 //the output data is convertible to inport type
1078 return new NeutralXml(inport);
1080 //non convertible type
1082 msg << "Cannot connect Xml InputPort to OutputNeutralPort : " ;
1083 msg << "(" <<__FILE__ << ":" <<__LINE__<< ")";
1084 throw ConversionException(msg.str());
1087 //! Adapt a XML input port to a Xml output port
1089 * \param inport : input port to adapt to Xml type type
1090 * \param type : output port type
1091 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1092 * \return an adaptated input port of type Xmlxxxx
1094 InputPort* RuntimeSALOME::adaptXmlToXml(InputXmlPort* inport,
1095 TypeCode * type,bool init)
1098 return new ProxyPort(inport);
1100 if(inport->edGetType()->isAdaptable(type))
1101 return new ProxyPort(inport);
1104 msg << "Cannot connect Xml output port with type: " << type->id() ;
1105 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1107 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1109 throw ConversionException(msg.str());
1112 //! Adapt an Xml input port to an output port which implementation is given by impl
1114 * \param source : input port to adapt to implementation impl and type type
1115 * \param impl : output port implementation (C++, Python or Corba)
1116 * \param type : output port supported type
1117 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1118 * \return the adaptated port
1121 InputPort* RuntimeSALOME::adapt(InputXmlPort* source,
1122 const std::string& impl,
1123 TypeCode * type,bool init)
1125 if(impl == CORBANode::IMPL_NAME)
1127 return adaptXmlToCorba(source,type);
1129 else if(impl == PythonNode::IMPL_NAME)
1131 return adaptXmlToPython(source,type);
1133 else if(impl == CppNode::IMPL_NAME)
1135 return adaptXmlToCpp(source,type);
1137 else if(impl == XmlNode::IMPL_NAME )
1139 return adaptXmlToXml(source,type,init);
1141 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1143 return adaptXmlToNeutral(source,type);
1148 msg << "Cannot connect InputXmlPort to " << impl << " implementation";
1149 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1150 throw ConversionException(msg.str());
1155 //! Adapt a CORBA input port to a CORBA output port
1157 * \param inport : input port to adapt to CORBA outport data type
1158 * \param type : outport data type
1159 * \return an adaptator port of type InputCORBAPort
1161 InputPort* RuntimeSALOME::adaptCorbaToCorba(InputCorbaPort* inport,
1164 if(type->isA(inport->edGetType()))
1166 //types are compatible : no conversion
1167 //outport data type is more specific than inport required type
1168 //so the inport can be used safely
1169 return new ProxyPort(inport);
1171 else if(isAdaptableCorbaCorba(type,inport->edGetType()))
1173 //ouport data can be converted to inport data type
1174 return new CorbaCorba(inport);
1176 //outport data can not be converted
1178 msg << "Cannot connect Corba output port with type: " << type->id() ;
1179 msg << " to CORBA input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1181 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1183 throw ConversionException(msg.str());
1186 //! Adapt a CORBA input port to a Python output port
1188 * \param inport : input port to adapt to Python type type
1189 * \param type : outport data type
1190 * \return an adaptator port of type InputPyPort
1193 InputPort* RuntimeSALOME::adaptCorbaToPython(InputCorbaPort* inport,
1196 if(inport->edGetType()->kind() == Double)
1198 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaDouble(inport);
1200 else if(inport->edGetType()->kind() == Int)
1202 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaInt(inport);
1204 else if(inport->edGetType()->kind() == String)
1206 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaString(inport);
1208 else if(inport->edGetType()->kind() == Bool)
1210 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaBool(inport);
1212 else if(inport->edGetType()->kind() == Objref )
1214 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1216 return new PyCorbaObjref(inport);
1221 msg << "Cannot connect Python output port with type: " << type->id() ;
1222 msg << " to CORBA input port " << inport->getName() << " with incompatible objref type: " << inport->edGetType()->id();
1223 msg << " (" << __FILE__ << ":" <<__LINE__ << ")";
1224 throw ConversionException(msg.str());
1227 else if(inport->edGetType()->kind() == Sequence)
1229 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1231 return new PyCorbaSequence(inport);
1236 msg << "Cannot convert this sequence type " ;
1237 msg << __FILE__ << ":" <<__LINE__;
1238 throw ConversionException(msg.str());
1241 else if(inport->edGetType()->kind() == YACS::ENGINE::Struct)
1243 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1245 return new PyCorbaStruct(inport);
1250 msg << "Cannot convert this struct type " << type->id() << " to " << inport->edGetType()->id();
1251 msg << __FILE__ << ":" <<__LINE__;
1252 throw ConversionException(msg.str());
1255 // Adaptation not possible
1257 msg << "Cannot connect Python output port with type: " << type->id() ;
1258 msg << " to CORBA input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1260 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1262 throw ConversionException(msg.str());
1265 //! Adapt a CORBA input port to connect it to a XML output port
1267 * \param inport : input port to adapt to Xml type type
1268 * \param type : type supported by output port
1269 * \return an adaptator port of type InputXmlPort
1272 InputPort* RuntimeSALOME::adaptCorbaToXml(InputCorbaPort* inport,
1275 // BEWARE : using the generic check
1276 if(inport->edGetType()->isAdaptable(type))
1278 //output type is convertible to input type
1279 return new XmlCorba(inport);
1281 //output type is not convertible
1283 msg << "Cannot connect Xml output port with type: " << type->id() ;
1284 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1286 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1288 throw ConversionException(msg.str());
1291 //! Adapt a CORBA input port to a C++ output port
1293 * \param inport : input port to adapt to C++ type type
1294 * \param type : outport data type
1295 * \return an adaptator port of type InputCPPPort
1298 InputPort* RuntimeSALOME::adaptCorbaToCpp(InputCorbaPort* inport,
1301 DEBTRACE("RuntimeSALOME::adaptCorbaToCpp(InputCorbaPort* inport" );
1302 if(isAdaptableCorbaCpp(type,inport->edGetType()))
1304 //output type is convertible to input type
1305 return new CppCorba(inport);
1307 //output type is not convertible
1309 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1310 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1312 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1314 throw ConversionException(msg.str());
1317 //! Adapt a CORBA input port to a neutral data
1319 * \param inport : InputPort to adapt to Neutral type type
1320 * \param type : outport data type
1321 * \return an adaptator port of type Neutralxxxx
1324 InputPort* RuntimeSALOME::adaptCorbaToNeutral(InputCorbaPort* inport,
1327 if(inport->edGetType()->kind() == Double)
1329 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaDouble(inport);
1331 else if(inport->edGetType()->kind() == Int)
1333 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaInt(inport);
1335 else if(inport->edGetType()->kind() == String)
1337 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaString(inport);
1339 else if(inport->edGetType()->kind() == Bool)
1341 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaBool(inport);
1343 else if(inport->edGetType()->kind() == Objref)
1345 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaObjref(inport);
1347 else if(inport->edGetType()->kind() == Sequence)
1349 if(isAdaptableCorbaNeutral(type,inport->edGetType()))
1350 return new NeutralCorbaSequence(inport);
1354 msg << "Cannot convert this sequence type " ;
1355 msg << __FILE__ << ":" <<__LINE__;
1356 throw ConversionException(msg.str());
1359 else if(inport->edGetType()->kind() == Struct)
1361 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaStruct(inport);
1364 // Adaptation not possible
1366 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1367 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1369 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1371 throw ConversionException(msg.str());
1374 //! Adapt a CORBA input port to an output which implementation and type are given by impl and type
1376 * \param source : input port to adapt to implementation impl and type type
1377 * \param impl : output port implementation (C++, Python or Corba)
1378 * \param type : outport data type
1379 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1380 * \return an adaptator port which type depends on impl
1383 InputPort* RuntimeSALOME::adapt(InputCorbaPort* source,
1384 const std::string& impl,
1385 TypeCode * type,bool init)
1387 if(impl == CppNode::IMPL_NAME)
1389 return adaptCorbaToCpp(source,type);
1391 else if(impl == PythonNode::IMPL_NAME)
1393 return adaptCorbaToPython(source,type);
1395 else if(impl == CORBANode::IMPL_NAME)
1398 return adaptCorbaToCorba(source,type);
1400 return adaptCorbaToCorba(source,type);
1402 else if(impl == XmlNode::IMPL_NAME )
1404 return adaptCorbaToXml(source,type);
1406 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1408 return adaptCorbaToNeutral(source,type);
1413 msg << "Cannot connect InputCorbaPort : unknown implementation " ;
1414 msg << __FILE__ << ":" <<__LINE__;
1415 throw ConversionException(msg.str());
1419 //! Adapt a Python input port to a Python output port
1421 * No need to make conversion or cast.
1422 * Only check, it's possible.
1423 * \param inport : InputPort to adapt to Python type type
1424 * \param type : outport data type
1425 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1426 * \return an adaptator port of type InputPyPort
1429 InputPort* RuntimeSALOME::adaptPythonToPython(InputPyPort* inport,
1430 TypeCode * type,bool init)
1433 return new PyInit(inport);
1435 if(isAdaptablePyObjectPyObject(type,inport->edGetType()))
1437 //output data is convertible to input type
1438 //With python, no need to convert. Conversion will be done automatically
1439 //by the interpreter
1440 return new ProxyPort(inport);
1442 //output data is not convertible to input type
1444 msg << "Cannot connect Python output port with type: " << type->id() ;
1445 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1447 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1449 throw ConversionException(msg.str());
1452 //! Adapt a Python input port to a C++ output port
1454 * \param inport : InputPort to adapt to C++ type type
1455 * \param type : outport data type
1456 * \return an adaptator port of C++ type (InputCppPort)
1459 InputPort* RuntimeSALOME::adaptPythonToCpp(InputPyPort* inport,
1462 DEBTRACE("RuntimeSALOME::adaptPythonToCpp(InputPyPort* inport" );
1463 if(isAdaptablePyObjectCpp(type,inport->edGetType()))
1465 //output type is convertible to input type
1466 return new CppPy(inport);
1468 //output type is not convertible
1470 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1471 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1473 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1475 throw ConversionException(msg.str());
1478 //! Adapt a Python input port to a Neutral data port
1480 * \param inport : InputPort to adapt to Neutral type type
1481 * \param type : outport data type
1482 * \return an adaptator port of Neutral type (Neutralxxxx)
1485 InputPort* RuntimeSALOME::adaptPythonToNeutral(InputPyPort* inport,
1488 if(inport->edGetType()->kind() == Double)
1490 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyDouble(inport);
1492 else if(inport->edGetType()->kind() == Int)
1494 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyInt(inport);
1496 else if(inport->edGetType()->kind() == String)
1498 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyString(inport);
1500 else if(inport->edGetType()->kind() == Bool)
1502 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyBool(inport);
1504 else if(inport->edGetType()->kind() == Objref)
1506 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyObjref(inport);
1508 else if(inport->edGetType()->kind() == Sequence)
1510 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))
1511 return new NeutralPySequence(inport);
1515 msg << "Cannot convert this sequence type " ;
1516 msg << __FILE__ << ":" <<__LINE__;
1517 throw ConversionException(msg.str());
1520 else if(inport->edGetType()->kind() == Struct)
1522 if(isAdaptablePyObjectNeutral(type,inport->edGetType())) return new NeutralPyStruct(inport);
1525 // Adaptation not possible
1527 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1528 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1530 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1532 throw ConversionException(msg.str());
1535 //! Adapt a Python input port to a Corba output port
1537 * Always convert the data
1538 * \param inport : InputPort to adapt to Corba type type
1539 * \param type : outport data type
1540 * \return an adaptator port of Corba type (InputCorbaPort)
1543 InputPort* RuntimeSALOME::adaptPythonToCorba(InputPyPort* inport,
1546 if(inport->edGetType()->kind() == Double)
1548 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyDouble(inport);
1550 else if(inport->edGetType()->kind() == Int)
1552 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyInt(inport);
1554 else if(inport->edGetType()->kind() == String)
1556 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyString(inport);
1558 else if(inport->edGetType()->kind() == Bool)
1560 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyBool(inport);
1562 else if(inport->edGetType()->kind() == Objref)
1564 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1566 return new CorbaPyObjref(inport);
1571 msg << "Cannot connect InputCorbaPort : incompatible objref types " << type->id() << " " << inport->edGetType()->id();
1572 msg << " " << __FILE__ << ":" <<__LINE__;
1573 throw ConversionException(msg.str());
1576 else if(inport->edGetType()->kind() == Sequence)
1578 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1580 return new CorbaPySequence(inport);
1585 msg << "Cannot convert this sequence type " ;
1586 msg << __FILE__ << ":" <<__LINE__;
1587 throw ConversionException(msg.str());
1590 else if(inport->edGetType()->kind() == YACS::ENGINE::Struct)
1592 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1594 return new CorbaPyStruct(inport);
1599 msg << "Cannot convert this struct type " << type->id() << " to " << inport->edGetType()->id();
1600 msg << " " << __FILE__ << ":" <<__LINE__;
1601 throw ConversionException(msg.str());
1604 // Adaptation not possible
1606 msg << "Cannot connect Corba output port with type: " << type->id() ;
1607 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1609 msg << " ("__FILE__ << ":" << __LINE__ << ")";
1611 throw ConversionException(msg.str());
1614 //! Adapt a Python input port to a Xml output port
1616 * \param inport : input port to adapt to Xml type type
1617 * \param type : output port type
1618 * \return an input port of type InputXmlPort
1621 InputPort* RuntimeSALOME::adaptPythonToXml(InputPyPort* inport,
1624 // BEWARE : using the generic check
1625 if(inport->edGetType()->isAdaptable(type))
1628 return new XmlPython(inport);
1630 //non convertible type
1632 msg << "Cannot connect Xml output port with type: " << type->id() ;
1633 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1635 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1637 throw ConversionException(msg.str());
1640 //! Adapt a Python input port to an output port with a given implementation
1642 * \param source : input port to adapt to implementation impl and type type
1643 * \param impl : output port implementation (C++, Python or Corba)
1644 * \param type : output port type
1645 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1646 * \return adaptated input port
1649 InputPort* RuntimeSALOME::adapt(InputPyPort* source,
1650 const std::string& impl,
1651 TypeCode * type,bool init)
1653 if(impl == CppNode::IMPL_NAME)
1655 return adaptPythonToCpp(source,type);
1657 else if(impl == PythonNode::IMPL_NAME)
1659 return adaptPythonToPython(source,type,init);
1661 else if(impl == CORBANode::IMPL_NAME)
1663 return adaptPythonToCorba(source,type);
1665 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1667 return adaptPythonToNeutral(source,type);
1669 else if(impl == XmlNode::IMPL_NAME)
1671 return adaptPythonToXml(source,type);
1676 msg << "Cannot connect InputPyPort : unknown implementation " << impl;
1677 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1678 throw ConversionException(msg.str());
1683 //! Adapt a C++ input port to connect it to a CORBA output port
1685 * \param inport : input port to adapt to CORBA type type
1686 * \param type : type supported by output port
1687 * \return an adaptator port of type InputCorbaPort
1690 InputPort* RuntimeSALOME::adaptCppToCorba(InputCppPort* inport,
1693 DEBTRACE("RuntimeSALOME::adaptCppToCorba(InputCppPort* inport)");
1694 if(isAdaptableCppCorba(type,inport->edGetType()))
1696 //output type is convertible to input type
1697 return new CorbaCpp(inport);
1699 //output type is not convertible
1701 msg << "Cannot connect Corba output port with type: " << type->id() ;
1702 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1704 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1706 throw ConversionException(msg.str());
1709 //! Adapt a C++ input port to a Python output port
1711 * \param inport : input port to adapt to Python type type
1712 * \param type : output port type
1713 * \return an adaptated input port of type InputPyPort
1715 InputPort* RuntimeSALOME::adaptCppToPython(InputCppPort* inport,
1718 DEBTRACE("RuntimeSALOME::adaptCppToPython(InputCppPort* inport)");
1719 if(isAdaptableCppPyObject(type,inport->edGetType()))
1721 //output type is convertible to input type
1722 return new PyCpp(inport);
1724 //output type is not convertible
1726 msg << "Cannot connect Python output port with type: " << type->id() ;
1727 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1729 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1731 throw ConversionException(msg.str());
1734 //! Adapt a C++ input port to a C++ output port
1736 * \param inport : input port to adapt to C++ type type
1737 * \param type : output port type
1738 * \return an adaptated input port of type InputPyPort
1740 InputPort* RuntimeSALOME::adaptCppToCpp(InputCppPort* inport,
1743 DEBTRACE("RuntimeSALOME::adaptCppToCpp(InputPort* inport" );
1744 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1745 if(type->isAdaptable(inport->edGetType()))
1747 //the output data is convertible to inport type
1748 return new CppCpp(inport);
1750 //non convertible type
1752 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1753 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1755 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1757 throw ConversionException(msg.str());
1760 //! Adapt a C++ input port to a Neutral output port
1762 * \param inport : input port to adapt to C++ type type
1763 * \param type : output port type
1764 * \return an adaptated input port of type InputPyPort
1766 InputPort* RuntimeSALOME::adaptCppToNeutral(InputCppPort* inport,
1769 DEBTRACE("RuntimeSALOME::adaptCppToNeutral(InputPort* inport" );
1770 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1771 if(type->isAdaptable(inport->edGetType()))
1773 //the output data is convertible to inport type
1774 return new NeutralCpp(inport);
1776 //non convertible type
1778 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1779 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1781 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1783 throw ConversionException(msg.str());
1786 InputPort* RuntimeSALOME::adaptCppToXml(InputCppPort* inport,
1789 DEBTRACE("RuntimeSALOME::adaptCppToXml(InputCppPort* inport" );
1790 if(isAdaptableCppXml(type,inport->edGetType()))
1793 return new XmlCpp(inport);
1795 //non convertible type
1797 msg << "Cannot connect Xml output port with type: " << type->id() ;
1798 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1800 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1802 throw ConversionException(msg.str());
1805 //! Adapt a C++ input port to connect it to an output port with a given implementation
1807 * \param source : input port to adapt to implementation impl and type type
1808 * \param impl : output port implementation (C++, Python or Corba)
1809 * \param type : output port supported type
1810 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1811 * \return the adaptated port
1814 InputPort* RuntimeSALOME::adapt(InputCppPort* source,
1815 const std::string& impl,
1816 TypeCode * type,bool init)
1818 DEBTRACE("RuntimeSALOME::adapt(InputCppPort* source)");
1819 if(impl == CORBANode::IMPL_NAME)
1821 return adaptCppToCorba(source,type);
1823 else if(impl == PythonNode::IMPL_NAME)
1825 return adaptCppToPython(source,type);
1827 else if(impl == XmlNode::IMPL_NAME)
1829 return adaptCppToXml(source,type);
1831 else if(impl == CppNode::IMPL_NAME)
1833 return adaptCppToCpp(source, type);
1835 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1837 return adaptCppToNeutral(source, type);
1842 msg << "Cannot connect InputCppPort to " << impl << " implementation";
1843 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1844 throw ConversionException(msg.str());
1848 // bool RuntimeSALOME::isCompatible(const OutputPort* outputPort,
1849 // const InputPort* inputPort)
1851 // bool result=true;
1855 CORBA::ORB_ptr RuntimeSALOME::getOrb() const
1860 PyObject * RuntimeSALOME::getPyOrb() const
1865 PyObject * RuntimeSALOME::getBuiltins() const
1870 DynamicAny::DynAnyFactory_ptr RuntimeSALOME::getDynFactory() const
1875 PyObject * RuntimeSALOME::get_omnipy()
1880 omniORBpyAPI* RuntimeSALOME::getApi()
1885 void* RuntimeSALOME::convertNeutral(TypeCode * type, Any *data)
1888 return (void *)convertNeutralPyObject(type,data);
1892 return (void *)Py_None;
1896 std::string RuntimeSALOME::convertNeutralAsString(TypeCode * type, Any *data)
1901 // The call to PyGILState_Ensure was moved here because there was also
1902 // a crash when calling convertNeutralPyObject with a sequence of pyobj.
1903 // see also the comment below.
1904 PyGILState_STATE gstate = PyGILState_Ensure();
1905 ob=convertNeutralPyObject(type,data);
1906 std::string s=convertPyObjectToString(ob);
1908 // Note (Renaud Barate, 8 jan 2013): With Python 2.7, this call to Py_DECREF causes a crash
1909 // (SIGSEGV) when ob is a sequence and the call is not protected with the global interpreter
1910 // lock. I thus added the call to PyGILState_Ensure / PyGILState_Release. It worked fine in
1911 // Python 2.6 without this call. If anyone finds the real reason of this bug and another fix,
1912 // feel free to change this code.
1913 //PyGILState_STATE gstate = PyGILState_Ensure();
1915 PyGILState_Release(gstate);
1924 std::string RuntimeSALOME::convertPyObjectToString(PyObject* ob)
1926 return YACS::ENGINE::convertPyObjectToString(ob);
1929 PyObject* RuntimeSALOME::convertStringToPyObject(const std::string& s)
1934 PyGILState_STATE gstate = PyGILState_Ensure();
1935 mainmod = PyImport_AddModule("__main__");
1936 globals = PyModule_GetDict(mainmod);
1937 PyObject* d = PyDict_New();
1938 //PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
1939 ob= PyRun_String( s.c_str(), Py_eval_input, globals, d);
1945 PyObject* new_stderr = newPyStdOut(error);
1946 PySys_SetObject((char *)"stderr", new_stderr);
1948 PySys_SetObject((char *)"stderr", PySys_GetObject((char *)"__stderr__"));
1949 Py_DECREF(new_stderr);
1950 PyGILState_Release(gstate);
1951 throw Exception(error);
1953 PyGILState_Release(gstate);