1 // Copyright (C) 2006-2021 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"
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 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 std::unique_ptr<SALOME_NamingService_Container_Abstract> RuntimeSALOME::getNS()
137 std::unique_ptr<SALOME_NamingService_Container_Abstract> ret(new SALOME_NamingService_Wrapper);
141 void RuntimeSALOME::setRuntime(long flags, int argc, char* argv[]) // singleton creation (not thread safe!)
143 if (! Runtime::_singleton)
145 RuntimeSALOME* r=new RuntimeSALOME(flags, argc, argv);
146 Runtime::_singleton = r;
149 DEBTRACE("RuntimeSALOME::setRuntime() done !");
152 RuntimeSALOME* YACS::ENGINE::getSALOMERuntime()
154 YASSERT(RuntimeSALOME::getSingleton());
155 return dynamic_cast< RuntimeSALOME* >(RuntimeSALOME::getSingleton());
159 * Singleton creation, initialize converter map
162 RuntimeSALOME::RuntimeSALOME()
167 void RuntimeSALOME::initBuiltins()
169 //Fill the builtin catalog with nodes specific to the runtime
170 std::map<std::string,TypeCode*>& typeMap=_builtinCatalog->_typeMap;
171 std::map<std::string,Node*>& nodeMap=_builtinCatalog->_nodeMap;
172 std::map<std::string,ComposedNode*>& composednodeMap=_builtinCatalog->_composednodeMap;
173 std::map<std::string,ComponentDefinition*>& componentMap=_builtinCatalog->_componentMap;
174 nodeMap["PyFunction"]=new PyFuncNode("PyFunction");
175 nodeMap["PyScript"]=new PythonNode("PyScript");
176 nodeMap["CORBANode"]=new CORBANode("CORBANode");
177 nodeMap["XmlNode"]=new XmlNode("XmlNode");
178 nodeMap["SalomeNode"]=new SalomeNode("SalomeNode");
179 nodeMap["CppNode"]=new CppNode("CppNode");
180 nodeMap["SalomePythonNode"]=new SalomePythonNode("SalomePythonNode");
181 nodeMap["PresetNode"]=new PresetNode("PresetNode");
182 nodeMap["OutNode"]=new OutNode("OutNode");
183 nodeMap["StudyInNode"]=new StudyInNode("StudyInNode");
184 nodeMap["StudyOutNode"]=new StudyOutNode("StudyOutNode");
185 composednodeMap["OptimizerLoop"]=createOptimizerLoop("OptimizerLoop","","",true);
186 typeMap["dblevec"]= createSequenceTc("dblevec","dblevec",_tc_double);
187 typeMap["intvec"]= createSequenceTc("intvec","intvec",_tc_int);
188 typeMap["stringvec"]= createSequenceTc("stringvec","stringvec",_tc_string);
189 typeMap["boolvec"]= createSequenceTc("boolvec","boolvec",_tc_bool);
190 typeMap["seqdblevec"]= createSequenceTc("seqdblevec","seqdblevec",typeMap["dblevec"]);
191 typeMap["seqintvec"]= createSequenceTc("seqintvec","seqintvec",typeMap["intvec"]);
192 typeMap["seqstringvec"]= createSequenceTc("seqstringvec","seqstringvec",typeMap["stringvec"]);
193 typeMap["seqboolvec"]= createSequenceTc("seqboolvec","seqboolvec",typeMap["boolvec"]);
194 std::list<TypeCodeObjref *> ltc;
195 typeMap["pyobj"]= createInterfaceTc("python:obj:1.0","pyobj",ltc);
196 typeMap["seqpyobj"]= createSequenceTc("seqpyobj","seqpyobj",typeMap["pyobj"]);
197 composednodeMap["Bloc"]=createBloc("Bloc");
198 composednodeMap["Switch"]=createSwitch("Switch");
199 composednodeMap["WhileLoop"]=createWhileLoop("WhileLoop");
200 composednodeMap["ForLoop"]=createForLoop("ForLoop");
201 composednodeMap["ForEachLoop_double"]=createForEachLoop("ForEachLoop_double",Runtime::_tc_double);
202 composednodeMap["ForEachLoop_string"]=createForEachLoop("ForEachLoop_string",Runtime::_tc_string);
203 composednodeMap["ForEachLoop_int"]=createForEachLoop("ForEachLoop_int",Runtime::_tc_int);
204 composednodeMap["ForEachLoop_bool"]=createForEachLoop("ForEachLoop_bool",Runtime::_tc_bool);
205 composednodeMap["ForEachLoop_pyobj"]=createForEachLoop("ForEachLoop_pyobj",typeMap["pyobj"]);;
206 ENGINE::TypeCodeStruct *t = createStructTc("","Engines/dataref");
207 t->addMember("ref",_tc_string);
208 typeMap["dataref"]= t;
211 RuntimeSALOME::RuntimeSALOME(long flags, int argc, char* argv[])
213 // If all flags (apart the IsPyExt flags) are unset, force them to true
214 if ((flags - flags & RuntimeSALOME::IsPyExt) == 0)
215 flags += RuntimeSALOME::UseCorba + RuntimeSALOME::UsePython
216 + RuntimeSALOME::UseCpp + RuntimeSALOME::UseXml;
218 // Salome Nodes implies Corba Nodes
219 if (flags & RuntimeSALOME::UseSalome)
220 flags |= RuntimeSALOME::UseCorba;
222 // Corba Nodes implies Python Nodes
223 if (flags & RuntimeSALOME::UseCorba)
224 flags |= RuntimeSALOME::UsePython;
226 _useCorba = flags & RuntimeSALOME::UseCorba;
227 _usePython = flags & RuntimeSALOME::UsePython;
228 _useCpp = flags & RuntimeSALOME::UseCpp;
229 _useXml = flags & RuntimeSALOME::UseXml;
234 if (_useCpp) _setOfImplementation.insert(CppNode::IMPL_NAME);
235 if (_usePython) _setOfImplementation.insert(PythonNode::IMPL_NAME);
236 if (_useCorba) _setOfImplementation.insert(CORBANode::IMPL_NAME);
237 if (_useXml) _setOfImplementation.insert(XmlNode::IMPL_NAME);
238 init(flags, argc, argv);
241 RuntimeSALOME::~RuntimeSALOME()
243 DEBTRACE("RuntimeSALOME::~RuntimeSALOME");
244 // destroy catalog loader prototypes
245 std::map<std::string, CatalogLoader*>::const_iterator pt;
246 for(pt=_catalogLoaderFactoryMap.begin();pt!=_catalogLoaderFactoryMap.end();pt++)
252 void RuntimeSALOME::loadModulCatalog()
255 const char * SCRIPT = "from salome_kernel import list_of_catalogs_regarding_environement\n"
256 "import KernelModuleCatalog\n"
257 "KernelModuleCatalog.myModuleCatalog( list_of_catalogs_regarding_environement() )\n";
258 PyRun_SimpleString(SCRIPT);
261 //! CORBA and Python initialization
263 * \param flags contains several bits
264 * bit0 (ispyext) true when method is called from Python
265 * (Python initialization must not be done!)
266 * bit1 (UsePython) true if python nodes are needed
267 * bit1 (UseCorba) true if CORBA nodes are needed
268 * bit1 (UseXml) true if python nodes are needed
269 * bit1 (UseCpp) true if C++ nodes are needed
270 * bit1 (UseSalome) true if Salome nodes are needed
271 * \param argc number of command line arguments (used to initialize the Python interpreter)
272 * \param argv command line arguments (used to initialize the Python interpreter)
276 void RuntimeSALOME::init(long flags, int argc, char* argv[])
278 bool ispyext = flags & RuntimeSALOME::IsPyExt;
281 PortableServer::POA_var root_poa;
282 PortableServer::POAManager_var pman;
283 CORBA::Object_var obj;
284 int nbargs = 0; char **args = 0;
285 _orb = CORBA::ORB_init (nbargs, args);
286 obj = _orb->resolve_initial_references("RootPOA");
287 root_poa = PortableServer::POA::_narrow(obj);
288 pman = root_poa->the_POAManager();
292 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
294 obj = _orb->resolve_initial_references("DynAnyFactory");
295 _dynFactory = DynamicAny::DynAnyFactory::_narrow(obj);
300 DEBTRACE("RuntimeSALOME::init, is python extension = " << ispyext);
302 // Initialize Python interpreter in embedded mode
303 if (!Py_IsInitialized())
305 #if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
308 Py_InitializeEx(0); // do not install signal handlers
310 if (argc > 0 && argv != NULL)
312 wchar_t **changed_argv = new wchar_t*[argc];
313 for (int i = 0; i < argc; i++)
315 changed_argv[i] = Py_DecodeLocale(argv[i], NULL);
317 PySys_SetArgv(argc, changed_argv);
323 char defaultName[] = "SALOME_YACS_RUNTIME";
324 wchar_t **changed_pyArgv = new wchar_t*[pyArgc];
325 pyArgv[0] = defaultName;
326 for (int i = 0; i < pyArgc; i++)
328 changed_pyArgv[i] = Py_DecodeLocale(pyArgv[i], NULL);
330 PySys_SetArgv(pyArgc, changed_pyArgv);
332 #if PY_VERSION_HEX < 0x03070000
333 PyEval_InitThreads(); /* Create (and acquire) the interpreter lock (for threads)*/
335 PyEval_SaveThread(); /* Release the thread state */
336 //here we do not have the Global Interpreter Lock
339 PyObject *mainmod,*pyapi,*res ;
341 PyGILState_STATE gstate;
342 gstate = PyGILState_Ensure(); // acquire the Global Interpreter Lock
344 mainmod = PyImport_AddModule("__main__");
345 globals = PyModule_GetDict(mainmod);
346 /* globals is a borrowed reference */
348 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
350 PyObject *bimod = PyImport_ImportModule("builtins");
351 if (bimod == NULL || PyDict_SetItemString(globals, "__builtins__", bimod) != 0)
352 Py_FatalError("can't add __builtins__ to __main__");
356 _bltins = PyEval_GetBuiltins(); /* borrowed ref */
362 _omnipy = PyImport_ImportModule((char*)"_omnipy");
366 PyErr_SetString(PyExc_ImportError, (char*)"Cannot import _omnipy");
369 pyapi = PyObject_GetAttrString(_omnipy, (char*)"API");
374 _api = (omniORBpyAPI*)PyCapsule_GetPointer(pyapi,"_omnipy.API");
377 res=PyRun_String("\n"
378 "from math import *\n"
380 "sys.path.insert(0,'.')\n"
381 "from omniORB import CORBA\n"
382 "from omniORB import any\n"
383 "orb = CORBA.ORB_init([], CORBA.ORB_ID)\n"
384 "#print(sys.getrefcount(orb))\n"
390 Py_file_input,globals,globals );
398 _pyorb = PyDict_GetItemString(globals,"orb");
399 /* PyDict_GetItemString returns a borrowed reference. There is no need to decref _pyorb */
402 pyany = PyDict_GetItemString(globals,"any");
403 /* PyDict_GetItemString returns a borrowed reference. There is no need to decref pyany */
406 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
410 PyGILState_Release(gstate); // Release the Global Interpreter Lock
414 // initialize the catalogLoaderFactory map with the session one
415 _catalogLoaderFactoryMap["session"]=new SessionCataLoader;
419 void RuntimeSALOME::fini()
423 PyGILState_STATE gstate = PyGILState_Ensure();
425 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
427 PyObject *mainmod, *globals;
428 mainmod = PyImport_AddModule("__main__");
429 globals = PyModule_GetDict(mainmod);
433 res=PyRun_String("orb.destroy()\n"
435 Py_file_input,globals,globals );
441 std::map<std::string,Node*>& nodeMap=_builtinCatalog->_nodeMap;
442 delete nodeMap["PyFunction"];
443 delete nodeMap["PyScript"];
444 delete nodeMap["SalomePythonNode"];
445 nodeMap.erase("PyFunction");
446 nodeMap.erase("PyScript");
447 nodeMap.erase("SalomePythonNode");
451 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
459 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
466 std::vector< std::pair<std::string,int> > RuntimeSALOME::getCatalogOfComputeNodes() const
468 CORBA::ORB_ptr orb(getOrb());
469 SALOME_NamingService_Wrapper namingService;
472 namingService.init_orb(orb);
474 catch(SALOME_Exception& e)
476 throw Exception("RuntimeSALOME::getCatalogOfComputeNodes : Unable to contact the SALOME Naming Service");
478 CORBA::Object_var obj(namingService.Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS));
479 if(CORBA::is_nil(obj))
480 throw Exception("RuntimeSALOME::getCatalogOfComputeNodes : Unable to access to the resource manager !");
481 Engines::ResourcesManager_var resManager(Engines::ResourcesManager::_narrow(obj));
482 if(CORBA::is_nil(resManager))
483 throw Exception("RuntimeSALOME::getCatalogOfComputeNodes : Internal error ! The entry attached to the res manager in NS does not have right type !");
484 std::vector< std::pair<std::string,int> > ret;
485 Engines::ResourceParameters params;
487 params.hostname = "";
491 params.cpu_clock = 0;
493 params.nb_proc_per_node = 0;
495 params.can_launch_batch_jobs = false;
496 params.can_run_containers = true;
497 params.componentList.length(0);
500 Engines::ResourceList_var resourceList;
501 resourceList = resManager->GetFittingResources(params);
502 ret.reserve(resourceList->length());
503 for(int i = 0; i<resourceList->length(); i++)
505 const char* resource_name = resourceList[i];
506 std::string std_resource_name = resource_name;
507 Engines::ResourceDefinition_var resource_definition
508 = resManager->GetResourceDefinition(resource_name);
509 int nb_cores = resource_definition->nb_node *
510 resource_definition->nb_proc_per_node;
511 ret.push_back(std::pair<std::string,int>(resource_name, nb_cores));
514 catch(SALOME::SALOME_Exception& e)
517 message=e.details.text.in();
518 throw Exception(message);
524 std::string RuntimeSALOME::getVersion() const
526 #ifdef YACS_DEVELOPMENT
527 return CORBA::string_dup(YACS_VERSION_STR"dev");
529 return CORBA::string_dup(YACS_VERSION_STR);
533 Proc* RuntimeSALOME::createProc(const std::string& name)
535 return new SalomeProc(name);
538 TypeCode * RuntimeSALOME::createInterfaceTc(const std::string& id, const std::string& name,
539 std::list<TypeCodeObjref *> ltc)
542 if(id == "") myName = "IDL:" + name + ":1.0";
544 return TypeCode::interfaceTc(myName.c_str(),name.c_str(),ltc);
547 TypeCode * RuntimeSALOME::createSequenceTc(const std::string& id,
548 const std::string& name,
551 return TypeCode::sequenceTc(id.c_str(),name.c_str(),content);
554 TypeCodeStruct * RuntimeSALOME::createStructTc(const std::string& id, const std::string& name)
557 if(id == "") myName = "IDL:" + name + ":1.0";
559 return (TypeCodeStruct *)TypeCode::structTc(myName.c_str(),name.c_str());
562 Bloc* RuntimeSALOME::createBloc(const std::string& name)
564 return new Bloc(name);
567 WhileLoop* RuntimeSALOME::createWhileLoop(const std::string& name)
569 return new WhileLoop(name);
572 ForLoop* RuntimeSALOME::createForLoop(const std::string& name)
574 return new ForLoop(name);
577 OptimizerLoop* RuntimeSALOME::createOptimizerLoop(const std::string& name,const std::string& algLib,const std::string& factoryName,
578 bool algInitOnFile, const std::string& kind, Proc * procForTypes)
580 OptimizerLoop * ol = (kind == "base") ? new OptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes) :
581 new SalomeOptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes);
582 ol->edGetNbOfBranchesPort()->edInit(1);
586 DataNode* RuntimeSALOME::createInDataNode(const std::string& kind,const std::string& name)
591 node = new PresetNode(name);
594 else if(kind == "study" )
596 return new StudyInNode(name);
598 std::string msg="DataNode kind ("+kind+") unknown";
599 throw Exception(msg);
602 DataNode* RuntimeSALOME::createOutDataNode(const std::string& kind,const std::string& name)
606 return new OutNode(name);
608 else if(kind == "study" )
610 return new StudyOutNode(name);
613 std::string msg="OutDataNode kind ("+kind+") unknown";
614 throw Exception(msg);
617 InlineFuncNode* RuntimeSALOME::createFuncNode(const std::string& kind,const std::string& name)
619 InlineFuncNode* node;
620 if(kind == "" || kind == SalomeNode::KIND || kind == PythonNode::KIND)
622 node = new PyFuncNode(name);
625 if(kind == DistributedPythonNode::KIND)
626 return new DistributedPythonNode(name);
627 std::string msg="FuncNode kind ("+kind+") unknown";
628 throw Exception(msg);
631 InlineNode* RuntimeSALOME::createScriptNode(const std::string& kind,const std::string& name)
634 if(kind == "" || kind == SalomeNode::KIND || kind == PythonNode::KIND)
636 node = new PythonNode(name);
639 std::string msg="ScriptNode kind ("+kind+") unknown";
640 throw Exception(msg);
643 ServiceNode* RuntimeSALOME::createRefNode(const std::string& kind,const std::string& name)
646 if(kind == "" || kind == SalomeNode::KIND || kind == CORBANode::KIND)
648 node = new CORBANode(name);
651 else if(kind == XmlNode::KIND)
653 node = new XmlNode(name);
656 std::string msg="RefNode kind ("+kind+") unknown";
657 throw Exception(msg);
660 ServiceNode* RuntimeSALOME::createCompoNode(const std::string& kind,const std::string& name)
663 if(kind == "" || kind == SalomeNode::KIND )
665 node=new SalomeNode(name);
668 else if (kind == CppNode::KIND)
670 node = new CppNode(name);
673 std::string msg="CompoNode kind ("+kind+") unknown";
674 throw Exception(msg);
677 ServiceInlineNode *RuntimeSALOME::createSInlineNode(const std::string& kind, const std::string& name)
679 if(kind == "" || kind == SalomeNode::KIND )
680 return new SalomePythonNode(name);
681 std::string msg="CompoNode kind ("+kind+") unknown";
682 throw Exception(msg);
685 ComponentInstance* RuntimeSALOME::createComponentInstance(const std::string& name,
686 const std::string& kind)
688 ComponentInstance* compo;
689 if(kind == "" || kind == SalomeComponent::KIND)
690 return new SalomeComponent(name);
691 else if(kind == CORBAComponent::KIND)
692 return new CORBAComponent(name);
693 else if(kind == SalomePythonComponent::KIND)
694 return new SalomePythonComponent(name);
695 else if (kind == CppComponent::KIND)
696 return new CppComponent(name);
697 else if (kind == SalomeHPComponent::KIND)
698 return new SalomeHPComponent(name);
699 std::string msg="Component Instance kind ("+kind+") unknown";
700 throw Exception(msg);
703 Container *RuntimeSALOME::createContainer(const std::string& kind)
705 if(kind == "" || kind == SalomeContainer::KIND)
706 return new SalomeContainer;
707 if(kind==SalomeHPContainer::KIND)
708 return new SalomeHPContainer;
709 else if (kind == CppContainer::KIND)
710 return new CppContainer;
711 std::string msg="Container kind ("+kind+") unknown";
712 throw Exception(msg);
715 InputPort * RuntimeSALOME::createInputPort(const std::string& name,
716 const std::string& impl,
720 if(impl == CppNode::IMPL_NAME)
722 return new InputCppPort(name, node, type);
724 else if(impl == PythonNode::IMPL_NAME)
726 return new InputPyPort(name, node, type);
728 else if(impl == CORBANode::IMPL_NAME)
730 return new InputCorbaPort(name, node, type);
732 else if(impl == XmlNode::IMPL_NAME)
734 return new InputXmlPort(name, node, type);
739 msg << "Cannot create " << impl << " InputPort" ;
740 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
741 throw Exception(msg.str());
745 OutputPort * RuntimeSALOME::createOutputPort(const std::string& name,
746 const std::string& impl,
750 if(impl == CppNode::IMPL_NAME)
752 return new OutputCppPort(name, node, type);
754 else if(impl == PythonNode::IMPL_NAME)
756 return new OutputPyPort(name, node, type);
758 else if(impl == CORBANode::IMPL_NAME)
760 return new OutputCorbaPort(name, node, type);
762 else if(impl == XmlNode::IMPL_NAME)
764 return new OutputXmlPort(name, node, type);
769 msg << "Cannot create " << impl << " OutputPort" ;
770 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
771 throw Exception(msg.str());
775 InputDataStreamPort* RuntimeSALOME::createInputDataStreamPort(const std::string& name,
776 Node *node,TypeCode *type)
778 DEBTRACE("createInputDataStreamPort: " << name << " " << type->shortName());
779 if(type->kind() == Objref && std::string(type->shortName(),7) == "CALCIUM")
781 return new InputCalStreamPort(name,node,type);
785 return new InputDataStreamPort(name,node,type);
789 OutputDataStreamPort* RuntimeSALOME::createOutputDataStreamPort(const std::string& name,
790 Node *node,TypeCode *type)
792 DEBTRACE("createOutputDataStreamPort: " << name << " " << type->shortName());
793 if(type->kind() == Objref && std::string(type->shortName(),7) == "CALCIUM")
795 return new OutputCalStreamPort(name,node,type);
799 return new OutputDataStreamPort(name,node,type);
803 //! Main adapter function : adapt an InputPort to be able to connect it to an OutputPort with a possible different implementation
805 * \param source : InputPort to be adapted
806 * \param impl : new implementation (C++, python, CORBA, XML, Neutral)
807 * \param type : data type provided by the InputPort
808 * \param init : indicates if the adapted InputPort will be used for initialization (value true) or not (value false)
810 * \return : adapted InputPort
812 InputPort* RuntimeSALOME::adapt(InputPort* source,
813 const std::string& impl,
814 TypeCode * type,bool init)
816 string imp_source=source->getNode()->getImplementation();
817 if(imp_source == PythonNode::IMPL_NAME)
819 return adapt((InputPyPort*)source,impl,type,init);
821 else if(imp_source == CppNode::IMPL_NAME)
823 return adapt((InputCppPort*)source,impl,type,init);
825 else if(imp_source == CORBANode::IMPL_NAME)
827 return adapt((InputCorbaPort*)source,impl,type,init);
829 else if(imp_source == XmlNode::IMPL_NAME)
831 return adapt((InputXmlPort*)source,impl,type,init);
833 else if(imp_source == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
835 return adaptNeutral(source,impl,type,init);
840 msg << "Cannot adapt " << imp_source << " InputPort to " << impl;
841 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
842 throw ConversionException(msg.str());
846 //! Adapter function for InPropertyPort
848 * \param source : InPropertyPort to be adapted
849 * \param impl : new implementation (C++, python, CORBA, XML, Neutral)
850 * \param type : data type provided by the InPropertyPort
851 * \param init : indicates if the adapted InPropertyPort will be used for initialization (value true) or not (value false)
853 * \return : adapted InputPort
855 InputPort* RuntimeSALOME::adapt(InPropertyPort* source,
856 const std::string& impl,
857 TypeCode * type,bool init)
859 return adaptNeutral((InputPort *)source,impl,type,init);
862 //! Adapt a Neutral input port to a Corba output port
864 * \param inport : Neutral input port to adapt to Corba type type
865 * \param type : output port type
866 * \return an adaptated input port of type InputCorbaPort
868 InputPort* RuntimeSALOME::adaptNeutralToCorba(InputPort* inport,
871 // BEWARE : using the generic check
872 if(inport->edGetType()->isAdaptable(type))
874 //the output data is convertible to inport type
875 return new CorbaNeutral(inport);
877 //non convertible type
879 msg << "Cannot connect Corba output port with type: " << type->id() ;
880 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
882 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
884 throw ConversionException(msg.str());
887 //! Adapt a Neutral input port to a Python output port
889 * \param inport : input port to adapt to Python type type
890 * \param type : output port type
891 * \return an adaptated input port of type InputPyPort
893 InputPort* RuntimeSALOME::adaptNeutralToPython(InputPort* inport,
896 // BEWARE : using the generic check
897 if(inport->edGetType()->isAdaptable(type))
900 return new PyNeutral(inport);
902 //last chance : an py output that is seq[objref] can be connected to a neutral input objref (P13268)
903 else if(type->kind()==Sequence && type->contentType()->kind()==Objref && inport->edGetType()->kind()==Objref)
905 return new PyNeutral(inport);
907 //non convertible type
909 msg << "Cannot connect Python output port with type: " << type->id() ;
910 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
912 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
914 throw ConversionException(msg.str());
917 //! Adapt a Neutral input port to a Xml output port
919 * \param inport : input port to adapt to Xml type type
920 * \param type : output port type
921 * \return an input port of type InputXmlPort
923 InputPort* RuntimeSALOME::adaptNeutralToXml(InputPort* inport,
926 // BEWARE : using the generic check
927 if(inport->edGetType()->isAdaptable(type))
930 return new XmlNeutral(inport);
932 //non convertible type
934 msg << "Cannot connect Xml output port with type: " << type->id() ;
935 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
937 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
939 throw ConversionException(msg.str());
942 //! Adapt a Neutral input port to a C++ output port
944 * \param inport : input port to adapt to C++ type type
945 * \param type : output port type
946 * \return an input port of type InputCppPort
948 InputPort* RuntimeSALOME::adaptNeutralToCpp(InputPort* inport,
951 DEBTRACE("RuntimeSALOME::adaptNeutralToCpp(InputPort* inport" );
952 if(isAdaptableNeutralCpp(type,inport->edGetType()))
955 return new CppNeutral(inport);
957 //non convertible type
959 msg << "Cannot connect Cpp output port with type: " << type->id() ;
960 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
962 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
964 throw ConversionException(msg.str());
967 //! Adapt a Neutral input port to connect it to an output port with a given implementation
969 * \param source : Neutral input port to adapt to implementation impl and type type
970 * \param impl : output port implementation (C++, Python, Corba, Xml or Neutral)
971 * \param type : output port supported type
972 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
973 * \return the adaptated port
975 InputPort* RuntimeSALOME::adaptNeutral(InputPort* source,
976 const std::string& impl,
977 TypeCode * type,bool init)
979 if(impl == CppNode::IMPL_NAME)
981 return adaptNeutralToCpp(source,type);
983 else if(impl == PythonNode::IMPL_NAME)
985 return adaptNeutralToPython(source,type);
987 else if(impl == CORBANode::IMPL_NAME)
989 return adaptNeutralToCorba(source,type);
991 else if(impl == XmlNode::IMPL_NAME )
993 return adaptNeutralToXml(source,type);
995 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
998 return new NeutralInit(source);
1000 return new ProxyPort(source);
1003 msg << "Cannot connect InputPort : unknown implementation " << impl;
1004 msg << " (" <<__FILE__ << ":" <<__LINE__ << ")";
1005 throw ConversionException(msg.str());
1008 //! Adapt a XML input port to connect it to a CORBA output port
1010 * \param inport : input port to adapt to CORBA type type
1011 * \param type : type supported by output port
1012 * \return an adaptator port of type InputCorbaPort
1015 InputPort* RuntimeSALOME::adaptXmlToCorba(InputXmlPort* inport,
1018 if(isAdaptableXmlCorba(type,inport->edGetType()))
1020 //output type is convertible to input type
1021 return new CorbaXml(inport);
1023 //output type is not convertible
1025 msg << "Cannot connect Corba output port with type: " << type->id() ;
1026 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1028 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1030 throw ConversionException(msg.str());
1033 //! Adapt a XML input port to a Python output port
1035 * \param inport : input port to adapt to Python type type
1036 * \param type : output port type
1037 * \return an adaptated input port of type InputPyPort
1039 InputPort* RuntimeSALOME::adaptXmlToPython(InputXmlPort* inport,
1042 if(inport->edGetType()->isAdaptable(type))
1044 //the output data is convertible to inport type
1045 return new PyXml(inport);
1047 //non convertible type
1049 msg << "Cannot connect Python output port with type: " << type->id() ;
1050 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1052 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1054 throw ConversionException(msg.str());
1057 //! Adapt a XML input port to a C++ output port
1059 * \param inport : input port to adapt to C++ type type
1060 * \param type : output port type
1061 * \return an adaptated input port of type InputPyPort
1063 InputPort* RuntimeSALOME::adaptXmlToCpp(InputXmlPort* inport,
1066 DEBTRACE("RuntimeSALOME::adaptXmlToCpp(InputPort* inport" );
1067 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1068 if(type->isAdaptable(inport->edGetType()))
1070 //the output data is convertible to inport type
1071 return new CppXml(inport);
1073 //non convertible type
1075 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1076 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1078 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1080 throw ConversionException(msg.str());
1083 //! Adapt a XML input port to a Neutral output port
1085 * \param inport : input port to adapt to Neutral type type
1086 * \param type : output port type
1087 * \return an adaptated input port of type Neutralxxxx
1089 InputPort* RuntimeSALOME::adaptXmlToNeutral(InputXmlPort* inport,
1092 if(inport->edGetType()->isAdaptable(type))
1094 //the output data is convertible to inport type
1095 return new NeutralXml(inport);
1097 //non convertible type
1099 msg << "Cannot connect Xml InputPort to OutputNeutralPort : " ;
1100 msg << "(" <<__FILE__ << ":" <<__LINE__<< ")";
1101 throw ConversionException(msg.str());
1104 //! Adapt a XML input port to a Xml output port
1106 * \param inport : input port to adapt to Xml type type
1107 * \param type : output port type
1108 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1109 * \return an adaptated input port of type Xmlxxxx
1111 InputPort* RuntimeSALOME::adaptXmlToXml(InputXmlPort* inport,
1112 TypeCode * type,bool init)
1115 return new ProxyPort(inport);
1117 if(inport->edGetType()->isAdaptable(type))
1118 return new ProxyPort(inport);
1121 msg << "Cannot connect Xml output port with type: " << type->id() ;
1122 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1124 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1126 throw ConversionException(msg.str());
1129 //! Adapt an Xml input port to an output port which implementation is given by impl
1131 * \param source : input port to adapt to implementation impl and type type
1132 * \param impl : output port implementation (C++, Python or Corba)
1133 * \param type : output port supported type
1134 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1135 * \return the adaptated port
1138 InputPort* RuntimeSALOME::adapt(InputXmlPort* source,
1139 const std::string& impl,
1140 TypeCode * type,bool init)
1142 if(impl == CORBANode::IMPL_NAME)
1144 return adaptXmlToCorba(source,type);
1146 else if(impl == PythonNode::IMPL_NAME)
1148 return adaptXmlToPython(source,type);
1150 else if(impl == CppNode::IMPL_NAME)
1152 return adaptXmlToCpp(source,type);
1154 else if(impl == XmlNode::IMPL_NAME )
1156 return adaptXmlToXml(source,type,init);
1158 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1160 return adaptXmlToNeutral(source,type);
1165 msg << "Cannot connect InputXmlPort to " << impl << " implementation";
1166 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1167 throw ConversionException(msg.str());
1172 //! Adapt a CORBA input port to a CORBA output port
1174 * \param inport : input port to adapt to CORBA outport data type
1175 * \param type : outport data type
1176 * \return an adaptator port of type InputCORBAPort
1178 InputPort* RuntimeSALOME::adaptCorbaToCorba(InputCorbaPort* inport,
1181 if(type->isA(inport->edGetType()))
1183 //types are compatible : no conversion
1184 //outport data type is more specific than inport required type
1185 //so the inport can be used safely
1186 return new ProxyPort(inport);
1188 else if(isAdaptableCorbaCorba(type,inport->edGetType()))
1190 //ouport data can be converted to inport data type
1191 return new CorbaCorba(inport);
1193 //outport data can not be converted
1195 msg << "Cannot connect Corba output port with type: " << type->id() ;
1196 msg << " to CORBA input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1198 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1200 throw ConversionException(msg.str());
1203 //! Adapt a CORBA input port to a Python output port
1205 * \param inport : input port to adapt to Python type type
1206 * \param type : outport data type
1207 * \return an adaptator port of type InputPyPort
1210 InputPort* RuntimeSALOME::adaptCorbaToPython(InputCorbaPort* inport,
1213 if(inport->edGetType()->kind() == Double)
1215 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaDouble(inport);
1217 else if(inport->edGetType()->kind() == Int)
1219 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaInt(inport);
1221 else if(inport->edGetType()->kind() == String)
1223 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaString(inport);
1225 else if(inport->edGetType()->kind() == Bool)
1227 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaBool(inport);
1229 else if(inport->edGetType()->kind() == Objref )
1231 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1233 return new PyCorbaObjref(inport);
1238 msg << "Cannot connect Python output port with type: " << type->id() ;
1239 msg << " to CORBA input port " << inport->getName() << " with incompatible objref type: " << inport->edGetType()->id();
1240 msg << " (" << __FILE__ << ":" <<__LINE__ << ")";
1241 throw ConversionException(msg.str());
1244 else if(inport->edGetType()->kind() == Sequence)
1246 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1248 return new PyCorbaSequence(inport);
1253 msg << "Cannot convert this sequence type " ;
1254 msg << __FILE__ << ":" <<__LINE__;
1255 throw ConversionException(msg.str());
1258 else if(inport->edGetType()->kind() == YACS::ENGINE::Struct)
1260 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1262 return new PyCorbaStruct(inport);
1267 msg << "Cannot convert this struct type " << type->id() << " to " << inport->edGetType()->id();
1268 msg << __FILE__ << ":" <<__LINE__;
1269 throw ConversionException(msg.str());
1272 // Adaptation not possible
1274 msg << "Cannot connect Python output port with type: " << type->id() ;
1275 msg << " to CORBA input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1277 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1279 throw ConversionException(msg.str());
1282 //! Adapt a CORBA input port to connect it to a XML output port
1284 * \param inport : input port to adapt to Xml type type
1285 * \param type : type supported by output port
1286 * \return an adaptator port of type InputXmlPort
1289 InputPort* RuntimeSALOME::adaptCorbaToXml(InputCorbaPort* inport,
1292 // BEWARE : using the generic check
1293 if(inport->edGetType()->isAdaptable(type))
1295 //output type is convertible to input type
1296 return new XmlCorba(inport);
1298 //output type is not convertible
1300 msg << "Cannot connect Xml output port with type: " << type->id() ;
1301 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1303 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1305 throw ConversionException(msg.str());
1308 //! Adapt a CORBA input port to a C++ output port
1310 * \param inport : input port to adapt to C++ type type
1311 * \param type : outport data type
1312 * \return an adaptator port of type InputCPPPort
1315 InputPort* RuntimeSALOME::adaptCorbaToCpp(InputCorbaPort* inport,
1318 DEBTRACE("RuntimeSALOME::adaptCorbaToCpp(InputCorbaPort* inport" );
1319 if(isAdaptableCorbaCpp(type,inport->edGetType()))
1321 //output type is convertible to input type
1322 return new CppCorba(inport);
1324 //output type is not convertible
1326 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1327 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1329 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1331 throw ConversionException(msg.str());
1334 //! Adapt a CORBA input port to a neutral data
1336 * \param inport : InputPort to adapt to Neutral type type
1337 * \param type : outport data type
1338 * \return an adaptator port of type Neutralxxxx
1341 InputPort* RuntimeSALOME::adaptCorbaToNeutral(InputCorbaPort* inport,
1344 if(inport->edGetType()->kind() == Double)
1346 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaDouble(inport);
1348 else if(inport->edGetType()->kind() == Int)
1350 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaInt(inport);
1352 else if(inport->edGetType()->kind() == String)
1354 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaString(inport);
1356 else if(inport->edGetType()->kind() == Bool)
1358 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaBool(inport);
1360 else if(inport->edGetType()->kind() == Objref)
1362 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaObjref(inport);
1364 else if(inport->edGetType()->kind() == Sequence)
1366 if(isAdaptableCorbaNeutral(type,inport->edGetType()))
1367 return new NeutralCorbaSequence(inport);
1371 msg << "Cannot convert this sequence type " ;
1372 msg << __FILE__ << ":" <<__LINE__;
1373 throw ConversionException(msg.str());
1376 else if(inport->edGetType()->kind() == Struct)
1378 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaStruct(inport);
1381 // Adaptation not possible
1383 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1384 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1386 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1388 throw ConversionException(msg.str());
1391 //! Adapt a CORBA input port to an output which implementation and type are given by impl and type
1393 * \param source : input port to adapt to implementation impl and type type
1394 * \param impl : output port implementation (C++, Python or Corba)
1395 * \param type : outport data type
1396 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1397 * \return an adaptator port which type depends on impl
1400 InputPort* RuntimeSALOME::adapt(InputCorbaPort* source,
1401 const std::string& impl,
1402 TypeCode * type,bool init)
1404 if(impl == CppNode::IMPL_NAME)
1406 return adaptCorbaToCpp(source,type);
1408 else if(impl == PythonNode::IMPL_NAME)
1410 return adaptCorbaToPython(source,type);
1412 else if(impl == CORBANode::IMPL_NAME)
1415 return adaptCorbaToCorba(source,type);
1417 return adaptCorbaToCorba(source,type);
1419 else if(impl == XmlNode::IMPL_NAME )
1421 return adaptCorbaToXml(source,type);
1423 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1425 return adaptCorbaToNeutral(source,type);
1430 msg << "Cannot connect InputCorbaPort : unknown implementation " ;
1431 msg << __FILE__ << ":" <<__LINE__;
1432 throw ConversionException(msg.str());
1436 //! Adapt a Python input port to a Python output port
1438 * No need to make conversion or cast.
1439 * Only check, it's possible.
1440 * \param inport : InputPort to adapt to Python type type
1441 * \param type : outport data type
1442 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1443 * \return an adaptator port of type InputPyPort
1446 InputPort* RuntimeSALOME::adaptPythonToPython(InputPyPort* inport,
1447 TypeCode * type,bool init)
1450 return new PyInit(inport);
1452 if(isAdaptablePyObjectPyObject(type,inport->edGetType()))
1454 //output data is convertible to input type
1455 //With python, no need to convert. Conversion will be done automatically
1456 //by the interpreter
1457 return new ProxyPort(inport);
1459 //output data is not convertible to input type
1461 msg << "Cannot connect Python output port with type: " << type->id() ;
1462 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1464 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1466 throw ConversionException(msg.str());
1469 //! Adapt a Python input port to a C++ output port
1471 * \param inport : InputPort to adapt to C++ type type
1472 * \param type : outport data type
1473 * \return an adaptator port of C++ type (InputCppPort)
1476 InputPort* RuntimeSALOME::adaptPythonToCpp(InputPyPort* inport,
1479 DEBTRACE("RuntimeSALOME::adaptPythonToCpp(InputPyPort* inport" );
1480 if(isAdaptablePyObjectCpp(type,inport->edGetType()))
1482 //output type is convertible to input type
1483 return new CppPy(inport);
1485 //output type is not convertible
1487 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1488 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1490 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1492 throw ConversionException(msg.str());
1495 //! Adapt a Python input port to a Neutral data port
1497 * \param inport : InputPort to adapt to Neutral type type
1498 * \param type : outport data type
1499 * \return an adaptator port of Neutral type (Neutralxxxx)
1502 InputPort* RuntimeSALOME::adaptPythonToNeutral(InputPyPort* inport,
1505 if(inport->edGetType()->kind() == Double)
1507 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyDouble(inport);
1509 else if(inport->edGetType()->kind() == Int)
1511 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyInt(inport);
1513 else if(inport->edGetType()->kind() == String)
1515 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyString(inport);
1517 else if(inport->edGetType()->kind() == Bool)
1519 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyBool(inport);
1521 else if(inport->edGetType()->kind() == Objref)
1523 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyObjref(inport);
1525 else if(inport->edGetType()->kind() == Sequence)
1527 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))
1528 return new NeutralPySequence(inport);
1532 msg << "Cannot convert this sequence type " ;
1533 msg << __FILE__ << ":" <<__LINE__;
1534 throw ConversionException(msg.str());
1537 else if(inport->edGetType()->kind() == Struct)
1539 if(isAdaptablePyObjectNeutral(type,inport->edGetType())) return new NeutralPyStruct(inport);
1542 // Adaptation not possible
1544 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1545 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1547 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1549 throw ConversionException(msg.str());
1552 //! Adapt a Python input port to a Corba output port
1554 * Always convert the data
1555 * \param inport : InputPort to adapt to Corba type type
1556 * \param type : outport data type
1557 * \return an adaptator port of Corba type (InputCorbaPort)
1560 InputPort* RuntimeSALOME::adaptPythonToCorba(InputPyPort* inport,
1563 if(inport->edGetType()->kind() == Double)
1565 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyDouble(inport);
1567 else if(inport->edGetType()->kind() == Int)
1569 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyInt(inport);
1571 else if(inport->edGetType()->kind() == String)
1573 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyString(inport);
1575 else if(inport->edGetType()->kind() == Bool)
1577 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyBool(inport);
1579 else if(inport->edGetType()->kind() == Objref)
1581 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1583 return new CorbaPyObjref(inport);
1588 msg << "Cannot connect InputCorbaPort : incompatible objref types " << type->id() << " " << inport->edGetType()->id();
1589 msg << " " << __FILE__ << ":" <<__LINE__;
1590 throw ConversionException(msg.str());
1593 else if(inport->edGetType()->kind() == Sequence)
1595 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1597 return new CorbaPySequence(inport);
1602 msg << "Cannot convert this sequence type " ;
1603 msg << __FILE__ << ":" <<__LINE__;
1604 throw ConversionException(msg.str());
1607 else if(inport->edGetType()->kind() == YACS::ENGINE::Struct)
1609 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1611 return new CorbaPyStruct(inport);
1616 msg << "Cannot convert this struct type " << type->id() << " to " << inport->edGetType()->id();
1617 msg << " " << __FILE__ << ":" <<__LINE__;
1618 throw ConversionException(msg.str());
1621 // Adaptation not possible
1623 msg << "Cannot connect Corba output port with type: " << type->id() ;
1624 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1626 msg << " ("__FILE__ << ":" << __LINE__ << ")";
1628 throw ConversionException(msg.str());
1631 //! Adapt a Python input port to a Xml output port
1633 * \param inport : input port to adapt to Xml type type
1634 * \param type : output port type
1635 * \return an input port of type InputXmlPort
1638 InputPort* RuntimeSALOME::adaptPythonToXml(InputPyPort* inport,
1641 // BEWARE : using the generic check
1642 if(inport->edGetType()->isAdaptable(type))
1645 return new XmlPython(inport);
1647 //non convertible type
1649 msg << "Cannot connect Xml output port with type: " << type->id() ;
1650 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1652 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1654 throw ConversionException(msg.str());
1657 //! Adapt a Python input port to an output port with a given implementation
1659 * \param source : input port to adapt to implementation impl and type type
1660 * \param impl : output port implementation (C++, Python or Corba)
1661 * \param type : output port type
1662 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1663 * \return adaptated input port
1666 InputPort* RuntimeSALOME::adapt(InputPyPort* source,
1667 const std::string& impl,
1668 TypeCode * type,bool init)
1670 if(impl == CppNode::IMPL_NAME)
1672 return adaptPythonToCpp(source,type);
1674 else if(impl == PythonNode::IMPL_NAME)
1676 return adaptPythonToPython(source,type,init);
1678 else if(impl == CORBANode::IMPL_NAME)
1680 return adaptPythonToCorba(source,type);
1682 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1684 return adaptPythonToNeutral(source,type);
1686 else if(impl == XmlNode::IMPL_NAME)
1688 return adaptPythonToXml(source,type);
1693 msg << "Cannot connect InputPyPort : unknown implementation " << impl;
1694 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1695 throw ConversionException(msg.str());
1700 //! Adapt a C++ input port to connect it to a CORBA output port
1702 * \param inport : input port to adapt to CORBA type type
1703 * \param type : type supported by output port
1704 * \return an adaptator port of type InputCorbaPort
1707 InputPort* RuntimeSALOME::adaptCppToCorba(InputCppPort* inport,
1710 DEBTRACE("RuntimeSALOME::adaptCppToCorba(InputCppPort* inport)");
1711 if(isAdaptableCppCorba(type,inport->edGetType()))
1713 //output type is convertible to input type
1714 return new CorbaCpp(inport);
1716 //output type is not convertible
1718 msg << "Cannot connect Corba output port with type: " << type->id() ;
1719 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1721 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1723 throw ConversionException(msg.str());
1726 //! Adapt a C++ input port to a Python output port
1728 * \param inport : input port to adapt to Python type type
1729 * \param type : output port type
1730 * \return an adaptated input port of type InputPyPort
1732 InputPort* RuntimeSALOME::adaptCppToPython(InputCppPort* inport,
1735 DEBTRACE("RuntimeSALOME::adaptCppToPython(InputCppPort* inport)");
1736 if(isAdaptableCppPyObject(type,inport->edGetType()))
1738 //output type is convertible to input type
1739 return new PyCpp(inport);
1741 //output type is not convertible
1743 msg << "Cannot connect Python output port with type: " << type->id() ;
1744 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1746 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1748 throw ConversionException(msg.str());
1751 //! Adapt a C++ input port to a C++ output port
1753 * \param inport : input port to adapt to C++ type type
1754 * \param type : output port type
1755 * \return an adaptated input port of type InputPyPort
1757 InputPort* RuntimeSALOME::adaptCppToCpp(InputCppPort* inport,
1760 DEBTRACE("RuntimeSALOME::adaptCppToCpp(InputPort* inport" );
1761 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1762 if(type->isAdaptable(inport->edGetType()))
1764 //the output data is convertible to inport type
1765 return new CppCpp(inport);
1767 //non convertible type
1769 msg << "Cannot connect Cpp output port with type: " << type->id() ;
1770 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1772 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1774 throw ConversionException(msg.str());
1777 //! Adapt a C++ input port to a Neutral output port
1779 * \param inport : input port to adapt to C++ type type
1780 * \param type : output port type
1781 * \return an adaptated input port of type InputPyPort
1783 InputPort* RuntimeSALOME::adaptCppToNeutral(InputCppPort* inport,
1786 DEBTRACE("RuntimeSALOME::adaptCppToNeutral(InputPort* inport" );
1787 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1788 if(type->isAdaptable(inport->edGetType()))
1790 //the output data is convertible to inport type
1791 return new NeutralCpp(inport);
1793 //non convertible type
1795 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1796 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1798 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1800 throw ConversionException(msg.str());
1803 InputPort* RuntimeSALOME::adaptCppToXml(InputCppPort* inport,
1806 DEBTRACE("RuntimeSALOME::adaptCppToXml(InputCppPort* inport" );
1807 if(isAdaptableCppXml(type,inport->edGetType()))
1810 return new XmlCpp(inport);
1812 //non convertible type
1814 msg << "Cannot connect Xml output port with type: " << type->id() ;
1815 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1817 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1819 throw ConversionException(msg.str());
1822 //! Adapt a C++ input port to connect it to an output port with a given implementation
1824 * \param source : input port to adapt to implementation impl and type type
1825 * \param impl : output port implementation (C++, Python or Corba)
1826 * \param type : output port supported type
1827 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1828 * \return the adaptated port
1831 InputPort* RuntimeSALOME::adapt(InputCppPort* source,
1832 const std::string& impl,
1833 TypeCode * type,bool init)
1835 DEBTRACE("RuntimeSALOME::adapt(InputCppPort* source)");
1836 if(impl == CORBANode::IMPL_NAME)
1838 return adaptCppToCorba(source,type);
1840 else if(impl == PythonNode::IMPL_NAME)
1842 return adaptCppToPython(source,type);
1844 else if(impl == XmlNode::IMPL_NAME)
1846 return adaptCppToXml(source,type);
1848 else if(impl == CppNode::IMPL_NAME)
1850 return adaptCppToCpp(source, type);
1852 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1854 return adaptCppToNeutral(source, type);
1859 msg << "Cannot connect InputCppPort to " << impl << " implementation";
1860 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1861 throw ConversionException(msg.str());
1865 // bool RuntimeSALOME::isCompatible(const OutputPort* outputPort,
1866 // const InputPort* inputPort)
1868 // bool result=true;
1872 CORBA::ORB_ptr RuntimeSALOME::getOrb() const
1877 PyObject * RuntimeSALOME::getPyOrb() const
1882 PyObject * RuntimeSALOME::getBuiltins() const
1887 DynamicAny::DynAnyFactory_ptr RuntimeSALOME::getDynFactory() const
1892 PyObject * RuntimeSALOME::get_omnipy()
1897 omniORBpyAPI* RuntimeSALOME::getApi()
1902 void* RuntimeSALOME::convertNeutral(TypeCode * type, Any *data)
1905 return (void *)convertNeutralPyObject(type,data);
1909 return (void *)Py_None;
1913 std::string RuntimeSALOME::convertNeutralAsString(TypeCode * type, Any *data)
1918 // The call to PyGILState_Ensure was moved here because there was also
1919 // a crash when calling convertNeutralPyObject with a sequence of pyobj.
1920 // see also the comment below.
1921 PyGILState_STATE gstate = PyGILState_Ensure();
1922 ob=convertNeutralPyObject(type,data);
1923 std::string s=convertPyObjectToString(ob);
1925 // Note (Renaud Barate, 8 jan 2013): With Python 2.7, this call to Py_DECREF causes a crash
1926 // (SIGSEGV) when ob is a sequence and the call is not protected with the global interpreter
1927 // lock. I thus added the call to PyGILState_Ensure / PyGILState_Release. It worked fine in
1928 // Python 2.6 without this call. If anyone finds the real reason of this bug and another fix,
1929 // feel free to change this code.
1930 //PyGILState_STATE gstate = PyGILState_Ensure();
1932 PyGILState_Release(gstate);
1941 std::string RuntimeSALOME::convertPyObjectToString(PyObject* ob)
1943 return YACS::ENGINE::convertPyObjectToString(ob);
1946 PyObject* RuntimeSALOME::convertStringToPyObject(const std::string& s)
1951 PyGILState_STATE gstate = PyGILState_Ensure();
1952 mainmod = PyImport_AddModule("__main__");
1953 globals = PyModule_GetDict(mainmod);
1954 PyObject* d = PyDict_New();
1955 //PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
1956 ob= PyRun_String( s.c_str(), Py_eval_input, globals, d);
1962 PyObject* new_stderr = newPyStdOut(error);
1963 PySys_SetObject((char *)"stderr", new_stderr);
1965 PySys_SetObject((char *)"stderr", PySys_GetObject((char *)"__stderr__"));
1966 Py_DECREF(new_stderr);
1967 PyGILState_Release(gstate);
1968 throw Exception(error);
1970 PyGILState_Release(gstate);