1 # Copyright (C) 2011-2023 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 ## \defgroup study_exchange_vars study_exchange_vars
23 # This module provides classes and functions to handle "Exchange Variables",
24 # i.e. description of variables to be exchanged between a supervision code and a
25 # computation code. These Exchange Variables can be stored in a SObject in
30 This module provides classes and functions to handle "Exchange Variables",
31 i.e. description of variables to be exchanged between a supervision code and a
32 computation code. These Exchange Variables can be stored in a SObject in
36 from xml.dom.minidom import getDOMImplementation, parse
37 from salome.kernel.studyedit import getStudyEditor
39 DEFAULT_NAME = "Variables"
40 INPUT_VAR_NAMES = "ExchangeVariables.InputVarNames"
41 OUTPUT_VAR_NAMES = "ExchangeVariables.OutputVarNames"
42 REF_ENTRY = "ExchangeVariables.RefEntry"
44 ## This class describes a single variable. For now, it is only described by
45 # its name. Other attributes are reserved for future use.
46 # \ingroup study_exchange_vars
49 This class describes a single variable. For now, it is only described by
50 its name. Other attributes are reserved for future use.
53 def __init__(self, name, dimension = None, minValue = None, maxValue = None,
59 # Reserved for future use
60 self.dimension = dimension
61 self.minValue = minValue
62 self.maxValue = maxValue
63 self.initialValue = initialValue
65 ## This class describes "Exchange Variables", i.e. a structure containing all
66 # the necessary information to exchange variables between a supervision code
67 # and a computation code.
69 # \param inputVarList This instance attribute is a list of \b Variable objects,
70 # describing the input variables for the computation code.
72 # \param outputVarList This instance attribute is a list of \b Variable objects,
73 # describing the output variables for the computation code.
75 # \param refEntry This instance attribute is optional and can be used to store a
76 # reference to a Salome entry containing an "Exchange Variable" SObject
77 # that was used to build this one (when the current object has been built
78 # by selecting variables of interest in a list of potential variables).
79 # \ingroup study_exchange_vars
80 class ExchangeVariables:
82 This class describes "Exchange Variables", i.e. a structure containing all
83 the necessary information to exchange variables between a supervision code
84 and a computation code.
86 .. attribute:: inputVarList
88 This instance attribute is a list of :class:`Variable` objects,
89 describing the input variables for the computation code.
91 .. attribute:: outputVarList
93 This instance attribute is a list of :class:`Variable` objects,
94 describing the output variables for the computation code.
96 .. attribute:: refEntry
98 This instance attribute is optional and can be used to store a
99 reference to a Salome entry containing an "Exchange Variable" SObject
100 that was used to build this one (when the current object has been built
101 by selecting variables of interest in a list of potential variables).
105 def __init__(self, inputVarList = None, outputVarList = None,
107 if inputVarList is None:
109 if outputVarList is None:
111 self.inputVarList = inputVarList
112 self.outputVarList = outputVarList
113 self.refEntry = refEntry
115 ## Save this object to an XML file.
117 # \param filepath (string) path of the XML file.
118 def saveToXmlFile(self, filepath):
120 Save this object to an XML file.
122 :type filepath: string
123 :param filepath: path of the XML file.
126 doc = getDOMImplementation().createDocument(None, "variables", None)
127 top_el = doc.documentElement
128 top_el.setAttribute("version", "6.4")
129 input_variables_el = doc.createElement("input_variables")
130 top_el.appendChild(input_variables_el)
131 output_variables_el = doc.createElement("output_variables")
132 top_el.appendChild(output_variables_el)
133 for input_var in self.inputVarList:
134 input_var_el = doc.createElement("variable")
135 input_var_el.setAttribute("name", input_var.name)
136 input_variables_el.appendChild(input_var_el)
137 for output_var in self.outputVarList:
138 output_var_el = doc.createElement("variable")
139 output_var_el.setAttribute("name", output_var.name)
140 output_variables_el.appendChild(output_var_el)
141 f = open(filepath, "w")
142 f.write(doc.toprettyxml(indent = " "))
145 ## Create a SObject to store an \b ExchangeVariables instance.
147 # \param fatherSobj (SObject) parent of the SObject to create.
149 # \param exchangeVariables (ExchangeVariables) \b ExchangeVariables instance to store in
152 # \param name (string) name of the SObject to create.
154 # \param icon (string) icon of the SObject to create.
156 # \param typeId (integer) type of the SObject to create.
158 # \return the newly created SObject.
159 # \ingroup study_exchange_vars
160 def createSObjectForExchangeVariables(fatherSobj, exchangeVariables,
162 icon = None, typeId = None):
164 Create a SObject to store an :class:`ExchangeVariables` instance.
166 :type fatherSobj: SObject
167 :param fatherSobj: parent of the SObject to create.
169 :type exchangeVariables: :class:`ExchangeVariables`
170 :param exchangeVariables: :class:`ExchangeVariables` instance to store in
174 :param name: name of the SObject to create.
177 :param icon: icon of the SObject to create.
179 :type typeId: integer
180 :param typeId: type of the SObject to create.
182 :return: the newly created SObject.
185 editor = getStudyEditor()
186 sobj = editor.createItem(fatherSobj,
190 _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
192 ## Update an existing SObject storing an \b ExchangeVariables instance.
194 # \param sobj (SObject) the SObject to update.
196 # See \b createSObjectForExchangeVariables() for the description of the
198 # \ingroup study_exchange_vars
199 def updateSObjectForExchangeVariables(sobj, exchangeVariables,
201 icon = None, typeId = None):
203 Update an existing SObject storing an :class:`ExchangeVariables` instance.
206 :param sobj: the SObject to update.
208 See :func:`createSObjectForExchangeVariables` for the description of the
212 editor = getStudyEditor()
213 editor.setItem(sobj, name = name, icon = icon, typeId = typeId)
214 editor.builder.RemoveAttribute(sobj, "AttributeParameter")
215 _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
217 def _setSObjectForExchangeVariables(editor, sobj, exchangeVariables):
218 attr = editor.builder.FindOrCreateAttribute(sobj, "AttributeParameter")
219 attr.SetStrArray(INPUT_VAR_NAMES,
220 [x.name for x in exchangeVariables.inputVarList])
221 attr.SetStrArray(OUTPUT_VAR_NAMES,
222 [x.name for x in exchangeVariables.outputVarList])
223 if exchangeVariables.refEntry is not None:
224 attr.SetString(REF_ENTRY, exchangeVariables.refEntry)
226 ## Get an \b ExchangeVariables instance from a SObject that stores it.
228 # \param sobj (SObject) the SObject from which to read the \b ExchangeVariables
231 # \return the newly created \b ExchangeVariables instance.
232 # \ingroup study_exchange_vars
233 def getExchangeVariablesFromSObject(sobj):
235 Get an :class:`ExchangeVariables` instance from a SObject that stores it.
238 :param sobj: the SObject from which to read the :class:`ExchangeVariables`
241 :return: the newly created :class:`ExchangeVariables` instance.
244 (found, attr) = sobj.FindAttribute("AttributeParameter")
248 if attr.IsSet(REF_ENTRY, 3):
249 refEntry = attr.GetString(REF_ENTRY)
250 return ExchangeVariables(
251 [Variable(name) for name in attr.GetStrArray(INPUT_VAR_NAMES)],
252 [Variable(name) for name in attr.GetStrArray(OUTPUT_VAR_NAMES)],
255 ## Load an \b ExchangeVariables instance from an XML file.
257 # \param filepath (string) path of the XML file to load.
259 # \return the newly created \b ExchangeVariables instance.
260 # \ingroup study_exchange_vars
261 def loadExchangeVariablesFromXmlFile(filepath):
263 Load an :class:`ExchangeVariables` instance from an XML file.
265 :type filepath: string
266 :param filepath: path of the XML file to load.
268 :return: the newly created :class:`ExchangeVariables` instance.
271 doc = parse(filepath)
272 top_el = doc.documentElement
274 version = top_el.getAttribute("version")
276 raise Exception(self.tr("Unsupported version: %s" % version))
277 input_variables_el = top_el.getElementsByTagName("input_variables")[0]
278 input_var_list = [Variable(input_var_el.getAttribute("name"))
280 in input_variables_el.getElementsByTagName("variable")]
281 output_variables_el = top_el.getElementsByTagName("output_variables")[0]
282 output_var_list = [Variable(output_var_el.getAttribute("name"))
284 in output_variables_el.getElementsByTagName("variable")]
285 return ExchangeVariables(input_var_list, output_var_list)