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