1 # Copyright (C) 2011-2014 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 studyId = fatherSobj.GetStudy()._get_StudyId()
186 editor = getStudyEditor(studyId)
187 sobj = editor.createItem(fatherSobj,
191 _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
193 ## Update an existing SObject storing an \b ExchangeVariables instance.
195 # \param sobj (SObject) the SObject to update.
197 # See \b createSObjectForExchangeVariables() for the description of the
199 # \ingroup study_exchange_vars
200 def updateSObjectForExchangeVariables(sobj, exchangeVariables,
202 icon = None, typeId = None):
204 Update an existing SObject storing an :class:`ExchangeVariables` instance.
207 :param sobj: the SObject to update.
209 See :func:`createSObjectForExchangeVariables` for the description of the
213 studyId = sobj.GetStudy()._get_StudyId()
214 editor = getStudyEditor(studyId)
215 editor.setItem(sobj, name = name, icon = icon, typeId = typeId)
216 editor.builder.RemoveAttribute(sobj, "AttributeParameter")
217 _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
219 def _setSObjectForExchangeVariables(editor, sobj, exchangeVariables):
220 attr = editor.builder.FindOrCreateAttribute(sobj, "AttributeParameter")
221 attr.SetStrArray(INPUT_VAR_NAMES,
222 [x.name for x in exchangeVariables.inputVarList])
223 attr.SetStrArray(OUTPUT_VAR_NAMES,
224 [x.name for x in exchangeVariables.outputVarList])
225 if exchangeVariables.refEntry is not None:
226 attr.SetString(REF_ENTRY, exchangeVariables.refEntry)
228 ## Get an \b ExchangeVariables instance from a SObject that stores it.
230 # \param sobj (SObject) the SObject from which to read the \b ExchangeVariables
233 # \return the newly created \b ExchangeVariables instance.
234 # \ingroup study_exchange_vars
235 def getExchangeVariablesFromSObject(sobj):
237 Get an :class:`ExchangeVariables` instance from a SObject that stores it.
240 :param sobj: the SObject from which to read the :class:`ExchangeVariables`
243 :return: the newly created :class:`ExchangeVariables` instance.
246 (found, attr) = sobj.FindAttribute("AttributeParameter")
250 if attr.IsSet(REF_ENTRY, 3):
251 refEntry = attr.GetString(REF_ENTRY)
252 return ExchangeVariables(
253 [Variable(name) for name in attr.GetStrArray(INPUT_VAR_NAMES)],
254 [Variable(name) for name in attr.GetStrArray(OUTPUT_VAR_NAMES)],
257 ## Load an \b ExchangeVariables instance from an XML file.
259 # \param filepath (string) path of the XML file to load.
261 # \return the newly created \b ExchangeVariables instance.
262 # \ingroup study_exchange_vars
263 def loadExchangeVariablesFromXmlFile(filepath):
265 Load an :class:`ExchangeVariables` instance from an XML file.
267 :type filepath: string
268 :param filepath: path of the XML file to load.
270 :return: the newly created :class:`ExchangeVariables` instance.
273 doc = parse(filepath)
274 top_el = doc.documentElement
276 version = top_el.getAttribute("version")
278 raise Exception(self.tr("Unsupported version: %s" % version))
279 input_variables_el = top_el.getElementsByTagName("input_variables")[0]
280 input_var_list = [Variable(input_var_el.getAttribute("name"))
282 in input_variables_el.getElementsByTagName("variable")]
283 output_variables_el = top_el.getElementsByTagName("output_variables")[0]
284 output_var_list = [Variable(output_var_el.getAttribute("name"))
286 in output_variables_el.getElementsByTagName("variable")]
287 return ExchangeVariables(input_var_list, output_var_list)