4 #include "WhileLoop.hxx"
6 #include "ForEachLoop.hxx"
10 #include "InputDataStreamPort.hxx"
11 #include "OutputDataStreamPort.hxx"
12 #include "Catalog.hxx"
13 #include "TypeCode.hxx"
18 #include "YacsTrace.hxx"
20 using namespace YACS::ENGINE;
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;
30 // --- init typecodes for edInit with C++ Any
33 const char Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME[]="Neutral";
35 // singleton creation must be done before by a derived class
37 Runtime* YACS::ENGINE::getRuntime() throw(Exception)
39 if ( ! Runtime::_singleton )
40 throw Exception("Runtime is not yet initialized");
41 return Runtime::_singleton;
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;
76 void Runtime::removeRuntime()
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++)
96 Runtime::_singleton=0;
97 DEBTRACE( "Total YACS::ENGINE::Refcount: " << RefCounter::_totalCnt );
100 DataNode* Runtime::createInDataNode(const std::string& kind,const std::string& name)
102 throw Exception("InDataNode factory not implemented");
105 DataNode* Runtime::createOutDataNode(const std::string& kind,const std::string& name)
107 throw Exception("OutDataNode factory not implemented");
110 InlineFuncNode* Runtime::createFuncNode(const std::string& kind,const std::string& name)
112 throw Exception("FuncNode factory not implemented");
115 InlineNode* Runtime::createScriptNode(const std::string& kind,const std::string& name)
117 throw Exception("ScriptNode factory not implemented");
120 ServiceNode* Runtime::createRefNode(const std::string& kind,const std::string& name)
122 throw Exception("RefNode factory not implemented");
125 ServiceNode* Runtime::createCompoNode(const std::string& kind,const std::string& name)
127 throw Exception("CompoNode factory not implemented");
130 ServiceInlineNode *Runtime::createSInlineNode(const std::string& kind, const std::string& name)
132 throw Exception("SInlineNode factory not implemented");
135 ComponentInstance* Runtime::createComponentInstance(const std::string& name,
136 const std::string& kind)
138 throw Exception("ComponentInstance factory not implemented");
141 Container *Runtime::createContainer(const std::string& kind)
143 throw Exception("Container factory not implemented");
146 Proc* Runtime::createProc(const std::string& name)
148 return new Proc(name);
151 Bloc* Runtime::createBloc(const std::string& name)
153 return new Bloc(name);
156 Switch* Runtime::createSwitch(const std::string& name)
158 return new Switch(name);
161 WhileLoop* Runtime::createWhileLoop(const std::string& name)
163 return new WhileLoop(name);
166 ForLoop* Runtime::createForLoop(const std::string& name)
168 return new ForLoop(name);
171 ForEachLoop* Runtime::createForEachLoop(const std::string& name,TypeCode *type)
173 return new ForEachLoop(name,type);
176 InputDataStreamPort* Runtime::createInputDataStreamPort(const std::string& name,Node *node,TypeCode *type)
178 return new InputDataStreamPort(name,node,type);
181 OutputDataStreamPort* Runtime::createOutputDataStreamPort(const std::string& name,Node *node,TypeCode *type)
183 return new OutputDataStreamPort(name,node,type);
186 //! Load a catalog of calculation to use as factory
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
192 Catalog* Runtime::loadCatalog(const std::string& sourceKind,const std::string& path)
194 if (_catalogLoaderFactoryMap.find(sourceKind) == _catalogLoaderFactoryMap.end())
196 throw Exception("This type of catalog loader does not exist: " + sourceKind);
200 Catalog* cata=new Catalog(path);
201 CatalogLoader* proto=_catalogLoaderFactoryMap[sourceKind];
202 proto->load(cata,path);
207 //! Add a catalog loader factory to the map _catalogLoaderFactoryMap under the name name
209 * \param name: name under which the factory is registered
210 * \param factory: the factory
212 void Runtime::setCatalogLoaderFactory(const std::string& name, CatalogLoader* factory)
214 _catalogLoaderFactoryMap[name]=factory;
217 //! Get the catalog of base nodes (elementary and composed)
219 * \return the builtin Catalog
221 Catalog* Runtime::getBuiltinCatalog()
223 return _builtinCatalog;
226 //! Add a catalog of types and nodes to the runtime
228 * These catalogs are searched when calling getTypeCode method
230 void Runtime::addCatalog(Catalog* catalog)
232 _catalogs.push_back(catalog);
235 //! Get a typecode by its name from runtime catalogs
237 * \return the typecode if it exists or NULL
239 TypeCode* Runtime::getTypeCode(const std::string& name)
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++)
245 if ((*it)->_typeMap.count(name) != 0)
246 return (*it)->_typeMap[name];