Salome HOME
Copyright update: 2016
[modules/yacs.git] / src / runtime / SessionCataLoader.cxx
1 // Copyright (C) 2006-2016  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 "RuntimeSALOME.hxx"
21 #include "SessionCataLoader.hxx"
22 #include "ComponentDefinition.hxx"
23 #include "ServiceNode.hxx"
24 #include "TypeCode.hxx"
25
26 #include <iostream>
27 #include <sstream>
28
29 //#define _DEVDEBUG_
30 #include "YacsTrace.hxx"
31
32 #define NEW_KERNEL
33
34 using namespace YACS::ENGINE;
35
36 SessionCataLoader::SessionCataLoader(const std::string& path):CatalogLoader(path)
37 {
38   //path should be a CORBA URI like that : corbaname::machine:port/NameService#Kernel.dir/ModulCatalog.object
39   //or equivalent corbaname
40 }
41
42 /*
43  In salome catalog, the type names for CALCIUM are : INTEGER, FLOAT,DOUBLE,STRING,BOOLEAN
44   enum DataStreamType {
45     DATASTREAM_UNKNOWN,
46     DATASTREAM_INTEGER,
47     DATASTREAM_FLOAT,
48     DATASTREAM_DOUBLE,
49     DATASTREAM_STRING,
50     DATASTREAM_BOOLEAN
51   } ;
52 */
53 #ifndef NEW_KERNEL
54 std::map<SALOME_ModuleCatalog::DataStreamType,std::string> datastreamMap;
55 #endif
56
57 //! import a SALOME component definition in a YACS catalog
58 /*!
59  * SALOME components can have several interfaces. 
60  * YACS for the moment allows only one interface by component, so if there is only one interface we import it.
61  * If we have more than one interface, we import the one with the same name as the component or the first one.
62  *
63  *   \param cata  : a YACS catalog
64  *   \param compo : a CORBA reference on a component in a SALOME_ModuleCatalog
65  */
66 void SessionCataLoader::importComponent(Catalog* cata,SALOME_ModuleCatalog::Acomponent_ptr compo)
67 {
68   CORBA::String_var componame = compo->componentname();
69
70   DEBTRACE("Implementation type : " << compo->implementation_type());
71 #ifdef NEW_KERNEL
72   SALOME_ModuleCatalog::ImplType imptyp=compo->implementation_type();
73   if(imptyp == SALOME_ModuleCatalog::SO)DEBTRACE("Dyn lib");
74   if(imptyp == SALOME_ModuleCatalog::PY)DEBTRACE("Python module");
75   if(imptyp == SALOME_ModuleCatalog::EXE)DEBTRACE("Executable");
76   DEBTRACE("Implementation name : " << compo->implementation_name());
77 #endif
78
79   SALOME_ModuleCatalog::ListOfInterfaces_var interfacename_list = compo->GetInterfaceList();
80   if(interfacename_list->length() == 0)
81     return;
82   int found=0;
83   for (int j = 0; j < interfacename_list->length();j++) 
84     {
85       DEBTRACE ("Interface : " << interfacename_list[j]);
86       if(strcmp(componame,interfacename_list[j]) == 0)
87          {
88            found=j;
89            break;
90          }
91     }
92   DEBTRACE ("Interface found : " << interfacename_list[found]);
93   ComponentDefinition* compodef=new ComponentDefinition((const char*)componame);
94
95   SALOME_ModuleCatalog::DefinitionInterface_var interface;
96   interface= compo->GetInterface(interfacename_list[found]);
97   SALOME_ModuleCatalog::ListOfInterfaceService services = interface->interfaceservicelist;
98   for (int k = 0; k < services.length(); k++)
99     {
100       DEBTRACE("Service : " << services[k].ServiceName);
101       const char* s=services[k].ServiceName;
102       //create a new service node (prototype)
103       ServiceNode* node=getRuntime()->createCompoNode("Salome",s);
104       node->setRef((const char*)componame);
105       node->setMethod(s);
106       for (int kk=0;kk< services[k].ServiceinParameter.length();kk++)
107         {
108           DEBTRACE("Parameter : " <<  services[k].ServiceinParameter[kk].Parametername);
109           DEBTRACE("Type : " <<  services[k].ServiceinParameter[kk].Parametertype);
110           const char* ty=services[k].ServiceinParameter[kk].Parametertype;
111           if(cata->_typeMap.count(ty)==0)
112             {
113               std::stringstream msg; 
114               msg << "Type " <<ty <<" does not exist. Service " << s << " of component " << componame << " is not available";
115               _errors=_errors+"\n"+msg.str();
116               std::cerr << msg.str() << std::endl;
117               compodef->_serviceMap[s+std::string("_IS_INVALID")]=0;
118               delete node;
119               node=0;
120               break;
121             }
122           const char* name=services[k].ServiceinParameter[kk].Parametername;
123           node->edAddInputPort(name,cata->_typeMap[ty]);
124         }
125       if(node == 0)continue;
126
127       for (int kk=0;kk< services[k].ServiceoutParameter.length();kk++)
128         {
129           DEBTRACE("Parameter : " <<  services[k].ServiceoutParameter[kk].Parametername);
130           DEBTRACE("Type : " <<  services[k].ServiceoutParameter[kk].Parametertype);
131           const char* ty=services[k].ServiceoutParameter[kk].Parametertype;
132           if(cata->_typeMap.count(ty)==0)
133             {
134               std::stringstream msg; 
135               msg << "Type " <<ty <<" does not exist. Service " << s << " of component " << componame << " is not available";
136               _errors=_errors+"\n"+msg.str();
137               std::cerr << msg.str() << std::endl;
138               compodef->_serviceMap[s+std::string("_IS_INVALID")]=0;
139               delete node;
140               node=0;
141               break;
142             }
143           const char* name=services[k].ServiceoutParameter[kk].Parametername;
144           node->edAddOutputPort(name,cata->_typeMap[ty]);
145         }
146       if(node == 0)continue;
147
148       for (int kk=0;kk< services[k].ServiceinDataStreamParameter.length();kk++)
149         {
150           DEBTRACE("Parameter : " <<  services[k].ServiceinDataStreamParameter[kk].Parametername);
151           DEBTRACE("Type : " <<  services[k].ServiceinDataStreamParameter[kk].Parametertype);
152           DEBTRACE("Dependency : " <<  services[k].ServiceinDataStreamParameter[kk].Parameterdependency);
153 #ifdef NEW_KERNEL
154           const char* ty=services[k].ServiceinDataStreamParameter[kk].Parametertype;
155 #else
156           std::string ty=datastreamMap[services[k].ServiceinDataStreamParameter[kk].Parametertype];
157 #endif
158           if(cata->_typeMap.count(ty)==0)
159             {
160               std::stringstream msg; 
161               msg << "Type " <<ty <<" does not exist. Service " << s << " of component " << componame << " is not available";
162               _errors=_errors+"\n"+msg.str();
163               std::cerr << msg.str() << std::endl;
164               compodef->_serviceMap[s+std::string("_IS_INVALID")]=0;
165               delete node;
166               node=0;
167               break;
168             }
169           const char* name=services[k].ServiceinDataStreamParameter[kk].Parametername;
170           InputDataStreamPort* port;
171           port=node->edAddInputDataStreamPort(name,cata->_typeMap[ty]);
172         }
173       if(node == 0)continue;
174
175       for (int kk=0;kk< services[k].ServiceoutDataStreamParameter.length();kk++)
176         {
177           DEBTRACE("Parameter : " <<  services[k].ServiceoutDataStreamParameter[kk].Parametername);
178           DEBTRACE("Type : " <<  services[k].ServiceoutDataStreamParameter[kk].Parametertype);
179           DEBTRACE("Dependency : " <<  services[k].ServiceoutDataStreamParameter[kk].Parameterdependency);
180 #ifdef NEW_KERNEL
181           const char* ty=services[k].ServiceoutDataStreamParameter[kk].Parametertype;
182 #else
183           std::string ty=datastreamMap[services[k].ServiceoutDataStreamParameter[kk].Parametertype];
184 #endif
185           if(cata->_typeMap.count(ty)==0)
186             {
187               std::stringstream msg; 
188               msg << "Type " <<ty <<" does not exist. Service " << s << " of component " << componame << " is not available";
189               _errors=_errors+"\n"+msg.str();
190               std::cerr << msg.str() << std::endl;
191               compodef->_serviceMap[s+std::string("_IS_INVALID")]=0;
192               delete node;
193               node=0;
194               break;
195             }
196           const char* name=services[k].ServiceoutDataStreamParameter[kk].Parametername;
197           OutputDataStreamPort* port;
198           port=node->edAddOutputDataStreamPort(name,cata->_typeMap[ty]);
199           // Set all properties for this port
200           /*
201           std::map<std::string, std::string>::const_iterator pt;
202           for(pt=p._props.begin();pt!=p._props.end();pt++)
203             port->setProperty((*pt).first,(*pt).second);
204             */
205         }
206       if(node == 0)continue;
207       compodef->_serviceMap[s]=node;
208     }
209
210   /* Even if a component has no service, put it in the catalog
211   if(compodef->_serviceMap.size() > 0)
212     {
213       cata->_componentMap[(const char*)componame]=compodef;
214     }
215   else
216     {
217       delete compodef;
218     }
219     */
220   cata->_componentMap[(const char*)componame]=compodef;
221 }
222
223 SessionCataLoader::~SessionCataLoader()
224 {
225   DEBTRACE ("SessionCataLoader::~SessionCataLoader");
226 }
227
228 void SessionCataLoader::loadTypes(Catalog* cata,SALOME_ModuleCatalog::ModuleCatalog_ptr catalog)
229 {
230   Runtime* r=getRuntime();
231   std::map<std::string,TypeCode*>& typeMap=cata->_typeMap;
232   // Get types list
233   SALOME_ModuleCatalog::ListOfTypeDefinition_var types_list=catalog->GetTypes();
234   for (int i=0; i< types_list->length(); i++)
235     {
236       const char* name=types_list[i].name;
237       DEBTRACE ("type : " << types_list[i].name << " " << types_list[i].kind);
238       if(types_list[i].kind == SALOME_ModuleCatalog::Dble)
239         {
240           r->_tc_double->incrRef();
241           typeMap[name]=r->_tc_double;
242         }
243       else if(types_list[i].kind == SALOME_ModuleCatalog::Int)
244         {
245           r->_tc_int->incrRef();
246           typeMap[name]=r->_tc_int;
247         }
248       else if(types_list[i].kind == SALOME_ModuleCatalog::Bool)
249         {
250           r->_tc_bool->incrRef();
251           typeMap[name]=r->_tc_bool;
252         }
253       else if(types_list[i].kind == SALOME_ModuleCatalog::Str)
254         {
255           r->_tc_string->incrRef();
256           typeMap[name]=r->_tc_string;
257         }
258       else if(types_list[i].kind == SALOME_ModuleCatalog::Seq)
259         {
260           const char* content=types_list[i].content;
261           if ( typeMap.find(content) != typeMap.end() )
262             typeMap[name]=r->createSequenceTc(name,name,typeMap[content]);
263           //else ignored !!
264         }
265       else if(types_list[i].kind == SALOME_ModuleCatalog::Array)
266         {
267           //ignored, for the moment !!
268         }
269       else if(types_list[i].kind == SALOME_ModuleCatalog::Objref)
270         {
271           std::list<TypeCodeObjref *> ltc;
272           const char* id=types_list[i].id;
273           for (int m=0; m< types_list[i].bases.length(); m++)
274             {
275               const char* b_name=types_list[i].bases[m];
276               if(typeMap.find(b_name) != typeMap.end())
277                 ltc.push_back((TypeCodeObjref *)typeMap[b_name]);
278               //else ignored !!!
279             }
280           typeMap[name]=r->createInterfaceTc(id,name,ltc);
281         }
282       else if(types_list[i].kind == SALOME_ModuleCatalog::Struc)
283         {
284           TypeCodeStruct* t=r->createStructTc("",name);
285           for (int m=0; m< types_list[i].members.length(); m++)
286             {
287               const char* m_name=types_list[i].members[m].name;
288               const char* m_type=types_list[i].members[m].type;
289               if(typeMap.find(m_type) != typeMap.end())
290                 t->addMember(m_name,typeMap[m_type]);
291               //else ignored !!!
292             }
293           typeMap[name]=t;
294         }
295       else
296         {
297           std::cerr << "Unknown kind: " << types_list[i].kind << std::endl;
298         }
299     }
300 }
301
302 void SessionCataLoader::loadTypesOld(Catalog* cata)
303 {
304   // Fill the types map with built in types
305   Runtime* r=getRuntime();
306   std::map<std::string,TypeCode*>& typeMap=cata->_typeMap;
307
308 #ifdef NEW_KERNEL
309   //Add old Calcium data types for KERNEL
310   if(typeMap.count("CALCIUM_double")!=0)
311     {
312       typeMap["DOUBLE"]=typeMap["CALCIUM_double"];
313       typeMap["DOUBLE"]->incrRef();
314     }
315   if(typeMap.count("CALCIUM_real")!=0)
316     {
317       typeMap["FLOAT"]=typeMap["CALCIUM_real"];
318       typeMap["FLOAT"]->incrRef();
319     }
320   if(typeMap.count("CALCIUM_integer")!=0)
321     {
322       typeMap["INTEGER"]=typeMap["CALCIUM_integer"];
323       typeMap["INTEGER"]->incrRef();
324     }
325   if(typeMap.count("CALCIUM_boolean")!=0)
326     {
327       typeMap["BOOLEAN"]=typeMap["CALCIUM_boolean"];
328       typeMap["BOOLEAN"]->incrRef();
329     }
330   if(typeMap.count("CALCIUM_string")!=0)
331     {
332       typeMap["STRING"]=typeMap["CALCIUM_string"];
333       typeMap["STRING"]->incrRef();
334     }
335 #endif
336
337   //GEOM
338   /*
339   typeMap["GEOM_Object"]=TypeCode::interfaceTc("IDL:GEOM/GEOM_Object:1.0","GEOM_Object");
340   typeMap["ListOfLong"]=TypeCode::sequenceTc("ListOfLong","ListOfLong",r->_tc_int);
341   typeMap["GEOM_List"]=TypeCode::interfaceTc("IDL:GEOM/GEOM_List:1.0","GEOM_List");
342   typeMap["GEOM_Superv"]=TypeCode::interfaceTc("IDL:GEOM/GEOM_Superv:1.0","GEOM_Superv");
343   typeMap["ListOfGO"]=TypeCode::sequenceTc("ListOfGO","ListOfGO",cata->_typeMap["GEOM_Object"]);
344   typeMap["string_array"]=TypeCode::sequenceTc("string_array","string_array",r->_tc_string);
345   r->_tc_int->incrRef();
346   typeMap["BCErrorType"]=r->_tc_int;
347   typeMap["BCError"]=TypeCode::structTc("","BCError");
348   ((TypeCodeStruct*)typeMap["BCError"])->addMember("incriminated",typeMap["ListOfLong"]);
349   typeMap["BCErrors"]=TypeCode::sequenceTc("BCErrors","BCErrors",typeMap["BCError"]);
350   typeMap["GEOM_Gen"]=TypeCode::interfaceTc("IDL:GEOM/GEOM_Gen:1.0","GEOM_Gen");
351   typeMap["GEOM_Object"]->incrRef();
352   typeMap["GEOM_Shape"]=typeMap["GEOM_Object"];
353   */
354
355   //SMESH
356   /*
357   typeMap["SMESH_Mesh"]=TypeCode::interfaceTc("IDL:Mesh/SMESH_Mesh:1.0","SMESH_Mesh");
358   typeMap["SMESH_Hypothesis"]=TypeCode::interfaceTc("IDL:SMESH/SMESH_Hypothesis:1.0","SMESH_Hypothesis");
359   */
360
361   //MED
362   /*
363   typeMap["MED"]=TypeCode::interfaceTc("IDL:SALOME_MED/MED:1.0","MED");
364   typeMap["FIELD"]=TypeCode::interfaceTc("IDL:SALOME_MED/FIELD:1.0","FIELD");
365   // TODO : FIELDDOUBLE inherits FIELD
366   typeMap["FIELDDOUBLE"]=TypeCode::interfaceTc("IDL:SALOME_MED/FIELDDOUBLE:1.0","FIELDDOUBLE");
367   */
368
369   //COMPONENT
370   /*
371   typeMap["AddComponent"]=TypeCode::interfaceTc("IDL:SuperVisionTest/AddComponent:1.0","AddComponent");
372   typeMap["AddComponent"]->incrRef();
373   typeMap["AddInterface"]=typeMap["AddComponent"];
374   typeMap["SuperVisionTest/Adder"]=TypeCode::interfaceTc("IDL:SuperVisionTest/Adder:1.0","SuperVisionTest/Adder");
375   typeMap["AdditionComponent/Adder"]=TypeCode::interfaceTc("IDL:AdditionComponent/Adder:1.0","AdditionComponent/Adder");
376   typeMap["Syr"]=TypeCode::interfaceTc("IDL:SuperVisionTest/Syr:1.0","Syr");
377   typeMap["ListOfSyr"]=TypeCode::interfaceTc("IDL:SuperVisionTest/ListOfSyr:1.0","ListOfSyr");
378   */
379 }
380
381 void SessionCataLoader::loadCata(Catalog* cata)
382 {
383   DEBTRACE("SessionCataLoader::load")
384 #ifndef NEW_KERNEL
385   datastreamMap[SALOME_ModuleCatalog::DATASTREAM_UNKNOWN]="CALCIUM_unknown";
386   datastreamMap[SALOME_ModuleCatalog::DATASTREAM_INTEGER]="CALCIUM_integer";
387   datastreamMap[SALOME_ModuleCatalog::DATASTREAM_FLOAT]="CALCIUM_real";
388   datastreamMap[SALOME_ModuleCatalog::DATASTREAM_DOUBLE]="CALCIUM_double";
389   datastreamMap[SALOME_ModuleCatalog::DATASTREAM_BOOLEAN]="CALCIUM_boolean";
390   datastreamMap[SALOME_ModuleCatalog::DATASTREAM_STRING]="CALCIUM_string";
391 #endif
392
393   CORBA::ORB_ptr orb = getSALOMERuntime()->getOrb();
394   CORBA::Object_var obj;
395   SALOME_ModuleCatalog::ModuleCatalog_var catalog;
396   obj=orb->string_to_object(_path.c_str());
397   catalog= SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
398   _errors="";
399
400   // Get types in catalog
401   loadTypes(cata,catalog);
402   // Get old types 
403   loadTypesOld(cata);
404
405   // Get component list
406   SALOME_ModuleCatalog::ListOfComponents_var componentname_list;
407   componentname_list  = catalog->GetComponentList();
408   for (int i=0; i<componentname_list->length(); i++)
409     {
410       DEBTRACE ("Component : " << componentname_list[i]);
411       SALOME_ModuleCatalog::Acomponent_var component = catalog->GetComponent(componentname_list[i]);
412       importComponent(cata,component);
413     }
414   cata->setErrors(_errors);
415 }