1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SalomeOptimizerLoop.hxx"
22 // rnv: avoid compilation warning on Linux : "_POSIX_C_SOURCE" and "_XOPEN_SOURCE" are redefined
23 #ifdef _POSIX_C_SOURCE
24 #undef _POSIX_C_SOURCE
31 #include "TypeCode.hxx"
32 #include "PyStdout.hxx"
35 #include "YacsTrace.hxx"
37 using namespace YACS::ENGINE;
41 /*! \class YACS::ENGINE::SalomeOptimizerLoop
42 * \brief class to build optimization loops
47 SalomeOptimizerLoop::SalomeOptimizerLoop(const std::string& name, const std::string& algLibWthOutExt,
48 const std::string& symbolNameToOptimizerAlgBaseInstanceFactory,
49 bool algInitOnFile,bool initAlgo, Proc * procForTypes):
50 OptimizerLoop(name,algLibWthOutExt,symbolNameToOptimizerAlgBaseInstanceFactory,algInitOnFile,false)
56 setAlgorithm(algLibWthOutExt, symbolNameToOptimizerAlgBaseInstanceFactory, true, procForTypes);
58 //catch(YACS::Exception& e)
65 SalomeOptimizerLoop::SalomeOptimizerLoop(const SalomeOptimizerLoop& other, ComposedNode *father, bool editionOnly):
66 OptimizerLoop(other,father,editionOnly)
70 SalomeOptimizerLoop::~SalomeOptimizerLoop()
74 Node *SalomeOptimizerLoop::simpleClone(ComposedNode *father, bool editionOnly) const
76 SalomeOptimizerLoop* sol=new SalomeOptimizerLoop(*this,father,editionOnly);
77 // TODO: Remove this const_cast (find a better design to get the type codes from the original node)
78 Proc * procForTypes = sol->getProc();
79 if (procForTypes == NULL) {
80 const Proc * origProc = getProc();
81 procForTypes = const_cast<Proc *>(origProc);
83 sol->setAlgorithm(_alglib, _symbol, false, procForTypes);
87 //! Load the algorithm object from a Python module or a dynamic library
89 * Member _alglib is the library name (shared library WITHOUT extension .so or python
90 * module WITH extension .py). Member _symbol is a symbol name in the library to use as
91 * an algorithm factory.
93 void SalomeOptimizerLoop::loadAlgorithm()
97 if(_alglib.size() > 3 && _alglib.substr(_alglib.size()-3,3)==".py")
99 //if alglib extension is .py try to import the corresponding python module
100 PyGILState_STATE gstate=PyGILState_Ensure();
102 PyObject* mainmod = PyImport_AddModule("__main__");
103 PyObject* globals = PyModule_GetDict(mainmod);
105 std::string pyscript;
106 pyscript="import sys\n"
107 "import SALOMERuntime\n"
109 pyscript=pyscript+_alglib+"'\nentry='"+_symbol+"'\n";
110 pyscript=pyscript+"import os\n"
112 "rep,mod=os.path.split(os.path.splitext(filename)[0])\n"
114 " sys.path.insert(0,rep)\n"
115 "algomodule=__import__(mod)\n"
118 "algoclass= getattr(algomodule,entry)\n"
119 "algo= algoclass()\n"
120 "swigalgo= algo.this\n"
123 PyObject* res=PyRun_String(pyscript.c_str(), Py_file_input, globals, globals );
127 //error during import
129 PyObject* new_stderr = newPyStdOut(_errorDetails);
130 PySys_SetObject((char*)"stderr", new_stderr);
132 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
133 Py_DECREF(new_stderr);
135 PyGILState_Release(gstate);
136 throw YACS::Exception(_errorDetails);
150 SwigPyObject* pyalgo = (SwigPyObject*)PyDict_GetItemString(globals, "swigalgo");
151 _alg=(OptimizerAlgBase*)pyalgo->ptr;
152 _alg->setPool(&_myPool);
155 PyGILState_Release(gstate);
159 //else try to load a dynamic library
160 OptimizerLoop::loadAlgorithm();