1 // Copyright (C) 2006-2016 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, or (at your option) any later version.
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
20 #include "RuntimeSALOME.hxx"
21 #include "StudyNodes.hxx"
22 #include "StudyPorts.hxx"
23 #include "Visitor.hxx"
24 #include "TypeCode.hxx"
25 #include "SalomeProc.hxx"
27 #include "Basics_Utils.hxx"
28 #include "SALOME_NamingService.hxx"
29 #include "SALOMEDS.hh"
30 #include "SALOMEDS_Attributes.hh"
39 #include "YacsTrace.hxx"
46 const char StudyInNode::IMPL_NAME[]="XML";
48 StudyInNode::StudyInNode(const std::string& name)
51 _implementation=IMPL_NAME;
54 StudyInNode::StudyInNode(const StudyInNode& other, ComposedNode *father)
55 : DataNode(other, father)
59 Node *StudyInNode::simpleClone(ComposedNode *father, bool editionOnly) const
61 return new StudyInNode(*this,father);
64 OutputPort* StudyInNode::createOutputPort(const std::string& outputPortName, TypeCode* type)
66 return new OutputStudyPort(outputPortName, this, type);
69 void StudyInNode::setData(OutputPort* port, const std::string& data)
71 OutputStudyPort *outp = dynamic_cast<OutputStudyPort *>(port);
75 void StudyInNode::execute()
77 DEBTRACE("+++++++ StudyInNode::execute +++++++++++");
78 SALOME_NamingService NS(getSALOMERuntime()->getOrb());
79 CORBA::Object_var obj=NS.Resolve("/myStudyManager");
80 if(CORBA::is_nil(obj))
82 _errorDetails="Execution problem: no naming service";
83 throw Exception(_errorDetails);
86 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(obj);
87 if(CORBA::is_nil(aStudyManager))
89 _errorDetails="Execution problem: no naming service";
90 throw Exception(_errorDetails);
94 if (getProperty("StudyID") != "")
96 // StudyId is specified
97 studyid=atoi(getProperty("StudyID").c_str());
104 std::string value=p->getProperty("DefaultStudyID");
106 studyid= atoi(value.c_str());
111 SALOMEDS::Study_var myStudy =aStudyManager->GetStudyByID(studyid);
112 if(CORBA::is_nil(myStudy))
114 std::stringstream msg;
115 msg << "Execution problem: no study with id " << studyid;
116 _errorDetails=msg.str();
117 throw Exception(_errorDetails);
120 std::list<OutputPort *>::const_iterator iter;
121 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
123 OutputStudyPort *outp = dynamic_cast<OutputStudyPort *>(*iter);
126 outp->getDataFromStudy(myStudy);
130 _errorDetails=e.what();
134 DEBTRACE("+++++++ end StudyInNode::execute +++++++++++" );
137 void StudyInNode::checkBasicConsistency() const throw(YACS::Exception)
139 DEBTRACE("StudyInNode::checkBasicConsistency");
140 if (! _setOfInputPort.empty())
142 std::string what = "StudyNode ";
144 what += " only accepts OutputStudyPort, no InputPort";
145 throw Exception(what);
148 std::list<OutputPort *>::const_iterator iter;
149 for(iter=_setOfOutputPort.begin();iter!=_setOfOutputPort.end();iter++)
151 OutputStudyPort *inp = dynamic_cast<OutputStudyPort*>(*iter);
154 std::string what("Output port: ");
155 what += (*iter)->getName();
156 what += " is not an OutputStudyPort. StudyNode ";
158 what += " only accepts OutputStudyPorts";
159 throw Exception(what);
162 std::string data = inp->getData();
166 std::string what("OutputStudyPort: ");
167 what += (*iter)->getName();
168 what += " is not initialised";
169 throw Exception(what);
174 void StudyInNode::accept(Visitor *visitor)
176 visitor->visitStudyInNode(this);
179 const char StudyOutNode::IMPL_NAME[]="XML";
181 StudyOutNode::StudyOutNode(const std::string& name)
184 _implementation=IMPL_NAME;
187 StudyOutNode::StudyOutNode(const StudyOutNode& other, ComposedNode *father)
188 : DataNode(other, father)
192 Node *StudyOutNode::simpleClone(ComposedNode *father, bool editionOnly) const
194 return new StudyOutNode(*this,father);
197 InputPort* StudyOutNode::createInputPort(const std::string& inputPortName, TypeCode* type)
199 return new InputStudyPort(inputPortName, this, type);
202 void StudyOutNode::setData(InputPort* port, const std::string& data)
204 InputStudyPort *inp = dynamic_cast<InputStudyPort *>(port);
209 SALOMEDS::SObject_ptr findOrCreateSoWithName(SALOMEDS::Study_ptr study, SALOMEDS::StudyBuilder_ptr builder,
210 SALOMEDS::SObject_ptr sobj, const std::string& name)
212 SALOMEDS::ChildIterator_var anIterator= study->NewChildIterator(sobj);
213 SALOMEDS::GenericAttribute_var anAttr;
214 SALOMEDS::AttributeName_var namAttr ;
215 SALOMEDS::SObject_var result=SALOMEDS::SObject::_nil();
217 for (; anIterator->More(); anIterator->Next())
219 SALOMEDS::SObject_var anObj=anIterator->Value();
220 if(anObj->FindAttribute(anAttr, "AttributeName"))
222 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
223 CORBA::String_var value=namAttr->Value();
224 if(name == (const char*)value)
231 if(CORBA::is_nil(result))
234 result = builder->NewObject( sobj );
235 anAttr=builder->FindOrCreateAttribute(result,"AttributeName");
236 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
237 namAttr->SetValue(name.c_str());
239 return result._retn();
243 void StudyOutNode::execute()
245 DEBTRACE("+++++++ StudyOutNode::execute +++++++++++");
246 SALOME_NamingService NS(getSALOMERuntime()->getOrb());
247 CORBA::Object_var obj=NS.Resolve("/myStudyManager");
248 if(CORBA::is_nil(obj))
250 _errorDetails="Execution problem: no naming service";
251 throw Exception(_errorDetails);
254 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(obj);
255 if(CORBA::is_nil(aStudyManager))
257 _errorDetails="Execution problem: no naming service";
258 throw Exception(_errorDetails);
262 if (getProperty("StudyID") != "")
264 // StudyId is specified
265 studyid=atoi(getProperty("StudyID").c_str());
272 std::string value=p->getProperty("DefaultStudyID");
274 studyid= atoi(value.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(Kernel_Utils::decode_s(msg.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 inp->putDataInStudy(myStudy,aBuilder);
312 // save in file if ref is given
315 aStudyManager->SaveAs(Kernel_Utils::decode_s(_ref),myStudy, false);
317 DEBTRACE("+++++++ end StudyOutNode::execute +++++++++++" );
320 void StudyOutNode::checkBasicConsistency() const throw(YACS::Exception)
322 DEBTRACE("StudyOutNode::checkBasicConsistency");
323 if (! _setOfOutputPort.empty())
325 std::string what = "StudyNode ";
327 what += " only accepts InputStudyPort, no OutputPort";
328 throw Exception(what);
331 std::list<InputPort *>::const_iterator iter;
332 for(iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
334 InputStudyPort *inp = dynamic_cast<InputStudyPort*>(*iter);
337 std::string what("Input port: ");
338 what += (*iter)->getName();
339 what += " is not an InputStudyPort. StudyNode ";
341 what += " only accepts InputStudyPorts";
342 throw Exception(what);
344 inp->checkBasicConsistency();
346 std::string data = inp->getData();
350 std::string what("InputStudyPort: ");
351 what += (*iter)->getName();
352 what += " is not initialised";
353 throw Exception(what);
359 void StudyOutNode::accept(Visitor *visitor)
361 visitor->visitStudyOutNode(this);
364 } //end namespace ENGINE
365 } //end namespace YACS