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 "StudyPorts.hxx"
21 #include "TypeCode.hxx"
24 #include "ComponentInstance.hxx"
25 #include "SalomeComponent.hxx"
26 #include "RuntimeSALOME.hxx"
28 #include "SALOMEDS_Attributes.hh"
35 #include "YacsTrace.hxx"
43 /*! \class YACS::ENGINE::OutputStudyPort
44 * \brief Class for Study output Ports
51 OutputStudyPort::OutputStudyPort(const std::string& name, Node* node, TypeCode* type)
52 : OutputXmlPort(name, node, type),
53 DataPort(name, node, type),
58 OutputStudyPort::OutputStudyPort(const OutputStudyPort& other, Node *newHelder)
59 : OutputXmlPort(other,newHelder),
60 DataPort(other,newHelder),
61 Port(other,newHelder),_storeData(other._storeData)
65 OutputPort* OutputStudyPort::clone(Node *newHelder) const
67 return new OutputStudyPort(*this,newHelder);
70 void OutputStudyPort::setData(const std::string& data)
73 DEBTRACE( "OutputStudyPort::setData " << _storeData );
77 std::string OutputStudyPort::getData()
79 DEBTRACE("OutputStudyPort::getData " << _storeData);
83 void OutputStudyPort::putIOR(const std::string& ior)
85 DEBTRACE("OutputStudyPort::putIOR " << ior);
86 if(ior.substr(0,7) == "<value>")
92 int tk=edGetType()->kind();
97 value="<value><double>"+ior+"</double></value>";
100 value="<value><int>"+ior+"</int></value>";
103 value="<value><string>"+ior+"</string></value>";
106 value="<value><boolean>"+ior+"</boolean></value>";
109 value="<value><objref>"+ior+"</objref></value>";
121 std::string OutputStudyPort::dump()
123 DEBTRACE( "OutputStudyPort::dump " << _storeData );
124 DEBTRACE( "OutputStudyPort::dump " << _data );
125 //return "<value><string>"+_storeData+"</string></value>";
129 std::string OutputStudyPort::getPyObj()
133 std::string OutputStudyPort::getAsString()
138 void OutputStudyPort::getDataFromStudy(SALOMEDS::Study_var myStudy)
140 std::string data = getData();
141 DEBTRACE("data: " << data );
143 SALOMEDS::SObject_var aSO = myStudy->FindObjectID(data.c_str());
144 if(CORBA::is_nil(aSO))
147 aSO=myStudy->FindObjectByPath(data.c_str());
148 if(CORBA::is_nil(aSO))
150 std::stringstream msg;
151 msg << "Execution problem: no id or path: " << data << " in study " << myStudy->StudyId();
152 throw Exception(msg.str());
156 CORBA::String_var path=myStudy->GetObjectPath(aSO);
158 CORBA::String_var id=aSO->GetID();
160 //CORBA::Object_var sobj=aSO->GetObject();
162 SALOMEDS::GenericAttribute_var aGAttr;
163 CORBA::String_var value;
165 if(edGetType()->kind()==Objref)
167 if ( aSO->FindAttribute( aGAttr, "AttributeIOR" ) )
169 SALOMEDS::AttributeIOR_var anAttr = SALOMEDS::AttributeIOR::_narrow( aGAttr );
170 value=anAttr->Value();
171 putIOR((const char*)value);
176 std::string error="Execution problem: no AttributeIOR in study object: ";
178 throw Exception(error);
181 else if(edGetType()->kind()==Double )
183 if ( aSO->FindAttribute( aGAttr, "AttributeReal" ) )
185 SALOMEDS::AttributeReal_var anAttr = SALOMEDS::AttributeReal::_narrow( aGAttr );
186 CORBA::Double d=anAttr->Value();
187 std::stringstream msg;
188 msg << "<value><double>" << std::setprecision(16) << d << "</double></value>";
189 put(msg.str().c_str());
193 std::string error="Execution problem: no AttributeReal in study object: ";
194 throw Exception(error+data);
197 else if(edGetType()->kind()== Int)
199 if ( aSO->FindAttribute( aGAttr, "AttributeInteger" ) )
201 SALOMEDS::AttributeInteger_var anAttr = SALOMEDS::AttributeInteger::_narrow( aGAttr );
202 CORBA::Long l=anAttr->Value();
203 std::stringstream msg;
204 msg << "<value><int>" << l << "</int></value>";
205 put(msg.str().c_str());
209 std::string error="Execution problem: no AttributeInteger in study object: ";
210 throw Exception(error+data);
215 if ( aSO->FindAttribute( aGAttr, "AttributeComment" ) )
217 SALOMEDS::AttributeComment_var anAttr = SALOMEDS::AttributeComment::_narrow( aGAttr );
218 value=anAttr->Value();
220 putIOR((const char*)value);
224 std::string error="Execution problem: no AttributeComment in study object: ";
225 throw Exception(error+data);
233 SALOMEDS::SObject_ptr findOrCreateSoWithName(SALOMEDS::Study_ptr study, SALOMEDS::StudyBuilder_ptr builder,
234 SALOMEDS::SObject_ptr sobj, const std::string& name)
236 SALOMEDS::ChildIterator_var anIterator= study->NewChildIterator(sobj);
237 SALOMEDS::GenericAttribute_var anAttr;
238 SALOMEDS::AttributeName_var namAttr ;
239 SALOMEDS::SObject_var result=SALOMEDS::SObject::_nil();
241 for (; anIterator->More(); anIterator->Next())
243 SALOMEDS::SObject_var anObj=anIterator->Value();
244 if(anObj->FindAttribute(anAttr, "AttributeName"))
246 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
247 CORBA::String_var value=namAttr->Value();
248 if(name == (const char*)value)
255 if(CORBA::is_nil(result))
258 result = builder->NewObject( sobj );
259 anAttr=builder->FindOrCreateAttribute(result,"AttributeName");
260 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
261 namAttr->SetValue(name.c_str());
263 return result._retn();
266 /*! \class YACS::ENGINE::InputStudyPort
267 * \brief Class for Study input Ports
274 InputStudyPort::InputStudyPort(const std::string& name, Node* node, TypeCode* type)
275 : InputXmlPort(name, node, type),
276 DataPort(name, node, type),
281 InputStudyPort::InputStudyPort(const InputStudyPort& other, Node *newHelder)
282 : InputXmlPort(other,newHelder),
283 DataPort(other,newHelder),
284 Port(other,newHelder),_storeData(other._storeData)
288 InputPort* InputStudyPort::clone(Node *newHelder) const
290 return new InputStudyPort(*this,newHelder);
293 void InputStudyPort::setData(const std::string& data)
296 DEBTRACE( "InputStudyPort::setData " << _storeData );
300 std::string InputStudyPort::getData()
302 DEBTRACE("InputStudyPort::getData " << _storeData);
306 std::string InputStudyPort::getIOR()
308 switch(edGetType()->kind())
311 return splitXML(_data);
317 std::string InputStudyPort::splitXML(const std::string& s)
319 // <value><tag>content</tag></value>
320 std::string::size_type begin = s.find_first_of("<"); // <value>
321 begin = s.find_first_of(">",begin); // end of <value>
322 begin = s.find_first_of("<",begin); // beginning of <tag>
323 begin = s.find_first_of(">",begin); // end of <tag>
324 begin = s.find_first_not_of("> ",begin); // beginning of content
325 std::string::size_type last = s.find_first_of("<",begin); // beginning of </tag>
326 return s.substr(begin,last-begin);
329 std::string InputStudyPort::dump()
331 DEBTRACE( "InputStudyPort::dump " << _storeData );
332 DEBTRACE( "InputStudyPort::dump " << _data );
333 //return "<value><string>"+_storeData+"</string></value>";
336 std::string InputStudyPort::getPyObj()
340 std::string InputStudyPort::getAsString()
345 void InputStudyPort::putDataInStudy(SALOMEDS::Study_var myStudy,SALOMEDS::StudyBuilder_var aBuilder)
347 SALOMEDS::GenericAttribute_var aGAttr;
348 SALOMEDS::SObject_var aSO ;
349 SALOMEDS::AttributeName_var anAttr ;
350 SALOMEDS::AttributeIOR_var iorAttr ;
351 SALOMEDS::SComponent_var aFather;
353 std::string data = getData();
354 DEBTRACE("data: " << data );
355 //try to find an existing id (i:j:k...)
356 aSO = myStudy->FindObjectID(data.c_str());
357 if(CORBA::is_nil(aSO))
359 // the id does not exist. Try to create it by id
360 aSO=myStudy->CreateObjectID(data.c_str());
361 if(!CORBA::is_nil(aSO))
363 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeName");
364 anAttr = SALOMEDS::AttributeName::_narrow( aGAttr );
365 anAttr->SetValue(getName().c_str());
369 if(CORBA::is_nil(aSO))
371 //try to publish the object with a given path
372 std::string name; // the component instance name
373 std::string objname; // the object name (eventually with "/")
374 std::string::size_type begin = data.find_first_not_of("/");
375 std::string::size_type pos=data.find_first_of("/", begin);
376 if (pos != std::string::npos)
378 name=data.substr(begin,pos-begin);
379 objname=data.substr(pos+1);
383 name=data.substr(begin);
386 std::string pname="/"+name;
390 Proc* proc=getNode()->getProc();
391 if(proc->componentInstanceMap.count(name)!=0)
393 // There is a component instance with this name. Is it a Salome component or not ?
394 ComponentInstance* compo=proc->componentInstanceMap[name];
395 if(SalomeComponent* scompo=dynamic_cast<SalomeComponent*>(compo))
397 //It's a Salome component, make it the right way : component name, component instance reference, ...
398 CORBA::Object_var compovar= scompo->getCompoPtr();
399 SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(compovar);
400 if ( !CORBA::is_nil( aDriver ) )
402 //It's a Salome component that implements the Driver interface. Use it to publish in study
404 CORBA::Object_var anObject;
407 orb = getSALOMERuntime()->getOrb();
408 anObject=orb->string_to_object(getIOR().c_str());
412 std::cerr << "Execution problem: can not get the object to publish" << std::endl;
415 if ( aDriver->CanPublishInStudy( anObject ) )
417 //It's fine use the driver to publish
418 SALOMEDS::SObject_var aTmpSO; // initialized to nil
421 aTmpSO = aDriver->PublishInStudy(myStudy, aTmpSO, anObject,objname.c_str() );
426 std::cerr << "Execution problem: error in PublishInStudy" << std::endl;
434 // Does component entry exist ?
435 aSO=myStudy->FindObjectByPath(pname.c_str());
436 if(CORBA::is_nil(aSO))
438 // We have not been able to publish the object with Salome Driver, make it the light way
439 aFather=aBuilder->NewComponent(name.c_str());
440 if(CORBA::is_nil(aFather))
442 std::cerr << "Execution problem: can not create component: " + name << std::endl;
445 aGAttr=aBuilder->FindOrCreateAttribute(aFather,"AttributeName");
446 anAttr = SALOMEDS::AttributeName::_narrow( aGAttr );
447 anAttr->SetValue(name.c_str());
448 aSO=myStudy->FindObjectByPath(pname.c_str());
451 begin=data.find_first_not_of("/",pos);
452 while (begin != std::string::npos)
454 pos = data.find_first_of("/", begin);
455 if (pos != std::string::npos)
456 name=data.substr(begin,pos-begin);
458 name=data.substr(begin);
459 aSO=findOrCreateSoWithName(myStudy,aBuilder,aSO,name);
460 begin=data.find_first_not_of("/",pos);
464 if(CORBA::is_nil(aSO))
466 std::cerr << "Execution problem: can not create id or path: " + data + " in study" << std::endl;
471 SALOMEDS::AttributeComment_var commentAttr ;
472 SALOMEDS::AttributeReal_var realAttr ;
473 SALOMEDS::AttributeInteger_var intAttr ;
474 SALOMEDS::AttributeString_var stringAttr ;
477 switch(edGetType()->kind())
482 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeIOR");
483 iorAttr = SALOMEDS::AttributeIOR::_narrow( aGAttr );
484 iorAttr->SetValue(value.c_str());
487 value=splitXML(dump());
489 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeReal");
490 realAttr = SALOMEDS::AttributeReal::_narrow( aGAttr );
491 d=atof(value.c_str());
492 realAttr->SetValue(d);
495 value=splitXML(dump());
497 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeInteger");
498 intAttr = SALOMEDS::AttributeInteger::_narrow( aGAttr );
499 v=atol(value.c_str());
500 intAttr->SetValue(v);
504 value=splitXML(dump());
506 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeComment");
507 commentAttr = SALOMEDS::AttributeComment::_narrow( aGAttr );
508 commentAttr->SetValue(value.c_str());
513 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeComment");
514 commentAttr = SALOMEDS::AttributeComment::_narrow( aGAttr );
515 commentAttr->SetValue(value.c_str());
519 } //end namespace ENGINE
520 } //end namespace YACS