]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/Runtime.cxx
Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / src / engine / Runtime.cxx
1 #include "Runtime.hxx"
2
3 #include<cassert>
4 #include "WhileLoop.hxx"
5 #include "ForLoop.hxx"
6 #include "ForEachLoop.hxx"
7 #include "Switch.hxx"
8 #include "Bloc.hxx"
9 #include "Proc.hxx"
10 #include "InputDataStreamPort.hxx"
11 #include "OutputDataStreamPort.hxx"
12 #include "Catalog.hxx"
13 #include "TypeCode.hxx"
14
15 #include<iostream>
16
17 //#define _DEVDEBUG_
18 #include "YacsTrace.hxx"
19
20 using namespace YACS::ENGINE;
21 using namespace std;
22
23 Runtime* Runtime::_singleton = 0;
24 TypeCode* Runtime::_tc_double = 0;
25 TypeCode* Runtime::_tc_int = 0;
26 TypeCode* Runtime::_tc_bool = 0;
27 TypeCode* Runtime::_tc_string = 0;
28 TypeCode* Runtime::_tc_file = 0;
29
30 // --- init typecodes for edInit with C++ Any
31
32
33 const char Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME[]="Neutral";
34
35 // singleton creation must be done before by a derived class
36
37 Runtime* YACS::ENGINE::getRuntime() throw(Exception)
38 {
39   if ( !  Runtime::_singleton )
40     throw Exception("Runtime is not yet initialized");
41   return Runtime::_singleton;
42 }
43
44 Runtime::Runtime()
45 {
46   DEBTRACE("Runtime::Runtime");
47   Runtime::_tc_double = new TypeCode(Double);
48   Runtime::_tc_int    = new TypeCode(Int);
49   Runtime::_tc_bool   = new TypeCode(Bool);
50   Runtime::_tc_string = new TypeCode(String);
51   Runtime::_tc_file = new TypeCodeObjref("file", "file");
52   DEBTRACE( "_tc_double refcnt: " << Runtime::_tc_double->getRefCnt() );
53   DEBTRACE( "_tc_int refcnt: " << Runtime::_tc_int->getRefCnt() );
54   DEBTRACE( "_tc_bool refcnt: " << Runtime::_tc_bool->getRefCnt() );
55   DEBTRACE( "_tc_string refcnt: " << Runtime::_tc_string->getRefCnt() );
56   DEBTRACE( "_tc_file refcnt: " << Runtime::_tc_file->getRefCnt() );
57   _builtinCatalog = new Catalog("builtins");
58   _builtinCatalog->_composednodeMap["Bloc"]=new Bloc("Bloc");
59   _builtinCatalog->_composednodeMap["Switch"]=new Switch("Switch");
60   _builtinCatalog->_composednodeMap["WhileLoop"]=new WhileLoop("WhileLoop");
61   _builtinCatalog->_composednodeMap["ForLoop"]=new ForLoop("ForLoop");
62   _builtinCatalog->_composednodeMap["ForEachLoopDouble"]=new ForEachLoop("ForEachLoopDouble",Runtime::_tc_double);
63   std::map<std::string,TypeCode*>& typeMap=_builtinCatalog->_typeMap;
64   Runtime::_tc_double->incrRef();
65   typeMap["double"]=Runtime::_tc_double;
66   Runtime::_tc_int->incrRef();
67   typeMap["int"]=Runtime::_tc_int;
68   Runtime::_tc_bool->incrRef();
69   typeMap["bool"]=Runtime::_tc_bool;
70   Runtime::_tc_string->incrRef();
71   typeMap["string"]=Runtime::_tc_string;
72   Runtime::_tc_file->incrRef();
73   typeMap["file"]=Runtime::_tc_file;
74 }
75
76 void Runtime::removeRuntime()
77 {
78   delete this;
79 }
80
81 Runtime::~Runtime()
82 {
83   delete _builtinCatalog;
84   DEBTRACE( "_tc_double refcnt: " << Runtime::_tc_double->getRefCnt() );
85   DEBTRACE( "_tc_int refcnt: " << Runtime::_tc_int->getRefCnt() );
86   DEBTRACE( "_tc_bool refcnt: " << Runtime::_tc_bool->getRefCnt() );
87   DEBTRACE( "_tc_string refcnt: " << Runtime::_tc_string->getRefCnt() );
88   DEBTRACE( "_tc_file refcnt: " << Runtime::_tc_file->getRefCnt() );
89   Runtime::_tc_double->decrRef();
90   Runtime::_tc_int->decrRef();
91   Runtime::_tc_bool->decrRef();
92   Runtime::_tc_string->decrRef();
93   Runtime::_tc_file->decrRef();
94   for(std::vector<Catalog *>::const_iterator it=_catalogs.begin();it !=_catalogs.end();it++)
95     delete (*it);
96   Runtime::_singleton=0;
97   DEBTRACE( "Total YACS::ENGINE::Refcount: " << RefCounter::_totalCnt );
98 }
99
100 DataNode* Runtime::createInDataNode(const std::string& kind,const std::string& name)
101 {
102   throw Exception("InDataNode factory not implemented");
103 }
104
105 DataNode* Runtime::createOutDataNode(const std::string& kind,const std::string& name)
106 {
107   throw Exception("OutDataNode factory not implemented");
108 }
109
110 InlineFuncNode* Runtime::createFuncNode(const std::string& kind,const std::string& name)
111 {
112   throw Exception("FuncNode factory not implemented");
113 }
114
115 InlineNode* Runtime::createScriptNode(const std::string& kind,const std::string& name)
116 {
117   throw Exception("ScriptNode factory not implemented");
118 }
119
120 ServiceNode* Runtime::createRefNode(const std::string& kind,const std::string& name)
121 {
122   throw Exception("RefNode factory not implemented");
123 }
124
125 ServiceNode* Runtime::createCompoNode(const std::string& kind,const std::string& name)
126 {
127   throw Exception("CompoNode factory not implemented");
128 }
129
130 ServiceInlineNode *Runtime::createSInlineNode(const std::string& kind, const std::string& name)
131 {
132   throw Exception("SInlineNode factory not implemented");
133 }
134
135 ComponentInstance* Runtime::createComponentInstance(const std::string& name,
136                                                     const std::string& kind)
137 {
138   throw Exception("ComponentInstance factory not implemented");
139 }
140
141 Container *Runtime::createContainer(const std::string& kind)
142 {
143   throw Exception("Container factory not implemented");
144 }
145
146 Proc* Runtime::createProc(const std::string& name)
147 {
148   return new Proc(name);
149 }
150
151 Bloc* Runtime::createBloc(const std::string& name)
152 {
153   return new Bloc(name);
154 }
155
156 Switch* Runtime::createSwitch(const std::string& name)
157 {
158   return new Switch(name);
159 }
160
161 WhileLoop* Runtime::createWhileLoop(const std::string& name)
162 {
163   return new WhileLoop(name);
164 }
165
166 ForLoop* Runtime::createForLoop(const std::string& name)
167 {
168   return new ForLoop(name);
169 }
170
171 ForEachLoop* Runtime::createForEachLoop(const std::string& name,TypeCode *type)
172 {
173   return new ForEachLoop(name,type);
174 }
175
176 InputDataStreamPort* Runtime::createInputDataStreamPort(const std::string& name,Node *node,TypeCode *type)
177 {
178   return new InputDataStreamPort(name,node,type);
179 }
180
181 OutputDataStreamPort* Runtime::createOutputDataStreamPort(const std::string& name,Node *node,TypeCode *type)
182 {
183   return new OutputDataStreamPort(name,node,type);
184 }
185
186 //! Load a catalog of calculation to use as factory
187 /*!
188  * \param sourceKind: the kind of catalog source. It depends on runtime. It could be a file, a server, a directory
189  * \param path: the path to the catalog. 
190  * \return the loaded Catalog
191  */
192 Catalog* Runtime::loadCatalog(const std::string& sourceKind,const std::string& path)
193 {
194   if (_catalogLoaderFactoryMap.find(sourceKind) == _catalogLoaderFactoryMap.end())
195     {
196       throw Exception("This type of catalog loader does not exist: " + sourceKind);
197     }
198   else
199     {
200       Catalog* cata=new Catalog(path);
201       CatalogLoader* proto=_catalogLoaderFactoryMap[sourceKind];
202       proto->load(cata,path);
203       return cata;
204     }
205 }
206
207 //! Add a catalog loader factory to the map _catalogLoaderFactoryMap under the name name
208 /*!
209  * \param name: name under which the factory is registered
210  * \param factory: the factory
211  */
212 void Runtime::setCatalogLoaderFactory(const std::string& name, CatalogLoader* factory)
213 {
214   _catalogLoaderFactoryMap[name]=factory;
215 }
216
217 //! Get the catalog of base nodes (elementary and composed)
218 /*!
219  * \return the builtin Catalog
220  */
221 Catalog* Runtime::getBuiltinCatalog()
222 {
223   return _builtinCatalog;
224 }
225
226 //! Add a catalog of types and nodes to the runtime
227 /*!
228  * These catalogs are searched when calling getTypeCode method
229  */
230 void Runtime::addCatalog(Catalog* catalog)
231 {
232   _catalogs.push_back(catalog);
233 }
234
235 //! Get a typecode by its name from runtime catalogs
236 /*!
237  * \return the typecode if it exists or NULL
238  */
239 TypeCode* Runtime::getTypeCode(const std::string& name)
240 {
241   if (_builtinCatalog->_typeMap.count(name) != 0)
242     return _builtinCatalog->_typeMap[name];
243   for(std::vector<Catalog *>::const_iterator it=_catalogs.begin();it !=_catalogs.end();it++)
244     {
245       if ((*it)->_typeMap.count(name) != 0)
246         return (*it)->_typeMap[name];
247     }
248   return 0;
249 }
250