1 #include "RuntimeSALOME.hxx"
2 #include "StudyNodes.hxx"
3 #include "StudyPorts.hxx"
6 #include "SALOME_NamingService.hxx"
8 #include "SALOMEDS_Attributes.hh"
16 #include "YacsTrace.hxx"
23 const char StudyInNode::IMPL_NAME[]="XML";
25 StudyInNode::StudyInNode(const std::string& name)
28 _implementation=IMPL_NAME;
31 StudyInNode::StudyInNode(const StudyInNode& other, ComposedNode *father)
32 : DataNode(other, father)
36 Node *StudyInNode::simpleClone(ComposedNode *father, bool editionOnly) const
38 return new StudyInNode(*this,father);
41 OutputPort* StudyInNode::createOutputPort(const std::string& outputPortName, TypeCode* type)
43 return new OutputStudyPort(outputPortName, this, type);
46 void StudyInNode::setData(OutputPort* port, std::string& data)
48 OutputStudyPort *outp = dynamic_cast<OutputStudyPort *>(port);
52 void StudyInNode::execute()
54 DEBTRACE("+++++++ StudyInNode::execute +++++++++++");
55 SALOME_NamingService NS(getSALOMERuntime()->getOrb());
56 CORBA::Object_var obj=NS.Resolve("/myStudyManager");
57 if(CORBA::is_nil(obj))
59 _errorDetails="Execution problem: no naming service";
60 throw Exception(_errorDetails);
63 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(obj);
64 if(CORBA::is_nil(aStudyManager))
66 _errorDetails="Execution problem: no naming service";
67 throw Exception(_errorDetails);
71 if (_propertyMap.find("StudyID") != _propertyMap.end())
73 // StudyId is specified
74 studyid=atoi(_propertyMap["StudyID"].c_str());
77 SALOMEDS::Study_var myStudy =aStudyManager->GetStudyByID(studyid);
78 if(CORBA::is_nil(myStudy))
80 std::stringstream msg;
81 msg << "Execution problem: no study with id " << studyid;
82 _errorDetails=msg.str();
83 throw Exception(_errorDetails);
86 std::list<OutputPort *>::const_iterator iter;
87 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
89 OutputStudyPort *outp = dynamic_cast<OutputStudyPort *>(*iter);
90 std::string data = outp->getData();
91 DEBTRACE("data: " << data );
93 SALOMEDS::SObject_var aSO = myStudy->FindObjectID(data.c_str());
94 if(CORBA::is_nil(aSO))
97 aSO=myStudy->FindObjectByPath(data.c_str());
98 if(CORBA::is_nil(aSO))
100 _errorDetails="Execution problem: no id or path: ";
101 _errorDetails=_errorDetails+data+" in study";
102 throw Exception(_errorDetails);
106 CORBA::String_var path=myStudy->GetObjectPath(aSO);
108 CORBA::String_var id=aSO->GetID();
110 //CORBA::Object_var sobj=aSO->GetObject();
111 SALOMEDS::GenericAttribute_var aGAttr;
113 CORBA::String_var value;
114 if ( aSO->FindAttribute( aGAttr, "AttributeIOR" ) )
116 SALOMEDS::AttributeIOR_var anAttr = SALOMEDS::AttributeIOR::_narrow( aGAttr );
117 value=anAttr->Value();
122 _errorDetails="Execution problem: no AttributeIOR in study object: ";
123 _errorDetails=_errorDetails+data;
124 throw Exception(_errorDetails);
126 outp->putIOR((const char*)value);
128 DEBTRACE("+++++++ end StudyInNode::execute +++++++++++" );
131 void StudyInNode::checkBasicConsistency() const throw(Exception)
133 DEBTRACE("StudyInNode::checkBasicConsistency");
134 if (! _setOfInputPort.empty())
136 std::string what = "StudyNode ";
138 what += " only accepts OutputStudyPort, no InputPort";
139 throw Exception(what);
142 std::list<OutputPort *>::const_iterator iter;
143 for(iter=_setOfOutputPort.begin();iter!=_setOfOutputPort.end();iter++)
145 OutputStudyPort *inp = dynamic_cast<OutputStudyPort*>(*iter);
148 std::string what("Output port: ");
149 what += (*iter)->getName();
150 what += " is not an OutputStudyPort. StudyNode ";
152 what += " only accepts OutputStudyPorts";
153 throw Exception(what);
156 std::string data = inp->getData();
160 std::string what("OutputStudyPort: ");
161 what += (*iter)->getName();
162 what += " is not initialised";
163 throw Exception(what);
168 void StudyInNode::accept(Visitor *visitor)
170 visitor->visitStudyInNode(this);
173 const char StudyOutNode::IMPL_NAME[]="XML";
175 StudyOutNode::StudyOutNode(const std::string& name)
178 _implementation=IMPL_NAME;
181 StudyOutNode::StudyOutNode(const StudyOutNode& other, ComposedNode *father)
182 : DataNode(other, father)
186 Node *StudyOutNode::simpleClone(ComposedNode *father, bool editionOnly) const
188 return new StudyOutNode(*this,father);
191 InputPort* StudyOutNode::createInputPort(const std::string& inputPortName, TypeCode* type)
193 return new InputStudyPort(inputPortName, this, type);
196 void StudyOutNode::setData(InputPort* port, std::string& data)
198 InputStudyPort *inp = dynamic_cast<InputStudyPort *>(port);
202 SALOMEDS::SObject_ptr findOrCreateSoWithName(SALOMEDS::Study_ptr study, SALOMEDS::StudyBuilder_ptr builder,
203 SALOMEDS::SObject_ptr sobj, const std::string& name)
205 SALOMEDS::ChildIterator_var anIterator= study->NewChildIterator(sobj);
206 SALOMEDS::GenericAttribute_var anAttr;
207 SALOMEDS::AttributeName_var namAttr ;
208 SALOMEDS::SObject_var result=SALOMEDS::SObject::_nil();
210 for (; anIterator->More(); anIterator->Next())
212 SALOMEDS::SObject_var anObj=anIterator->Value();
213 if(anObj->FindAttribute(anAttr, "AttributeName"))
215 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
216 CORBA::String_var value=namAttr->Value();
217 if(name == (const char*)value)
224 if(CORBA::is_nil(result))
227 result = builder->NewObject( sobj );
228 anAttr=builder->FindOrCreateAttribute(result,"AttributeName");
229 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
230 namAttr->SetValue(name.c_str());
232 return result._retn();
235 void StudyOutNode::execute()
237 DEBTRACE("+++++++ StudyOutNode::execute +++++++++++");
238 SALOME_NamingService NS(getSALOMERuntime()->getOrb());
239 CORBA::Object_var obj=NS.Resolve("/myStudyManager");
240 if(CORBA::is_nil(obj))
242 _errorDetails="Execution problem: no naming service";
243 throw Exception(_errorDetails);
246 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(obj);
247 if(CORBA::is_nil(aStudyManager))
249 _errorDetails="Execution problem: no naming service";
250 throw Exception(_errorDetails);
254 if (_propertyMap.find("StudyID") != _propertyMap.end())
256 // StudyId is specified
257 studyid=atoi(_propertyMap["StudyID"].c_str());
260 SALOMEDS::Study_var myStudy =aStudyManager->GetStudyByID(studyid);
261 if(CORBA::is_nil(myStudy))
264 std::stringstream msg;
265 msg << "Study" << studyid;
266 myStudy=aStudyManager->NewStudy(msg.str().c_str());
267 if(CORBA::is_nil(myStudy))
269 _errorDetails="Execution problem: can not create new study " + msg.str();
270 throw Exception(_errorDetails);
273 DEBTRACE(myStudy->StudyId());
275 SALOMEDS::StudyBuilder_var aBuilder =myStudy->NewBuilder() ;
276 if(CORBA::is_nil(aBuilder))
278 _errorDetails="Execution problem: can not create StudyBuilder";
279 throw Exception(_errorDetails);
282 SALOMEDS::GenericAttribute_var aGAttr;
283 SALOMEDS::SObject_var aSO ;
284 SALOMEDS::AttributeName_var anAttr ;
285 SALOMEDS::AttributeIOR_var iorAttr ;
287 std::list<InputPort *>::const_iterator iter;
288 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
290 InputStudyPort *inp = dynamic_cast<InputStudyPort *>(*iter);
291 std::string data = inp->getData();
292 DEBTRACE("data: " << data );
294 aSO = myStudy->FindObjectID(data.c_str());
295 if(CORBA::is_nil(aSO))
297 // the id does not exist. Try to create it by id
298 aSO=myStudy->CreateObjectID(data.c_str());
299 if(!CORBA::is_nil(aSO))
301 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeName");
302 anAttr = SALOMEDS::AttributeName::_narrow( aGAttr );
303 anAttr->SetValue(inp->getName().c_str());
306 if(CORBA::is_nil(aSO))
309 aSO=myStudy->FindObjectByPath(data.c_str());
311 if(CORBA::is_nil(aSO))
313 //try to create it by path
315 std::string::size_type begin = data.find_first_not_of("/");
316 std::string::size_type pos=data.find_first_of("/", begin);
317 if (pos != std::string::npos)
318 name=data.substr(begin,pos-begin);
320 name=data.substr(begin);
323 aSO=myStudy->FindObjectByPath(name.c_str());
324 if(CORBA::is_nil(aSO))
326 DEBTRACE("Create an entry " << name);
327 //create a container component
328 aSO=aBuilder->NewComponent(name.c_str());
329 if(CORBA::is_nil(aSO))
331 std::cerr << "Execution problem: can not create component: " + data << std::endl;
334 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeIOR");
335 iorAttr = SALOMEDS::AttributeIOR::_narrow( aGAttr );
336 iorAttr->SetValue(name.c_str());
338 begin=data.find_first_not_of("/",pos);
339 while (begin != std::string::npos)
341 pos = data.find_first_of("/", begin);
342 if (pos != std::string::npos)
343 name=data.substr(begin,pos-begin);
345 name=data.substr(begin);
346 aSO=findOrCreateSoWithName(myStudy,aBuilder,aSO,name);
347 begin=data.find_first_not_of("/",pos);
350 if(CORBA::is_nil(aSO))
352 std::cerr << "Execution problem: can not create id or path: " + data + " in study" << std::endl;
355 std::string value=inp->getIOR();
357 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeIOR");
358 iorAttr = SALOMEDS::AttributeIOR::_narrow( aGAttr );
359 iorAttr->SetValue(value.c_str());
362 // save in file if ref is given
365 aStudyManager->SaveAs(_ref.c_str(),myStudy, false);
367 DEBTRACE("+++++++ end StudyOutNode::execute +++++++++++" );
370 void StudyOutNode::checkBasicConsistency() const throw(Exception)
372 DEBTRACE("StudyOutNode::checkBasicConsistency");
373 if (! _setOfOutputPort.empty())
375 std::string what = "StudyNode ";
377 what += " only accepts InputStudyPort, no OutputPort";
378 throw Exception(what);
381 std::list<InputPort *>::const_iterator iter;
382 for(iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
384 InputStudyPort *inp = dynamic_cast<InputStudyPort*>(*iter);
387 std::string what("Input port: ");
388 what += (*iter)->getName();
389 what += " is not an InputStudyPort. StudyNode ";
391 what += " only accepts InputStudyPorts";
392 throw Exception(what);
394 inp->checkBasicConsistency();
396 std::string data = inp->getData();
400 std::string what("InputStudyPort: ");
401 what += (*iter)->getName();
402 what += " is not initialised";
403 throw Exception(what);
409 void StudyOutNode::accept(Visitor *visitor)
411 visitor->visitStudyOutNode(this);
414 } //end namespace ENGINE
415 } //end namespace YACS