1 // Copyright (C) 2006-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "RuntimeSALOME.hxx"
20 #include "StudyNodes.hxx"
21 #include "StudyPorts.hxx"
22 #include "Visitor.hxx"
24 #include "SALOME_NamingService.hxx"
25 #include "SALOMEDS.hh"
26 #include "SALOMEDS_Attributes.hh"
34 #include "YacsTrace.hxx"
41 const char StudyInNode::IMPL_NAME[]="XML";
43 StudyInNode::StudyInNode(const std::string& name)
46 _implementation=IMPL_NAME;
49 StudyInNode::StudyInNode(const StudyInNode& other, ComposedNode *father)
50 : DataNode(other, father)
54 Node *StudyInNode::simpleClone(ComposedNode *father, bool editionOnly) const
56 return new StudyInNode(*this,father);
59 OutputPort* StudyInNode::createOutputPort(const std::string& outputPortName, TypeCode* type)
61 return new OutputStudyPort(outputPortName, this, type);
64 void StudyInNode::setData(OutputPort* port, const std::string& data)
66 OutputStudyPort *outp = dynamic_cast<OutputStudyPort *>(port);
70 void StudyInNode::execute()
72 DEBTRACE("+++++++ StudyInNode::execute +++++++++++");
73 SALOME_NamingService NS(getSALOMERuntime()->getOrb());
74 CORBA::Object_var obj=NS.Resolve("/myStudyManager");
75 if(CORBA::is_nil(obj))
77 _errorDetails="Execution problem: no naming service";
78 throw Exception(_errorDetails);
81 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(obj);
82 if(CORBA::is_nil(aStudyManager))
84 _errorDetails="Execution problem: no naming service";
85 throw Exception(_errorDetails);
89 if (_propertyMap.find("StudyID") != _propertyMap.end())
91 // StudyId is specified
92 studyid=atoi(_propertyMap["StudyID"].c_str());
95 SALOMEDS::Study_var myStudy =aStudyManager->GetStudyByID(studyid);
96 if(CORBA::is_nil(myStudy))
98 std::stringstream msg;
99 msg << "Execution problem: no study with id " << studyid;
100 _errorDetails=msg.str();
101 throw Exception(_errorDetails);
104 std::list<OutputPort *>::const_iterator iter;
105 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
107 OutputStudyPort *outp = dynamic_cast<OutputStudyPort *>(*iter);
108 std::string data = outp->getData();
109 DEBTRACE("data: " << data );
111 SALOMEDS::SObject_var aSO = myStudy->FindObjectID(data.c_str());
112 if(CORBA::is_nil(aSO))
115 aSO=myStudy->FindObjectByPath(data.c_str());
116 if(CORBA::is_nil(aSO))
118 _errorDetails="Execution problem: no id or path: ";
119 _errorDetails=_errorDetails+data+" in study";
120 throw Exception(_errorDetails);
124 CORBA::String_var path=myStudy->GetObjectPath(aSO);
126 CORBA::String_var id=aSO->GetID();
128 //CORBA::Object_var sobj=aSO->GetObject();
129 SALOMEDS::GenericAttribute_var aGAttr;
131 CORBA::String_var value;
132 if ( aSO->FindAttribute( aGAttr, "AttributeIOR" ) )
134 SALOMEDS::AttributeIOR_var anAttr = SALOMEDS::AttributeIOR::_narrow( aGAttr );
135 value=anAttr->Value();
140 _errorDetails="Execution problem: no AttributeIOR in study object: ";
141 _errorDetails=_errorDetails+data;
142 throw Exception(_errorDetails);
144 outp->putIOR((const char*)value);
146 DEBTRACE("+++++++ end StudyInNode::execute +++++++++++" );
149 void StudyInNode::checkBasicConsistency() const throw(Exception)
151 DEBTRACE("StudyInNode::checkBasicConsistency");
152 if (! _setOfInputPort.empty())
154 std::string what = "StudyNode ";
156 what += " only accepts OutputStudyPort, no InputPort";
157 throw Exception(what);
160 std::list<OutputPort *>::const_iterator iter;
161 for(iter=_setOfOutputPort.begin();iter!=_setOfOutputPort.end();iter++)
163 OutputStudyPort *inp = dynamic_cast<OutputStudyPort*>(*iter);
166 std::string what("Output port: ");
167 what += (*iter)->getName();
168 what += " is not an OutputStudyPort. StudyNode ";
170 what += " only accepts OutputStudyPorts";
171 throw Exception(what);
174 std::string data = inp->getData();
178 std::string what("OutputStudyPort: ");
179 what += (*iter)->getName();
180 what += " is not initialised";
181 throw Exception(what);
186 void StudyInNode::accept(Visitor *visitor)
188 visitor->visitStudyInNode(this);
191 const char StudyOutNode::IMPL_NAME[]="XML";
193 StudyOutNode::StudyOutNode(const std::string& name)
196 _implementation=IMPL_NAME;
199 StudyOutNode::StudyOutNode(const StudyOutNode& other, ComposedNode *father)
200 : DataNode(other, father)
204 Node *StudyOutNode::simpleClone(ComposedNode *father, bool editionOnly) const
206 return new StudyOutNode(*this,father);
209 InputPort* StudyOutNode::createInputPort(const std::string& inputPortName, TypeCode* type)
211 return new InputStudyPort(inputPortName, this, type);
214 void StudyOutNode::setData(InputPort* port, const std::string& data)
216 InputStudyPort *inp = dynamic_cast<InputStudyPort *>(port);
220 SALOMEDS::SObject_ptr findOrCreateSoWithName(SALOMEDS::Study_ptr study, SALOMEDS::StudyBuilder_ptr builder,
221 SALOMEDS::SObject_ptr sobj, const std::string& name)
223 SALOMEDS::ChildIterator_var anIterator= study->NewChildIterator(sobj);
224 SALOMEDS::GenericAttribute_var anAttr;
225 SALOMEDS::AttributeName_var namAttr ;
226 SALOMEDS::SObject_var result=SALOMEDS::SObject::_nil();
228 for (; anIterator->More(); anIterator->Next())
230 SALOMEDS::SObject_var anObj=anIterator->Value();
231 if(anObj->FindAttribute(anAttr, "AttributeName"))
233 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
234 CORBA::String_var value=namAttr->Value();
235 if(name == (const char*)value)
242 if(CORBA::is_nil(result))
245 result = builder->NewObject( sobj );
246 anAttr=builder->FindOrCreateAttribute(result,"AttributeName");
247 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
248 namAttr->SetValue(name.c_str());
250 return result._retn();
253 void StudyOutNode::execute()
255 DEBTRACE("+++++++ StudyOutNode::execute +++++++++++");
256 SALOME_NamingService NS(getSALOMERuntime()->getOrb());
257 CORBA::Object_var obj=NS.Resolve("/myStudyManager");
258 if(CORBA::is_nil(obj))
260 _errorDetails="Execution problem: no naming service";
261 throw Exception(_errorDetails);
264 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(obj);
265 if(CORBA::is_nil(aStudyManager))
267 _errorDetails="Execution problem: no naming service";
268 throw Exception(_errorDetails);
272 if (_propertyMap.find("StudyID") != _propertyMap.end())
274 // StudyId is specified
275 studyid=atoi(_propertyMap["StudyID"].c_str());
278 SALOMEDS::Study_var myStudy =aStudyManager->GetStudyByID(studyid);
279 if(CORBA::is_nil(myStudy))
282 std::stringstream msg;
283 msg << "Study" << studyid;
284 myStudy=aStudyManager->NewStudy(msg.str().c_str());
285 if(CORBA::is_nil(myStudy))
287 _errorDetails="Execution problem: can not create new study " + msg.str();
288 throw Exception(_errorDetails);
291 DEBTRACE(myStudy->StudyId());
293 SALOMEDS::StudyBuilder_var aBuilder =myStudy->NewBuilder() ;
294 if(CORBA::is_nil(aBuilder))
296 _errorDetails="Execution problem: can not create StudyBuilder";
297 throw Exception(_errorDetails);
300 SALOMEDS::GenericAttribute_var aGAttr;
301 SALOMEDS::SObject_var aSO ;
302 SALOMEDS::AttributeName_var anAttr ;
303 SALOMEDS::AttributeIOR_var iorAttr ;
305 std::list<InputPort *>::const_iterator iter;
306 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
308 InputStudyPort *inp = dynamic_cast<InputStudyPort *>(*iter);
309 std::string data = inp->getData();
310 DEBTRACE("data: " << data );
312 aSO = myStudy->FindObjectID(data.c_str());
313 if(CORBA::is_nil(aSO))
315 // the id does not exist. Try to create it by id
316 aSO=myStudy->CreateObjectID(data.c_str());
317 if(!CORBA::is_nil(aSO))
319 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeName");
320 anAttr = SALOMEDS::AttributeName::_narrow( aGAttr );
321 anAttr->SetValue(inp->getName().c_str());
324 if(CORBA::is_nil(aSO))
327 aSO=myStudy->FindObjectByPath(data.c_str());
329 if(CORBA::is_nil(aSO))
331 //try to create it by path
333 std::string::size_type begin = data.find_first_not_of("/");
334 std::string::size_type pos=data.find_first_of("/", begin);
335 if (pos != std::string::npos)
336 name=data.substr(begin,pos-begin);
338 name=data.substr(begin);
341 aSO=myStudy->FindObjectByPath(name.c_str());
342 if(CORBA::is_nil(aSO))
344 DEBTRACE("Create an entry " << name);
345 //create a container component
346 aSO=aBuilder->NewComponent(name.c_str());
347 if(CORBA::is_nil(aSO))
349 std::cerr << "Execution problem: can not create component: " + data << std::endl;
352 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeIOR");
353 iorAttr = SALOMEDS::AttributeIOR::_narrow( aGAttr );
354 iorAttr->SetValue(name.c_str());
356 begin=data.find_first_not_of("/",pos);
357 while (begin != std::string::npos)
359 pos = data.find_first_of("/", begin);
360 if (pos != std::string::npos)
361 name=data.substr(begin,pos-begin);
363 name=data.substr(begin);
364 aSO=findOrCreateSoWithName(myStudy,aBuilder,aSO,name);
365 begin=data.find_first_not_of("/",pos);
368 if(CORBA::is_nil(aSO))
370 std::cerr << "Execution problem: can not create id or path: " + data + " in study" << std::endl;
373 std::string value=inp->getIOR();
375 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeIOR");
376 iorAttr = SALOMEDS::AttributeIOR::_narrow( aGAttr );
377 iorAttr->SetValue(value.c_str());
380 // save in file if ref is given
383 aStudyManager->SaveAs(_ref.c_str(),myStudy, false);
385 DEBTRACE("+++++++ end StudyOutNode::execute +++++++++++" );
388 void StudyOutNode::checkBasicConsistency() const throw(Exception)
390 DEBTRACE("StudyOutNode::checkBasicConsistency");
391 if (! _setOfOutputPort.empty())
393 std::string what = "StudyNode ";
395 what += " only accepts InputStudyPort, no OutputPort";
396 throw Exception(what);
399 std::list<InputPort *>::const_iterator iter;
400 for(iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
402 InputStudyPort *inp = dynamic_cast<InputStudyPort*>(*iter);
405 std::string what("Input port: ");
406 what += (*iter)->getName();
407 what += " is not an InputStudyPort. StudyNode ";
409 what += " only accepts InputStudyPorts";
410 throw Exception(what);
412 inp->checkBasicConsistency();
414 std::string data = inp->getData();
418 std::string what("InputStudyPort: ");
419 what += (*iter)->getName();
420 what += " is not initialised";
421 throw Exception(what);
427 void StudyOutNode::accept(Visitor *visitor)
429 visitor->visitStudyOutNode(this);
432 } //end namespace ENGINE
433 } //end namespace YACS