Salome HOME
Documentation update
[modules/yacs.git] / src / engine / Runtime.cxx
1 // Copyright (C) 2006-2015  CEA/DEN, EDF R&D
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"
30 #include "InputDataStreamPort.hxx"
31 #include "OutputDataStreamPort.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
42 using namespace YACS::ENGINE;
43 using namespace std;
44
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;
53
54 // --- init typecodes for edInit with C++ Any
55
56
57 const char Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME[]="Neutral";
58
59 // singleton creation must be done before by a derived class
60
61 Runtime* YACS::ENGINE::getRuntime() throw(Exception)
62 {
63   if ( !  Runtime::_singleton )
64     throw Exception("Runtime is not yet initialized");
65   return Runtime::_singleton;
66 }
67
68 Runtime::Runtime()
69 {
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   _builtinCatalog->_composednodeMap["Bloc"]=createBloc("Bloc");
90   _builtinCatalog->_composednodeMap["Switch"]=createSwitch("Switch");
91   _builtinCatalog->_composednodeMap["WhileLoop"]=createWhileLoop("WhileLoop");
92   _builtinCatalog->_composednodeMap["ForLoop"]=createForLoop("ForLoop");
93   _builtinCatalog->_composednodeMap["ForEachLoop_double"]=createForEachLoop("ForEachLoop_double",Runtime::_tc_double);
94   _builtinCatalog->_composednodeMap["ForEachLoop_string"]=createForEachLoop("ForEachLoop_string",Runtime::_tc_string);
95   _builtinCatalog->_composednodeMap["ForEachLoop_int"]=createForEachLoop("ForEachLoop_int",Runtime::_tc_int);
96   _builtinCatalog->_composednodeMap["ForEachLoop_bool"]=createForEachLoop("ForEachLoop_bool",Runtime::_tc_bool);
97   std::map<std::string,TypeCode*>& typeMap=_builtinCatalog->_typeMap;
98   Runtime::_tc_double->incrRef();
99   typeMap["double"]=Runtime::_tc_double;
100   Runtime::_tc_int->incrRef();
101   typeMap["int"]=Runtime::_tc_int;
102   Runtime::_tc_bool->incrRef();
103   typeMap["bool"]=Runtime::_tc_bool;
104   Runtime::_tc_string->incrRef();
105   typeMap["string"]=Runtime::_tc_string;
106   Runtime::_tc_file->incrRef();
107   typeMap["file"]=Runtime::_tc_file;
108   Runtime::_tc_stringpair->incrRef();
109   typeMap["stringpair"]=Runtime::_tc_stringpair;
110   Runtime::_tc_propvec->incrRef();
111   typeMap["propvec"]=Runtime::_tc_propvec;
112
113   // Get dynamic trace level
114   YACS::traceLevel=0;
115   char* valenv=getenv("YACS_TRACELEVEL");
116   if(valenv)
117     {
118       std::istringstream iss(valenv);
119       int temp;
120       if (iss >> temp)
121         YACS::traceLevel=temp;
122     }
123
124   // Get max threads number
125   char *maxThreadStr = getenv("YACS_MAX_THREADS");
126   if (maxThreadStr != NULL)
127     {
128       int maxThreads = atoi(maxThreadStr);
129       DEBTRACE("maxThreads = " << maxThreads);
130       if (maxThreads > 0) Executor::_maxThreads = maxThreads;
131     }
132
133   // Get thread stack size
134   char *threadStackSizeStr = getenv("YACS_THREADS_STACK_SIZE");
135   if (threadStackSizeStr != NULL)
136     {
137       size_t threadStackSize = strtoul(threadStackSizeStr, NULL, 0);
138       DEBTRACE("threadStackSize = " << threadStackSize);
139       if (threadStackSize > 0) Executor::_threadStackSize = threadStackSize;
140     }
141 }
142
143 void Runtime::removeRuntime()
144 {
145   delete this;
146 }
147
148 Runtime::~Runtime()
149 {
150   delete _builtinCatalog;
151   DEBTRACE( "_tc_double refcnt: " << Runtime::_tc_double->getRefCnt() );
152   DEBTRACE( "_tc_int refcnt: " << Runtime::_tc_int->getRefCnt() );
153   DEBTRACE( "_tc_bool refcnt: " << Runtime::_tc_bool->getRefCnt() );
154   DEBTRACE( "_tc_string refcnt: " << Runtime::_tc_string->getRefCnt() );
155   DEBTRACE( "_tc_file refcnt: " << Runtime::_tc_file->getRefCnt() );
156   Runtime::_tc_double->decrRef();
157   Runtime::_tc_int->decrRef();
158   Runtime::_tc_bool->decrRef();
159   Runtime::_tc_string->decrRef();
160   Runtime::_tc_file->decrRef();
161   for(std::vector<Catalog *>::iterator it=_catalogs.begin();it !=_catalogs.end();it++)
162     (*it)->decrRef();
163   Runtime::_singleton=0;
164   DEBTRACE( "Total YACS::ENGINE::Refcount: " << RefCounter::_totalCnt );
165 }
166
167 TypeCode * Runtime::createInterfaceTc(const std::string& id, const std::string& name,
168                                       std::list<TypeCodeObjref *> ltc)
169 {
170   return TypeCode::interfaceTc(id.c_str(),name.c_str(),ltc);
171 }
172
173 TypeCode * Runtime::createSequenceTc(const std::string& id,
174                                      const std::string& name,
175                                      TypeCode *content)
176 {
177   return TypeCode::sequenceTc(id.c_str(),name.c_str(),content);
178 };
179
180 TypeCodeStruct * Runtime::createStructTc(const std::string& id, const std::string& name)
181 {
182   return (TypeCodeStruct *)TypeCode::structTc(id.c_str(),name.c_str());
183 }
184
185 DataNode* Runtime::createInDataNode(const std::string& kind,const std::string& name)
186 {
187   throw Exception("InDataNode factory not implemented");
188 }
189
190 DataNode* Runtime::createOutDataNode(const std::string& kind,const std::string& name)
191 {
192   throw Exception("OutDataNode factory not implemented");
193 }
194
195 InlineFuncNode* Runtime::createFuncNode(const std::string& kind,const std::string& name)
196 {
197   throw Exception("FuncNode factory not implemented");
198 }
199
200 InlineNode* Runtime::createScriptNode(const std::string& kind,const std::string& name)
201 {
202   throw Exception("ScriptNode factory not implemented");
203 }
204
205 ServiceNode* Runtime::createRefNode(const std::string& kind,const std::string& name)
206 {
207   throw Exception("RefNode factory not implemented");
208 }
209
210 ServiceNode* Runtime::createCompoNode(const std::string& kind,const std::string& name)
211 {
212   throw Exception("CompoNode factory not implemented");
213 }
214
215 ServiceInlineNode *Runtime::createSInlineNode(const std::string& kind, const std::string& name)
216 {
217   throw Exception("SInlineNode factory not implemented");
218 }
219
220 ComponentInstance* Runtime::createComponentInstance(const std::string& name,
221                                                     const std::string& kind)
222 {
223   throw Exception("ComponentInstance factory not implemented");
224 }
225
226 Container *Runtime::createContainer(const std::string& kind)
227 {
228   throw Exception("Container factory not implemented");
229 }
230
231 Proc* Runtime::createProc(const std::string& name)
232 {
233   return new Proc(name);
234 }
235
236 Bloc* Runtime::createBloc(const std::string& name)
237 {
238   return new Bloc(name);
239 }
240
241 Switch* Runtime::createSwitch(const std::string& name)
242 {
243   return new Switch(name);
244 }
245
246 WhileLoop* Runtime::createWhileLoop(const std::string& name)
247 {
248   return new WhileLoop(name);
249 }
250
251 ForLoop* Runtime::createForLoop(const std::string& name)
252 {
253   return new ForLoop(name);
254 }
255
256 ForEachLoop* Runtime::createForEachLoop(const std::string& name,TypeCode *type)
257 {
258   return new ForEachLoop(name,type);
259 }
260
261 OptimizerLoop* Runtime::createOptimizerLoop(const std::string& name,const std::string& algLib,const std::string& factoryName,bool algInitOnFile,
262                                             const std::string& kind, Proc * procForTypes)
263 {
264   return new OptimizerLoop(name,algLib,factoryName,algInitOnFile, true, procForTypes);
265 }
266
267 InputDataStreamPort* Runtime::createInputDataStreamPort(const std::string& name,Node *node,TypeCode *type)
268 {
269   return new InputDataStreamPort(name,node,type);
270 }
271
272 OutputDataStreamPort* Runtime::createOutputDataStreamPort(const std::string& name,Node *node,TypeCode *type)
273 {
274   return new OutputDataStreamPort(name,node,type);
275 }
276
277 //! Load a catalog of calculation to use as factory
278 /*!
279  * \param sourceKind: the kind of catalog source. It depends on runtime. It could be a file, a server, a directory
280  * \param path: the path to the catalog. 
281  * \return the loaded Catalog
282  */
283 Catalog* Runtime::loadCatalog(const std::string& sourceKind,const std::string& path)
284 {
285   if (_catalogLoaderFactoryMap.find(sourceKind) == _catalogLoaderFactoryMap.end())
286     {
287       throw Exception("This type of catalog loader does not exist: " + sourceKind);
288     }
289   else
290     {
291       Catalog* cata=new Catalog(path);
292       CatalogLoader* proto=_catalogLoaderFactoryMap[sourceKind];
293       proto->load(cata,path);
294       return cata;
295     }
296 }
297
298 //! Add a catalog loader factory to the map _catalogLoaderFactoryMap under the name name
299 /*!
300  * \param name: name under which the factory is registered
301  * \param factory: the factory
302  */
303 void Runtime::setCatalogLoaderFactory(const std::string& name, CatalogLoader* factory)
304 {
305   _catalogLoaderFactoryMap[name]=factory;
306 }
307
308 //! Get the catalog of base nodes (elementary and composed)
309 /*!
310  * \return the builtin Catalog
311  */
312 Catalog* Runtime::getBuiltinCatalog()
313 {
314   return _builtinCatalog;
315 }
316
317 //! Add a catalog of types and nodes to the runtime
318 /*!
319  * These catalogs are searched when calling getTypeCode method
320  */
321 void Runtime::addCatalog(Catalog* catalog)
322 {
323   _catalogs.push_back(catalog);
324   catalog->incrRef();
325 }
326
327 //! Get a typecode by its name from runtime catalogs
328 /*!
329  * \return the typecode if it exists or NULL
330  */
331 TypeCode* Runtime::getTypeCode(const std::string& name)
332 {
333   if (_builtinCatalog->_typeMap.count(name) != 0)
334     return _builtinCatalog->_typeMap[name];
335   for(std::vector<Catalog *>::const_iterator it=_catalogs.begin();it !=_catalogs.end();it++)
336     {
337       if ((*it)->_typeMap.count(name) != 0)
338         return (*it)->_typeMap[name];
339     }
340   return 0;
341 }
342
343 //! Convert a YACS::ENGINE::Any object to an external object with type type
344 /*!
345  * This method is used to convert Neutral objects to script languages. For example
346  * Python language. The runtime has one external script language.
347  * The object is returned as a void * because engine knows nothing about external script language.
348  *
349  * \param type: the type of the converted object if the conversion is possible
350  * \param data: the object to convert
351  * \return the converted object
352  */
353 void* Runtime::convertNeutral(TypeCode * type, Any *data)
354 {
355   throw Exception("convertNeutral is not implemented by your runtime");
356 }
357
358 //! Convert a YACS::ENGINE::Any object to a string to be used in GUI for example
359 /*!
360  * engine package does not provide a conversion to string. It has to be implemented in the 
361  * runtime package.
362  *
363  * \param type: the type of the object to convert
364  * \param data: the object to convert
365  * \return the string representation of the object
366  */
367 std::string Runtime::convertNeutralAsString(TypeCode * type, Any *data)
368 {
369   throw Exception("convertNeutralAsString is not implemented by your runtime");
370 }
371