Salome HOME
updated copyright message
[modules/kernel.git] / src / KERNEL_PY / kernel / parametric / study_exchange_vars.py
1 # Copyright (C) 2011-2023  CEA, EDF, OPEN CASCADE
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 ## \defgroup study_exchange_vars study_exchange_vars
21 #  \{ 
22 #  \details
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
26 #  Salome study.
27 #  \}
28
29 """
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
33 Salome study.
34 """
35
36 from xml.dom.minidom import getDOMImplementation, parse
37 from salome.kernel.studyedit import getStudyEditor
38
39 DEFAULT_NAME = "Variables"
40 INPUT_VAR_NAMES = "ExchangeVariables.InputVarNames"
41 OUTPUT_VAR_NAMES = "ExchangeVariables.OutputVarNames"
42 REF_ENTRY = "ExchangeVariables.RefEntry"
43
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
47 class Variable:
48     """
49     This class describes a single variable. For now, it is only described by
50     its name. Other attributes are reserved for future use.
51     """
52     
53     def __init__(self, name, dimension = None, minValue = None, maxValue = None,
54                  initialValue = None):
55         if dimension is None:
56             dimension = []
57         self.name = name
58         
59         # Reserved for future use
60         self.dimension = dimension
61         self.minValue = minValue
62         self.maxValue = maxValue
63         self.initialValue = initialValue
64
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.
68 #
69 #  \param inputVarList This instance attribute is a list of \b Variable objects,
70 #  describing the input variables for the computation code.
71 #
72 #  \param outputVarList This instance attribute is a list of \b Variable objects,
73 #  describing the output variables for the computation code.
74 #
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:
81     """
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.
85
86     .. attribute:: inputVarList
87     
88        This instance attribute is a list of :class:`Variable` objects,
89        describing the input variables for the computation code.
90
91     .. attribute:: outputVarList
92     
93        This instance attribute is a list of :class:`Variable` objects,
94        describing the output variables for the computation code.
95
96     .. attribute:: refEntry
97
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).
102
103     """
104     
105     def __init__(self, inputVarList = None, outputVarList = None,
106                  refEntry = None):
107         if inputVarList is None:
108             inputVarList = []
109         if outputVarList is None:
110             outputVarList = []
111         self.inputVarList = inputVarList
112         self.outputVarList = outputVarList
113         self.refEntry = refEntry
114
115     ## Save this object to an XML file.
116     #
117     #  \param filepath (string) path of the XML file.
118     def saveToXmlFile(self, filepath):
119         """
120         Save this object to an XML file.
121     
122         :type  filepath: string
123         :param filepath: path of the XML file.
124     
125         """
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 = "  "))
143         f.close()
144
145 ## Create a SObject to store an \b ExchangeVariables instance.
146 #
147 #  \param fatherSobj (SObject) parent of the SObject to create.
148 #
149 #  \param exchangeVariables (ExchangeVariables) \b ExchangeVariables instance to store in
150 #  Salome study.
151 #
152 #  \param name (string) name of the SObject to create.
153 #
154 #  \param icon (string) icon of the SObject to create.
155 #
156 #  \param typeId (integer) type of the SObject to create.
157 #
158 #  \return the newly created SObject.
159 #  \ingroup study_exchange_vars
160 def createSObjectForExchangeVariables(fatherSobj, exchangeVariables,
161                                       name = DEFAULT_NAME,
162                                       icon = None, typeId = None):
163     """
164     Create a SObject to store an :class:`ExchangeVariables` instance.
165
166     :type  fatherSobj: SObject
167     :param fatherSobj: parent of the SObject to create.
168
169     :type  exchangeVariables: :class:`ExchangeVariables`
170     :param exchangeVariables: :class:`ExchangeVariables` instance to store in
171                               Salome study.
172
173     :type  name: string
174     :param name: name of the SObject to create.
175
176     :type  icon: string
177     :param icon: icon of the SObject to create.
178
179     :type  typeId: integer
180     :param typeId: type of the SObject to create.
181
182     :return: the newly created SObject.
183
184     """
185     editor = getStudyEditor()
186     sobj = editor.createItem(fatherSobj,
187                              name = name,
188                              icon = icon,
189                              typeId = typeId)
190     _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
191
192 ## Update an existing SObject storing an \b ExchangeVariables instance.
193 #
194 #  \param sobj (SObject) the SObject to update.
195 #
196 #  See \b createSObjectForExchangeVariables() for the description of the
197 #  other parameters.
198 #  \ingroup study_exchange_vars
199 def updateSObjectForExchangeVariables(sobj, exchangeVariables,
200                                       name = DEFAULT_NAME,
201                                       icon = None, typeId = None):
202     """
203     Update an existing SObject storing an :class:`ExchangeVariables` instance.
204
205     :type  sobj: SObject
206     :param sobj: the SObject to update.
207
208     See :func:`createSObjectForExchangeVariables` for the description of the
209     other parameters.
210
211     """
212     editor = getStudyEditor()
213     editor.setItem(sobj, name = name, icon = icon, typeId = typeId)
214     editor.builder.RemoveAttribute(sobj, "AttributeParameter")
215     _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
216
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)
225
226 ## Get an \b ExchangeVariables instance from a SObject that stores it.
227 #
228 #  \param sobj (SObject) the SObject from which to read the \b ExchangeVariables
229 #  instance.
230 #
231 #  \return the newly created \b ExchangeVariables instance.
232 #  \ingroup study_exchange_vars
233 def getExchangeVariablesFromSObject(sobj):
234     """
235     Get an :class:`ExchangeVariables` instance from a SObject that stores it.
236
237     :type  sobj: SObject
238     :param sobj: the SObject from which to read the :class:`ExchangeVariables`
239                  instance.
240
241     :return: the newly created :class:`ExchangeVariables` instance.
242
243     """
244     (found, attr) = sobj.FindAttribute("AttributeParameter")
245     if not found:
246         return None
247     refEntry = None
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)],
253             refEntry)
254
255 ## Load an \b ExchangeVariables instance from an XML file.
256 #
257 #  \param filepath (string) path of the XML file to load.
258 #
259 #  \return the newly created \b ExchangeVariables instance.
260 #  \ingroup study_exchange_vars
261 def loadExchangeVariablesFromXmlFile(filepath):
262     """
263     Load an :class:`ExchangeVariables` instance from an XML file.
264
265     :type  filepath: string
266     :param filepath: path of the XML file to load.
267
268     :return: the newly created :class:`ExchangeVariables` instance.
269
270     """
271     doc = parse(filepath)
272     top_el = doc.documentElement
273     # Check version
274     version = top_el.getAttribute("version")
275     if version != "6.4":
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"))
279                       for input_var_el
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"))
283                        for output_var_el
284                        in output_variables_el.getElementsByTagName("variable")]
285     return ExchangeVariables(input_var_list, output_var_list)