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 = [], minValue = None, maxValue = None,
57 # Reserved for future use
58 self.dimension = dimension
59 self.minValue = minValue
60 self.maxValue = maxValue
61 self.initialValue = initialValue
63 ## This class describes "Exchange Variables", i.e. a structure containing all
64 # the necessary information to exchange variables between a supervision code
65 # and a computation code.
67 # \param inputVarList This instance attribute is a list of \b Variable objects,
68 # describing the input variables for the computation code.
70 # \param outputVarList This instance attribute is a list of \b Variable objects,
71 # describing the output variables for the computation code.
73 # \param refEntry This instance attribute is optional and can be used to store a
74 # reference to a Salome entry containing an "Exchange Variable" SObject
75 # that was used to build this one (when the current object has been built
76 # by selecting variables of interest in a list of potential variables).
77 # \ingroup study_exchange_vars
78 class ExchangeVariables:
80 This class describes "Exchange Variables", i.e. a structure containing all
81 the necessary information to exchange variables between a supervision code
82 and a computation code.
84 .. attribute:: inputVarList
86 This instance attribute is a list of :class:`Variable` objects,
87 describing the input variables for the computation code.
89 .. attribute:: outputVarList
91 This instance attribute is a list of :class:`Variable` objects,
92 describing the output variables for the computation code.
94 .. attribute:: refEntry
96 This instance attribute is optional and can be used to store a
97 reference to a Salome entry containing an "Exchange Variable" SObject
98 that was used to build this one (when the current object has been built
99 by selecting variables of interest in a list of potential variables).
103 def __init__(self, inputVarList = [], outputVarList = [],
105 self.inputVarList = inputVarList
106 self.outputVarList = outputVarList
107 self.refEntry = refEntry
109 ## Save this object to an XML file.
111 # \param filepath (string) path of the XML file.
112 def saveToXmlFile(self, filepath):
114 Save this object to an XML file.
116 :type filepath: string
117 :param filepath: path of the XML file.
120 doc = getDOMImplementation().createDocument(None, "variables", None)
121 top_el = doc.documentElement
122 top_el.setAttribute("version", "6.4")
123 input_variables_el = doc.createElement("input_variables")
124 top_el.appendChild(input_variables_el)
125 output_variables_el = doc.createElement("output_variables")
126 top_el.appendChild(output_variables_el)
127 for input_var in self.inputVarList:
128 input_var_el = doc.createElement("variable")
129 input_var_el.setAttribute("name", input_var.name)
130 input_variables_el.appendChild(input_var_el)
131 for output_var in self.outputVarList:
132 output_var_el = doc.createElement("variable")
133 output_var_el.setAttribute("name", output_var.name)
134 output_variables_el.appendChild(output_var_el)
135 f = open(filepath, "w")
136 f.write(doc.toprettyxml(indent = " "))
139 ## Create a SObject to store an \b ExchangeVariables instance.
141 # \param fatherSobj (SObject) parent of the SObject to create.
143 # \param exchangeVariables (ExchangeVariables) \b ExchangeVariables instance to store in
146 # \param name (string) name of the SObject to create.
148 # \param icon (string) icon of the SObject to create.
150 # \param typeId (integer) type of the SObject to create.
152 # \return the newly created SObject.
153 # \ingroup study_exchange_vars
154 def createSObjectForExchangeVariables(fatherSobj, exchangeVariables,
156 icon = None, typeId = None):
158 Create a SObject to store an :class:`ExchangeVariables` instance.
160 :type fatherSobj: SObject
161 :param fatherSobj: parent of the SObject to create.
163 :type exchangeVariables: :class:`ExchangeVariables`
164 :param exchangeVariables: :class:`ExchangeVariables` instance to store in
168 :param name: name of the SObject to create.
171 :param icon: icon of the SObject to create.
173 :type typeId: integer
174 :param typeId: type of the SObject to create.
176 :return: the newly created SObject.
179 studyId = fatherSobj.GetStudy()._get_StudyId()
180 editor = getStudyEditor(studyId)
181 sobj = editor.createItem(fatherSobj,
185 _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
187 ## Update an existing SObject storing an \b ExchangeVariables instance.
189 # \param sobj (SObject) the SObject to update.
191 # See \b createSObjectForExchangeVariables() for the description of the
193 # \ingroup study_exchange_vars
194 def updateSObjectForExchangeVariables(sobj, exchangeVariables,
196 icon = None, typeId = None):
198 Update an existing SObject storing an :class:`ExchangeVariables` instance.
201 :param sobj: the SObject to update.
203 See :func:`createSObjectForExchangeVariables` for the description of the
207 studyId = sobj.GetStudy()._get_StudyId()
208 editor = getStudyEditor(studyId)
209 editor.setItem(sobj, name = name, icon = icon, typeId = typeId)
210 editor.builder.RemoveAttribute(sobj, "AttributeParameter")
211 _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
213 def _setSObjectForExchangeVariables(editor, sobj, exchangeVariables):
214 attr = editor.builder.FindOrCreateAttribute(sobj, "AttributeParameter")
215 attr.SetStrArray(INPUT_VAR_NAMES,
216 [x.name for x in exchangeVariables.inputVarList])
217 attr.SetStrArray(OUTPUT_VAR_NAMES,
218 [x.name for x in exchangeVariables.outputVarList])
219 if exchangeVariables.refEntry is not None:
220 attr.SetString(REF_ENTRY, exchangeVariables.refEntry)
222 ## Get an \b ExchangeVariables instance from a SObject that stores it.
224 # \param sobj (SObject) the SObject from which to read the \b ExchangeVariables
227 # \return the newly created \b ExchangeVariables instance.
228 # \ingroup study_exchange_vars
229 def getExchangeVariablesFromSObject(sobj):
231 Get an :class:`ExchangeVariables` instance from a SObject that stores it.
234 :param sobj: the SObject from which to read the :class:`ExchangeVariables`
237 :return: the newly created :class:`ExchangeVariables` instance.
240 (found, attr) = sobj.FindAttribute("AttributeParameter")
244 if attr.IsSet(REF_ENTRY, 3):
245 refEntry = attr.GetString(REF_ENTRY)
246 return ExchangeVariables(
247 [Variable(name) for name in attr.GetStrArray(INPUT_VAR_NAMES)],
248 [Variable(name) for name in attr.GetStrArray(OUTPUT_VAR_NAMES)],
251 ## Load an \b ExchangeVariables instance from an XML file.
253 # \param filepath (string) path of the XML file to load.
255 # \return the newly created \b ExchangeVariables instance.
256 # \ingroup study_exchange_vars
257 def loadExchangeVariablesFromXmlFile(filepath):
259 Load an :class:`ExchangeVariables` instance from an XML file.
261 :type filepath: string
262 :param filepath: path of the XML file to load.
264 :return: the newly created :class:`ExchangeVariables` instance.
267 doc = parse(filepath)
268 top_el = doc.documentElement
270 version = top_el.getAttribute("version")
272 raise Exception(self.tr("Unsupported version: %s" % version))
273 input_variables_el = top_el.getElementsByTagName("input_variables")[0]
274 input_var_list = [Variable(input_var_el.getAttribute("name"))
276 in input_variables_el.getElementsByTagName("variable")]
277 output_variables_el = top_el.getElementsByTagName("output_variables")[0]
278 output_var_list = [Variable(output_var_el.getAttribute("name"))
280 in output_variables_el.getElementsByTagName("variable")]
281 return ExchangeVariables(input_var_list, output_var_list)