Salome HOME
Porting Python3: Decode strings
[modules/yacs.git] / src / runtime / StudyNodes.cxx
1 // Copyright (C) 2006-2016  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "RuntimeSALOME.hxx"
21 #include "StudyNodes.hxx"
22 #include "StudyPorts.hxx"
23 #include "Visitor.hxx"
24 #include "TypeCode.hxx"
25 #include "SalomeProc.hxx"
26
27 #include "Basics_Utils.hxx"
28 #include "SALOME_NamingService.hxx"
29 #include "SALOMEDS.hh"
30 #include "SALOMEDS_Attributes.hh"
31
32 #include <iostream>
33 #include <sstream>
34 #include <string>
35 #include <list>
36 #include <stdlib.h>
37
38 //#define _DEVDEBUG_
39 #include "YacsTrace.hxx"
40
41 namespace YACS
42 {
43 namespace ENGINE
44 {
45
46 const char StudyInNode::IMPL_NAME[]="XML";
47
48 StudyInNode::StudyInNode(const std::string& name)
49   : DataNode(name)
50 {
51   _implementation=IMPL_NAME;
52 }
53
54 StudyInNode::StudyInNode(const StudyInNode& other, ComposedNode *father)
55   : DataNode(other, father)
56 {
57 }
58
59 Node *StudyInNode::simpleClone(ComposedNode *father, bool editionOnly) const
60 {
61   return new StudyInNode(*this,father);
62 }
63
64 OutputPort* StudyInNode::createOutputPort(const std::string& outputPortName, TypeCode* type)
65 {
66   return new OutputStudyPort(outputPortName, this, type);
67 }
68
69 void StudyInNode::setData(OutputPort* port, const std::string& data)
70 {
71   OutputStudyPort *outp = dynamic_cast<OutputStudyPort *>(port);
72   outp->setData(data);
73 }
74
75 void StudyInNode::execute()
76 {
77   DEBTRACE("+++++++ StudyInNode::execute +++++++++++");
78   SALOME_NamingService NS(getSALOMERuntime()->getOrb());
79   CORBA::Object_var obj=NS.Resolve("/myStudyManager");
80   if(CORBA::is_nil(obj)) 
81     {
82       _errorDetails="Execution problem: no naming service";
83       throw Exception(_errorDetails);
84     }
85
86   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(obj);
87   if(CORBA::is_nil(aStudyManager)) 
88     {
89       _errorDetails="Execution problem: no naming service";
90       throw Exception(_errorDetails);
91     }
92
93   int studyid=1;
94   if (getProperty("StudyID") != "")
95     {
96       // StudyId is specified
97       studyid=atoi(getProperty("StudyID").c_str());
98     }
99   else
100     {
101       Proc* p=getProc();
102       if(p)
103         {
104           std::string value=p->getProperty("DefaultStudyID");
105           if(!value.empty())
106             studyid= atoi(value.c_str());
107         }
108     }
109
110
111   SALOMEDS::Study_var myStudy =aStudyManager->GetStudyByID(studyid);
112   if(CORBA::is_nil(myStudy)) 
113     {
114       std::stringstream msg;
115       msg << "Execution problem: no study with id " << studyid;
116       _errorDetails=msg.str();
117       throw Exception(_errorDetails);
118     }
119
120   std::list<OutputPort *>::const_iterator iter;
121   for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
122     {
123       OutputStudyPort *outp = dynamic_cast<OutputStudyPort *>(*iter);
124       try
125         {
126           outp->getDataFromStudy(myStudy);
127         }
128       catch(Exception& e)
129         {
130           _errorDetails=e.what();
131           throw;
132         }
133     }
134   DEBTRACE("+++++++ end StudyInNode::execute +++++++++++" );
135 }
136
137 void StudyInNode::checkBasicConsistency() const throw(YACS::Exception)
138 {
139   DEBTRACE("StudyInNode::checkBasicConsistency");
140   if (! _setOfInputPort.empty())
141     {
142       std::string what = "StudyNode ";
143       what += getName();
144       what += " only accepts OutputStudyPort, no InputPort";
145       throw Exception(what);
146     }
147
148   std::list<OutputPort *>::const_iterator iter;
149   for(iter=_setOfOutputPort.begin();iter!=_setOfOutputPort.end();iter++)
150     {
151       OutputStudyPort *inp = dynamic_cast<OutputStudyPort*>(*iter);
152       if (!inp)
153         {
154           std::string what("Output port: ");
155           what += (*iter)->getName();
156           what += " is not an OutputStudyPort. StudyNode ";
157           what += getName();
158           what += " only accepts OutputStudyPorts";
159           throw Exception(what);
160         }
161
162       std::string data = inp->getData();
163       DEBTRACE(data);
164       if (data.empty())
165         {
166           std::string what("OutputStudyPort: ");
167           what += (*iter)->getName();
168           what += " is not initialised";
169           throw Exception(what);
170         }
171     }
172 }
173
174 void StudyInNode::accept(Visitor *visitor)
175 {
176   visitor->visitStudyInNode(this);
177 }
178
179 const char StudyOutNode::IMPL_NAME[]="XML";
180
181 StudyOutNode::StudyOutNode(const std::string& name)
182   : DataNode(name)
183 {
184   _implementation=IMPL_NAME;
185 }
186
187 StudyOutNode::StudyOutNode(const StudyOutNode& other, ComposedNode *father)
188   : DataNode(other, father)
189 {
190 }
191
192 Node *StudyOutNode::simpleClone(ComposedNode *father, bool editionOnly) const
193 {
194   return new StudyOutNode(*this,father);
195 }
196
197 InputPort* StudyOutNode::createInputPort(const std::string& inputPortName, TypeCode* type)
198 {
199   return new InputStudyPort(inputPortName, this, type);
200 }
201
202 void StudyOutNode::setData(InputPort* port, const std::string& data)
203 {
204   InputStudyPort *inp = dynamic_cast<InputStudyPort *>(port);
205   inp->setData(data);
206 }
207
208 /*
209 SALOMEDS::SObject_ptr findOrCreateSoWithName(SALOMEDS::Study_ptr study, SALOMEDS::StudyBuilder_ptr builder,
210                                              SALOMEDS::SObject_ptr sobj, const std::string& name)
211 {
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();
216
217   for (; anIterator->More(); anIterator->Next())
218     {
219       SALOMEDS::SObject_var anObj=anIterator->Value();
220       if(anObj->FindAttribute(anAttr, "AttributeName"))
221         {
222           namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
223           CORBA::String_var value=namAttr->Value();
224           if(name == (const char*)value)
225             {
226               result=anObj;
227               break;
228             }
229         }
230     }
231   if(CORBA::is_nil(result))
232     {
233       //create it
234       result = builder->NewObject( sobj );
235       anAttr=builder->FindOrCreateAttribute(result,"AttributeName");
236       namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
237       namAttr->SetValue(name.c_str());
238     }
239   return result._retn();
240 }
241 */
242
243 void StudyOutNode::execute()
244 {
245   DEBTRACE("+++++++ StudyOutNode::execute +++++++++++");
246   SALOME_NamingService NS(getSALOMERuntime()->getOrb());
247   CORBA::Object_var obj=NS.Resolve("/myStudyManager");
248   if(CORBA::is_nil(obj))
249     {
250       _errorDetails="Execution problem: no naming service";
251       throw Exception(_errorDetails);
252     }
253
254   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(obj);
255   if(CORBA::is_nil(aStudyManager))
256     {
257       _errorDetails="Execution problem: no naming service";
258       throw Exception(_errorDetails);
259     }
260
261   int studyid=1;
262   if (getProperty("StudyID") != "")
263     {
264       // StudyId is specified
265       studyid=atoi(getProperty("StudyID").c_str());
266     }
267   else
268     {
269       Proc* p=getProc();
270       if(p)
271         {
272           std::string value=p->getProperty("DefaultStudyID");
273           if(!value.empty())
274             studyid= atoi(value.c_str());
275         }
276     }
277
278   SALOMEDS::Study_var myStudy =aStudyManager->GetStudyByID(studyid);
279   if(CORBA::is_nil(myStudy))
280     {
281       //open a new one
282       std::stringstream msg;
283       msg << "Study" << studyid;
284       myStudy=aStudyManager->NewStudy(Kernel_Utils::decode_s(msg.str()));
285       if(CORBA::is_nil(myStudy))
286         {
287           _errorDetails="Execution problem: can not create new study " + msg.str();
288           throw Exception(_errorDetails);
289         }
290     }
291   DEBTRACE(myStudy->StudyId());
292
293   SALOMEDS::StudyBuilder_var aBuilder =myStudy->NewBuilder() ;
294   if(CORBA::is_nil(aBuilder))
295     {
296       _errorDetails="Execution problem: can not create StudyBuilder";
297       throw Exception(_errorDetails);
298     }
299
300   SALOMEDS::GenericAttribute_var aGAttr;
301   SALOMEDS::SObject_var aSO ;
302   SALOMEDS::AttributeName_var anAttr ;
303   SALOMEDS::AttributeIOR_var iorAttr ;
304
305   std::list<InputPort *>::const_iterator iter;
306   for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
307     {
308       InputStudyPort *inp = dynamic_cast<InputStudyPort *>(*iter);
309       inp->putDataInStudy(myStudy,aBuilder);
310     }
311
312   // save in file if ref is given
313   if(_ref != "")
314     {
315       aStudyManager->SaveAs(Kernel_Utils::decode_s(_ref),myStudy, false);
316     }
317   DEBTRACE("+++++++ end StudyOutNode::execute +++++++++++" );
318 }
319
320 void StudyOutNode::checkBasicConsistency() const throw(YACS::Exception)
321 {
322   DEBTRACE("StudyOutNode::checkBasicConsistency");
323   if (! _setOfOutputPort.empty())
324     {
325       std::string what = "StudyNode ";
326       what += getName();
327       what += " only accepts InputStudyPort, no OutputPort";
328       throw Exception(what);
329     }
330
331   std::list<InputPort *>::const_iterator iter;
332   for(iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
333     {
334       InputStudyPort *inp = dynamic_cast<InputStudyPort*>(*iter);
335       if (!inp)
336         {
337           std::string what("Input port: ");
338           what += (*iter)->getName();
339           what += " is not an InputStudyPort. StudyNode ";
340           what += getName();
341           what += " only accepts InputStudyPorts";
342           throw Exception(what);
343         }
344       inp->checkBasicConsistency();
345
346       std::string data = inp->getData();
347       DEBTRACE(data);
348       if (data.empty())
349         {
350           std::string what("InputStudyPort: ");
351           what += (*iter)->getName();
352           what += " is not initialised";
353           throw Exception(what);
354         }
355     }
356
357 }
358
359 void StudyOutNode::accept(Visitor *visitor)
360 {
361   visitor->visitStudyOutNode(this);
362 }
363
364 } //end namespace ENGINE
365 } //end namespace YACS