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