Version: 9.16.0
Runtime.cxx
Go to the documentation of this file.
1// Copyright (C) 2006-2026 CEA, EDF
2//
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.
7//
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.
12//
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
16//
17// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18//
19
20#include "Runtime.hxx"
21
22#include<cassert>
23#include "WhileLoop.hxx"
24#include "ForLoop.hxx"
25#include "ForEachLoop.hxx"
26#include "OptimizerLoop.hxx"
27#include "Switch.hxx"
28#include "Bloc.hxx"
29#include "Proc.hxx"
32#include "Catalog.hxx"
33#include "TypeCode.hxx"
34#include "Executor.hxx"
35
36#include<iostream>
37#include<cstdlib>
38
39//#define _DEVDEBUG_
40#include "YacsTrace.hxx"
41
42using namespace YACS::ENGINE;
43using namespace std;
44
45Runtime* Runtime::_singleton = 0;
46TypeCode* Runtime::_tc_double = 0;
47TypeCode* Runtime::_tc_int = 0;
48TypeCode* Runtime::_tc_bool = 0;
49TypeCode* Runtime::_tc_string = 0;
50TypeCode* Runtime::_tc_file = 0;
51TypeCode* Runtime::_tc_stringpair = 0;
52TypeCode* Runtime::_tc_propvec = 0;
53
54// --- init typecodes for edInit with C++ Any
55
56
57const char Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME[]="Neutral";
58
59// singleton creation must be done before by a derived class
60
62{
63 if ( ! Runtime::_singleton )
64 throw Exception("Runtime is not yet initialized");
65 return Runtime::_singleton;
66}
67
68Runtime::Runtime()
69{
70 DEBTRACE("Runtime::Runtime");
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. */
94 typeMap["double"]=Runtime::_tc_double;
96 typeMap["int"]=Runtime::_tc_int;
98 typeMap["bool"]=Runtime::_tc_bool;
100 typeMap["string"]=Runtime::_tc_string;
102 typeMap["file"]=Runtime::_tc_file;
104 typeMap["stringpair"]=Runtime::_tc_stringpair;
106 typeMap["propvec"]=Runtime::_tc_propvec;
107
108 // Get dynamic trace level
110 char* valenv=getenv("YACS_TRACELEVEL");
111 if(valenv)
112 {
113 std::istringstream iss(valenv);
114 int temp;
115 if (iss >> temp)
116 YACS::traceLevel=temp;
117 }
118
119 // Get max threads number
120 char *maxThreadStr = getenv("YACS_MAX_THREADS");
121 if (maxThreadStr != NULL)
122 {
123 int maxThreads = atoi(maxThreadStr);
124 DEBTRACE("maxThreads = " << maxThreads);
125 if (maxThreads > 0) Executor::_maxThreads = maxThreads;
126 }
127
128 // Get thread stack size
129 char *threadStackSizeStr = getenv("YACS_THREADS_STACK_SIZE");
130 if (threadStackSizeStr != NULL)
131 {
132 size_t threadStackSize = strtoul(threadStackSizeStr, NULL, 0);
133 DEBTRACE("threadStackSize = " << threadStackSize);
134 if (threadStackSize > 0) Executor::_threadStackSize = threadStackSize;
135 }
136}
137
139{
140 delete this;
141}
142
144{
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() );
156 for(std::vector<Catalog *>::iterator it=_catalogs.begin();it !=_catalogs.end();it++)
157 (*it)->decrRef();
159 DEBTRACE( "Total YACS::ENGINE::Refcount: " << RefCounter::_totalCnt );
160}
161
162TypeCode * Runtime::createInterfaceTc(const std::string& id, const std::string& name,
163 std::list<TypeCodeObjref *> ltc)
164{
165 return TypeCode::interfaceTc(id.c_str(),name.c_str(),ltc);
166}
167
168TypeCode * Runtime::createSequenceTc(const std::string& id,
169 const std::string& name,
170 TypeCode *content)
171{
172 return TypeCode::sequenceTc(id.c_str(),name.c_str(),content);
173};
174
175TypeCodeStruct * Runtime::createStructTc(const std::string& id, const std::string& name)
176{
177 return (TypeCodeStruct *)TypeCode::structTc(id.c_str(),name.c_str());
178}
179
180DataNode* Runtime::createInDataNode(const std::string& kind,const std::string& name)
181{
182 throw Exception("InDataNode factory not implemented");
183}
184
185DataNode* Runtime::createOutDataNode(const std::string& kind,const std::string& name)
186{
187 throw Exception("OutDataNode factory not implemented");
188}
189
190InlineFuncNode* Runtime::createFuncNode(const std::string& kind,const std::string& name)
191{
192 throw Exception("FuncNode factory not implemented");
193}
194
195InlineNode* Runtime::createScriptNode(const std::string& kind,const std::string& name)
196{
197 throw Exception("ScriptNode factory not implemented");
198}
199
200ServiceNode* Runtime::createRefNode(const std::string& kind,const std::string& name)
201{
202 throw Exception("RefNode factory not implemented");
203}
204
205ServiceNode* Runtime::createCompoNode(const std::string& kind,const std::string& name)
206{
207 throw Exception("CompoNode factory not implemented");
208}
209
210ServiceInlineNode *Runtime::createSInlineNode(const std::string& kind, const std::string& name)
211{
212 throw Exception("SInlineNode factory not implemented");
213}
214
216 const std::string& kind)
217{
218 throw Exception("ComponentInstance factory not implemented");
219}
220
221Container *Runtime::createContainer(const std::string& kind)
222{
223 throw Exception("Container factory not implemented");
224}
225
226Proc* Runtime::createProc(const std::string& name)
227{
228 return new Proc(name);
229}
230
231Bloc* Runtime::createBloc(const std::string& name)
232{
233 return new Bloc(name);
234}
235
236Switch* Runtime::createSwitch(const std::string& name)
237{
238 return new Switch(name);
239}
240
241WhileLoop* Runtime::createWhileLoop(const std::string& name)
242{
243 return new WhileLoop(name);
244}
245
246ForLoop* Runtime::createForLoop(const std::string& name)
247{
248 return new ForLoop(name);
249}
250
251ForEachLoop* Runtime::createForEachLoop(const std::string& name,TypeCode *type)
252{
253 ForEachLoop* ret = new ForEachLoop(name,type);
254 ret->edGetNbOfBranchesPort()->edInit(1);
255 return ret;
256}
257
259{
260 return new ForEachLoopDyn(name,type);
261}
262
263OptimizerLoop* Runtime::createOptimizerLoop(const std::string& name,const std::string& algLib,const std::string& factoryName,bool algInitOnFile,
264 const std::string& kind, Proc * procForTypes)
265{
266 return new OptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes);
267}
268
270{
271 return new InputDataStreamPort(name,node,type);
272}
273
275{
276 return new OutputDataStreamPort(name,node,type);
277}
278
280
285Catalog* Runtime::loadCatalog(const std::string& sourceKind,const std::string& path)
286{
287 if (_catalogLoaderFactoryMap.find(sourceKind) == _catalogLoaderFactoryMap.end())
288 {
289 throw Exception("This type of catalog loader does not exist: " + sourceKind);
290 }
291 else
292 {
293 Catalog* cata=new Catalog(path);
294 CatalogLoader* proto=_catalogLoaderFactoryMap[sourceKind];
295 proto->load(cata,path);
296 return cata;
297 }
298}
299
301
305void Runtime::setCatalogLoaderFactory(const std::string& name, CatalogLoader* factory)
306{
307 _catalogLoaderFactoryMap[name]=factory;
308}
309
311
315{
316 return _builtinCatalog;
317}
318
320
324{
325 _catalogs.push_back(catalog);
326 catalog->incrRef();
327}
328
330
333TypeCode* Runtime::getTypeCode(const std::string& name)
334{
335 if (_builtinCatalog->_typeMap.count(name) != 0)
336 return _builtinCatalog->_typeMap[name];
337 for(std::vector<Catalog *>::const_iterator it=_catalogs.begin();it !=_catalogs.end();it++)
338 {
339 if ((*it)->_typeMap.count(name) != 0)
340 return (*it)->_typeMap[name];
341 }
342 return 0;
343}
344
346
356{
357 throw Exception("convertNeutral is not implemented by your runtime");
358}
359
361
370{
371 throw Exception("convertNeutralAsString is not implemented by your runtime");
372}
373
#define DEBTRACE(msg)
Definition: YacsTrace.hxx:31
: Interface for management of storage of data formated dynamically in its TypeCode....
Definition: Any.hxx:79
Composed node to group elementary and composed nodes.
Definition: Bloc.hxx:36
class for YACS catalog loader.
Definition: Catalog.hxx:63
virtual void load(Catalog *cata, const std::string &path)
Definition: Catalog.cxx:74
class for YACS catalogs.
Definition: Catalog.hxx:42
std::map< std::string, TypeCode * > _typeMap
Definition: Catalog.hxx:49
Base class for all component instances.
Class for data parameters specification.
Definition: DataNode.hxx:38
InputPort * edGetNbOfBranchesPort()
Definition: DynParaLoop.hxx:83
static size_t _threadStackSize
Definition: Executor.hxx:132
static int _maxThreads
Definition: Executor.hxx:131
Loop node for parametric calculation.
Class for for loop node.
Definition: ForLoop.hxx:33
Class for calculation node (function) inlined (and executed) in the schema.
Definition: InlineNode.hxx:93
Class for calculation node (script) inlined (and executed) in the schema.
Definition: InlineNode.hxx:44
void edInit(T value)
Definition: InputPort.hxx:129
Base class for all nodes.
Definition: Node.hxx:70
class to build optimization loops
Base class for all schema objects.
Definition: Proc.hxx:44
static unsigned int _totalCnt
Definition: RefCounter.hxx:35
Catalog * getBuiltinCatalog()
Get the catalog of base nodes (elementary and composed)
Definition: Runtime.cxx:314
virtual ServiceNode * createRefNode(const std::string &kind, const std::string &name)
Definition: Runtime.cxx:200
virtual OptimizerLoop * createOptimizerLoop(const std::string &name, const std::string &algLib, const std::string &factoryName, bool algInitOnFile, const std::string &kind="", Proc *procForTypes=NULL)
Definition: Runtime.cxx:263
static YACS::ENGINE::TypeCode * _tc_double
Definition: Runtime.hxx:136
virtual InlineFuncNode * createFuncNode(const std::string &kind, const std::string &name)
Definition: Runtime.cxx:190
virtual std::string convertNeutralAsString(TypeCode *type, Any *data)
Convert a YACS::ENGINE::Any object to a string to be used in GUI for example.
Definition: Runtime.cxx:369
virtual Switch * createSwitch(const std::string &name)
Definition: Runtime.cxx:236
virtual void addCatalog(Catalog *catalog)
Add a catalog of types and nodes to the runtime.
Definition: Runtime.cxx:323
virtual InputDataStreamPort * createInputDataStreamPort(const std::string &name, Node *node, TypeCode *type)
Definition: Runtime.cxx:269
static YACS::ENGINE::TypeCode * _tc_bool
Definition: Runtime.hxx:138
virtual ForLoop * createForLoop(const std::string &name)
Definition: Runtime.cxx:246
virtual ServiceInlineNode * createSInlineNode(const std::string &kind, const std::string &name)
Definition: Runtime.cxx:210
virtual InlineNode * createScriptNode(const std::string &kind, const std::string &name)
Definition: Runtime.cxx:195
Catalog * _builtinCatalog
Definition: Runtime.hxx:153
virtual DataNode * createOutDataNode(const std::string &kind, const std::string &name)
Definition: Runtime.cxx:185
virtual Container * createContainer(const std::string &kind="")
Definition: Runtime.cxx:221
virtual TypeCode * getTypeCode(const std::string &name)
Get a typecode by its name from runtime catalogs.
Definition: Runtime.cxx:333
virtual TypeCode * createInterfaceTc(const std::string &id, const std::string &name, std::list< TypeCodeObjref * > ltc)
Definition: Runtime.cxx:162
virtual ForEachLoop * createForEachLoop(const std::string &name, TypeCode *type)
Definition: Runtime.cxx:251
virtual Bloc * createBloc(const std::string &name)
Definition: Runtime.cxx:231
virtual Catalog * loadCatalog(const std::string &sourceKind, const std::string &path)
Load a catalog of calculation to use as factory.
Definition: Runtime.cxx:285
virtual TypeCode * createSequenceTc(const std::string &id, const std::string &name, TypeCode *content)
Definition: Runtime.cxx:168
virtual OutputDataStreamPort * createOutputDataStreamPort(const std::string &name, Node *node, TypeCode *type)
Definition: Runtime.cxx:274
virtual void setCatalogLoaderFactory(const std::string &name, CatalogLoader *factory)
Add a catalog loader factory to the map _catalogLoaderFactoryMap under the name name.
Definition: Runtime.cxx:305
virtual DataNode * createInDataNode(const std::string &kind, const std::string &name)
Definition: Runtime.cxx:180
virtual Proc * createProc(const std::string &name)
Definition: Runtime.cxx:226
virtual ForEachLoopDyn * createForEachLoopDyn(const std::string &name, TypeCode *type)
Definition: Runtime.cxx:258
static YACS::ENGINE::TypeCode * _tc_file
Definition: Runtime.hxx:140
virtual ServiceNode * createCompoNode(const std::string &kind, const std::string &name)
Definition: Runtime.cxx:205
std::vector< Catalog * > _catalogs
Definition: Runtime.hxx:154
static Runtime * _singleton
Definition: Runtime.hxx:150
virtual TypeCodeStruct * createStructTc(const std::string &id, const std::string &name)
Definition: Runtime.cxx:175
static YACS::ENGINE::TypeCode * _tc_propvec
Definition: Runtime.hxx:142
virtual void removeRuntime()
Definition: Runtime.cxx:138
std::map< std::string, CatalogLoader * > _catalogLoaderFactoryMap
Definition: Runtime.hxx:144
virtual ComponentInstance * createComponentInstance(const std::string &name, const std::string &kind="")
Definition: Runtime.cxx:215
static YACS::ENGINE::TypeCode * _tc_int
Definition: Runtime.hxx:137
static YACS::ENGINE::TypeCode * _tc_stringpair
Definition: Runtime.hxx:141
virtual WhileLoop * createWhileLoop(const std::string &name)
Definition: Runtime.cxx:241
static YACS::ENGINE::TypeCode * _tc_string
Definition: Runtime.hxx:139
virtual void * convertNeutral(TypeCode *type, Any *data)
Convert a YACS::ENGINE::Any object to an external object with type type.
Definition: Runtime.cxx:355
Class for calculation node associated with a component service.
Definition: ServiceNode.hxx:35
Control node that emulates the C switch.
Definition: Switch.hxx:85
Class for reference objects.
Definition: TypeCode.hxx:126
Class for sequence objects.
Definition: TypeCode.hxx:160
Class for struct type.
Definition: TypeCode.hxx:228
virtual void addMember(const std::string &name, TypeCode *tc)
The only non const method.
Definition: TypeCode.cxx:781
Base class for all type objects.
Definition: TypeCode.hxx:68
static TypeCode * interfaceTc(const char *id, const char *name)
static factory of object reference type given an id and a name
Definition: TypeCode.cxx:229
static TypeCode * sequenceTc(const char *id, const char *name, TypeCode *content)
static factory of sequence type given an id, a name and a content type
Definition: TypeCode.cxx:254
static TypeCode * structTc(const char *id, const char *name)
static factory of struct type given an id and a name
Definition: TypeCode.cxx:269
Class for a while loop.
Definition: WhileLoop.hxx:39
YACSLIBENGINE_EXPORT Runtime * getRuntime()
Definition: Runtime.cxx:61
int traceLevel
Definition: YacsTrace.cxx:37