1 // Copyright (C) 2006-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #define private public
24 #define protected public
25 #include <omniORB4/CORBA.h>
26 #include <omniORB4/internal/typecode.h>
27 #include <omniORB4/internal/corbaOrb.h>
30 #include "yacsconfig.h"
31 #include "YACS_version.h"
32 #include "RuntimeSALOME.hxx"
33 #include "SALOMEDispatcher.hxx"
35 #include "TypeCode.hxx"
36 #include "WhileLoop.hxx"
37 #include "ForLoop.hxx"
38 #include "ForEachLoop.hxx"
39 #include "SalomeOptimizerLoop.hxx"
41 #include "InputPort.hxx"
42 #include "OutputPort.hxx"
43 #include "PresetPorts.hxx"
44 #include "InputDataStreamPort.hxx"
45 #include "OutputDataStreamPort.hxx"
47 #include "SalomeProc.hxx"
48 #include "PyStdout.hxx"
50 #include "SessionCataLoader.hxx"
53 #include "CORBAComponent.hxx"
54 #include "SalomeComponent.hxx"
55 #include "SalomeHPComponent.hxx"
56 #include "SalomePythonComponent.hxx"
57 #include "CppComponent.hxx"
59 #include "SalomeContainer.hxx"
60 #include "CppContainer.hxx"
61 #include "SalomeHPContainer.hxx"
64 #include "PythonNode.hxx"
65 #include "CORBANode.hxx"
66 #include "XMLNode.hxx"
67 #include "CppNode.hxx"
68 #include "PresetNode.hxx"
69 #include "OutNode.hxx"
70 #include "StudyNodes.hxx"
71 #include "SalomePythonNode.hxx"
72 #include "DistributedPythonNode.hxx"
75 #include "CORBACORBAConv.hxx"
76 #include "CORBAPythonConv.hxx"
77 #include "CORBAXMLConv.hxx"
78 #include "CORBACppConv.hxx"
79 #include "CORBANeutralConv.hxx"
81 #include "TypeConversions.hxx"
83 #include "PythonCORBAConv.hxx"
84 #include "PythonXMLConv.hxx"
85 #include "PythonCppConv.hxx"
86 #include "PythonNeutralConv.hxx"
87 #include "PythonInitConv.hxx"
90 #include "NeutralCORBAConv.hxx"
91 #include "NeutralPythonConv.hxx"
92 #include "NeutralXMLConv.hxx"
93 #include "NeutralCppConv.hxx"
94 #include "NeutralInitConv.hxx"
97 #include "CppCORBAConv.hxx"
98 #include "CppPythonConv.hxx"
99 #include "CppXMLConv.hxx"
100 #include "CppCppConv.hxx"
101 #include "CppNeutralConv.hxx"
104 #include "XMLCORBAConv.hxx"
105 #include "XMLPythonConv.hxx"
106 #include "XMLCppConv.hxx"
107 #include "XMLNeutralConv.hxx"
109 //Calcium specific ports
110 #include "CalStreamPort.hxx"
113 #include "SALOME_NamingService.hxx"
114 #include "SALOME_LifeCycleCORBA.hxx"
115 #include "SALOME_NamingService.hxx"
116 #include "SALOME_ResourcesManager.hxx"
117 #include "SALOME_ContainerManager.hxx"
118 #include "SALOMEconfig.h"
119 #include CORBA_CLIENT_HEADER(SALOME_ContainerManager)
123 #include <libxml/parser.h>
124 #include <omniORB4/CORBA.h>
130 #include "YacsTrace.hxx"
133 using namespace YACS::ENGINE;
135 void RuntimeSALOME::setRuntime(long flags, int argc, char* argv[]) // singleton creation (not thread safe!)
137 if (! Runtime::_singleton)
139 RuntimeSALOME* r=new RuntimeSALOME(flags, argc, argv);
140 Runtime::_singleton = r;
143 DEBTRACE("RuntimeSALOME::setRuntime() done !");
146 RuntimeSALOME* YACS::ENGINE::getSALOMERuntime()
148 YASSERT(RuntimeSALOME::getSingleton());
149 return dynamic_cast< RuntimeSALOME* >(RuntimeSALOME::getSingleton());
153 * Singleton creation, initialize converter map
156 RuntimeSALOME::RuntimeSALOME()
161 void RuntimeSALOME::initBuiltins()
163 //Fill the builtin catalog with nodes specific to the runtime
164 std::map<std::string,TypeCode*>& typeMap=_builtinCatalog->_typeMap;
165 std::map<std::string,Node*>& nodeMap=_builtinCatalog->_nodeMap;
166 std::map<std::string,ComposedNode*>& composednodeMap=_builtinCatalog->_composednodeMap;
167 std::map<std::string,ComponentDefinition*>& componentMap=_builtinCatalog->_componentMap;
168 nodeMap["PyFunction"]=new PyFuncNode("PyFunction");
169 nodeMap["PyScript"]=new PythonNode("PyScript");
170 nodeMap["CORBANode"]=new CORBANode("CORBANode");
171 nodeMap["XmlNode"]=new XmlNode("XmlNode");
172 nodeMap["SalomeNode"]=new SalomeNode("SalomeNode");
173 nodeMap["CppNode"]=new CppNode("CppNode");
174 nodeMap["SalomePythonNode"]=new SalomePythonNode("SalomePythonNode");
175 nodeMap["PresetNode"]=new PresetNode("PresetNode");
176 nodeMap["OutNode"]=new OutNode("OutNode");
177 nodeMap["StudyInNode"]=new StudyInNode("StudyInNode");
178 nodeMap["StudyOutNode"]=new StudyOutNode("StudyOutNode");
179 composednodeMap["OptimizerLoop"]=createOptimizerLoop("OptimizerLoop","","",true);
180 typeMap["dblevec"]= createSequenceTc("dblevec","dblevec",_tc_double);
181 typeMap["intvec"]= createSequenceTc("intvec","intvec",_tc_int);
182 typeMap["stringvec"]= createSequenceTc("stringvec","stringvec",_tc_string);
183 typeMap["boolvec"]= createSequenceTc("boolvec","boolvec",_tc_bool);
184 typeMap["seqdblevec"]= createSequenceTc("seqdblevec","seqdblevec",typeMap["dblevec"]);
185 typeMap["seqintvec"]= createSequenceTc("seqintvec","seqintvec",typeMap["intvec"]);
186 typeMap["seqstringvec"]= createSequenceTc("seqstringvec","seqstringvec",typeMap["stringvec"]);
187 typeMap["seqboolvec"]= createSequenceTc("seqboolvec","seqboolvec",typeMap["boolvec"]);
188 std::list<TypeCodeObjref *> ltc;
189 typeMap["pyobj"]= createInterfaceTc("python:obj:1.0","pyobj",ltc);
190 typeMap["seqpyobj"]= createSequenceTc("seqpyobj","seqpyobj",typeMap["pyobj"]);
191 composednodeMap["Bloc"]=createBloc("Bloc");
192 composednodeMap["Switch"]=createSwitch("Switch");
193 composednodeMap["WhileLoop"]=createWhileLoop("WhileLoop");
194 composednodeMap["ForLoop"]=createForLoop("ForLoop");
195 composednodeMap["ForEachLoop_double"]=createForEachLoop("ForEachLoop_double",Runtime::_tc_double);
196 composednodeMap["ForEachLoop_string"]=createForEachLoop("ForEachLoop_string",Runtime::_tc_string);
197 composednodeMap["ForEachLoop_int"]=createForEachLoop("ForEachLoop_int",Runtime::_tc_int);
198 composednodeMap["ForEachLoop_bool"]=createForEachLoop("ForEachLoop_bool",Runtime::_tc_bool);
199 composednodeMap["ForEachLoop_pyobj"]=createForEachLoop("ForEachLoop_pyobj",typeMap["pyobj"]);;
200 ENGINE::TypeCodeStruct *t = createStructTc("","Engines/dataref");
201 t->addMember("ref",_tc_string);
202 typeMap["dataref"]= t;
205 RuntimeSALOME::RuntimeSALOME(long flags, int argc, char* argv[])
207 // If all flags (apart the IsPyExt flags) are unset, force them to true
208 if ((flags - flags & RuntimeSALOME::IsPyExt) == 0)
209 flags += RuntimeSALOME::UseCorba + RuntimeSALOME::UsePython
210 + RuntimeSALOME::UseCpp + RuntimeSALOME::UseXml;
212 // Salome Nodes implies Corba Nodes
213 if (flags & RuntimeSALOME::UseSalome)
214 flags |= RuntimeSALOME::UseCorba;
216 // Corba Nodes implies Python Nodes
217 if (flags & RuntimeSALOME::UseCorba)
218 flags |= RuntimeSALOME::UsePython;
220 _useCorba = flags & RuntimeSALOME::UseCorba;
221 _usePython = flags & RuntimeSALOME::UsePython;
222 _useCpp = flags & RuntimeSALOME::UseCpp;
223 _useXml = flags & RuntimeSALOME::UseXml;
228 if (_useCpp) _setOfImplementation.insert(CppNode::IMPL_NAME);
229 if (_usePython) _setOfImplementation.insert(PythonNode::IMPL_NAME);
230 if (_useCorba) _setOfImplementation.insert(CORBANode::IMPL_NAME);
231 if (_useXml) _setOfImplementation.insert(XmlNode::IMPL_NAME);
232 init(flags, argc, argv);
235 RuntimeSALOME::~RuntimeSALOME()
237 DEBTRACE("RuntimeSALOME::~RuntimeSALOME");
238 // destroy catalog loader prototypes
239 std::map<std::string, CatalogLoader*>::const_iterator pt;
240 for(pt=_catalogLoaderFactoryMap.begin();pt!=_catalogLoaderFactoryMap.end();pt++)
246 //! CORBA and Python initialization
248 * \param flags contains several bits
249 * bit0 (ispyext) true when method is called from Python
250 * (Python initialization must not be done!)
251 * bit1 (UsePython) true if python nodes are needed
252 * bit1 (UseCorba) true if CORBA nodes are needed
253 * bit1 (UseXml) true if python nodes are needed
254 * bit1 (UseCpp) true if C++ nodes are needed
255 * bit1 (UseSalome) true if Salome nodes are needed
256 * \param argc number of command line arguments (used to initialize the Python interpreter)
257 * \param argv command line arguments (used to initialize the Python interpreter)
261 void RuntimeSALOME::init(long flags, int argc, char* argv[])
263 bool ispyext = flags & RuntimeSALOME::IsPyExt;
266 PortableServer::POA_var root_poa;
267 PortableServer::POAManager_var pman;
268 CORBA::Object_var obj;
269 int nbargs = 0; char **args = 0;
270 _orb = CORBA::ORB_init (nbargs, args);
271 obj = _orb->resolve_initial_references("RootPOA");
272 root_poa = PortableServer::POA::_narrow(obj);
273 pman = root_poa->the_POAManager();
277 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
279 obj = _orb->resolve_initial_references("DynAnyFactory");
280 _dynFactory = DynamicAny::DynAnyFactory::_narrow(obj);
285 DEBTRACE("RuntimeSALOME::init, is python extension = " << ispyext);
287 // Initialize Python interpreter in embedded mode
288 if (!Py_IsInitialized())
290 #if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
293 Py_InitializeEx(0); // do not install signal handlers
295 if (argc > 0 && argv != NULL)
297 wchar_t **changed_argv = new wchar_t*[argc];
298 for (int i = 0; i < argc; i++)
300 changed_argv[i] = Py_DecodeLocale(argv[i], NULL);
302 PySys_SetArgv(argc, changed_argv);
308 char defaultName[] = "SALOME_YACS_RUNTIME";
309 wchar_t **changed_pyArgv = new wchar_t*[pyArgc];
310 pyArgv[0] = defaultName;
311 for (int i = 0; i < pyArgc; i++)
313 changed_pyArgv[i] = Py_DecodeLocale(pyArgv[i], NULL);
315 PySys_SetArgv(pyArgc, changed_pyArgv);
317 PyEval_InitThreads(); /* Create (and acquire) the interpreter lock (for threads)*/
318 PyEval_SaveThread(); /* Release the thread state */
319 //here we do not have the Global Interpreter Lock
322 PyObject *mainmod,*pyapi,*res ;
324 PyGILState_STATE gstate;
325 gstate = PyGILState_Ensure(); // acquire the Global Interpreter Lock
327 mainmod = PyImport_AddModule("__main__");
328 globals = PyModule_GetDict(mainmod);
329 /* globals is a borrowed reference */
331 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
333 PyObject *bimod = PyImport_ImportModule("builtins");
334 if (bimod == NULL || PyDict_SetItemString(globals, "__builtins__", bimod) != 0)
335 Py_FatalError("can't add __builtins__ to __main__");
339 _bltins = PyEval_GetBuiltins(); /* borrowed ref */
345 _omnipy = PyImport_ImportModule((char*)"_omnipy");
349 PyErr_SetString(PyExc_ImportError, (char*)"Cannot import _omnipy");
352 pyapi = PyObject_GetAttrString(_omnipy, (char*)"API");
357 _api = (omniORBpyAPI*)PyCapsule_GetPointer(pyapi,"_omnipy.API");
360 res=PyRun_String("\n"
361 "from math import *\n"
363 "sys.path.insert(0,'.')\n"
364 "from omniORB import CORBA\n"
365 "from omniORB import any\n"
366 "orb = CORBA.ORB_init([], CORBA.ORB_ID)\n"
367 "#print(sys.getrefcount(orb))\n"
373 Py_file_input,globals,globals );
381 _pyorb = PyDict_GetItemString(globals,"orb");
382 /* PyDict_GetItemString returns a borrowed reference. There is no need to decref _pyorb */
385 pyany = PyDict_GetItemString(globals,"any");
386 /* PyDict_GetItemString returns a borrowed reference. There is no need to decref pyany */
389 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
393 PyGILState_Release(gstate); // Release the Global Interpreter Lock
397 // initialize the catalogLoaderFactory map with the session one
398 _catalogLoaderFactoryMap["session"]=new SessionCataLoader;
402 void RuntimeSALOME::fini()
406 PyGILState_STATE gstate = PyGILState_Ensure();
408 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
410 PyObject *mainmod, *globals;
411 mainmod = PyImport_AddModule("__main__");
412 globals = PyModule_GetDict(mainmod);
416 res=PyRun_String("orb.destroy()\n"
418 Py_file_input,globals,globals );
424 std::map<std::string,Node*>& nodeMap=_builtinCatalog->_nodeMap;
425 delete nodeMap["PyFunction"];
426 delete nodeMap["PyScript"];
427 delete nodeMap["SalomePythonNode"];
428 nodeMap.erase("PyFunction");
429 nodeMap.erase("PyScript");
430 nodeMap.erase("SalomePythonNode");
434 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
442 DEBTRACE("_orb refCount: " << ((omniOrbORB*)_orb.in())->pd_refCount);
449 std::vector< std::pair<std::string,int> > RuntimeSALOME::getCatalogOfComputeNodes() const
451 CORBA::ORB_ptr orb(getOrb());
452 SALOME_NamingService namingService;
455 namingService.init_orb(orb);
457 catch(SALOME_Exception& e)
459 throw Exception("SalomeContainerToolsSpreadOverTheResDecorator::getParameters : Unable to contact the SALOME Naming Service");
461 CORBA::Object_var obj(namingService.Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS));
462 if(CORBA::is_nil(obj))
463 throw Exception("SalomeContainerToolsSpreadOverTheResDecorator::getParameters : Unable to access to the resource manager !");
464 Engines::ResourcesManager_var resManager(Engines::ResourcesManager::_narrow(obj));
465 if(CORBA::is_nil(resManager))
466 throw Exception("SalomeContainerToolsSpreadOverTheResDecorator::getParameters : Internal error ! The entry attached to the res manager in NS does not have right type !");
467 std::vector< std::pair<std::string,int> > ret;
468 Engines::ResourceParameters params;
470 params.hostname = "";
474 params.cpu_clock = 0;
476 params.nb_proc_per_node = 0;
478 params.can_launch_batch_jobs = false;
479 params.can_run_containers = true;
480 params.componentList.length(0);
481 Engines::ResourceList_var resourceList;
482 resourceList = resManager->GetFittingResources(params);
483 ret.reserve(resourceList->length());
484 for(int i = 0; i<resourceList->length(); i++)
486 const char* resource_name = resourceList[i];
487 std::string std_resource_name = resource_name;
488 Engines::ResourceDefinition_var resource_definition
489 = resManager->GetResourceDefinition(resource_name);
490 int nb_cores = resource_definition->nb_node *
491 resource_definition->nb_proc_per_node;
492 ret.push_back(std::pair<std::string,int>(resource_name, nb_cores));
498 std::string RuntimeSALOME::getVersion() const
500 #ifdef YACS_DEVELOPMENT
501 return CORBA::string_dup(YACS_VERSION_STR"dev");
503 return CORBA::string_dup(YACS_VERSION_STR);
507 Proc* RuntimeSALOME::createProc(const std::string& name)
509 return new SalomeProc(name);
512 TypeCode * RuntimeSALOME::createInterfaceTc(const std::string& id, const std::string& name,
513 std::list<TypeCodeObjref *> ltc)
516 if(id == "") myName = "IDL:" + name + ":1.0";
518 return TypeCode::interfaceTc(myName.c_str(),name.c_str(),ltc);
521 TypeCode * RuntimeSALOME::createSequenceTc(const std::string& id,
522 const std::string& name,
525 return TypeCode::sequenceTc(id.c_str(),name.c_str(),content);
528 TypeCodeStruct * RuntimeSALOME::createStructTc(const std::string& id, const std::string& name)
531 if(id == "") myName = "IDL:" + name + ":1.0";
533 return (TypeCodeStruct *)TypeCode::structTc(myName.c_str(),name.c_str());
536 Bloc* RuntimeSALOME::createBloc(const std::string& name)
538 return new Bloc(name);
541 WhileLoop* RuntimeSALOME::createWhileLoop(const std::string& name)
543 return new WhileLoop(name);
546 ForLoop* RuntimeSALOME::createForLoop(const std::string& name)
548 return new ForLoop(name);
551 OptimizerLoop* RuntimeSALOME::createOptimizerLoop(const std::string& name,const std::string& algLib,const std::string& factoryName,
552 bool algInitOnFile, const std::string& kind, Proc * procForTypes)
554 OptimizerLoop * ol = (kind == "base") ? new OptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes) :
555 new SalomeOptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes);
556 ol->edGetNbOfBranchesPort()->edInit(1);
560 DataNode* RuntimeSALOME::createInDataNode(const std::string& kind,const std::string& name)
565 node = new PresetNode(name);
568 else if(kind == "study" )
570 return new StudyInNode(name);
572 std::string msg="DataNode kind ("+kind+") unknown";
573 throw Exception(msg);
576 DataNode* RuntimeSALOME::createOutDataNode(const std::string& kind,const std::string& name)
580 return new OutNode(name);
582 else if(kind == "study" )
584 return new StudyOutNode(name);
587 std::string msg="OutDataNode kind ("+kind+") unknown";
588 throw Exception(msg);
591 InlineFuncNode* RuntimeSALOME::createFuncNode(const std::string& kind,const std::string& name)
593 InlineFuncNode* node;
594 if(kind == "" || kind == SalomeNode::KIND || kind == PythonNode::KIND)
596 node = new PyFuncNode(name);
599 if(kind == DistributedPythonNode::KIND)
600 return new DistributedPythonNode(name);
601 std::string msg="FuncNode kind ("+kind+") unknown";
602 throw Exception(msg);
605 InlineNode* RuntimeSALOME::createScriptNode(const std::string& kind,const std::string& name)
608 if(kind == "" || kind == SalomeNode::KIND || kind == PythonNode::KIND)
610 node = new PythonNode(name);
613 std::string msg="ScriptNode kind ("+kind+") unknown";
614 throw Exception(msg);
617 ServiceNode* RuntimeSALOME::createRefNode(const std::string& kind,const std::string& name)
620 if(kind == "" || kind == SalomeNode::KIND || kind == CORBANode::KIND)
622 node = new CORBANode(name);
625 else if(kind == XmlNode::KIND)
627 node = new XmlNode(name);
630 std::string msg="RefNode kind ("+kind+") unknown";
631 throw Exception(msg);
634 ServiceNode* RuntimeSALOME::createCompoNode(const std::string& kind,const std::string& name)
637 if(kind == "" || kind == SalomeNode::KIND )
639 node=new SalomeNode(name);
642 else if (kind == CppNode::KIND)
644 node = new CppNode(name);
647 std::string msg="CompoNode kind ("+kind+") unknown";
648 throw Exception(msg);
651 ServiceInlineNode *RuntimeSALOME::createSInlineNode(const std::string& kind, const std::string& name)
653 if(kind == "" || kind == SalomeNode::KIND )
654 return new SalomePythonNode(name);
655 std::string msg="CompoNode kind ("+kind+") unknown";
656 throw Exception(msg);
659 ComponentInstance* RuntimeSALOME::createComponentInstance(const std::string& name,
660 const std::string& kind)
662 ComponentInstance* compo;
663 if(kind == "" || kind == SalomeComponent::KIND)
664 return new SalomeComponent(name);
665 else if(kind == CORBAComponent::KIND)
666 return new CORBAComponent(name);
667 else if(kind == SalomePythonComponent::KIND)
668 return new SalomePythonComponent(name);
669 else if (kind == CppComponent::KIND)
670 return new CppComponent(name);
671 else if (kind == SalomeHPComponent::KIND)
672 return new SalomeHPComponent(name);
673 std::string msg="Component Instance kind ("+kind+") unknown";
674 throw Exception(msg);
677 Container *RuntimeSALOME::createContainer(const std::string& kind)
679 if(kind == "" || kind == SalomeContainer::KIND)
680 return new SalomeContainer;
681 if(kind==SalomeHPContainer::KIND)
682 return new SalomeHPContainer;
683 else if (kind == CppContainer::KIND)
684 return new CppContainer;
685 std::string msg="Container kind ("+kind+") unknown";
686 throw Exception(msg);
689 InputPort * RuntimeSALOME::createInputPort(const std::string& name,
690 const std::string& impl,
694 if(impl == CppNode::IMPL_NAME)
696 return new InputCppPort(name, node, type);
698 else if(impl == PythonNode::IMPL_NAME)
700 return new InputPyPort(name, node, type);
702 else if(impl == CORBANode::IMPL_NAME)
704 return new InputCorbaPort(name, node, type);
706 else if(impl == XmlNode::IMPL_NAME)
708 return new InputXmlPort(name, node, type);
713 msg << "Cannot create " << impl << " InputPort" ;
714 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
715 throw Exception(msg.str());
719 OutputPort * RuntimeSALOME::createOutputPort(const std::string& name,
720 const std::string& impl,
724 if(impl == CppNode::IMPL_NAME)
726 return new OutputCppPort(name, node, type);
728 else if(impl == PythonNode::IMPL_NAME)
730 return new OutputPyPort(name, node, type);
732 else if(impl == CORBANode::IMPL_NAME)
734 return new OutputCorbaPort(name, node, type);
736 else if(impl == XmlNode::IMPL_NAME)
738 return new OutputXmlPort(name, node, type);
743 msg << "Cannot create " << impl << " OutputPort" ;
744 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
745 throw Exception(msg.str());
749 InputDataStreamPort* RuntimeSALOME::createInputDataStreamPort(const std::string& name,
750 Node *node,TypeCode *type)
752 DEBTRACE("createInputDataStreamPort: " << name << " " << type->shortName());
753 if(type->kind() == Objref && std::string(type->shortName(),7) == "CALCIUM")
755 return new InputCalStreamPort(name,node,type);
759 return new InputDataStreamPort(name,node,type);
763 OutputDataStreamPort* RuntimeSALOME::createOutputDataStreamPort(const std::string& name,
764 Node *node,TypeCode *type)
766 DEBTRACE("createOutputDataStreamPort: " << name << " " << type->shortName());
767 if(type->kind() == Objref && std::string(type->shortName(),7) == "CALCIUM")
769 return new OutputCalStreamPort(name,node,type);
773 return new OutputDataStreamPort(name,node,type);
777 //! Main adapter function : adapt an InputPort to be able to connect it to an OutputPort with a possible different implementation
779 * \param source : InputPort to be adapted
780 * \param impl : new implementation (C++, python, CORBA, XML, Neutral)
781 * \param type : data type provided by the InputPort
782 * \param init : indicates if the adapted InputPort will be used for initialization (value true) or not (value false)
784 * \return : adapted InputPort
786 InputPort* RuntimeSALOME::adapt(InputPort* source,
787 const std::string& impl,
788 TypeCode * type,bool init)
790 string imp_source=source->getNode()->getImplementation();
791 if(imp_source == PythonNode::IMPL_NAME)
793 return adapt((InputPyPort*)source,impl,type,init);
795 else if(imp_source == CppNode::IMPL_NAME)
797 return adapt((InputCppPort*)source,impl,type,init);
799 else if(imp_source == CORBANode::IMPL_NAME)
801 return adapt((InputCorbaPort*)source,impl,type,init);
803 else if(imp_source == XmlNode::IMPL_NAME)
805 return adapt((InputXmlPort*)source,impl,type,init);
807 else if(imp_source == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
809 return adaptNeutral(source,impl,type,init);
814 msg << "Cannot adapt " << imp_source << " InputPort to " << impl;
815 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
816 throw ConversionException(msg.str());
820 //! Adapter function for InPropertyPort
822 * \param source : InPropertyPort to be adapted
823 * \param impl : new implementation (C++, python, CORBA, XML, Neutral)
824 * \param type : data type provided by the InPropertyPort
825 * \param init : indicates if the adapted InPropertyPort will be used for initialization (value true) or not (value false)
827 * \return : adapted InputPort
829 InputPort* RuntimeSALOME::adapt(InPropertyPort* source,
830 const std::string& impl,
831 TypeCode * type,bool init)
833 return adaptNeutral((InputPort *)source,impl,type,init);
836 //! Adapt a Neutral input port to a Corba output port
838 * \param inport : Neutral input port to adapt to Corba type type
839 * \param type : output port type
840 * \return an adaptated input port of type InputCorbaPort
842 InputPort* RuntimeSALOME::adaptNeutralToCorba(InputPort* inport,
845 // BEWARE : using the generic check
846 if(inport->edGetType()->isAdaptable(type))
848 //the output data is convertible to inport type
849 return new CorbaNeutral(inport);
851 //non convertible type
853 msg << "Cannot connect Corba output port with type: " << type->id() ;
854 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
856 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
858 throw ConversionException(msg.str());
861 //! Adapt a Neutral input port to a Python output port
863 * \param inport : input port to adapt to Python type type
864 * \param type : output port type
865 * \return an adaptated input port of type InputPyPort
867 InputPort* RuntimeSALOME::adaptNeutralToPython(InputPort* inport,
870 // BEWARE : using the generic check
871 if(inport->edGetType()->isAdaptable(type))
874 return new PyNeutral(inport);
876 //last chance : an py output that is seq[objref] can be connected to a neutral input objref (P13268)
877 else if(type->kind()==Sequence && type->contentType()->kind()==Objref && inport->edGetType()->kind()==Objref)
879 return new PyNeutral(inport);
881 //non convertible type
883 msg << "Cannot connect Python output port with type: " << type->id() ;
884 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
886 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
888 throw ConversionException(msg.str());
891 //! Adapt a Neutral input port to a Xml output port
893 * \param inport : input port to adapt to Xml type type
894 * \param type : output port type
895 * \return an input port of type InputXmlPort
897 InputPort* RuntimeSALOME::adaptNeutralToXml(InputPort* inport,
900 // BEWARE : using the generic check
901 if(inport->edGetType()->isAdaptable(type))
904 return new XmlNeutral(inport);
906 //non convertible type
908 msg << "Cannot connect Xml output port with type: " << type->id() ;
909 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
911 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
913 throw ConversionException(msg.str());
916 //! Adapt a Neutral input port to a C++ output port
918 * \param inport : input port to adapt to C++ type type
919 * \param type : output port type
920 * \return an input port of type InputCppPort
922 InputPort* RuntimeSALOME::adaptNeutralToCpp(InputPort* inport,
925 DEBTRACE("RuntimeSALOME::adaptNeutralToCpp(InputPort* inport" );
926 if(isAdaptableNeutralCpp(type,inport->edGetType()))
929 return new CppNeutral(inport);
931 //non convertible type
933 msg << "Cannot connect Cpp output port with type: " << type->id() ;
934 msg << " to Neutral input port " << inport->getName() << " with type: " << inport->edGetType()->id();
936 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
938 throw ConversionException(msg.str());
941 //! Adapt a Neutral input port to connect it to an output port with a given implementation
943 * \param source : Neutral input port to adapt to implementation impl and type type
944 * \param impl : output port implementation (C++, Python, Corba, Xml or Neutral)
945 * \param type : output port supported type
946 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
947 * \return the adaptated port
949 InputPort* RuntimeSALOME::adaptNeutral(InputPort* source,
950 const std::string& impl,
951 TypeCode * type,bool init)
953 if(impl == CppNode::IMPL_NAME)
955 return adaptNeutralToCpp(source,type);
957 else if(impl == PythonNode::IMPL_NAME)
959 return adaptNeutralToPython(source,type);
961 else if(impl == CORBANode::IMPL_NAME)
963 return adaptNeutralToCorba(source,type);
965 else if(impl == XmlNode::IMPL_NAME )
967 return adaptNeutralToXml(source,type);
969 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
972 return new NeutralInit(source);
974 return new ProxyPort(source);
977 msg << "Cannot connect InputPort : unknown implementation " << impl;
978 msg << " (" <<__FILE__ << ":" <<__LINE__ << ")";
979 throw ConversionException(msg.str());
982 //! Adapt a XML input port to connect it to a CORBA output port
984 * \param inport : input port to adapt to CORBA type type
985 * \param type : type supported by output port
986 * \return an adaptator port of type InputCorbaPort
989 InputPort* RuntimeSALOME::adaptXmlToCorba(InputXmlPort* inport,
992 if(isAdaptableXmlCorba(type,inport->edGetType()))
994 //output type is convertible to input type
995 return new CorbaXml(inport);
997 //output type is not convertible
999 msg << "Cannot connect Corba output port with type: " << type->id() ;
1000 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1002 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1004 throw ConversionException(msg.str());
1007 //! Adapt a XML input port to a Python output port
1009 * \param inport : input port to adapt to Python type type
1010 * \param type : output port type
1011 * \return an adaptated input port of type InputPyPort
1013 InputPort* RuntimeSALOME::adaptXmlToPython(InputXmlPort* inport,
1016 if(inport->edGetType()->isAdaptable(type))
1018 //the output data is convertible to inport type
1019 return new PyXml(inport);
1021 //non convertible type
1023 msg << "Cannot connect Python output port with type: " << type->id() ;
1024 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1026 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1028 throw ConversionException(msg.str());
1031 //! Adapt a XML input port to a C++ output port
1033 * \param inport : input port to adapt to C++ type type
1034 * \param type : output port type
1035 * \return an adaptated input port of type InputPyPort
1037 InputPort* RuntimeSALOME::adaptXmlToCpp(InputXmlPort* inport,
1040 DEBTRACE("RuntimeSALOME::adaptXmlToCpp(InputPort* inport" );
1041 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1042 if(type->isAdaptable(inport->edGetType()))
1044 //the output data is convertible to inport type
1045 return new CppXml(inport);
1047 //non convertible type
1049 msg << "Cannot connect Cpp 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 Neutral output port
1059 * \param inport : input port to adapt to Neutral type type
1060 * \param type : output port type
1061 * \return an adaptated input port of type Neutralxxxx
1063 InputPort* RuntimeSALOME::adaptXmlToNeutral(InputXmlPort* inport,
1066 if(inport->edGetType()->isAdaptable(type))
1068 //the output data is convertible to inport type
1069 return new NeutralXml(inport);
1071 //non convertible type
1073 msg << "Cannot connect Xml InputPort to OutputNeutralPort : " ;
1074 msg << "(" <<__FILE__ << ":" <<__LINE__<< ")";
1075 throw ConversionException(msg.str());
1078 //! Adapt a XML input port to a Xml output port
1080 * \param inport : input port to adapt to Xml type type
1081 * \param type : output port type
1082 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1083 * \return an adaptated input port of type Xmlxxxx
1085 InputPort* RuntimeSALOME::adaptXmlToXml(InputXmlPort* inport,
1086 TypeCode * type,bool init)
1089 return new ProxyPort(inport);
1091 if(inport->edGetType()->isAdaptable(type))
1092 return new ProxyPort(inport);
1095 msg << "Cannot connect Xml output port with type: " << type->id() ;
1096 msg << " to Xml input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1098 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1100 throw ConversionException(msg.str());
1103 //! Adapt an Xml input port to an output port which implementation is given by impl
1105 * \param source : input port to adapt to implementation impl and type type
1106 * \param impl : output port implementation (C++, Python or Corba)
1107 * \param type : output port supported type
1108 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1109 * \return the adaptated port
1112 InputPort* RuntimeSALOME::adapt(InputXmlPort* source,
1113 const std::string& impl,
1114 TypeCode * type,bool init)
1116 if(impl == CORBANode::IMPL_NAME)
1118 return adaptXmlToCorba(source,type);
1120 else if(impl == PythonNode::IMPL_NAME)
1122 return adaptXmlToPython(source,type);
1124 else if(impl == CppNode::IMPL_NAME)
1126 return adaptXmlToCpp(source,type);
1128 else if(impl == XmlNode::IMPL_NAME )
1130 return adaptXmlToXml(source,type,init);
1132 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1134 return adaptXmlToNeutral(source,type);
1139 msg << "Cannot connect InputXmlPort to " << impl << " implementation";
1140 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1141 throw ConversionException(msg.str());
1146 //! Adapt a CORBA input port to a CORBA output port
1148 * \param inport : input port to adapt to CORBA outport data type
1149 * \param type : outport data type
1150 * \return an adaptator port of type InputCORBAPort
1152 InputPort* RuntimeSALOME::adaptCorbaToCorba(InputCorbaPort* inport,
1155 if(type->isA(inport->edGetType()))
1157 //types are compatible : no conversion
1158 //outport data type is more specific than inport required type
1159 //so the inport can be used safely
1160 return new ProxyPort(inport);
1162 else if(isAdaptableCorbaCorba(type,inport->edGetType()))
1164 //ouport data can be converted to inport data type
1165 return new CorbaCorba(inport);
1167 //outport data can not be converted
1169 msg << "Cannot connect Corba output port with type: " << type->id() ;
1170 msg << " to CORBA input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1172 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1174 throw ConversionException(msg.str());
1177 //! Adapt a CORBA input port to a Python output port
1179 * \param inport : input port to adapt to Python type type
1180 * \param type : outport data type
1181 * \return an adaptator port of type InputPyPort
1184 InputPort* RuntimeSALOME::adaptCorbaToPython(InputCorbaPort* inport,
1187 if(inport->edGetType()->kind() == Double)
1189 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaDouble(inport);
1191 else if(inport->edGetType()->kind() == Int)
1193 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaInt(inport);
1195 else if(inport->edGetType()->kind() == String)
1197 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaString(inport);
1199 else if(inport->edGetType()->kind() == Bool)
1201 if(isAdaptableCorbaPyObject(type,inport->edGetType()))return new PyCorbaBool(inport);
1203 else if(inport->edGetType()->kind() == Objref )
1205 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1207 return new PyCorbaObjref(inport);
1212 msg << "Cannot connect Python output port with type: " << type->id() ;
1213 msg << " to CORBA input port " << inport->getName() << " with incompatible objref type: " << inport->edGetType()->id();
1214 msg << " (" << __FILE__ << ":" <<__LINE__ << ")";
1215 throw ConversionException(msg.str());
1218 else if(inport->edGetType()->kind() == Sequence)
1220 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1222 return new PyCorbaSequence(inport);
1227 msg << "Cannot convert this sequence type " ;
1228 msg << __FILE__ << ":" <<__LINE__;
1229 throw ConversionException(msg.str());
1232 else if(inport->edGetType()->kind() == YACS::ENGINE::Struct)
1234 if(isAdaptableCorbaPyObject(type,inport->edGetType()))
1236 return new PyCorbaStruct(inport);
1241 msg << "Cannot convert this struct type " << type->id() << " to " << inport->edGetType()->id();
1242 msg << __FILE__ << ":" <<__LINE__;
1243 throw ConversionException(msg.str());
1246 // Adaptation not possible
1248 msg << "Cannot connect Python output port with type: " << type->id() ;
1249 msg << " to CORBA input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1251 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1253 throw ConversionException(msg.str());
1256 //! Adapt a CORBA input port to connect it to a XML output port
1258 * \param inport : input port to adapt to Xml type type
1259 * \param type : type supported by output port
1260 * \return an adaptator port of type InputXmlPort
1263 InputPort* RuntimeSALOME::adaptCorbaToXml(InputCorbaPort* inport,
1266 // BEWARE : using the generic check
1267 if(inport->edGetType()->isAdaptable(type))
1269 //output type is convertible to input type
1270 return new XmlCorba(inport);
1272 //output type is not convertible
1274 msg << "Cannot connect Xml 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 a C++ output port
1284 * \param inport : input port to adapt to C++ type type
1285 * \param type : outport data type
1286 * \return an adaptator port of type InputCPPPort
1289 InputPort* RuntimeSALOME::adaptCorbaToCpp(InputCorbaPort* inport,
1292 DEBTRACE("RuntimeSALOME::adaptCorbaToCpp(InputCorbaPort* inport" );
1293 if(isAdaptableCorbaCpp(type,inport->edGetType()))
1295 //output type is convertible to input type
1296 return new CppCorba(inport);
1298 //output type is not convertible
1300 msg << "Cannot connect Cpp 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 neutral data
1310 * \param inport : InputPort to adapt to Neutral type type
1311 * \param type : outport data type
1312 * \return an adaptator port of type Neutralxxxx
1315 InputPort* RuntimeSALOME::adaptCorbaToNeutral(InputCorbaPort* inport,
1318 if(inport->edGetType()->kind() == Double)
1320 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaDouble(inport);
1322 else if(inport->edGetType()->kind() == Int)
1324 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaInt(inport);
1326 else if(inport->edGetType()->kind() == String)
1328 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaString(inport);
1330 else if(inport->edGetType()->kind() == Bool)
1332 if(isAdaptableCorbaNeutral(type,inport->edGetType()))return new NeutralCorbaBool(inport);
1334 else if(inport->edGetType()->kind() == Objref)
1336 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaObjref(inport);
1338 else if(inport->edGetType()->kind() == Sequence)
1340 if(isAdaptableCorbaNeutral(type,inport->edGetType()))
1341 return new NeutralCorbaSequence(inport);
1345 msg << "Cannot convert this sequence type " ;
1346 msg << __FILE__ << ":" <<__LINE__;
1347 throw ConversionException(msg.str());
1350 else if(inport->edGetType()->kind() == Struct)
1352 if(isAdaptableCorbaNeutral(type,inport->edGetType())) return new NeutralCorbaStruct(inport);
1355 // Adaptation not possible
1357 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1358 msg << " to Corba input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1360 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1362 throw ConversionException(msg.str());
1365 //! Adapt a CORBA input port to an output which implementation and type are given by impl and type
1367 * \param source : input port to adapt to implementation impl and type type
1368 * \param impl : output port implementation (C++, Python or Corba)
1369 * \param type : outport data type
1370 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1371 * \return an adaptator port which type depends on impl
1374 InputPort* RuntimeSALOME::adapt(InputCorbaPort* source,
1375 const std::string& impl,
1376 TypeCode * type,bool init)
1378 if(impl == CppNode::IMPL_NAME)
1380 return adaptCorbaToCpp(source,type);
1382 else if(impl == PythonNode::IMPL_NAME)
1384 return adaptCorbaToPython(source,type);
1386 else if(impl == CORBANode::IMPL_NAME)
1389 return adaptCorbaToCorba(source,type);
1391 return adaptCorbaToCorba(source,type);
1393 else if(impl == XmlNode::IMPL_NAME )
1395 return adaptCorbaToXml(source,type);
1397 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1399 return adaptCorbaToNeutral(source,type);
1404 msg << "Cannot connect InputCorbaPort : unknown implementation " ;
1405 msg << __FILE__ << ":" <<__LINE__;
1406 throw ConversionException(msg.str());
1410 //! Adapt a Python input port to a Python output port
1412 * No need to make conversion or cast.
1413 * Only check, it's possible.
1414 * \param inport : InputPort to adapt to Python type type
1415 * \param type : outport data type
1416 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1417 * \return an adaptator port of type InputPyPort
1420 InputPort* RuntimeSALOME::adaptPythonToPython(InputPyPort* inport,
1421 TypeCode * type,bool init)
1424 return new PyInit(inport);
1426 if(isAdaptablePyObjectPyObject(type,inport->edGetType()))
1428 //output data is convertible to input type
1429 //With python, no need to convert. Conversion will be done automatically
1430 //by the interpreter
1431 return new ProxyPort(inport);
1433 //output data is not convertible to input type
1435 msg << "Cannot connect Python output port with type: " << type->id() ;
1436 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1438 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1440 throw ConversionException(msg.str());
1443 //! Adapt a Python input port to a C++ output port
1445 * \param inport : InputPort to adapt to C++ type type
1446 * \param type : outport data type
1447 * \return an adaptator port of C++ type (InputCppPort)
1450 InputPort* RuntimeSALOME::adaptPythonToCpp(InputPyPort* inport,
1453 DEBTRACE("RuntimeSALOME::adaptPythonToCpp(InputPyPort* inport" );
1454 if(isAdaptablePyObjectCpp(type,inport->edGetType()))
1456 //output type is convertible to input type
1457 return new CppPy(inport);
1459 //output type is not convertible
1461 msg << "Cannot connect Cpp 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 Neutral data port
1471 * \param inport : InputPort to adapt to Neutral type type
1472 * \param type : outport data type
1473 * \return an adaptator port of Neutral type (Neutralxxxx)
1476 InputPort* RuntimeSALOME::adaptPythonToNeutral(InputPyPort* inport,
1479 if(inport->edGetType()->kind() == Double)
1481 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyDouble(inport);
1483 else if(inport->edGetType()->kind() == Int)
1485 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyInt(inport);
1487 else if(inport->edGetType()->kind() == String)
1489 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyString(inport);
1491 else if(inport->edGetType()->kind() == Bool)
1493 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyBool(inport);
1495 else if(inport->edGetType()->kind() == Objref)
1497 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))return new NeutralPyObjref(inport);
1499 else if(inport->edGetType()->kind() == Sequence)
1501 if(isAdaptablePyObjectNeutral(type,inport->edGetType()))
1502 return new NeutralPySequence(inport);
1506 msg << "Cannot convert this sequence type " ;
1507 msg << __FILE__ << ":" <<__LINE__;
1508 throw ConversionException(msg.str());
1511 else if(inport->edGetType()->kind() == Struct)
1513 if(isAdaptablePyObjectNeutral(type,inport->edGetType())) return new NeutralPyStruct(inport);
1516 // Adaptation not possible
1518 msg << "Cannot connect Neutral output port with type: " << type->id() ;
1519 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1521 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1523 throw ConversionException(msg.str());
1526 //! Adapt a Python input port to a Corba output port
1528 * Always convert the data
1529 * \param inport : InputPort to adapt to Corba type type
1530 * \param type : outport data type
1531 * \return an adaptator port of Corba type (InputCorbaPort)
1534 InputPort* RuntimeSALOME::adaptPythonToCorba(InputPyPort* inport,
1537 if(inport->edGetType()->kind() == Double)
1539 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyDouble(inport);
1541 else if(inport->edGetType()->kind() == Int)
1543 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyInt(inport);
1545 else if(inport->edGetType()->kind() == String)
1547 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyString(inport);
1549 else if(inport->edGetType()->kind() == Bool)
1551 if(isAdaptablePyObjectCorba(type,inport->edGetType()))return new CorbaPyBool(inport);
1553 else if(inport->edGetType()->kind() == Objref)
1555 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1557 return new CorbaPyObjref(inport);
1562 msg << "Cannot connect InputCorbaPort : incompatible objref types " << type->id() << " " << inport->edGetType()->id();
1563 msg << " " << __FILE__ << ":" <<__LINE__;
1564 throw ConversionException(msg.str());
1567 else if(inport->edGetType()->kind() == Sequence)
1569 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1571 return new CorbaPySequence(inport);
1576 msg << "Cannot convert this sequence type " ;
1577 msg << __FILE__ << ":" <<__LINE__;
1578 throw ConversionException(msg.str());
1581 else if(inport->edGetType()->kind() == YACS::ENGINE::Struct)
1583 if(isAdaptablePyObjectCorba(type,inport->edGetType()))
1585 return new CorbaPyStruct(inport);
1590 msg << "Cannot convert this struct type " << type->id() << " to " << inport->edGetType()->id();
1591 msg << " " << __FILE__ << ":" <<__LINE__;
1592 throw ConversionException(msg.str());
1595 // Adaptation not possible
1597 msg << "Cannot connect Corba output port with type: " << type->id() ;
1598 msg << " to Python input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1600 msg << " ("__FILE__ << ":" << __LINE__ << ")";
1602 throw ConversionException(msg.str());
1605 //! Adapt a Python input port to a Xml output port
1607 * \param inport : input port to adapt to Xml type type
1608 * \param type : output port type
1609 * \return an input port of type InputXmlPort
1612 InputPort* RuntimeSALOME::adaptPythonToXml(InputPyPort* inport,
1615 // BEWARE : using the generic check
1616 if(inport->edGetType()->isAdaptable(type))
1619 return new XmlPython(inport);
1621 //non convertible type
1623 msg << "Cannot connect Xml 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 an output port with a given implementation
1633 * \param source : input port to adapt to implementation impl and type type
1634 * \param impl : output port implementation (C++, Python or Corba)
1635 * \param type : output port type
1636 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1637 * \return adaptated input port
1640 InputPort* RuntimeSALOME::adapt(InputPyPort* source,
1641 const std::string& impl,
1642 TypeCode * type,bool init)
1644 if(impl == CppNode::IMPL_NAME)
1646 return adaptPythonToCpp(source,type);
1648 else if(impl == PythonNode::IMPL_NAME)
1650 return adaptPythonToPython(source,type,init);
1652 else if(impl == CORBANode::IMPL_NAME)
1654 return adaptPythonToCorba(source,type);
1656 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1658 return adaptPythonToNeutral(source,type);
1660 else if(impl == XmlNode::IMPL_NAME)
1662 return adaptPythonToXml(source,type);
1667 msg << "Cannot connect InputPyPort : unknown implementation " << impl;
1668 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1669 throw ConversionException(msg.str());
1674 //! Adapt a C++ input port to connect it to a CORBA output port
1676 * \param inport : input port to adapt to CORBA type type
1677 * \param type : type supported by output port
1678 * \return an adaptator port of type InputCorbaPort
1681 InputPort* RuntimeSALOME::adaptCppToCorba(InputCppPort* inport,
1684 DEBTRACE("RuntimeSALOME::adaptCppToCorba(InputCppPort* inport)");
1685 if(isAdaptableCppCorba(type,inport->edGetType()))
1687 //output type is convertible to input type
1688 return new CorbaCpp(inport);
1690 //output type is not convertible
1692 msg << "Cannot connect Corba output port with type: " << type->id() ;
1693 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1695 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1697 throw ConversionException(msg.str());
1700 //! Adapt a C++ input port to a Python output port
1702 * \param inport : input port to adapt to Python type type
1703 * \param type : output port type
1704 * \return an adaptated input port of type InputPyPort
1706 InputPort* RuntimeSALOME::adaptCppToPython(InputCppPort* inport,
1709 DEBTRACE("RuntimeSALOME::adaptCppToPython(InputCppPort* inport)");
1710 if(isAdaptableCppPyObject(type,inport->edGetType()))
1712 //output type is convertible to input type
1713 return new PyCpp(inport);
1715 //output type is not convertible
1717 msg << "Cannot connect Python output port with type: " << type->id() ;
1718 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1720 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1722 throw ConversionException(msg.str());
1725 //! Adapt a C++ input port to a C++ output port
1727 * \param inport : input port to adapt to C++ type type
1728 * \param type : output port type
1729 * \return an adaptated input port of type InputPyPort
1731 InputPort* RuntimeSALOME::adaptCppToCpp(InputCppPort* inport,
1734 DEBTRACE("RuntimeSALOME::adaptCppToCpp(InputPort* inport" );
1735 DEBTRACE(type->kind() << " " << inport->edGetType()->kind() );
1736 if(type->isAdaptable(inport->edGetType()))
1738 //the output data is convertible to inport type
1739 return new CppCpp(inport);
1741 //non convertible type
1743 msg << "Cannot connect Cpp 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 Neutral 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::adaptCppToNeutral(InputCppPort* inport,
1760 DEBTRACE("RuntimeSALOME::adaptCppToNeutral(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 NeutralCpp(inport);
1767 //non convertible type
1769 msg << "Cannot connect Neutral 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 InputPort* RuntimeSALOME::adaptCppToXml(InputCppPort* inport,
1780 DEBTRACE("RuntimeSALOME::adaptCppToXml(InputCppPort* inport" );
1781 if(isAdaptableCppXml(type,inport->edGetType()))
1784 return new XmlCpp(inport);
1786 //non convertible type
1788 msg << "Cannot connect Xml output port with type: " << type->id() ;
1789 msg << " to Cpp input port " << inport->getName() << " with type: " << inport->edGetType()->id();
1791 msg << " ("<<__FILE__ << ":" << __LINE__<<")";
1793 throw ConversionException(msg.str());
1796 //! Adapt a C++ input port to connect it to an output port with a given implementation
1798 * \param source : input port to adapt to implementation impl and type type
1799 * \param impl : output port implementation (C++, Python or Corba)
1800 * \param type : output port supported type
1801 * \param init : if init is true the proxy port will be used in initialization of input port (needs value check)
1802 * \return the adaptated port
1805 InputPort* RuntimeSALOME::adapt(InputCppPort* source,
1806 const std::string& impl,
1807 TypeCode * type,bool init)
1809 DEBTRACE("RuntimeSALOME::adapt(InputCppPort* source)");
1810 if(impl == CORBANode::IMPL_NAME)
1812 return adaptCppToCorba(source,type);
1814 else if(impl == PythonNode::IMPL_NAME)
1816 return adaptCppToPython(source,type);
1818 else if(impl == XmlNode::IMPL_NAME)
1820 return adaptCppToXml(source,type);
1822 else if(impl == CppNode::IMPL_NAME)
1824 return adaptCppToCpp(source, type);
1826 else if(impl == Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME)
1828 return adaptCppToNeutral(source, type);
1833 msg << "Cannot connect InputCppPort to " << impl << " implementation";
1834 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1835 throw ConversionException(msg.str());
1839 // bool RuntimeSALOME::isCompatible(const OutputPort* outputPort,
1840 // const InputPort* inputPort)
1842 // bool result=true;
1846 CORBA::ORB_ptr RuntimeSALOME::getOrb() const
1851 PyObject * RuntimeSALOME::getPyOrb() const
1856 PyObject * RuntimeSALOME::getBuiltins() const
1861 DynamicAny::DynAnyFactory_ptr RuntimeSALOME::getDynFactory() const
1866 PyObject * RuntimeSALOME::get_omnipy()
1871 omniORBpyAPI* RuntimeSALOME::getApi()
1876 void* RuntimeSALOME::convertNeutral(TypeCode * type, Any *data)
1879 return (void *)convertNeutralPyObject(type,data);
1883 return (void *)Py_None;
1887 std::string RuntimeSALOME::convertNeutralAsString(TypeCode * type, Any *data)
1892 // The call to PyGILState_Ensure was moved here because there was also
1893 // a crash when calling convertNeutralPyObject with a sequence of pyobj.
1894 // see also the comment below.
1895 PyGILState_STATE gstate = PyGILState_Ensure();
1896 ob=convertNeutralPyObject(type,data);
1897 std::string s=convertPyObjectToString(ob);
1899 // Note (Renaud Barate, 8 jan 2013): With Python 2.7, this call to Py_DECREF causes a crash
1900 // (SIGSEGV) when ob is a sequence and the call is not protected with the global interpreter
1901 // lock. I thus added the call to PyGILState_Ensure / PyGILState_Release. It worked fine in
1902 // Python 2.6 without this call. If anyone finds the real reason of this bug and another fix,
1903 // feel free to change this code.
1904 //PyGILState_STATE gstate = PyGILState_Ensure();
1906 PyGILState_Release(gstate);
1915 std::string RuntimeSALOME::convertPyObjectToString(PyObject* ob)
1917 return YACS::ENGINE::convertPyObjectToString(ob);
1920 PyObject* RuntimeSALOME::convertStringToPyObject(const std::string& s)
1925 PyGILState_STATE gstate = PyGILState_Ensure();
1926 mainmod = PyImport_AddModule("__main__");
1927 globals = PyModule_GetDict(mainmod);
1928 PyObject* d = PyDict_New();
1929 //PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
1930 ob= PyRun_String( s.c_str(), Py_eval_input, globals, d);
1936 PyObject* new_stderr = newPyStdOut(error);
1937 PySys_SetObject((char *)"stderr", new_stderr);
1939 PySys_SetObject((char *)"stderr", PySys_GetObject((char *)"__stderr__"));
1940 Py_DECREF(new_stderr);
1941 PyGILState_Release(gstate);
1942 throw Exception(error);
1944 PyGILState_Release(gstate);