1 // Copyright (C) 2006-2024 CEA, EDF
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"
29 #include "SALOME_KernelServices.hxx"
36 #include "YacsTrace.hxx"
44 /*! \class YACS::ENGINE::OutputStudyPort
45 * \brief Class for Study output Ports
52 OutputStudyPort::OutputStudyPort(const std::string& name, Node* node, TypeCode* type)
53 : OutputXmlPort(name, node, type),
54 DataPort(name, node, type),
59 OutputStudyPort::OutputStudyPort(const OutputStudyPort& other, Node *newHelder)
60 : OutputXmlPort(other,newHelder),
61 DataPort(other,newHelder),
62 Port(other,newHelder),_storeData(other._storeData)
66 OutputPort* OutputStudyPort::clone(Node *newHelder) const
68 return new OutputStudyPort(*this,newHelder);
71 void OutputStudyPort::setData(const std::string& data)
74 DEBTRACE( "OutputStudyPort::setData " << _storeData );
78 std::string OutputStudyPort::getData()
80 DEBTRACE("OutputStudyPort::getData " << _storeData);
84 void OutputStudyPort::putIOR(const std::string& ior)
86 DEBTRACE("OutputStudyPort::putIOR " << ior);
87 if(ior.substr(0,7) == "<value>")
93 int tk=edGetType()->kind();
98 value="<value><double>"+ior+"</double></value>";
101 value="<value><int>"+ior+"</int></value>";
104 value="<value><string>"+ior+"</string></value>";
107 value="<value><boolean>"+ior+"</boolean></value>";
110 value="<value><objref>"+ior+"</objref></value>";
122 std::string OutputStudyPort::dump()
124 DEBTRACE( "OutputStudyPort::dump " << _storeData );
125 DEBTRACE( "OutputStudyPort::dump " << _data );
126 //return "<value><string>"+_storeData+"</string></value>";
130 std::string OutputStudyPort::getPyObj()
134 std::string OutputStudyPort::getAsString()
139 void OutputStudyPort::getDataFromStudy()
141 std::string data = getData();
142 DEBTRACE("data: " << data );
144 SALOMEDS::SObject_var aSO = KERNEL::getStudyServant()->FindObjectID(data.c_str());
145 if(CORBA::is_nil(aSO))
148 aSO=KERNEL::getStudyServant()->FindObjectByPath(data.c_str());
149 if(CORBA::is_nil(aSO))
151 std::stringstream msg;
152 msg << "Execution problem: no id or path: " << data << " in study";
153 throw Exception(msg.str());
157 CORBA::String_var path=KERNEL::getStudyServant()->GetObjectPath(aSO);
159 CORBA::String_var id=aSO->GetID();
161 //CORBA::Object_var sobj=aSO->GetObject();
163 SALOMEDS::GenericAttribute_var aGAttr;
164 CORBA::String_var value;
166 if(edGetType()->kind()==Objref)
168 if ( aSO->FindAttribute( aGAttr, "AttributeIOR" ) )
170 SALOMEDS::AttributeIOR_var anAttr = SALOMEDS::AttributeIOR::_narrow( aGAttr );
171 value=anAttr->Value();
172 putIOR((const char*)value);
177 std::string error="Execution problem: no AttributeIOR in study object: ";
179 throw Exception(error);
182 else if(edGetType()->kind()==Double )
184 if ( aSO->FindAttribute( aGAttr, "AttributeReal" ) )
186 SALOMEDS::AttributeReal_var anAttr = SALOMEDS::AttributeReal::_narrow( aGAttr );
187 CORBA::Double d=anAttr->Value();
188 std::stringstream msg;
189 msg << "<value><double>" << std::setprecision(16) << d << "</double></value>";
190 put(msg.str().c_str());
194 std::string error="Execution problem: no AttributeReal in study object: ";
195 throw Exception(error+data);
198 else if(edGetType()->kind()== Int)
200 if ( aSO->FindAttribute( aGAttr, "AttributeInteger" ) )
202 SALOMEDS::AttributeInteger_var anAttr = SALOMEDS::AttributeInteger::_narrow( aGAttr );
203 CORBA::Long l=anAttr->Value();
204 std::stringstream msg;
205 msg << "<value><int>" << l << "</int></value>";
206 put(msg.str().c_str());
210 std::string error="Execution problem: no AttributeInteger in study object: ";
211 throw Exception(error+data);
216 if ( aSO->FindAttribute( aGAttr, "AttributeComment" ) )
218 SALOMEDS::AttributeComment_var anAttr = SALOMEDS::AttributeComment::_narrow( aGAttr );
219 value=anAttr->Value();
221 putIOR((const char*)value);
225 std::string error="Execution problem: no AttributeComment in study object: ";
226 throw Exception(error+data);
234 SALOMEDS::SObject_ptr findOrCreateSoWithName(SALOMEDS::StudyBuilder_ptr builder,
235 SALOMEDS::SObject_ptr sobj, const std::string& name)
237 SALOMEDS::ChildIterator_var anIterator= KERNEL::getStudyServant()->NewChildIterator(sobj);
238 SALOMEDS::GenericAttribute_var anAttr;
239 SALOMEDS::AttributeName_var namAttr ;
240 SALOMEDS::SObject_var result=SALOMEDS::SObject::_nil();
242 for (; anIterator->More(); anIterator->Next())
244 SALOMEDS::SObject_var anObj=anIterator->Value();
245 if(anObj->FindAttribute(anAttr, "AttributeName"))
247 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
248 CORBA::String_var value=namAttr->Value();
249 if(name == (const char*)value)
256 if(CORBA::is_nil(result))
259 result = builder->NewObject( sobj );
260 anAttr=builder->FindOrCreateAttribute(result,"AttributeName");
261 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
262 namAttr->SetValue(name.c_str());
264 return result._retn();
267 /*! \class YACS::ENGINE::InputStudyPort
268 * \brief Class for Study input Ports
275 InputStudyPort::InputStudyPort(const std::string& name, Node* node, TypeCode* type)
276 : InputXmlPort(name, node, type),
277 DataPort(name, node, type),
282 InputStudyPort::InputStudyPort(const InputStudyPort& other, Node *newHelder)
283 : InputXmlPort(other,newHelder),
284 DataPort(other,newHelder),
285 Port(other,newHelder),_storeData(other._storeData)
289 InputPort* InputStudyPort::clone(Node *newHelder) const
291 return new InputStudyPort(*this,newHelder);
294 void InputStudyPort::setData(const std::string& data)
297 DEBTRACE( "InputStudyPort::setData " << _storeData );
301 std::string InputStudyPort::getData()
303 DEBTRACE("InputStudyPort::getData " << _storeData);
307 std::string InputStudyPort::getIOR()
309 switch(edGetType()->kind())
312 return splitXML(_data);
318 std::string InputStudyPort::splitXML(const std::string& s)
320 // <value><tag>content</tag></value>
321 std::string::size_type begin = s.find_first_of("<"); // <value>
322 begin = s.find_first_of(">",begin); // end of <value>
323 begin = s.find_first_of("<",begin); // beginning of <tag>
324 begin = s.find_first_of(">",begin); // end of <tag>
325 begin = s.find_first_not_of("> ",begin); // beginning of content
326 std::string::size_type last = s.find_first_of("<",begin); // beginning of </tag>
327 return s.substr(begin,last-begin);
330 std::string InputStudyPort::dump()
332 DEBTRACE( "InputStudyPort::dump " << _storeData );
333 DEBTRACE( "InputStudyPort::dump " << _data );
334 //return "<value><string>"+_storeData+"</string></value>";
337 std::string InputStudyPort::getPyObj()
341 std::string InputStudyPort::getAsString()
346 void InputStudyPort::putDataInStudy(SALOMEDS::StudyBuilder_var aBuilder)
348 SALOMEDS::GenericAttribute_var aGAttr;
349 SALOMEDS::SObject_var aSO ;
350 SALOMEDS::AttributeName_var anAttr ;
351 SALOMEDS::AttributeIOR_var iorAttr ;
352 SALOMEDS::SComponent_var aFather;
354 std::string data = getData();
355 DEBTRACE("data: " << data );
356 //try to find an existing id (i:j:k...)
357 aSO = KERNEL::getStudyServant()->FindObjectID(data.c_str());
358 if(CORBA::is_nil(aSO))
360 // the id does not exist. Try to create it by id
361 aSO=KERNEL::getStudyServant()->CreateObjectID(data.c_str());
362 if(!CORBA::is_nil(aSO))
364 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeName");
365 anAttr = SALOMEDS::AttributeName::_narrow( aGAttr );
366 anAttr->SetValue(getName().c_str());
370 if(CORBA::is_nil(aSO))
372 //try to publish the object with a given path
373 std::string name; // the component instance name
374 std::string objname; // the object name (eventually with "/")
375 std::string::size_type begin = data.find_first_not_of("/");
376 std::string::size_type pos=data.find_first_of("/", begin);
377 if (pos != std::string::npos)
379 name=data.substr(begin,pos-begin);
380 objname=data.substr(pos+1);
384 name=data.substr(begin);
387 std::string pname="/"+name;
391 Proc* proc=getNode()->getProc();
392 if(proc->componentInstanceMap.count(name)!=0)
394 // There is a component instance with this name. Is it a Salome component or not ?
395 ComponentInstance* compo=proc->componentInstanceMap[name];
396 if(SalomeComponent* scompo=dynamic_cast<SalomeComponent*>(compo))
398 //It's a Salome component, make it the right way : component name, component instance reference, ...
399 CORBA::Object_var compovar= scompo->getCompoPtr();
400 SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(compovar);
401 if ( !CORBA::is_nil( aDriver ) )
403 //It's a Salome component that implements the Driver interface. Use it to publish in study
405 CORBA::Object_var anObject;
408 orb = getSALOMERuntime()->getOrb();
409 anObject=orb->string_to_object(getIOR().c_str());
413 std::cerr << "Execution problem: can not get the object to publish" << std::endl;
416 if ( aDriver->CanPublishInStudy( anObject ) )
418 //It's fine use the driver to publish
419 SALOMEDS::SObject_var aTmpSO; // initialized to nil
422 aTmpSO = aDriver->PublishInStudy(aTmpSO, anObject,objname.c_str() );
427 std::cerr << "Execution problem: error in PublishInStudy" << std::endl;
435 // Does component entry exist ?
436 aSO=KERNEL::getStudyServant()->FindObjectByPath(pname.c_str());
437 if(CORBA::is_nil(aSO))
439 // We have not been able to publish the object with Salome Driver, make it the light way
440 aFather=aBuilder->NewComponent(name.c_str());
441 if(CORBA::is_nil(aFather))
443 std::cerr << "Execution problem: can not create component: " + name << std::endl;
446 aGAttr=aBuilder->FindOrCreateAttribute(aFather,"AttributeName");
447 anAttr = SALOMEDS::AttributeName::_narrow( aGAttr );
448 anAttr->SetValue(name.c_str());
449 aSO=KERNEL::getStudyServant()->FindObjectByPath(pname.c_str());
452 begin=data.find_first_not_of("/",pos);
453 while (begin != std::string::npos)
455 pos = data.find_first_of("/", begin);
456 if (pos != std::string::npos)
457 name=data.substr(begin,pos-begin);
459 name=data.substr(begin);
460 aSO=findOrCreateSoWithName(aBuilder,aSO,name);
461 begin=data.find_first_not_of("/",pos);
465 if(CORBA::is_nil(aSO))
467 std::cerr << "Execution problem: can not create id or path: " + data + " in study" << std::endl;
472 SALOMEDS::AttributeComment_var commentAttr ;
473 SALOMEDS::AttributeReal_var realAttr ;
474 SALOMEDS::AttributeInteger_var intAttr ;
475 SALOMEDS::AttributeString_var stringAttr ;
478 switch(edGetType()->kind())
483 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeIOR");
484 iorAttr = SALOMEDS::AttributeIOR::_narrow( aGAttr );
485 iorAttr->SetValue(value.c_str());
488 value=splitXML(dump());
490 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeReal");
491 realAttr = SALOMEDS::AttributeReal::_narrow( aGAttr );
492 d=atof(value.c_str());
493 realAttr->SetValue(d);
496 value=splitXML(dump());
498 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeInteger");
499 intAttr = SALOMEDS::AttributeInteger::_narrow( aGAttr );
500 v=atol(value.c_str());
501 intAttr->SetValue(v);
505 value=splitXML(dump());
507 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeComment");
508 commentAttr = SALOMEDS::AttributeComment::_narrow( aGAttr );
509 commentAttr->SetValue(value.c_str());
514 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeComment");
515 commentAttr = SALOMEDS::AttributeComment::_narrow( aGAttr );
516 commentAttr->SetValue(value.c_str());
520 } //end namespace ENGINE
521 } //end namespace YACS