Salome HOME
Merge Python 3 porting.
[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 "SALOME_KernelServices.hxx"
30 #include "SALOMEDS.hh"
31 #include "SALOMEDS_Attributes.hh"
32
33 #include <iostream>
34 #include <sstream>
35 #include <string>
36 #include <list>
37 #include <stdlib.h>
38
39 //#define _DEVDEBUG_
40 #include "YacsTrace.hxx"
41
42 namespace YACS
43 {
44 namespace ENGINE
45 {
46
47 const char StudyInNode::IMPL_NAME[]="XML";
48
49 StudyInNode::StudyInNode(const std::string& name)
50   : DataNode(name)
51 {
52   _implementation=IMPL_NAME;
53 }
54
55 StudyInNode::StudyInNode(const StudyInNode& other, ComposedNode *father)
56   : DataNode(other, father)
57 {
58 }
59
60 Node *StudyInNode::simpleClone(ComposedNode *father, bool editionOnly) const
61 {
62   return new StudyInNode(*this,father);
63 }
64
65 OutputPort* StudyInNode::createOutputPort(const std::string& outputPortName, TypeCode* type)
66 {
67   return new OutputStudyPort(outputPortName, this, type);
68 }
69
70 void StudyInNode::setData(OutputPort* port, const std::string& data)
71 {
72   OutputStudyPort *outp = dynamic_cast<OutputStudyPort *>(port);
73   outp->setData(data);
74 }
75
76 void StudyInNode::execute()
77 {
78   DEBTRACE("+++++++ StudyInNode::execute +++++++++++");
79
80   std::list<OutputPort *>::const_iterator iter;
81   for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
82     {
83       OutputStudyPort *outp = dynamic_cast<OutputStudyPort *>(*iter);
84       try
85         {
86           outp->getDataFromStudy();
87         }
88       catch(Exception& e)
89         {
90           _errorDetails=e.what();
91           throw;
92         }
93     }
94   DEBTRACE("+++++++ end StudyInNode::execute +++++++++++" );
95 }
96
97 void StudyInNode::checkBasicConsistency() const throw(YACS::Exception)
98 {
99   DEBTRACE("StudyInNode::checkBasicConsistency");
100   if (! _setOfInputPort.empty())
101     {
102       std::string what = "StudyNode ";
103       what += getName();
104       what += " only accepts OutputStudyPort, no InputPort";
105       throw Exception(what);
106     }
107
108   std::list<OutputPort *>::const_iterator iter;
109   for(iter=_setOfOutputPort.begin();iter!=_setOfOutputPort.end();iter++)
110     {
111       OutputStudyPort *inp = dynamic_cast<OutputStudyPort*>(*iter);
112       if (!inp)
113         {
114           std::string what("Output port: ");
115           what += (*iter)->getName();
116           what += " is not an OutputStudyPort. StudyNode ";
117           what += getName();
118           what += " only accepts OutputStudyPorts";
119           throw Exception(what);
120         }
121
122       std::string data = inp->getData();
123       DEBTRACE(data);
124       if (data.empty())
125         {
126           std::string what("OutputStudyPort: ");
127           what += (*iter)->getName();
128           what += " is not initialised";
129           throw Exception(what);
130         }
131     }
132 }
133
134 void StudyInNode::accept(Visitor *visitor)
135 {
136   visitor->visitStudyInNode(this);
137 }
138
139 const char StudyOutNode::IMPL_NAME[]="XML";
140
141 StudyOutNode::StudyOutNode(const std::string& name)
142   : DataNode(name)
143 {
144   _implementation=IMPL_NAME;
145 }
146
147 StudyOutNode::StudyOutNode(const StudyOutNode& other, ComposedNode *father)
148   : DataNode(other, father)
149 {
150 }
151
152 Node *StudyOutNode::simpleClone(ComposedNode *father, bool editionOnly) const
153 {
154   return new StudyOutNode(*this,father);
155 }
156
157 InputPort* StudyOutNode::createInputPort(const std::string& inputPortName, TypeCode* type)
158 {
159   return new InputStudyPort(inputPortName, this, type);
160 }
161
162 void StudyOutNode::setData(InputPort* port, const std::string& data)
163 {
164   InputStudyPort *inp = dynamic_cast<InputStudyPort *>(port);
165   inp->setData(data);
166 }
167
168 /*
169 SALOMEDS::SObject_ptr findOrCreateSoWithName(SALOMEDS::StudyBuilder_ptr builder,
170                                              SALOMEDS::SObject_ptr sobj, const std::string& name)
171 {
172   SALOMEDS::ChildIterator_var anIterator= KERNEL::getStudyServant()->NewChildIterator(sobj);
173   SALOMEDS::GenericAttribute_var anAttr;
174   SALOMEDS::AttributeName_var namAttr ;
175   SALOMEDS::SObject_var result=SALOMEDS::SObject::_nil();
176
177   for (; anIterator->More(); anIterator->Next())
178     {
179       SALOMEDS::SObject_var anObj=anIterator->Value();
180       if(anObj->FindAttribute(anAttr, "AttributeName"))
181         {
182           namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
183           CORBA::String_var value=namAttr->Value();
184           if(name == (const char*)value)
185             {
186               result=anObj;
187               break;
188             }
189         }
190     }
191   if(CORBA::is_nil(result))
192     {
193       //create it
194       result = builder->NewObject( sobj );
195       anAttr=builder->FindOrCreateAttribute(result,"AttributeName");
196       namAttr = SALOMEDS::AttributeName::_narrow( anAttr );
197       namAttr->SetValue(name.c_str());
198     }
199   return result._retn();
200 }
201 */
202
203 void StudyOutNode::execute()
204 {
205   DEBTRACE("+++++++ StudyOutNode::execute +++++++++++");
206
207   SALOMEDS::StudyBuilder_var aBuilder =KERNEL::getStudyServant()->NewBuilder() ;
208   if(CORBA::is_nil(aBuilder))
209     {
210       _errorDetails="Execution problem: can not create StudyBuilder";
211       throw Exception(_errorDetails);
212     }
213
214   SALOMEDS::GenericAttribute_var aGAttr;
215   SALOMEDS::SObject_var aSO ;
216   SALOMEDS::AttributeName_var anAttr ;
217   SALOMEDS::AttributeIOR_var iorAttr ;
218
219   std::list<InputPort *>::const_iterator iter;
220   for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
221     {
222       InputStudyPort *inp = dynamic_cast<InputStudyPort *>(*iter);
223       inp->putDataInStudy(aBuilder);
224     }
225
226   // save in file if ref is given
227   if(_ref != "")
228     {
229       KERNEL::getStudyServant()->SaveAs(Kernel_Utils::decode_s( _ref ), false, false);
230     }
231   DEBTRACE("+++++++ end StudyOutNode::execute +++++++++++" );
232 }
233
234 void StudyOutNode::checkBasicConsistency() const throw(YACS::Exception)
235 {
236   DEBTRACE("StudyOutNode::checkBasicConsistency");
237   if (! _setOfOutputPort.empty())
238     {
239       std::string what = "StudyNode ";
240       what += getName();
241       what += " only accepts InputStudyPort, no OutputPort";
242       throw Exception(what);
243     }
244
245   std::list<InputPort *>::const_iterator iter;
246   for(iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
247     {
248       InputStudyPort *inp = dynamic_cast<InputStudyPort*>(*iter);
249       if (!inp)
250         {
251           std::string what("Input port: ");
252           what += (*iter)->getName();
253           what += " is not an InputStudyPort. StudyNode ";
254           what += getName();
255           what += " only accepts InputStudyPorts";
256           throw Exception(what);
257         }
258       inp->checkBasicConsistency();
259
260       std::string data = inp->getData();
261       DEBTRACE(data);
262       if (data.empty())
263         {
264           std::string what("InputStudyPort: ");
265           what += (*iter)->getName();
266           what += " is not initialised";
267           throw Exception(what);
268         }
269     }
270
271 }
272
273 void StudyOutNode::accept(Visitor *visitor)
274 {
275   visitor->visitStudyOutNode(this);
276 }
277
278 } //end namespace ENGINE
279 } //end namespace YACS