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 "SALOME_NamingService.hxx"
28 #include "SALOMEDS.hh"
29 #include "SALOMEDS_Attributes.hh"
38 #include "YacsTrace.hxx"
45 const char StudyInNode::IMPL_NAME[]="XML";
47 StudyInNode::StudyInNode(const std::string& name)
50 _implementation=IMPL_NAME;
53 StudyInNode::StudyInNode(const StudyInNode& other, ComposedNode *father)
54 : DataNode(other, father)
58 Node *StudyInNode::simpleClone(ComposedNode *father, bool editionOnly) const
60 return new StudyInNode(*this,father);
63 OutputPort* StudyInNode::createOutputPort(const std::string& outputPortName, TypeCode* type)
65 return new OutputStudyPort(outputPortName, this, type);
68 void StudyInNode::setData(OutputPort* port, const std::string& data)
70 OutputStudyPort *outp = dynamic_cast<OutputStudyPort *>(port);
74 void StudyInNode::execute()
76 DEBTRACE("+++++++ StudyInNode::execute +++++++++++");
77 SALOME_NamingService NS(getSALOMERuntime()->getOrb());
78 CORBA::Object_var obj=NS.Resolve("/myStudyManager");
79 if(CORBA::is_nil(obj))
81 _errorDetails="Execution problem: no naming service";
82 throw Exception(_errorDetails);
85 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(obj);
86 if(CORBA::is_nil(aStudyManager))
88 _errorDetails="Execution problem: no naming service";
89 throw Exception(_errorDetails);
93 if (getProperty("StudyID") != "")
95 // StudyId is specified
96 studyid=atoi(getProperty("StudyID").c_str());
103 std::string value=p->getProperty("DefaultStudyID");
105 studyid= atoi(value.c_str());
110 SALOMEDS::Study_var myStudy =aStudyManager->GetStudyByID(studyid);
111 if(CORBA::is_nil(myStudy))
113 std::stringstream msg;
114 msg << "Execution problem: no study with id " << studyid;
115 _errorDetails=msg.str();
116 throw Exception(_errorDetails);
119 std::list<OutputPort *>::const_iterator iter;
120 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
122 OutputStudyPort *outp = dynamic_cast<OutputStudyPort *>(*iter);
125 outp->getDataFromStudy(myStudy);
129 _errorDetails=e.what();
133 DEBTRACE("+++++++ end StudyInNode::execute +++++++++++" );
136 void StudyInNode::checkBasicConsistency() const throw(YACS::Exception)
138 DEBTRACE("StudyInNode::checkBasicConsistency");
139 if (! _setOfInputPort.empty())
141 std::string what = "StudyNode ";
143 what += " only accepts OutputStudyPort, no InputPort";
144 throw Exception(what);
147 std::list<OutputPort *>::const_iterator iter;
148 for(iter=_setOfOutputPort.begin();iter!=_setOfOutputPort.end();iter++)
150 OutputStudyPort *inp = dynamic_cast<OutputStudyPort*>(*iter);
153 std::string what("Output port: ");
154 what += (*iter)->getName();
155 what += " is not an OutputStudyPort. StudyNode ";
157 what += " only accepts OutputStudyPorts";
158 throw Exception(what);
161 std::string data = inp->getData();
165 std::string what("OutputStudyPort: ");
166 what += (*iter)->getName();
167 what += " is not initialised";
168 throw Exception(what);
173 void StudyInNode::accept(Visitor *visitor)
175 visitor->visitStudyInNode(this);
178 const char StudyOutNode::IMPL_NAME[]="XML";
180 StudyOutNode::StudyOutNode(const std::string& name)
183 _implementation=IMPL_NAME;
186 StudyOutNode::StudyOutNode(const StudyOutNode& other, ComposedNode *father)
187 : DataNode(other, father)
191 Node *StudyOutNode::simpleClone(ComposedNode *father, bool editionOnly) const
193 return new StudyOutNode(*this,father);
196 InputPort* StudyOutNode::createInputPort(const std::string& inputPortName, TypeCode* type)
198 return new InputStudyPort(inputPortName, this, type);
201 void StudyOutNode::setData(InputPort* port, const std::string& data)
203 InputStudyPort *inp = dynamic_cast<InputStudyPort *>(port);
208 SALOMEDS::SObject_ptr findOrCreateSoWithName(SALOMEDS::Study_ptr study, SALOMEDS::StudyBuilder_ptr builder,
209 SALOMEDS::SObject_ptr sobj, const std::string& name)
211 SALOMEDS::ChildIterator_var anIterator= study->NewChildIterator(sobj);
212 SALOMEDS::GenericAttribute_var anAttr;
213 SALOMEDS::AttributeName_var namAttr ;
214 SALOMEDS::SObject_var result=SALOMEDS::SObject::_nil();
216 for (; anIterator->More(); anIterator->Next())
218 SALOMEDS::SObject_var anObj=anIterator->Value();
219 if(anObj->FindAttribute(anAttr, "AttributeName"))
221 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
222 CORBA::String_var value=namAttr->Value();
223 if(name == (const char*)value)
230 if(CORBA::is_nil(result))
233 result = builder->NewObject( sobj );
234 anAttr=builder->FindOrCreateAttribute(result,"AttributeName");
235 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
236 namAttr->SetValue(name.c_str());
238 return result._retn();
242 void StudyOutNode::execute()
244 DEBTRACE("+++++++ StudyOutNode::execute +++++++++++");
245 SALOME_NamingService NS(getSALOMERuntime()->getOrb());
246 CORBA::Object_var obj=NS.Resolve("/myStudyManager");
247 if(CORBA::is_nil(obj))
249 _errorDetails="Execution problem: no naming service";
250 throw Exception(_errorDetails);
253 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(obj);
254 if(CORBA::is_nil(aStudyManager))
256 _errorDetails="Execution problem: no naming service";
257 throw Exception(_errorDetails);
261 if (getProperty("StudyID") != "")
263 // StudyId is specified
264 studyid=atoi(getProperty("StudyID").c_str());
271 std::string value=p->getProperty("DefaultStudyID");
273 studyid= atoi(value.c_str());
277 SALOMEDS::Study_var myStudy =aStudyManager->GetStudyByID(studyid);
278 if(CORBA::is_nil(myStudy))
281 std::stringstream msg;
282 msg << "Study" << studyid;
283 myStudy=aStudyManager->NewStudy(msg.str().c_str());
284 if(CORBA::is_nil(myStudy))
286 _errorDetails="Execution problem: can not create new study " + msg.str();
287 throw Exception(_errorDetails);
290 DEBTRACE(myStudy->StudyId());
292 SALOMEDS::StudyBuilder_var aBuilder =myStudy->NewBuilder() ;
293 if(CORBA::is_nil(aBuilder))
295 _errorDetails="Execution problem: can not create StudyBuilder";
296 throw Exception(_errorDetails);
299 SALOMEDS::GenericAttribute_var aGAttr;
300 SALOMEDS::SObject_var aSO ;
301 SALOMEDS::AttributeName_var anAttr ;
302 SALOMEDS::AttributeIOR_var iorAttr ;
304 std::list<InputPort *>::const_iterator iter;
305 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
307 InputStudyPort *inp = dynamic_cast<InputStudyPort *>(*iter);
308 inp->putDataInStudy(myStudy,aBuilder);
311 // save in file if ref is given
314 aStudyManager->SaveAs(_ref.c_str(),myStudy, false);
316 DEBTRACE("+++++++ end StudyOutNode::execute +++++++++++" );
319 void StudyOutNode::checkBasicConsistency() const throw(YACS::Exception)
321 DEBTRACE("StudyOutNode::checkBasicConsistency");
322 if (! _setOfOutputPort.empty())
324 std::string what = "StudyNode ";
326 what += " only accepts InputStudyPort, no OutputPort";
327 throw Exception(what);
330 std::list<InputPort *>::const_iterator iter;
331 for(iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
333 InputStudyPort *inp = dynamic_cast<InputStudyPort*>(*iter);
336 std::string what("Input port: ");
337 what += (*iter)->getName();
338 what += " is not an InputStudyPort. StudyNode ";
340 what += " only accepts InputStudyPorts";
341 throw Exception(what);
343 inp->checkBasicConsistency();
345 std::string data = inp->getData();
349 std::string what("InputStudyPort: ");
350 what += (*iter)->getName();
351 what += " is not initialised";
352 throw Exception(what);
358 void StudyOutNode::accept(Visitor *visitor)
360 visitor->visitStudyOutNode(this);
363 } //end namespace ENGINE
364 } //end namespace YACS