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
20 #include "Runtime.hxx"
23 #include "WhileLoop.hxx"
24 #include "ForLoop.hxx"
25 #include "ForEachLoop.hxx"
26 #include "OptimizerLoop.hxx"
30 #include "InputDataStreamPort.hxx"
31 #include "OutputDataStreamPort.hxx"
32 #include "Catalog.hxx"
33 #include "TypeCode.hxx"
34 #include "Executor.hxx"
40 #include "YacsTrace.hxx"
42 using namespace YACS::ENGINE;
45 Runtime* Runtime::_singleton = 0;
46 TypeCode* Runtime::_tc_double = 0;
47 TypeCode* Runtime::_tc_int = 0;
48 TypeCode* Runtime::_tc_bool = 0;
49 TypeCode* Runtime::_tc_string = 0;
50 TypeCode* Runtime::_tc_file = 0;
51 TypeCode* Runtime::_tc_stringpair = 0;
52 TypeCode* Runtime::_tc_propvec = 0;
54 // --- init typecodes for edInit with C++ Any
57 const char Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME[]="Neutral";
59 // singleton creation must be done before by a derived class
61 Runtime* YACS::ENGINE::getRuntime() throw(Exception)
63 if ( ! Runtime::_singleton )
64 throw Exception("Runtime is not yet initialized");
65 return Runtime::_singleton;
70 DEBTRACE("Runtime::Runtime");
71 Runtime::_tc_double = new TypeCode(Double);
72 Runtime::_tc_int = new TypeCode(Int);
73 Runtime::_tc_bool = new TypeCode(Bool);
74 Runtime::_tc_string = new TypeCode(String);
75 Runtime::_tc_file = new TypeCodeObjref("file", "file");
76 TypeCodeStruct * stringpair = new TypeCodeStruct("stringpair", "stringpair");
77 stringpair->addMember("name", Runtime::_tc_string);
78 stringpair->addMember("value", Runtime::_tc_string);
79 Runtime::_tc_stringpair = stringpair;
80 Runtime::_tc_propvec = new TypeCodeSeq("propvec", "propvec", Runtime::_tc_stringpair);
81 DEBTRACE( "_tc_double refcnt: " << Runtime::_tc_double->getRefCnt() );
82 DEBTRACE( "_tc_int refcnt: " << Runtime::_tc_int->getRefCnt() );
83 DEBTRACE( "_tc_bool refcnt: " << Runtime::_tc_bool->getRefCnt() );
84 DEBTRACE( "_tc_string refcnt: " << Runtime::_tc_string->getRefCnt() );
85 DEBTRACE( "_tc_file refcnt: " << Runtime::_tc_file->getRefCnt() );
86 DEBTRACE( "_tc_stringpair refcnt: " << Runtime::_tc_stringpair->getRefCnt() );
87 DEBTRACE( "_tc_propvec refcnt: " << Runtime::_tc_propvec->getRefCnt() );
88 _builtinCatalog = new Catalog("builtins");
89 std::map<std::string,TypeCode*>& typeMap=_builtinCatalog->_typeMap;
90 /* All composed node creations are moved to RuntimeSALOME::initBuiltins.
91 It is not safe to have all those calls to virtual functions (create*)
92 in the constructor. */
93 Runtime::_tc_double->incrRef();
94 typeMap["double"]=Runtime::_tc_double;
95 Runtime::_tc_int->incrRef();
96 typeMap["int"]=Runtime::_tc_int;
97 Runtime::_tc_bool->incrRef();
98 typeMap["bool"]=Runtime::_tc_bool;
99 Runtime::_tc_string->incrRef();
100 typeMap["string"]=Runtime::_tc_string;
101 Runtime::_tc_file->incrRef();
102 typeMap["file"]=Runtime::_tc_file;
103 Runtime::_tc_stringpair->incrRef();
104 typeMap["stringpair"]=Runtime::_tc_stringpair;
105 Runtime::_tc_propvec->incrRef();
106 typeMap["propvec"]=Runtime::_tc_propvec;
108 // Get dynamic trace level
110 char* valenv=getenv("YACS_TRACELEVEL");
113 std::istringstream iss(valenv);
116 YACS::traceLevel=temp;
119 // Get max threads number
120 char *maxThreadStr = getenv("YACS_MAX_THREADS");
121 if (maxThreadStr != NULL)
123 int maxThreads = atoi(maxThreadStr);
124 DEBTRACE("maxThreads = " << maxThreads);
125 if (maxThreads > 0) Executor::_maxThreads = maxThreads;
128 // Get thread stack size
129 char *threadStackSizeStr = getenv("YACS_THREADS_STACK_SIZE");
130 if (threadStackSizeStr != NULL)
132 size_t threadStackSize = strtoul(threadStackSizeStr, NULL, 0);
133 DEBTRACE("threadStackSize = " << threadStackSize);
134 if (threadStackSize > 0) Executor::_threadStackSize = threadStackSize;
138 void Runtime::removeRuntime()
145 delete _builtinCatalog;
146 DEBTRACE( "_tc_double refcnt: " << Runtime::_tc_double->getRefCnt() );
147 DEBTRACE( "_tc_int refcnt: " << Runtime::_tc_int->getRefCnt() );
148 DEBTRACE( "_tc_bool refcnt: " << Runtime::_tc_bool->getRefCnt() );
149 DEBTRACE( "_tc_string refcnt: " << Runtime::_tc_string->getRefCnt() );
150 DEBTRACE( "_tc_file refcnt: " << Runtime::_tc_file->getRefCnt() );
151 Runtime::_tc_double->decrRef();
152 Runtime::_tc_int->decrRef();
153 Runtime::_tc_bool->decrRef();
154 Runtime::_tc_string->decrRef();
155 Runtime::_tc_file->decrRef();
156 for(std::vector<Catalog *>::iterator it=_catalogs.begin();it !=_catalogs.end();it++)
158 Runtime::_singleton=0;
159 DEBTRACE( "Total YACS::ENGINE::Refcount: " << RefCounter::_totalCnt );
162 TypeCode * Runtime::createInterfaceTc(const std::string& id, const std::string& name,
163 std::list<TypeCodeObjref *> ltc)
165 return TypeCode::interfaceTc(id.c_str(),name.c_str(),ltc);
168 TypeCode * Runtime::createSequenceTc(const std::string& id,
169 const std::string& name,
172 return TypeCode::sequenceTc(id.c_str(),name.c_str(),content);
175 TypeCodeStruct * Runtime::createStructTc(const std::string& id, const std::string& name)
177 return (TypeCodeStruct *)TypeCode::structTc(id.c_str(),name.c_str());
180 DataNode* Runtime::createInDataNode(const std::string& kind,const std::string& name)
182 throw Exception("InDataNode factory not implemented");
185 DataNode* Runtime::createOutDataNode(const std::string& kind,const std::string& name)
187 throw Exception("OutDataNode factory not implemented");
190 InlineFuncNode* Runtime::createFuncNode(const std::string& kind,const std::string& name)
192 throw Exception("FuncNode factory not implemented");
195 InlineNode* Runtime::createScriptNode(const std::string& kind,const std::string& name)
197 throw Exception("ScriptNode factory not implemented");
200 ServiceNode* Runtime::createRefNode(const std::string& kind,const std::string& name)
202 throw Exception("RefNode factory not implemented");
205 ServiceNode* Runtime::createCompoNode(const std::string& kind,const std::string& name)
207 throw Exception("CompoNode factory not implemented");
210 ServiceInlineNode *Runtime::createSInlineNode(const std::string& kind, const std::string& name)
212 throw Exception("SInlineNode factory not implemented");
215 ComponentInstance* Runtime::createComponentInstance(const std::string& name,
216 const std::string& kind)
218 throw Exception("ComponentInstance factory not implemented");
221 Container *Runtime::createContainer(const std::string& kind)
223 throw Exception("Container factory not implemented");
226 Proc* Runtime::createProc(const std::string& name)
228 return new Proc(name);
231 Bloc* Runtime::createBloc(const std::string& name)
233 return new Bloc(name);
236 Switch* Runtime::createSwitch(const std::string& name)
238 return new Switch(name);
241 WhileLoop* Runtime::createWhileLoop(const std::string& name)
243 return new WhileLoop(name);
246 ForLoop* Runtime::createForLoop(const std::string& name)
248 return new ForLoop(name);
251 ForEachLoop* Runtime::createForEachLoop(const std::string& name,TypeCode *type)
253 ForEachLoop* ret = new ForEachLoop(name,type);
254 ret->edGetNbOfBranchesPort()->edInit(1);
258 OptimizerLoop* Runtime::createOptimizerLoop(const std::string& name,const std::string& algLib,const std::string& factoryName,bool algInitOnFile,
259 const std::string& kind, Proc * procForTypes)
261 return new OptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes);
264 InputDataStreamPort* Runtime::createInputDataStreamPort(const std::string& name,Node *node,TypeCode *type)
266 return new InputDataStreamPort(name,node,type);
269 OutputDataStreamPort* Runtime::createOutputDataStreamPort(const std::string& name,Node *node,TypeCode *type)
271 return new OutputDataStreamPort(name,node,type);
274 //! Load a catalog of calculation to use as factory
276 * \param sourceKind: the kind of catalog source. It depends on runtime. It could be a file, a server, a directory
277 * \param path: the path to the catalog.
278 * \return the loaded Catalog
280 Catalog* Runtime::loadCatalog(const std::string& sourceKind,const std::string& path)
282 if (_catalogLoaderFactoryMap.find(sourceKind) == _catalogLoaderFactoryMap.end())
284 throw Exception("This type of catalog loader does not exist: " + sourceKind);
288 Catalog* cata=new Catalog(path);
289 CatalogLoader* proto=_catalogLoaderFactoryMap[sourceKind];
290 proto->load(cata,path);
295 //! Add a catalog loader factory to the map _catalogLoaderFactoryMap under the name name
297 * \param name: name under which the factory is registered
298 * \param factory: the factory
300 void Runtime::setCatalogLoaderFactory(const std::string& name, CatalogLoader* factory)
302 _catalogLoaderFactoryMap[name]=factory;
305 //! Get the catalog of base nodes (elementary and composed)
307 * \return the builtin Catalog
309 Catalog* Runtime::getBuiltinCatalog()
311 return _builtinCatalog;
314 //! Add a catalog of types and nodes to the runtime
316 * These catalogs are searched when calling getTypeCode method
318 void Runtime::addCatalog(Catalog* catalog)
320 _catalogs.push_back(catalog);
324 //! Get a typecode by its name from runtime catalogs
326 * \return the typecode if it exists or NULL
328 TypeCode* Runtime::getTypeCode(const std::string& name)
330 if (_builtinCatalog->_typeMap.count(name) != 0)
331 return _builtinCatalog->_typeMap[name];
332 for(std::vector<Catalog *>::const_iterator it=_catalogs.begin();it !=_catalogs.end();it++)
334 if ((*it)->_typeMap.count(name) != 0)
335 return (*it)->_typeMap[name];
340 //! Convert a YACS::ENGINE::Any object to an external object with type type
342 * This method is used to convert Neutral objects to script languages. For example
343 * Python language. The runtime has one external script language.
344 * The object is returned as a void * because engine knows nothing about external script language.
346 * \param type: the type of the converted object if the conversion is possible
347 * \param data: the object to convert
348 * \return the converted object
350 void* Runtime::convertNeutral(TypeCode * type, Any *data)
352 throw Exception("convertNeutral is not implemented by your runtime");
355 //! Convert a YACS::ENGINE::Any object to a string to be used in GUI for example
357 * engine package does not provide a conversion to string. It has to be implemented in the
360 * \param type: the type of the object to convert
361 * \param data: the object to convert
362 * \return the string representation of the object
364 std::string Runtime::convertNeutralAsString(TypeCode * type, Any *data)
366 throw Exception("convertNeutralAsString is not implemented by your runtime");