1 // Copyright (C) 2006-2014 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"
34 #include "YacsTrace.hxx"
42 /*! \class YACS::ENGINE::OutputStudyPort
43 * \brief Class for Study output Ports
50 OutputStudyPort::OutputStudyPort(const std::string& name, Node* node, TypeCode* type)
51 : OutputXmlPort(name, node, type),
52 DataPort(name, node, type),
57 OutputStudyPort::OutputStudyPort(const OutputStudyPort& other, Node *newHelder)
58 : OutputXmlPort(other,newHelder),
59 DataPort(other,newHelder),
60 Port(other,newHelder),_storeData(other._storeData)
64 OutputPort* OutputStudyPort::clone(Node *newHelder) const
66 return new OutputStudyPort(*this,newHelder);
69 void OutputStudyPort::setData(const std::string& data)
72 DEBTRACE( "OutputStudyPort::setData " << _storeData );
76 std::string OutputStudyPort::getData()
78 DEBTRACE("OutputStudyPort::getData " << _storeData);
82 void OutputStudyPort::putIOR(const std::string& ior)
84 DEBTRACE("OutputStudyPort::putIOR " << ior);
85 if(ior.substr(0,7) == "<value>")
91 int tk=edGetType()->kind();
96 value="<value><double>"+ior+"</double></value>";
99 value="<value><int>"+ior+"</int></value>";
102 value="<value><string>"+ior+"</string></value>";
105 value="<value><boolean>"+ior+"</boolean></value>";
108 value="<value><objref>"+ior+"</objref></value>";
120 std::string OutputStudyPort::dump()
122 DEBTRACE( "OutputStudyPort::dump " << _storeData );
123 DEBTRACE( "OutputStudyPort::dump " << _data );
124 //return "<value><string>"+_storeData+"</string></value>";
128 std::string OutputStudyPort::getPyObj()
132 std::string OutputStudyPort::getAsString()
137 void OutputStudyPort::getDataFromStudy(SALOMEDS::Study_var myStudy)
139 std::string data = getData();
140 DEBTRACE("data: " << data );
142 SALOMEDS::SObject_var aSO = myStudy->FindObjectID(data.c_str());
143 if(CORBA::is_nil(aSO))
146 aSO=myStudy->FindObjectByPath(data.c_str());
147 if(CORBA::is_nil(aSO))
149 std::stringstream msg;
150 msg << "Execution problem: no id or path: " << data << " in study " << myStudy->StudyId();
151 throw Exception(msg.str());
155 CORBA::String_var path=myStudy->GetObjectPath(aSO);
157 CORBA::String_var id=aSO->GetID();
159 //CORBA::Object_var sobj=aSO->GetObject();
161 SALOMEDS::GenericAttribute_var aGAttr;
162 CORBA::String_var value;
164 if(edGetType()->kind()==Objref)
166 if ( aSO->FindAttribute( aGAttr, "AttributeIOR" ) )
168 SALOMEDS::AttributeIOR_var anAttr = SALOMEDS::AttributeIOR::_narrow( aGAttr );
169 value=anAttr->Value();
170 putIOR((const char*)value);
175 std::string error="Execution problem: no AttributeIOR in study object: ";
177 throw Exception(error);
180 else if(edGetType()->kind()==Double )
182 if ( aSO->FindAttribute( aGAttr, "AttributeReal" ) )
184 SALOMEDS::AttributeReal_var anAttr = SALOMEDS::AttributeReal::_narrow( aGAttr );
185 CORBA::Double d=anAttr->Value();
186 std::stringstream msg;
187 msg << "<value><double>" << d << "</double></value>";
188 put(msg.str().c_str());
192 std::string error="Execution problem: no AttributeReal in study object: ";
193 throw Exception(error+data);
196 else if(edGetType()->kind()== Int)
198 if ( aSO->FindAttribute( aGAttr, "AttributeInteger" ) )
200 SALOMEDS::AttributeInteger_var anAttr = SALOMEDS::AttributeInteger::_narrow( aGAttr );
201 CORBA::Long l=anAttr->Value();
202 std::stringstream msg;
203 msg << "<value><int>" << l << "</int></value>";
204 put(msg.str().c_str());
208 std::string error="Execution problem: no AttributeInteger in study object: ";
209 throw Exception(error+data);
214 if ( aSO->FindAttribute( aGAttr, "AttributeComment" ) )
216 SALOMEDS::AttributeComment_var anAttr = SALOMEDS::AttributeComment::_narrow( aGAttr );
217 value=anAttr->Value();
219 putIOR((const char*)value);
223 std::string error="Execution problem: no AttributeComment in study object: ";
224 throw Exception(error+data);
232 SALOMEDS::SObject_ptr findOrCreateSoWithName(SALOMEDS::Study_ptr study, SALOMEDS::StudyBuilder_ptr builder,
233 SALOMEDS::SObject_ptr sobj, const std::string& name)
235 SALOMEDS::ChildIterator_var anIterator= study->NewChildIterator(sobj);
236 SALOMEDS::GenericAttribute_var anAttr;
237 SALOMEDS::AttributeName_var namAttr ;
238 SALOMEDS::SObject_var result=SALOMEDS::SObject::_nil();
240 for (; anIterator->More(); anIterator->Next())
242 SALOMEDS::SObject_var anObj=anIterator->Value();
243 if(anObj->FindAttribute(anAttr, "AttributeName"))
245 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
246 CORBA::String_var value=namAttr->Value();
247 if(name == (const char*)value)
254 if(CORBA::is_nil(result))
257 result = builder->NewObject( sobj );
258 anAttr=builder->FindOrCreateAttribute(result,"AttributeName");
259 namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
260 namAttr->SetValue(name.c_str());
262 return result._retn();
265 /*! \class YACS::ENGINE::InputStudyPort
266 * \brief Class for Study input Ports
273 InputStudyPort::InputStudyPort(const std::string& name, Node* node, TypeCode* type)
274 : InputXmlPort(name, node, type),
275 DataPort(name, node, type),
280 InputStudyPort::InputStudyPort(const InputStudyPort& other, Node *newHelder)
281 : InputXmlPort(other,newHelder),
282 DataPort(other,newHelder),
283 Port(other,newHelder),_storeData(other._storeData)
287 InputPort* InputStudyPort::clone(Node *newHelder) const
289 return new InputStudyPort(*this,newHelder);
292 void InputStudyPort::setData(const std::string& data)
295 DEBTRACE( "InputStudyPort::setData " << _storeData );
299 std::string InputStudyPort::getData()
301 DEBTRACE("InputStudyPort::getData " << _storeData);
305 std::string InputStudyPort::getIOR()
307 switch(edGetType()->kind())
310 return splitXML(_data);
316 std::string InputStudyPort::splitXML(const std::string& s)
318 // <value><tag>content</tag></value>
319 std::string::size_type begin = s.find_first_of("<"); // <value>
320 begin = s.find_first_of(">",begin); // end of <value>
321 begin = s.find_first_of("<",begin); // beginning of <tag>
322 begin = s.find_first_of(">",begin); // end of <tag>
323 begin = s.find_first_not_of("> ",begin); // beginning of content
324 std::string::size_type last = s.find_first_of("<",begin); // beginning of </tag>
325 return s.substr(begin,last-begin);
328 std::string InputStudyPort::dump()
330 DEBTRACE( "InputStudyPort::dump " << _storeData );
331 DEBTRACE( "InputStudyPort::dump " << _data );
332 //return "<value><string>"+_storeData+"</string></value>";
335 std::string InputStudyPort::getPyObj()
339 std::string InputStudyPort::getAsString()
344 void InputStudyPort::putDataInStudy(SALOMEDS::Study_var myStudy,SALOMEDS::StudyBuilder_var aBuilder)
346 SALOMEDS::GenericAttribute_var aGAttr;
347 SALOMEDS::SObject_var aSO ;
348 SALOMEDS::AttributeName_var anAttr ;
349 SALOMEDS::AttributeIOR_var iorAttr ;
350 SALOMEDS::SComponent_var aFather;
352 std::string data = getData();
353 DEBTRACE("data: " << data );
354 //try to find an existing id (i:j:k...)
355 aSO = myStudy->FindObjectID(data.c_str());
356 if(CORBA::is_nil(aSO))
358 // the id does not exist. Try to create it by id
359 aSO=myStudy->CreateObjectID(data.c_str());
360 if(!CORBA::is_nil(aSO))
362 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeName");
363 anAttr = SALOMEDS::AttributeName::_narrow( aGAttr );
364 anAttr->SetValue(getName().c_str());
368 if(CORBA::is_nil(aSO))
370 //try to publish the object with a given path
371 std::string name; // the component instance name
372 std::string objname; // the object name (eventually with "/")
373 std::string::size_type begin = data.find_first_not_of("/");
374 std::string::size_type pos=data.find_first_of("/", begin);
375 if (pos != std::string::npos)
377 name=data.substr(begin,pos-begin);
378 objname=data.substr(pos+1);
382 name=data.substr(begin);
385 std::string pname="/"+name;
389 Proc* proc=getNode()->getProc();
390 if(proc->componentInstanceMap.count(name)!=0)
392 // There is a component instance with this name. Is it a Salome component or not ?
393 ComponentInstance* compo=proc->componentInstanceMap[name];
394 if(SalomeComponent* scompo=dynamic_cast<SalomeComponent*>(compo))
396 //It's a Salome component, make it the right way : component name, component instance reference, ...
397 CORBA::Object_var compovar= scompo->getCompoPtr();
398 SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(compovar);
399 if ( !CORBA::is_nil( aDriver ) )
401 //It's a Salome component that implements the Driver interface. Use it to publish in study
403 CORBA::Object_var anObject;
406 orb = getSALOMERuntime()->getOrb();
407 anObject=orb->string_to_object(getIOR().c_str());
411 std::cerr << "Execution problem: can not get the object to publish" << std::endl;
414 if ( aDriver->CanPublishInStudy( anObject ) )
416 //It's fine use the driver to publish
417 SALOMEDS::SObject_var aTmpSO; // initialized to nil
420 aTmpSO = aDriver->PublishInStudy(myStudy, aTmpSO, anObject,objname.c_str() );
425 std::cerr << "Execution problem: error in PublishInStudy" << std::endl;
433 // Does component entry exist ?
434 aSO=myStudy->FindObjectByPath(pname.c_str());
435 if(CORBA::is_nil(aSO))
437 // We have not been able to publish the object with Salome Driver, make it the light way
438 aFather=aBuilder->NewComponent(name.c_str());
439 if(CORBA::is_nil(aFather))
441 std::cerr << "Execution problem: can not create component: " + name << std::endl;
444 aGAttr=aBuilder->FindOrCreateAttribute(aFather,"AttributeName");
445 anAttr = SALOMEDS::AttributeName::_narrow( aGAttr );
446 anAttr->SetValue(name.c_str());
447 aSO=myStudy->FindObjectByPath(pname.c_str());
450 begin=data.find_first_not_of("/",pos);
451 while (begin != std::string::npos)
453 pos = data.find_first_of("/", begin);
454 if (pos != std::string::npos)
455 name=data.substr(begin,pos-begin);
457 name=data.substr(begin);
458 aSO=findOrCreateSoWithName(myStudy,aBuilder,aSO,name);
459 begin=data.find_first_not_of("/",pos);
463 if(CORBA::is_nil(aSO))
465 std::cerr << "Execution problem: can not create id or path: " + data + " in study" << std::endl;
470 SALOMEDS::AttributeComment_var commentAttr ;
471 SALOMEDS::AttributeReal_var realAttr ;
472 SALOMEDS::AttributeInteger_var intAttr ;
473 SALOMEDS::AttributeString_var stringAttr ;
476 switch(edGetType()->kind())
481 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeIOR");
482 iorAttr = SALOMEDS::AttributeIOR::_narrow( aGAttr );
483 iorAttr->SetValue(value.c_str());
486 value=splitXML(dump());
488 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeReal");
489 realAttr = SALOMEDS::AttributeReal::_narrow( aGAttr );
490 d=atof(value.c_str());
491 realAttr->SetValue(d);
494 value=splitXML(dump());
496 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeInteger");
497 intAttr = SALOMEDS::AttributeInteger::_narrow( aGAttr );
498 v=atol(value.c_str());
499 intAttr->SetValue(v);
503 value=splitXML(dump());
505 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeComment");
506 commentAttr = SALOMEDS::AttributeComment::_narrow( aGAttr );
507 commentAttr->SetValue(value.c_str());
512 aGAttr=aBuilder->FindOrCreateAttribute(aSO,"AttributeComment");
513 commentAttr = SALOMEDS::AttributeComment::_narrow( aGAttr );
514 commentAttr->SetValue(value.c_str());
518 } //end namespace ENGINE
519 } //end namespace YACS