Salome HOME
33c903fc65e4021e7fc0734d2b5f231a11ce1ea5
[modules/yacs.git] / src / engine / ServiceNode.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 "ServiceNode.hxx"
21 #include "Visitor.hxx"
22 #include "ComponentInstance.hxx"
23 #include "ComposedNode.hxx"
24 #include "Runtime.hxx"
25 #include <iostream>
26 #include <cassert>
27
28 //#define _DEVDEBUG_
29 #include "YacsTrace.hxx"
30
31 using namespace YACS::ENGINE;
32
33 /*! \class YACS::ENGINE::ServiceNode
34  *  \brief Class for calculation node associated with a component service
35  *
36  * \ingroup Nodes
37  *
38  * \see InlineNode
39  * \see ElementaryNode
40  */
41
42 const char ServiceNode::KIND[]="";
43
44 //! Return the service node kind
45 /*!
46  * A runtime can provide several implementations of a service node.
47  * Each implementation has a different kind. A ComponentInstance can be
48  * associated to a ServiceNode with the same kind.
49  */
50 std::string ServiceNode::getKind() const
51 {
52   return KIND;
53 }
54
55 ServiceNode::ServiceNode(const std::string& name):ElementaryNode(name),_component(0)
56
57 }
58
59 ServiceNode::ServiceNode(const ServiceNode& other, ComposedNode *father)
60            :ElementaryNode(other,father),_method(other._method),_component(0)
61 {
62 }
63
64 void ServiceNode::performDuplicationOfPlacement(const Node& other)
65 {
66   const ServiceNode &otherC=*(dynamic_cast<const ServiceNode *>(&other));
67   //if other has no component don't clone: this will not have one
68   if(otherC._component)
69     _component=otherC._component->clone();
70 }
71
72 void ServiceNode::performShallowDuplicationOfPlacement(const Node& other)
73 {
74   const ServiceNode &otherC=*(dynamic_cast<const ServiceNode *>(&other));
75   //if other has no component don't clone: this will not have one
76   if(otherC._component)
77     {
78       _component=otherC._component;
79       _component->incrRef();
80     }
81 }
82
83 ServiceNode::~ServiceNode()
84 {
85   if(_component)
86     _component->decrRef();
87 }
88
89 //! Load the component associated to the node
90 void ServiceNode::load()
91 {
92   if(_component)
93     {
94       if(!_component->isLoaded(this))
95         {
96           try
97             {
98               _component->load(this);
99             }
100           catch(Exception& e)
101             {
102               _errorDetails=e.what();
103               throw e;
104             }
105         }
106     }
107   else
108     {
109       std::string what("ServiceNode::load : a load operation requested on ServiceNode called \"");
110       what+=_name; what+="\" with no component specified.";
111       _errorDetails=what;
112       throw Exception(what);
113     }
114 }
115
116 void ServiceNode::accept(Visitor *visitor)
117 {
118   visitor->visitServiceNode(this);
119 }
120
121 //! Return the associated component instance
122 ComponentInstance *ServiceNode::getComponent()
123 {
124   return _component;
125 }
126
127 const ComponentInstance *ServiceNode::getComponent() const
128 {
129   return _component;
130 }
131
132 //! Return the associated container
133 Container *ServiceNode::getContainer()
134 {
135   if(_component)return _component->getContainer();
136   return 0;
137 }
138
139 //! By definition of ServiceNode class.
140 bool ServiceNode::isDeployable() const
141 {
142   return true;
143 }
144
145 //! Associate an existing component instance to this service node \b AND check the consistency regarding the deployment from root node point of view.
146 void ServiceNode::setComponent(ComponentInstance* compo)
147 {
148   DEBTRACE("ServiceNode::setComponent " << compo);
149   if(compo)
150     {
151       DEBTRACE(compo->getInstanceName());
152       if(compo->getKindForNode() != this->getKind())
153         {
154           //Not allowed
155           std::string what("ServiceNode::setComponent : component instance kind not allowed ");
156           throw Exception(what);
157         }
158     }
159
160   ComponentInstance* oldcompo=_component;
161   std::string oldref=_ref;
162
163   _component=compo;
164   _ref=compo->getCompoName();
165   DEBTRACE(_component->getInstanceName());
166   if(_component)
167     {
168       if(_father)
169         try
170           {
171             getRootNode()->checkDeploymentTree(false);
172           }
173         catch(Exception& e)
174           {
175             // Impossible to associate compo to this node. Keep the old component instance and throws exception
176             _component=oldcompo;
177             _ref=oldref;
178             throw e;
179           }
180       _component->incrRef();
181     }
182
183   if(oldcompo)
184     oldcompo->decrRef();
185 }
186
187 //! Associate a new component instance to this service node
188 /*!
189  * A new component instance with type name ref is created (from runtime
190  * factory createComponentInstance) and associated to the node.
191  *
192  */
193 void ServiceNode::setRef(const std::string& ref)
194 {
195   _ref = ref;
196   if(_component)
197     {
198       //The node is already associated with a component instance
199       _component->decrRef();
200       //Don't forget to unassociate
201     }
202   _component= getRuntime()->createComponentInstance(ref,getKind());
203   YASSERT(_component);
204 }
205
206 std::string ServiceNode::getRef()
207 {
208   return _ref;
209 }