Salome HOME
[EDF27816] : fix all non regression tests
[modules/yacs.git] / src / runtime / OutNode.cxx
1 // Copyright (C) 2006-2022  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 "PresetPorts.hxx"
21 #include "OutNode.hxx"
22 #include "Visitor.hxx"
23
24 #include <iostream>
25 #include <fstream>
26 #include <list>
27 #include <cassert>
28
29 //#define _DEVDEBUG_
30 #include "YacsTrace.hxx"
31
32 using namespace std;
33
34 namespace YACS
35 {
36   namespace ENGINE
37     {
38
39 /*! \class YACS::ENGINE::OutNode
40  *  \brief Class for data out node
41  *
42  * \ingroup Nodes
43  */
44
45 const char OutNode::IMPL_NAME[]="XML";
46
47 OutNode::OutNode(const std::string& name)
48   : DataNode(name)
49 {
50   _implementation=IMPL_NAME;
51 }
52
53 OutNode::OutNode(const OutNode& other, ComposedNode *father)
54   : DataNode(other, father)
55 {
56 }
57
58 InputPort* OutNode::createInputPort(const std::string& inputPortName, TypeCode* type)
59 {
60   return new InputPresetPort(inputPortName, this, type);
61 }
62
63 void OutNode::dump(std::ostream &out)
64 {
65   std::list<InputPort *>::const_iterator iter;
66   for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
67     {
68       InputPresetPort *inp = dynamic_cast<InputPresetPort *>(*iter);
69       if(inp->getData() != "")
70         {
71           //save the file in the given reference
72           std::string xmlValue=inp->dump();
73           std::string::size_type i=xmlValue.find_first_of('/',0);
74           xmlValue=xmlValue.substr(i);
75           i=xmlValue.find_first_of('<',0);
76           std::ifstream fin(xmlValue.substr(0,i).c_str());
77           std::ofstream fout(inp->getData().c_str());
78           fout << fin.rdbuf(); // Dumps file contents to file
79           out << "<value><objref>" << inp->getData() << "</objref></value>" << std::endl;
80         }
81       else
82         out << inp->dump() << std::endl;
83     }
84 }
85
86 void OutNode::execute()
87 {
88   DEBTRACE("+++++++ OutNode::execute +++++++++++");
89   if(_ref != "")
90     {
91       std::ofstream out(_ref.c_str());
92       dump(out);
93     }
94   else
95     dump(std::cout);
96   DEBTRACE("+++++++ end OutNode::execute +++++++++++" );
97 }
98
99 void OutNode::accept(Visitor *visitor)
100 {
101   visitor->visitOutNode(this);
102 }
103
104 void OutNode::setData(InputPort* port, const std::string& data)
105 {
106   InputPresetPort *inp = dynamic_cast<InputPresetPort *>(port);
107   inp->setData(data);
108 }
109
110 void OutNode::checkBasicConsistency() const
111 {
112   DEBTRACE("OutNode::checkBasicConsistency");
113   if (! _setOfOutputPort.empty())
114     {
115       string what = "OutNode ";
116       what += getName();
117       what += " only accepts InputPorts, no OutputPorts";
118       throw Exception(what);
119     }
120   list<InputPort *>::const_iterator iter;
121   for(iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
122     {
123       InputPresetPort *preset = dynamic_cast<InputPresetPort*>(*iter);
124       if (!preset)
125         {
126           string what("Input port: ");
127           what += (*iter)->getName();
128           what += " is not an InputPresetPort. PresetNode ";
129           what += getName();
130           what += " only accepts InputPresetPorts";
131           throw Exception(what);
132         }
133       preset->checkBasicConsistency();
134     }
135
136 }
137
138 Node *OutNode::simpleClone(ComposedNode *father, bool editionOnly) const
139 {
140   return new OutNode(*this,father);
141 }
142
143     }
144 }
145