1 // Copyright (C) 2006-2023 CEA, EDF
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"
62 #include "PythonCppUtils.hxx"
65 #include "PythonNode.hxx"
66 #include "CORBANode.hxx"
67 #include "XMLNode.hxx"
68 #include "CppNode.hxx"
69 #include "PresetNode.hxx"
70 #include "OutNode.hxx"
71 #include "StudyNodes.hxx"
72 #include "SalomePythonNode.hxx"
73 #include "DistributedPythonNode.hxx"
76 #include "CORBACORBAConv.hxx"
77 #include "CORBAPythonConv.hxx"
78 #include "CORBAXMLConv.hxx"
79 #include "CORBACppConv.hxx"
80 #include "CORBANeutralConv.hxx"
82 #include "TypeConversions.hxx"
84 #include "PythonCORBAConv.hxx"
85 #include "PythonXMLConv.hxx"
86 #include "PythonCppConv.hxx"
87 #include "PythonNeutralConv.hxx"
88 #include "PythonInitConv.hxx"
91 #include "NeutralCORBAConv.hxx"
92 #include "NeutralPythonConv.hxx"
93 #include "NeutralXMLConv.hxx"
94 #include "NeutralCppConv.hxx"
95 #include "NeutralInitConv.hxx"
98 #include "CppCORBAConv.hxx"
99 #include "CppPythonConv.hxx"
100 #include "CppXMLConv.hxx"
101 #include "CppCppConv.hxx"
102 #include "CppNeutralConv.hxx"
105 #include "XMLCORBAConv.hxx"
106 #include "XMLPythonConv.hxx"
107 #include "XMLCppConv.hxx"
108 #include "XMLNeutralConv.hxx"
110 //Calcium specific ports
111 #include "CalStreamPort.hxx"
114 #include "SALOME_NamingService_Wrapper.hxx"
115 #include "SALOME_LifeCycleCORBA.hxx"
116 #include "SALOME_ResourcesManager.hxx"
117 #include "SALOME_ContainerManager.hxx"
118 #include "SALOMEconfig.h"
119 #include "SALOME_Embedded_NamingService.hxx"
120 #include CORBA_CLIENT_HEADER(SALOME_ContainerManager)
124 #include <libxml/parser.h>
125 #include <omniORB4/CORBA.h>
133 #include "YacsTrace.hxx"
136 using namespace YACS::ENGINE;
138 std::unique_ptr<SALOME_NamingService_Container_Abstract> RuntimeSALOME::getNS()
140 std::unique_ptr<SALOME_NamingService_Container_Abstract> ret(new SALOME_NamingService_Wrapper);
144 void RuntimeSALOME::setRuntime(long flags, int argc, char* argv[]) // singleton creation (not thread safe!)
146 if (! Runtime::_singleton)
148 RuntimeSALOME* r=new RuntimeSALOME(flags, argc, argv);
149 Runtime::_singleton = r;
152 DEBTRACE("RuntimeSALOME::setRuntime() done !");
155 RuntimeSALOME* YACS::ENGINE::getSALOMERuntime()
157 YASSERT(RuntimeSALOME::getSingleton());
158 return dynamic_cast< RuntimeSALOME* >(RuntimeSALOME::getSingleton());
162 * Singleton creation, initialize converter map
165 RuntimeSALOME::RuntimeSALOME()
170 void RuntimeSALOME::initBuiltins()
172 //Fill the builtin catalog with nodes specific to the runtime
173 std::map<std::string,TypeCode*>& typeMap=_builtinCatalog->_typeMap;
174 std::map<std::string,Node*>& nodeMap=_builtinCatalog->_nodeMap;
175 std::map<std::string,ComposedNode*>& composednodeMap=_builtinCatalog->_composednodeMap;
176 std::map<std::string,ComponentDefinition*>& componentMap=_builtinCatalog->_componentMap;
177 nodeMap["PyFunction"]=new PyFuncNode("PyFunction");
178 nodeMap["PyScript"]=new PythonNode("PyScript");
179 nodeMap["CORBANode"]=new CORBANode("CORBANode");
180 nodeMap["XmlNode"]=new XmlNode("XmlNode");
181 nodeMap["SalomeNode"]=new SalomeNode("SalomeNode");
182 nodeMap["CppNode"]=new CppNode("CppNode");
183 nodeMap["SalomePythonNode"]=new SalomePythonNode("SalomePythonNode");
184 nodeMap["PresetNode"]=new PresetNode("PresetNode");
185 nodeMap["OutNode"]=new OutNode("OutNode");
186 nodeMap["StudyInNode"]=new StudyInNode("StudyInNode");
187 nodeMap["StudyOutNode"]=new StudyOutNode("StudyOutNode");
188 composednodeMap["OptimizerLoop"]=createOptimizerLoop("OptimizerLoop","","",true);
189 typeMap["dblevec"]= createSequenceTc("dblevec","dblevec",_tc_double);
190 typeMap["intvec"]= createSequenceTc("intvec","intvec",_tc_int);
191 typeMap["stringvec"]= createSequenceTc("stringvec","stringvec",_tc_string);
192 typeMap["boolvec"]= createSequenceTc("boolvec","boolvec",_tc_bool);
193 typeMap["seqdblevec"]= createSequenceTc("seqdblevec","seqdblevec",typeMap["dblevec"]);
194 typeMap["seqintvec"]= createSequenceTc("seqintvec","seqintvec",typeMap["intvec"]);
195 typeMap["seqstringvec"]= createSequenceTc("seqstringvec","seqstringvec",typeMap["stringvec"]);
196 typeMap["seqboolvec"]= createSequenceTc("seqboolvec","seqboolvec",typeMap["boolvec"]);
197 std::list<TypeCodeObjref *> ltc;
198 typeMap["pyobj"]= createInterfaceTc("python:obj:1.0","pyobj",ltc);
199 typeMap["seqpyobj"]= createSequenceTc("seqpyobj","seqpyobj",typeMap["pyobj"]);
200 composednodeMap["Bloc"]=createBloc("Bloc");
201 composednodeMap["Switch"]=createSwitch("Switch");
202 composednodeMap["WhileLoop"]=createWhileLoop("WhileLoop");
203 composednodeMap["ForLoop"]=createForLoop("ForLoop");
204 composednodeMap["ForEachLoop_double"]=createForEachLoop("ForEachLoop_double",Runtime::_tc_double);
205 composednodeMap["ForEachLoop_string"]=createForEachLoop("ForEachLoop_string",Runtime::_tc_string);
206 composednodeMap["ForEachLoop_int"]=createForEachLoop("ForEachLoop_int",Runtime::_tc_int);
207 composednodeMap["ForEachLoop_bool"]=createForEachLoop("ForEachLoop_bool",Runtime::_tc_bool);
208 composednodeMap["ForEachLoop_pyobj"]=createForEachLoop("ForEachLoop_pyobj",typeMap["pyobj"]);;
209 ENGINE::TypeCodeStruct *t = createStructTc("","Engines/dataref");
210 t->addMember("ref",_tc_string);
211 typeMap["dataref"]= t;
214 RuntimeSALOME::RuntimeSALOME(long flags, int argc, char* argv[])
216 // If all flags (apart the IsPyExt flags) are unset, force them to true
217 if ((flags - flags & RuntimeSALOME::IsPyExt) == 0)
218 flags += RuntimeSALOME::UseCorba + RuntimeSALOME::UsePython
219 + RuntimeSALOME::UseCpp + RuntimeSALOME::UseXml;
221 // Salome Nodes implies Corba Nodes
222 if (flags & RuntimeSALOME::UseSalome)
223 flags |= RuntimeSALOME::UseCorba;
225 // Corba Nodes implies Python Nodes
226 if (flags & RuntimeSALOME::UseCorba)
227 flags |= RuntimeSALOME::UsePython;
229 _useCorba = flags & RuntimeSALOME::UseCorba;
230 _usePython = flags & RuntimeSALOME::UsePython;
231 _useCpp = flags & RuntimeSALOME::UseCpp;
232 _useXml = flags & RuntimeSALOME::UseXml;
237 if (_useCpp) _setOfImplementation.insert(CppNode::IMPL_NAME);
238 if (_usePython) _setOfImplementation.insert(PythonNode::IMPL_NAME);
239 if (_useCorba) _setOfImplementation.insert(CORBANode::IMPL_NAME);
240 if (_useXml) _setOfImplementation.insert(XmlNode::IMPL_NAME);
241 init(flags, argc, argv);
244 RuntimeSALOME::~RuntimeSALOME()
246 DEBTRACE("RuntimeSALOME::~RuntimeSALOME");
247 // destroy catalog loader prototypes
248 std::map<std::string, CatalogLoader*>::const_iterator pt;
249 for(pt=_catalogLoaderFactoryMap.begin();pt!=_catalogLoaderFactoryMap.end();pt++)
253 _connectionManager.ShutdownWithExit();
256 void RuntimeSALOME::loadModulCatalog()
259 const char * SCRIPT = "from salome_kernel import list_of_catalogs_regarding_environement\n"
260 "import KernelModuleCatalog\n"
261 "KernelModuleCatalog.myModuleCatalog( list_of_catalogs_regarding_environement() )\n";
262 PyRun_SimpleString(SCRIPT);
265 //! CORBA and Python initialization
267 * \param flags contains several bits
268 * bit0 (ispyext) true when method is called from Python
269 * (Python initialization must not be done!)
270 * bit1 (UsePython) true if python nodes are needed
271 * bit1 (UseCorba) true if CORBA nodes are needed
272 * bit1 (UseXml) true if python nodes are needed
273 * bit1 (UseCpp) true if C++ nodes are needed
274 * bit1 (UseSalome) true if Salome nodes are needed
275 * \param argc number of command line arguments (used to initialize the Python interpreter)
276 * \param argv command line arguments (used to initialize the Python interpreter)
280 void RuntimeSALOME::init(long flags, int argc, char* argv[])
282 bool ispyext = flags & RuntimeSALOME::IsPyExt;
285 PortableServer::POA_var root_poa;
286 PortableServer::POAManager_var pman;
287 CORBA::Object_var obj;
288 int nbargs = 0; char **args = 0;
289 _orb = CORBA::ORB_init (nbargs, args);
290 obj = _orb->resolve_initial_references("RootPOA");
291 root_poa = PortableServer::POA::_narrow(obj);
292 pman = root_poa->the_POAManager();
296 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
298 obj = _orb->resolve_initial_references("DynAnyFactory");
299 _dynFactory = DynamicAny::DynAnyFactory::_narrow(obj);
304 DEBTRACE("RuntimeSALOME::init, is python extension = " << ispyext);
306 // Initialize Python interpreter in embedded mode
307 if (!Py_IsInitialized())
309 #if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
312 Py_InitializeEx(0); // do not install signal handlers
314 if (argc > 0 && argv != NULL)
316 wchar_t **changed_argv = new wchar_t*[argc];
317 for (int i = 0; i < argc; i++)
319 changed_argv[i] = Py_DecodeLocale(argv[i], NULL);
321 PySys_SetArgv(argc, changed_argv);
327 char defaultName[] = "SALOME_YACS_RUNTIME";
328 wchar_t **changed_pyArgv = new wchar_t*[pyArgc];
329 pyArgv[0] = defaultName;
330 for (int i = 0; i < pyArgc; i++)
332 changed_pyArgv[i] = Py_DecodeLocale(pyArgv[i], NULL);
334 PySys_SetArgv(pyArgc, changed_pyArgv);
336 #if PY_VERSION_HEX < 0x03070000
337 PyEval_InitThreads(); /* Create (and acquire) the interpreter lock (for threads)*/
339 PyEval_SaveThread(); /* Release the thread state */
340 //here we do not have the Global Interpreter Lock
343 PyObject *mainmod,*pyapi,*res ;
345 PyGILState_STATE gstate;
346 gstate = PyGILState_Ensure(); // acquire the Global Interpreter Lock
348 mainmod = PyImport_AddModule("__main__");
349 globals = PyModule_GetDict(mainmod);
350 /* globals is a borrowed reference */
352 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
354 PyObject *bimod = PyImport_ImportModule("builtins");
355 if (bimod == NULL || PyDict_SetItemString(globals, "__builtins__", bimod) != 0)
356 Py_FatalError("can't add __builtins__ to __main__");
360 _bltins = PyEval_GetBuiltins(); /* borrowed ref */
366 _omnipy = PyImport_ImportModule((char*)"_omnipy");
370 PyErr_SetString(PyExc_ImportError, (char*)"Cannot import _omnipy");
373 pyapi = PyObject_GetAttrString(_omnipy, (char*)"API");
378 _api = (omniORBpyAPI*)PyCapsule_GetPointer(pyapi,"_omnipy.API");
381 res=PyRun_String("\n"
382 "from math import *\n"
384 "sys.path.insert(0,'.')\n"
385 "from omniORB import CORBA\n"
386 "from omniORB import any\n"
387 "orb = CORBA.ORB_init([], CORBA.ORB_ID)\n"
388 "#print(sys.getrefcount(orb))\n"
394 Py_file_input,globals,globals );
402 _pyorb = PyDict_GetItemString(globals,"orb");
403 /* PyDict_GetItemString returns a borrowed reference. There is no need to decref _pyorb */
406 pyany = PyDict_GetItemString(globals,"any");
407 /* PyDict_GetItemString returns a borrowed reference. There is no need to decref pyany */
410 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
414 PyGILState_Release(gstate); // Release the Global Interpreter Lock
418 // initialize the catalogLoaderFactory map with the session one
419 _catalogLoaderFactoryMap["session"]=new SessionCataLoader;
423 void RuntimeSALOME::fini()
427 PyGILState_STATE gstate = PyGILState_Ensure();
429 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
431 PyObject *mainmod, *globals;
432 mainmod = PyImport_AddModule("__main__");
433 globals = PyModule_GetDict(mainmod);
437 res=PyRun_String("orb.destroy()\n"
439 Py_file_input,globals,globals );
445 std::map<std::string,Node*>& nodeMap=_builtinCatalog->_nodeMap;
446 delete nodeMap["PyFunction"];
447 delete nodeMap["PyScript"];
448 delete nodeMap["SalomePythonNode"];
449 nodeMap.erase("PyFunction");
450 nodeMap.erase("PyScript");
451 nodeMap.erase("SalomePythonNode");
455 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
463 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
470 PyObject *RuntimeSALOME::launchSubProcess(const std::vector<std::string>& cmds)
472 std::ostringstream oss; oss << "from subprocess import Popen" << std::endl;
473 oss << "p = Popen([";
474 for(auto i = 0 ; i < cmds.size() ; ++i)
476 oss << " " << "\"" << cmds[i] << "\"";
477 if(i < cmds.size()-1)
484 AutoPyRef context = PyDict_New();
485 PyDict_SetItemString( context, "__builtins__", getBuiltins() );
486 std::string errorDetails;
489 PythonNode::ExecuteLocalInternal(oss.str().c_str(),context,errorDetails);
491 catch(const YACS::Exception& e)
493 std::cerr << e.what() << std::endl << errorDetails << std::endl;
496 PyObject *ret = PyDict_GetItemString(context,"p");
502 std::vector< std::pair<std::string,int> > RuntimeSALOME::getCatalogOfComputeNodes() const
504 CORBA::ORB_ptr orb(getOrb());
505 SALOME_NamingService_Wrapper namingService;
508 namingService.init_orb(orb);
510 catch(SALOME_Exception& e)
512 throw Exception("RuntimeSALOME::getCatalogOfComputeNodes : Unable to contact the SALOME Naming Service");
514 CORBA::Object_var obj(namingService.Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS));
515 if(CORBA::is_nil(obj))
516 throw Exception("RuntimeSALOME::getCatalogOfComputeNodes : Unable to access to the resource manager !");
517 Engines::ResourcesManager_var resManager(Engines::ResourcesManager::_narrow(obj));
518 if(CORBA::is_nil(resManager))
519 throw Exception("RuntimeSALOME::getCatalogOfComputeNodes : Internal error ! The entry attached to the res manager in NS does not have right type !");
520 std::vector< std::pair<std::string,int> > ret;
521 Engines::ResourceParameters params;
523 params.hostname = "";
527 params.cpu_clock = 0;
529 params.nb_proc_per_node = 0;
531 params.can_launch_batch_jobs = false;
532 params.can_run_containers = true;
533 params.componentList.length(0);
536 Engines::ResourceList_var resourceList;
537 resourceList = resManager->GetFittingResources(params);
538 ret.reserve(resourceList->length());
539 for(int i = 0; i<resourceList->length(); i++)
541 const char* resource_name = resourceList[i];
542 std::string std_resource_name = resource_name;
543 Engines::ResourceDefinition_var resource_definition
544 = resManager->GetResourceDefinition(resource_name);
545 int nb_cores = resource_definition->nb_node *
546 resource_definition->nb_proc_per_node;
547 ret.push_back(std::pair<std::string,int>(resource_name, nb_cores));
550 catch(SALOME::SALOME_Exception& e)
553 message=e.details.text.in();
554 throw Exception(message);
560 std::string RuntimeSALOME::getVersion() const
562 #ifdef YACS_DEVELOPMENT
563 return CORBA::string_dup(YACS_VERSION_STR"dev");
565 return CORBA::string_dup(YACS_VERSION_STR);
569 Proc* RuntimeSALOME::createProc(const std::string& name)
571 return new SalomeProc(name);
574 TypeCode * RuntimeSALOME::createInterfaceTc(const std::string& id, const std::string& name,
575 std::list<TypeCodeObjref *> ltc)
578 if(id == "") myName = "IDL:" + name + ":1.0";
580 return TypeCode::interfaceTc(myName.c_str(),name.c_str(),ltc);
583 TypeCode * RuntimeSALOME::createSequenceTc(const std::string& id,
584 const std::string& name,
587 return TypeCode::sequenceTc(id.c_str(),name.c_str(),content);
590 TypeCodeStruct * RuntimeSALOME::createStructTc(const std::string& id, const std::string& name)
593 if(id == "") myName = "IDL:" + name + ":1.0";
595 return (TypeCodeStruct *)TypeCode::structTc(myName.c_str(),name.c_str());
598 Bloc* RuntimeSALOME::createBloc(const std::string& name)
600 return new Bloc(name);
603 WhileLoop* RuntimeSALOME::createWhileLoop(const std::string& name)
605 return new WhileLoop(name);
608 ForLoop* RuntimeSALOME::createForLoop(const std::string& name)
610 return new ForLoop(name);
613 OptimizerLoop* RuntimeSALOME::createOptimizerLoop(const std::string& name,const std::string& algLib,const std::string& factoryName,
614 bool algInitOnFile, const std::string& kind, Proc * procForTypes)
616 OptimizerLoop * ol = (kind == "base") ? new OptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes) :
617 new SalomeOptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes);
618 ol->edGetNbOfBranchesPort()->edInit(1);
622 DataNode* RuntimeSALOME::createInDataNode(const std::string& kind,const std::string& name)
627 node = new PresetNode(name);
630 else if(kind == "study" )
632 return new StudyInNode(name);
634 std::string msg="DataNode kind ("+kind+") unknown";
635 throw Exception(msg);
638 DataNode* RuntimeSALOME::createOutDataNode(const std::string& kind,const std::string& name)
642 return new OutNode(name);
644 else if(kind == "study" )
646 return new StudyOutNode(name);
649 std::string msg="OutDataNode kind ("+kind+") unknown";
650 throw Exception(msg);
653 InlineFuncNode* RuntimeSALOME::createFuncNode(const std::string& kind,const std::string& name)
655 InlineFuncNode* node;
656 if(kind == "" || kind == SalomeNode::KIND || kind == PythonNode::KIND)
658 node = new PyFuncNode(name);
661 if(kind == DistributedPythonNode::KIND)
662 return new DistributedPythonNode(name);
663 std::string msg="FuncNode kind ("+kind+") unknown";
664 throw Exception(msg);
667 InlineNode* RuntimeSALOME::createScriptNode(const std::string& kind,const std::string& name)
670 if(kind == "" || kind == SalomeNode::KIND || kind == PythonNode::KIND)
672 node = new PythonNode(name);
675 std::string msg="ScriptNode kind ("+kind+") unknown";
676 throw Exception(msg);
679 ServiceNode* RuntimeSALOME::createRefNode(const std::string& kind,const std::string& name)
682 if(kind == "" || kind == SalomeNode::KIND || kind == CORBANode::KIND)
684 node = new CORBANode(name);
687 else if(kind == XmlNode::KIND)
689 node = new XmlNode(name);
692 std::string msg="RefNode kind ("+kind+") unknown";
693 throw Exception(msg);
696 ServiceNode* RuntimeSALOME::createCompoNode(const std::string& kind,const std::string& name)
699 if(kind == "" || kind == SalomeNode::KIND )
701 node=new SalomeNode(name);
704 else if (kind == CppNode::KIND)
706 node = new CppNode(name);
709 std::string msg="CompoNode kind ("+kind+") unknown";
710 throw Exception(msg);
713 ServiceInlineNode *RuntimeSALOME::createSInlineNode(const std::string& kind, const std::string& name)
715 if(kind == "" || kind == SalomeNode::KIND )
716 return new SalomePythonNode(name);
717 std::string msg="CompoNode kind ("+kind+") unknown";
718 throw Exception(msg);
721 ComponentInstance* RuntimeSALOME::createComponentInstance(const std::string& name,
722 const std::string& kind)
724 ComponentInstance* compo;
725 if(kind == "" || kind == SalomeComponent::KIND)
726 return new SalomeComponent(name);
727 else if(kind == CORBAComponent::KIND)
728 return new CORBAComponent(name);
729 else if(kind == SalomePythonComponent::KIND)
730 return new SalomePythonComponent(name);
731 else if (kind == CppComponent::KIND)
732 return new CppComponent(name);
733 else if (kind == SalomeHPComponent::KIND)
734 return new SalomeHPComponent(name);
735 std::string msg="Component Instance kind ("+kind+") unknown";
736 throw Exception(msg);
739 Container *RuntimeSALOME::createContainer(const std::string& kind)
741 if(kind == "" || kind == SalomeContainer::KIND)
742 return new SalomeContainer;
743 if(kind==SalomeHPContainer::KIND)
744 return new SalomeHPContainer;
745 else if (kind == CppContainer::KIND)
746 return new CppContainer;
747 std::string msg="Container kind ("+kind+") unknown";
748 throw Exception(msg);
751 InputPort * RuntimeSALOME::createInputPort(const std::string& name,
752 const std::string& impl,
756 if(impl == CppNode::IMPL_NAME)
758 return new InputCppPort(name, node, type);
760 else if(impl == PythonNode::IMPL_NAME)
762 return new InputPyPort(name, node, type);
764 else if(impl == CORBANode::IMPL_NAME)
766 return new InputCorbaPort(name, node, type);
768 else if(impl == XmlNode::IMPL_NAME)
770 return new InputXmlPort(name, node, type);
775 msg << "Cannot create " << impl << " InputPort" ;
776 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
777 throw Exception(msg.str());
781 OutputPort * RuntimeSALOME::createOutputPort(const std::string& name,
782 const std::string& impl,
786 if(impl == CppNode::IMPL_NAME)
788 return new OutputCppPort(name, node, type);
790 else if(impl == PythonNode::IMPL_NAME)
792 return new OutputPyPort(name, node, type);
794 else if(impl == CORBANode::IMPL_NAME)
796 return new OutputCorbaPort(name, node, type);
798 else if(impl == XmlNode::IMPL_NAME)
800 return new OutputXmlPort(name, node, type);
805 msg << "Cannot create " << impl << " OutputPort" ;
806 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
807 throw Exception(msg.str());
811 InputDataStreamPort* RuntimeSALOME::createInputDataStreamPort(const std::string& name,
812 Node *node,TypeCode *type)
814 DEBTRACE("createInputDataStreamPort: " << name << " " << type->shortName());
815 if(type->kind() == Objref && std::string(type->shortName(),7) == "CALCIUM")
817 return new InputCalStreamPort(name,node,type);
821 return new InputDataStreamPort(name,node,type);
825 OutputDataStreamPort* RuntimeSALOME::createOutputDataStreamPort(const std::string& name,
826 Node *node,TypeCode *type)
828 DEBTRACE("createOutputDataStreamPort: " << name << " " << type->shortName());
829 if(type->kind() == Objref && std::string(type->shortName(),7) == "CALCIUM")
831 return new OutputCalStreamPort(name,node,type);
835 return new OutputDataStreamPort(name,node,type);
839 //! Main adapter function : adapt an InputPort to be able to connect it to an OutputPort with a possible different implementation
841 * \param source : InputPort to be adapted
842 * \param impl : new implementation (C++, python, CORBA, XML, Neutral)
843 * \param type : data type provided by the InputPort
844 * \param init : indicates if the adapted InputPort will be used for initialization (value true) or not (value false)
846 * \return : adapted InputPort
848 InputPort* RuntimeSALOME::adapt(InputPort* source,
849 const std::string& impl,
850 TypeCode * type,bool init)
852 string imp_source=source->getNode()->getImplementation();
853 if(imp_source == PythonNode::IMPL_NAME)
855 return adapt((InputPyPort*)source,impl,type,init);
857 else if(imp_source == CppNode::IMPL_NAME)
859 return adapt((InputCppPort*)source,impl,type,init);
861 else if(imp_source == CORBANode::IMPL_NAME)
863 return adapt((InputCorbaPort*)source,impl,type,init);
865 else if(imp_source == XmlNode::IMPL_NAME)
867 return adapt((InputXmlPort*)source,impl,type,init);
869 else if(imp_source == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
871 return adaptNeutral(source,impl,type,init);
876 msg << "Cannot adapt " << imp_source << " InputPort to " << impl;
877 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
878 throw ConversionException(msg.str());
882 //! Adapter function for InPropertyPort
884 * \param source : InPropertyPort to be adapted
885 * \param impl : new implementation (C++, python, CORBA, XML, Neutral)
886 * \param type : data type provided by the InPropertyPort
887 * \param init : indicates if the adapted InPropertyPort will be used for initialization (value true) or not (value false)
889 * \return : adapted InputPort
891 InputPort* RuntimeSALOME::adapt(InPropertyPort* source,
892 const std::string& impl,
893 TypeCode * type,bool init)
895 return adaptNeutral((InputPort *)source,impl,type,init);
898 //! Adapt a Neutral input port to a Corba output port
900 * \param inport : Neutral input port to adapt to Corba type type
901 * \param type : output port type
902 * \return an adaptated input port of type InputCorbaPort
904 InputPort* RuntimeSALOME::adaptNeutralToCorba(InputPort* inport,
907 // BEWARE : using the generic check
908 if(inport->edGetType()->isAdaptable(type))
910 //the output data is convertible to inport type
911 return new CorbaNeutral(inport);
913 //non convertible type
915 msg << "Cannot connect Corba output port with type: " << type->id() ;
916 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
918 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
920 throw ConversionException(msg.str());
923 //! Adapt a Neutral input port to a Python output port
925 * \param inport : input port to adapt to Python type type
926 * \param type : output port type
927 * \return an adaptated input port of type InputPyPort
929 InputPort* RuntimeSALOME::adaptNeutralToPython(InputPort* inport,
932 // BEWARE : using the generic check
933 if(inport->edGetType()->isAdaptable(type))
936 return new PyNeutral(inport);
938 //last chance : an py output that is seq[objref] can be connected to a neutral input objref (P13268)
939 else if(type->kind()==Sequence && type->contentType()->kind()==Objref && inport->edGetType()->kind()==Objref)
941 return new PyNeutral(inport);
943 //non convertible type
945 msg << "Cannot connect Python output port with type: " << type->id() ;
946 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
948 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
950 throw ConversionException(msg.str());
953 //! Adapt a Neutral input port to a Xml output port
955 * \param inport : input port to adapt to Xml type type
956 * \param type : output port type
957 * \return an input port of type InputXmlPort
959 InputPort* RuntimeSALOME::adaptNeutralToXml(InputPort* inport,
962 // BEWARE : using the generic check
963 if(inport->edGetType()->isAdaptable(type))
966 return new XmlNeutral(inport);
968 //non convertible type
970 msg << "Cannot connect Xml output port with type: " << type->id() ;
971 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
973 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
975 throw ConversionException(msg.str());
978 //! Adapt a Neutral input port to a C++ output port
980 * \param inport : input port to adapt to C++ type type
981 * \param type : output port type
982 * \return an input port of type InputCppPort
984 InputPort* RuntimeSALOME::adaptNeutralToCpp(InputPort* inport,
987 DEBTRACE("RuntimeSALOME::adaptNeutralToCpp(InputPort* inport" );
988 if(isAdaptableNeutralCpp(type,inport->edGetType()))
991 return new CppNeutral(inport);
993 //non convertible type
995 msg << "Cannot connect Cpp output port with type: " << type->id() ;
996 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
998 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1000 throw ConversionException(msg.str());
1003 //! Adapt a Neutral input port to connect it to an output port with a given implementation
1005 * \param source : Neutral input port to adapt to implementation impl and type type
1006 * \param impl : output port implementation (C++, Python, Corba, Xml or Neutral)
1007 * \param type : output port supported type
1008 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1009 * \return the adaptated port
1011 InputPort* RuntimeSALOME::adaptNeutral(InputPort* source,
1012 const std::string& impl,
1013 TypeCode * type,bool init)
1015 if(impl == CppNode::IMPL_NAME)
1017 return adaptNeutralToCpp(source,type);
1019 else if(impl == PythonNode::IMPL_NAME)
1021 return adaptNeutralToPython(source,type);
1023 else if(impl == CORBANode::IMPL_NAME)
1025 return adaptNeutralToCorba(source,type);
1027 else if(impl == XmlNode::IMPL_NAME )
1029 return adaptNeutralToXml(source,type);
1031 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1034 return new NeutralInit(source);
1036 return new ProxyPort(source);
1039 msg << "Cannot connect InputPort : unknown implementation " << impl;
1040 msg << " (" <<__FILE__ << ":" <<__LINE__ << ")";
1041 throw ConversionException(msg.str());
1044 //! Adapt a XML input port to connect it to a CORBA output port
1046 * \param inport : input port to adapt to CORBA type type
1047 * \param type : type supported by output port
1048 * \return an adaptator port of type InputCorbaPort
1051 InputPort* RuntimeSALOME::adaptXmlToCorba(InputXmlPort* inport,
1054 if(isAdaptableXmlCorba(type,inport->edGetType()))
1056 //output type is convertible to input type
1057 return new CorbaXml(inport);
1059 //output type is not convertible
1061 msg << "Cannot connect Corba output port with type: " << type->id() ;
1062 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1064 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1066 throw ConversionException(msg.str());
1069 //! Adapt a XML input port to a Python output port
1071 * \param inport : input port to adapt to Python type type
1072 * \param type : output port type
1073 * \return an adaptated input port of type InputPyPort
1075 InputPort* RuntimeSALOME::adaptXmlToPython(InputXmlPort* inport,
1078 if(inport->edGetType()->isAdaptable(type))
1080 //the output data is convertible to inport type
1081 return new PyXml(inport);
1083 //non convertible type
1085 msg << "Cannot connect Python output port with type: " << type->id() ;
1086 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1088 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1090 throw ConversionException(msg.str());
1093 //! Adapt a XML input port to a C++ output port
1095 * \param inport : input port to adapt to C++ type type
1096 * \param type : output port type
1097 * \return an adaptated input port of type InputPyPort
1099 InputPort* RuntimeSALOME::adaptXmlToCpp(InputXmlPort* inport,
1102 DEBTRACE("RuntimeSALOME::adaptXmlToCpp(InputPort* inport" );
1103 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1104 if(type->isAdaptable(inport->edGetType()))
1106 //the output data is convertible to inport type
1107 return new CppXml(inport);
1109 //non convertible type
1111 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1112 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1114 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1116 throw ConversionException(msg.str());
1119 //! Adapt a XML input port to a Neutral output port
1121 * \param inport : input port to adapt to Neutral type type
1122 * \param type : output port type
1123 * \return an adaptated input port of type Neutralxxxx
1125 InputPort* RuntimeSALOME::adaptXmlToNeutral(InputXmlPort* inport,
1128 if(inport->edGetType()->isAdaptable(type))
1130 //the output data is convertible to inport type
1131 return new NeutralXml(inport);
1133 //non convertible type
1135 msg << "Cannot connect Xml InputPort to OutputNeutralPort : " ;
1136 msg << "(" <<__FILE__ << ":" <<__LINE__<< ")";
1137 throw ConversionException(msg.str());
1140 //! Adapt a XML input port to a Xml output port
1142 * \param inport : input port to adapt to Xml type type
1143 * \param type : output port type
1144 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1145 * \return an adaptated input port of type Xmlxxxx
1147 InputPort* RuntimeSALOME::adaptXmlToXml(InputXmlPort* inport,
1148 TypeCode * type,bool init)
1151 return new ProxyPort(inport);
1153 if(inport->edGetType()->isAdaptable(type))
1154 return new ProxyPort(inport);
1157 msg << "Cannot connect Xml output port with type: " << type->id() ;
1158 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1160 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1162 throw ConversionException(msg.str());
1165 //! Adapt an Xml input port to an output port which implementation is given by impl
1167 * \param source : input port to adapt to implementation impl and type type
1168 * \param impl : output port implementation (C++, Python or Corba)
1169 * \param type : output port supported type
1170 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1171 * \return the adaptated port
1174 InputPort* RuntimeSALOME::adapt(InputXmlPort* source,
1175 const std::string& impl,
1176 TypeCode * type,bool init)
1178 if(impl == CORBANode::IMPL_NAME)
1180 return adaptXmlToCorba(source,type);
1182 else if(impl == PythonNode::IMPL_NAME)
1184 return adaptXmlToPython(source,type);
1186 else if(impl == CppNode::IMPL_NAME)
1188 return adaptXmlToCpp(source,type);
1190 else if(impl == XmlNode::IMPL_NAME )
1192 return adaptXmlToXml(source,type,init);
1194 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1196 return adaptXmlToNeutral(source,type);
1201 msg << "Cannot connect InputXmlPort to " << impl << " implementation";
1202 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1203 throw ConversionException(msg.str());
1208 //! Adapt a CORBA input port to a CORBA output port
1210 * \param inport : input port to adapt to CORBA outport data type
1211 * \param type : outport data type
1212 * \return an adaptator port of type InputCORBAPort
1214 InputPort* RuntimeSALOME::adaptCorbaToCorba(InputCorbaPort* inport,
1217 if(type->isA(inport->edGetType()))
1219 //types are compatible : no conversion
1220 //outport data type is more specific than inport required type
1221 //so the inport can be used safely
1222 return new ProxyPort(inport);
1224 else if(isAdaptableCorbaCorba(type,inport->edGetType()))
1226 //ouport data can be converted to inport data type
1227 return new CorbaCorba(inport);
1229 //outport data can not be converted
1231 msg << "Cannot connect Corba output port with type: " << type->id() ;
1232 msg << " to CORBA input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1234 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1236 throw ConversionException(msg.str());
1239 //! Adapt a CORBA input port to a Python output port
1241 * \param inport : input port to adapt to Python type type
1242 * \param type : outport data type
1243 * \return an adaptator port of type InputPyPort
1246 InputPort* RuntimeSALOME::adaptCorbaToPython(InputCorbaPort* inport,
1249 if(inport->edGetType()->kind() == Double)
1251 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaDouble(inport);
1253 else if(inport->edGetType()->kind() == Int)
1255 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaInt(inport);
1257 else if(inport->edGetType()->kind() == String)
1259 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaString(inport);
1261 else if(inport->edGetType()->kind() == Bool)
1263 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaBool(inport);
1265 else if(inport->edGetType()->kind() == Objref )
1267 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1269 return new PyCorbaObjref(inport);
1274 msg << "Cannot connect Python output port with type: " << type->id() ;
1275 msg << " to CORBA input port " << inport->getName() << " with incompatible objref type: " << inport->edGetType()->id();
1276 msg << " (" << __FILE__ << ":" <<__LINE__ << ")";
1277 throw ConversionException(msg.str());
1280 else if(inport->edGetType()->kind() == Sequence)
1282 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1284 return new PyCorbaSequence(inport);
1289 msg << "Cannot convert this sequence type " ;
1290 msg << __FILE__ << ":" <<__LINE__;
1291 throw ConversionException(msg.str());
1294 else if(inport->edGetType()->kind() == YACS::ENGINE::Struct)
1296 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1298 return new PyCorbaStruct(inport);
1303 msg << "Cannot convert this struct type " << type->id() << " to " << inport->edGetType()->id();
1304 msg << __FILE__ << ":" <<__LINE__;
1305 throw ConversionException(msg.str());
1308 // Adaptation not possible
1310 msg << "Cannot connect Python output port with type: " << type->id() ;
1311 msg << " to CORBA input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1313 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1315 throw ConversionException(msg.str());
1318 //! Adapt a CORBA input port to connect it to a XML output port
1320 * \param inport : input port to adapt to Xml type type
1321 * \param type : type supported by output port
1322 * \return an adaptator port of type InputXmlPort
1325 InputPort* RuntimeSALOME::adaptCorbaToXml(InputCorbaPort* inport,
1328 // BEWARE : using the generic check
1329 if(inport->edGetType()->isAdaptable(type))
1331 //output type is convertible to input type
1332 return new XmlCorba(inport);
1334 //output type is not convertible
1336 msg << "Cannot connect Xml output port with type: " << type->id() ;
1337 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1339 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1341 throw ConversionException(msg.str());
1344 //! Adapt a CORBA input port to a C++ output port
1346 * \param inport : input port to adapt to C++ type type
1347 * \param type : outport data type
1348 * \return an adaptator port of type InputCPPPort
1351 InputPort* RuntimeSALOME::adaptCorbaToCpp(InputCorbaPort* inport,
1354 DEBTRACE("RuntimeSALOME::adaptCorbaToCpp(InputCorbaPort* inport" );
1355 if(isAdaptableCorbaCpp(type,inport->edGetType()))
1357 //output type is convertible to input type
1358 return new CppCorba(inport);
1360 //output type is not convertible
1362 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1363 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1365 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1367 throw ConversionException(msg.str());
1370 //! Adapt a CORBA input port to a neutral data
1372 * \param inport : InputPort to adapt to Neutral type type
1373 * \param type : outport data type
1374 * \return an adaptator port of type Neutralxxxx
1377 InputPort* RuntimeSALOME::adaptCorbaToNeutral(InputCorbaPort* inport,
1380 if(inport->edGetType()->kind() == Double)
1382 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaDouble(inport);
1384 else if(inport->edGetType()->kind() == Int)
1386 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaInt(inport);
1388 else if(inport->edGetType()->kind() == String)
1390 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaString(inport);
1392 else if(inport->edGetType()->kind() == Bool)
1394 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaBool(inport);
1396 else if(inport->edGetType()->kind() == Objref)
1398 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaObjref(inport);
1400 else if(inport->edGetType()->kind() == Sequence)
1402 if(isAdaptableCorbaNeutral(type,inport->edGetType()))
1403 return new NeutralCorbaSequence(inport);
1407 msg << "Cannot convert this sequence type " ;
1408 msg << __FILE__ << ":" <<__LINE__;
1409 throw ConversionException(msg.str());
1412 else if(inport->edGetType()->kind() == Struct)
1414 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaStruct(inport);
1417 // Adaptation not possible
1419 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1420 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1422 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1424 throw ConversionException(msg.str());
1427 //! Adapt a CORBA input port to an output which implementation and type are given by impl and type
1429 * \param source : input port to adapt to implementation impl and type type
1430 * \param impl : output port implementation (C++, Python or Corba)
1431 * \param type : outport data type
1432 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1433 * \return an adaptator port which type depends on impl
1436 InputPort* RuntimeSALOME::adapt(InputCorbaPort* source,
1437 const std::string& impl,
1438 TypeCode * type,bool init)
1440 if(impl == CppNode::IMPL_NAME)
1442 return adaptCorbaToCpp(source,type);
1444 else if(impl == PythonNode::IMPL_NAME)
1446 return adaptCorbaToPython(source,type);
1448 else if(impl == CORBANode::IMPL_NAME)
1451 return adaptCorbaToCorba(source,type);
1453 return adaptCorbaToCorba(source,type);
1455 else if(impl == XmlNode::IMPL_NAME )
1457 return adaptCorbaToXml(source,type);
1459 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1461 return adaptCorbaToNeutral(source,type);
1466 msg << "Cannot connect InputCorbaPort : unknown implementation " ;
1467 msg << __FILE__ << ":" <<__LINE__;
1468 throw ConversionException(msg.str());
1472 //! Adapt a Python input port to a Python output port
1474 * No need to make conversion or cast.
1475 * Only check, it's possible.
1476 * \param inport : InputPort to adapt to Python type type
1477 * \param type : outport data type
1478 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1479 * \return an adaptator port of type InputPyPort
1482 InputPort* RuntimeSALOME::adaptPythonToPython(InputPyPort* inport,
1483 TypeCode * type,bool init)
1486 return new PyInit(inport);
1488 if(isAdaptablePyObjectPyObject(type,inport->edGetType()))
1490 //output data is convertible to input type
1491 //With python, no need to convert. Conversion will be done automatically
1492 //by the interpreter
1493 return new ProxyPort(inport);
1495 //output data is not convertible to input type
1497 msg << "Cannot connect Python output port with type: " << type->id() ;
1498 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1500 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1502 throw ConversionException(msg.str());
1505 //! Adapt a Python input port to a C++ output port
1507 * \param inport : InputPort to adapt to C++ type type
1508 * \param type : outport data type
1509 * \return an adaptator port of C++ type (InputCppPort)
1512 InputPort* RuntimeSALOME::adaptPythonToCpp(InputPyPort* inport,
1515 DEBTRACE("RuntimeSALOME::adaptPythonToCpp(InputPyPort* inport" );
1516 if(isAdaptablePyObjectCpp(type,inport->edGetType()))
1518 //output type is convertible to input type
1519 return new CppPy(inport);
1521 //output type is not convertible
1523 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1524 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1526 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1528 throw ConversionException(msg.str());
1531 //! Adapt a Python input port to a Neutral data port
1533 * \param inport : InputPort to adapt to Neutral type type
1534 * \param type : outport data type
1535 * \return an adaptator port of Neutral type (Neutralxxxx)
1538 InputPort* RuntimeSALOME::adaptPythonToNeutral(InputPyPort* inport,
1541 if(inport->edGetType()->kind() == Double)
1543 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyDouble(inport);
1545 else if(inport->edGetType()->kind() == Int)
1547 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyInt(inport);
1549 else if(inport->edGetType()->kind() == String)
1551 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyString(inport);
1553 else if(inport->edGetType()->kind() == Bool)
1555 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyBool(inport);
1557 else if(inport->edGetType()->kind() == Objref)
1559 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyObjref(inport);
1561 else if(inport->edGetType()->kind() == Sequence)
1563 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))
1564 return new NeutralPySequence(inport);
1568 msg << "Cannot convert this sequence type " ;
1569 msg << __FILE__ << ":" <<__LINE__;
1570 throw ConversionException(msg.str());
1573 else if(inport->edGetType()->kind() == Struct)
1575 if(isAdaptablePyObjectNeutral(type,inport->edGetType())) return new NeutralPyStruct(inport);
1578 // Adaptation not possible
1580 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1581 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1583 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1585 throw ConversionException(msg.str());
1588 //! Adapt a Python input port to a Corba output port
1590 * Always convert the data
1591 * \param inport : InputPort to adapt to Corba type type
1592 * \param type : outport data type
1593 * \return an adaptator port of Corba type (InputCorbaPort)
1596 InputPort* RuntimeSALOME::adaptPythonToCorba(InputPyPort* inport,
1599 if(inport->edGetType()->kind() == Double)
1601 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyDouble(inport);
1603 else if(inport->edGetType()->kind() == Int)
1605 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyInt(inport);
1607 else if(inport->edGetType()->kind() == String)
1609 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyString(inport);
1611 else if(inport->edGetType()->kind() == Bool)
1613 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyBool(inport);
1615 else if(inport->edGetType()->kind() == Objref)
1617 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1619 return new CorbaPyObjref(inport);
1624 msg << "Cannot connect InputCorbaPort : incompatible objref types " << type->id() << " " << inport->edGetType()->id();
1625 msg << " " << __FILE__ << ":" <<__LINE__;
1626 throw ConversionException(msg.str());
1629 else if(inport->edGetType()->kind() == Sequence)
1631 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1633 return new CorbaPySequence(inport);
1638 msg << "Cannot convert this sequence type " ;
1639 msg << __FILE__ << ":" <<__LINE__;
1640 throw ConversionException(msg.str());
1643 else if(inport->edGetType()->kind() == YACS::ENGINE::Struct)
1645 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1647 return new CorbaPyStruct(inport);
1652 msg << "Cannot convert this struct type " << type->id() << " to " << inport->edGetType()->id();
1653 msg << " " << __FILE__ << ":" <<__LINE__;
1654 throw ConversionException(msg.str());
1657 // Adaptation not possible
1659 msg << "Cannot connect Corba output port with type: " << type->id() ;
1660 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1662 msg << " ("__FILE__ << ":" << __LINE__ << ")";
1664 throw ConversionException(msg.str());
1667 //! Adapt a Python input port to a Xml output port
1669 * \param inport : input port to adapt to Xml type type
1670 * \param type : output port type
1671 * \return an input port of type InputXmlPort
1674 InputPort* RuntimeSALOME::adaptPythonToXml(InputPyPort* inport,
1677 // BEWARE : using the generic check
1678 if(inport->edGetType()->isAdaptable(type))
1681 return new XmlPython(inport);
1683 //non convertible type
1685 msg << "Cannot connect Xml output port with type: " << type->id() ;
1686 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1688 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1690 throw ConversionException(msg.str());
1693 //! Adapt a Python input port to an output port with a given implementation
1695 * \param source : input port to adapt to implementation impl and type type
1696 * \param impl : output port implementation (C++, Python or Corba)
1697 * \param type : output port type
1698 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1699 * \return adaptated input port
1702 InputPort* RuntimeSALOME::adapt(InputPyPort* source,
1703 const std::string& impl,
1704 TypeCode * type,bool init)
1706 if(impl == CppNode::IMPL_NAME)
1708 return adaptPythonToCpp(source,type);
1710 else if(impl == PythonNode::IMPL_NAME)
1712 return adaptPythonToPython(source,type,init);
1714 else if(impl == CORBANode::IMPL_NAME)
1716 return adaptPythonToCorba(source,type);
1718 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1720 return adaptPythonToNeutral(source,type);
1722 else if(impl == XmlNode::IMPL_NAME)
1724 return adaptPythonToXml(source,type);
1729 msg << "Cannot connect InputPyPort : unknown implementation " << impl;
1730 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1731 throw ConversionException(msg.str());
1736 //! Adapt a C++ input port to connect it to a CORBA output port
1738 * \param inport : input port to adapt to CORBA type type
1739 * \param type : type supported by output port
1740 * \return an adaptator port of type InputCorbaPort
1743 InputPort* RuntimeSALOME::adaptCppToCorba(InputCppPort* inport,
1746 DEBTRACE("RuntimeSALOME::adaptCppToCorba(InputCppPort* inport)");
1747 if(isAdaptableCppCorba(type,inport->edGetType()))
1749 //output type is convertible to input type
1750 return new CorbaCpp(inport);
1752 //output type is not convertible
1754 msg << "Cannot connect Corba output port with type: " << type->id() ;
1755 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1757 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1759 throw ConversionException(msg.str());
1762 //! Adapt a C++ input port to a Python output port
1764 * \param inport : input port to adapt to Python type type
1765 * \param type : output port type
1766 * \return an adaptated input port of type InputPyPort
1768 InputPort* RuntimeSALOME::adaptCppToPython(InputCppPort* inport,
1771 DEBTRACE("RuntimeSALOME::adaptCppToPython(InputCppPort* inport)");
1772 if(isAdaptableCppPyObject(type,inport->edGetType()))
1774 //output type is convertible to input type
1775 return new PyCpp(inport);
1777 //output type is not convertible
1779 msg << "Cannot connect Python output port with type: " << type->id() ;
1780 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1782 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1784 throw ConversionException(msg.str());
1787 //! Adapt a C++ input port to a C++ output port
1789 * \param inport : input port to adapt to C++ type type
1790 * \param type : output port type
1791 * \return an adaptated input port of type InputPyPort
1793 InputPort* RuntimeSALOME::adaptCppToCpp(InputCppPort* inport,
1796 DEBTRACE("RuntimeSALOME::adaptCppToCpp(InputPort* inport" );
1797 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1798 if(type->isAdaptable(inport->edGetType()))
1800 //the output data is convertible to inport type
1801 return new CppCpp(inport);
1803 //non convertible type
1805 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1806 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1808 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1810 throw ConversionException(msg.str());
1813 //! Adapt a C++ input port to a Neutral output port
1815 * \param inport : input port to adapt to C++ type type
1816 * \param type : output port type
1817 * \return an adaptated input port of type InputPyPort
1819 InputPort* RuntimeSALOME::adaptCppToNeutral(InputCppPort* inport,
1822 DEBTRACE("RuntimeSALOME::adaptCppToNeutral(InputPort* inport" );
1823 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1824 if(type->isAdaptable(inport->edGetType()))
1826 //the output data is convertible to inport type
1827 return new NeutralCpp(inport);
1829 //non convertible type
1831 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1832 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1834 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1836 throw ConversionException(msg.str());
1839 InputPort* RuntimeSALOME::adaptCppToXml(InputCppPort* inport,
1842 DEBTRACE("RuntimeSALOME::adaptCppToXml(InputCppPort* inport" );
1843 if(isAdaptableCppXml(type,inport->edGetType()))
1846 return new XmlCpp(inport);
1848 //non convertible type
1850 msg << "Cannot connect Xml output port with type: " << type->id() ;
1851 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1853 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1855 throw ConversionException(msg.str());
1858 //! Adapt a C++ input port to connect it to an output port with a given implementation
1860 * \param source : input port to adapt to implementation impl and type type
1861 * \param impl : output port implementation (C++, Python or Corba)
1862 * \param type : output port supported type
1863 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1864 * \return the adaptated port
1867 InputPort* RuntimeSALOME::adapt(InputCppPort* source,
1868 const std::string& impl,
1869 TypeCode * type,bool init)
1871 DEBTRACE("RuntimeSALOME::adapt(InputCppPort* source)");
1872 if(impl == CORBANode::IMPL_NAME)
1874 return adaptCppToCorba(source,type);
1876 else if(impl == PythonNode::IMPL_NAME)
1878 return adaptCppToPython(source,type);
1880 else if(impl == XmlNode::IMPL_NAME)
1882 return adaptCppToXml(source,type);
1884 else if(impl == CppNode::IMPL_NAME)
1886 return adaptCppToCpp(source, type);
1888 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1890 return adaptCppToNeutral(source, type);
1895 msg << "Cannot connect InputCppPort to " << impl << " implementation";
1896 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1897 throw ConversionException(msg.str());
1901 // bool RuntimeSALOME::isCompatible(const OutputPort* outputPort,
1902 // const InputPort* inputPort)
1904 // bool result=true;
1908 CORBA::ORB_ptr RuntimeSALOME::getOrb() const
1914 * Retrieve from custom NS the entry. Custom NS is supposed to be hosted in current process.
1915 * This method try to emulate CORBA ns convention : "corbaname:rir:#test.my_context/Echo.Object" is converted into "Echo"
1917 * See Engines::EmbeddedNamingService
1919 CORBA::Object_var RuntimeSALOME::getFromNS(const char *entry) const
1921 CORBA::Object_var ret;
1922 std::string entryCpp(entry);
1923 if(entryCpp.substr(0,4) == "IOR:")
1925 ret = _orb->string_to_object( entry );
1929 auto pos = entryCpp.find_last_of('/');
1930 std::string entry1( entryCpp.substr(pos+1,std::string::npos) );
1931 pos = entry1.find_last_of('.');
1932 std::string entry2( entry1.substr(0,pos) );
1933 Engines::EmbeddedNamingService_var ns = GetEmbeddedNamingService();
1934 std::unique_ptr<Engines::IORType> iorRet( ns->Resolve( entry2.c_str() ) );
1935 auto len = iorRet->length();
1936 std::unique_ptr<char[]> iorTrans(new char[len+1]); iorTrans[len] = '\0';
1937 for(auto i = 0 ; i < len ; ++i) iorTrans[i] = (*iorRet)[i];
1938 ret = _orb->string_to_object(iorTrans.get());
1943 PyObject * RuntimeSALOME::getPyOrb() const
1948 PyObject * RuntimeSALOME::getBuiltins() const
1953 DynamicAny::DynAnyFactory_ptr RuntimeSALOME::getDynFactory() const
1958 PyObject * RuntimeSALOME::get_omnipy()
1963 omniORBpyAPI* RuntimeSALOME::getApi()
1968 void* RuntimeSALOME::convertNeutral(TypeCode * type, Any *data)
1971 return (void *)convertNeutralPyObject(type,data);
1975 return (void *)Py_None;
1979 std::string RuntimeSALOME::convertNeutralAsString(TypeCode * type, Any *data)
1984 // The call to PyGILState_Ensure was moved here because there was also
1985 // a crash when calling convertNeutralPyObject with a sequence of pyobj.
1986 // see also the comment below.
1987 PyGILState_STATE gstate = PyGILState_Ensure();
1988 ob=convertNeutralPyObject(type,data);
1989 std::string s=convertPyObjectToString(ob);
1991 // Note (Renaud Barate, 8 jan 2013): With Python 2.7, this call to Py_DECREF causes a crash
1992 // (SIGSEGV) when ob is a sequence and the call is not protected with the global interpreter
1993 // lock. I thus added the call to PyGILState_Ensure / PyGILState_Release. It worked fine in
1994 // Python 2.6 without this call. If anyone finds the real reason of this bug and another fix,
1995 // feel free to change this code.
1996 //PyGILState_STATE gstate = PyGILState_Ensure();
1998 PyGILState_Release(gstate);
2007 std::string RuntimeSALOME::convertPyObjectToString(PyObject* ob)
2009 return YACS::ENGINE::convertPyObjectToString(ob);
2012 PyObject* RuntimeSALOME::convertStringToPyObject(const std::string& s)
2017 PyGILState_STATE gstate = PyGILState_Ensure();
2018 mainmod = PyImport_AddModule("__main__");
2019 globals = PyModule_GetDict(mainmod);
2020 PyObject* d = PyDict_New();
2021 //PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
2022 ob= PyRun_String( s.c_str(), Py_eval_input, globals, d);
2028 PyObject* new_stderr = newPyStdOut(error);
2029 PySys_SetObject((char *)"stderr", new_stderr);
2031 PySys_SetObject((char *)"stderr", PySys_GetObject((char *)"__stderr__"));
2032 Py_DECREF(new_stderr);
2033 PyGILState_Release(gstate);
2034 throw Exception(error);
2036 PyGILState_Release(gstate);