Salome HOME
Revert "Synchronize adm files"
[modules/kernel.git] / src / KERNEL_PY / kernel / parametric / study_exchange_vars.py
1 # Copyright (C) 2011-2014  CEA/DEN, EDF R&D, 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 = [], minValue = None, maxValue = None,
54                  initialValue = None):
55         self.name = name
56         
57         # Reserved for future use
58         self.dimension = dimension
59         self.minValue = minValue
60         self.maxValue = maxValue
61         self.initialValue = initialValue
62
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.
66 #
67 #  \param inputVarList This instance attribute is a list of \b Variable objects,
68 #  describing the input variables for the computation code.
69 #
70 #  \param outputVarList This instance attribute is a list of \b Variable objects,
71 #  describing the output variables for the computation code.
72 #
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:
79     """
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.
83
84     .. attribute:: inputVarList
85     
86        This instance attribute is a list of :class:`Variable` objects,
87        describing the input variables for the computation code.
88
89     .. attribute:: outputVarList
90     
91        This instance attribute is a list of :class:`Variable` objects,
92        describing the output variables for the computation code.
93
94     .. attribute:: refEntry
95
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).
100
101     """
102     
103     def __init__(self, inputVarList = [], outputVarList = [],
104                  refEntry = None):
105         self.inputVarList = inputVarList
106         self.outputVarList = outputVarList
107         self.refEntry = refEntry
108
109     ## Save this object to an XML file.
110     #
111     #  \param filepath (string) path of the XML file.
112     def saveToXmlFile(self, filepath):
113         """
114         Save this object to an XML file.
115     
116         :type  filepath: string
117         :param filepath: path of the XML file.
118     
119         """
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 = "  "))
137         f.close()
138
139 ## Create a SObject to store an \b ExchangeVariables instance.
140 #
141 #  \param fatherSobj (SObject) parent of the SObject to create.
142 #
143 #  \param exchangeVariables (ExchangeVariables) \b ExchangeVariables instance to store in
144 #  Salome study.
145 #
146 #  \param name (string) name of the SObject to create.
147 #
148 #  \param icon (string) icon of the SObject to create.
149 #
150 #  \param typeId (integer) type of the SObject to create.
151 #
152 #  \return the newly created SObject.
153 #  \ingroup study_exchange_vars
154 def createSObjectForExchangeVariables(fatherSobj, exchangeVariables,
155                                       name = DEFAULT_NAME,
156                                       icon = None, typeId = None):
157     """
158     Create a SObject to store an :class:`ExchangeVariables` instance.
159
160     :type  fatherSobj: SObject
161     :param fatherSobj: parent of the SObject to create.
162
163     :type  exchangeVariables: :class:`ExchangeVariables`
164     :param exchangeVariables: :class:`ExchangeVariables` instance to store in
165                               Salome study.
166
167     :type  name: string
168     :param name: name of the SObject to create.
169
170     :type  icon: string
171     :param icon: icon of the SObject to create.
172
173     :type  typeId: integer
174     :param typeId: type of the SObject to create.
175
176     :return: the newly created SObject.
177
178     """
179     studyId = fatherSobj.GetStudy()._get_StudyId()
180     editor = getStudyEditor(studyId)
181     sobj = editor.createItem(fatherSobj,
182                              name = name,
183                              icon = icon,
184                              typeId = typeId)
185     _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
186
187 ## Update an existing SObject storing an \b ExchangeVariables instance.
188 #
189 #  \param sobj (SObject) the SObject to update.
190 #
191 #  See \b createSObjectForExchangeVariables() for the description of the
192 #  other parameters.
193 #  \ingroup study_exchange_vars
194 def updateSObjectForExchangeVariables(sobj, exchangeVariables,
195                                       name = DEFAULT_NAME,
196                                       icon = None, typeId = None):
197     """
198     Update an existing SObject storing an :class:`ExchangeVariables` instance.
199
200     :type  sobj: SObject
201     :param sobj: the SObject to update.
202
203     See :func:`createSObjectForExchangeVariables` for the description of the
204     other parameters.
205
206     """
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)
212
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)
221
222 ## Get an \b ExchangeVariables instance from a SObject that stores it.
223 #
224 #  \param sobj (SObject) the SObject from which to read the \b ExchangeVariables
225 #  instance.
226 #
227 #  \return the newly created \b ExchangeVariables instance.
228 #  \ingroup study_exchange_vars
229 def getExchangeVariablesFromSObject(sobj):
230     """
231     Get an :class:`ExchangeVariables` instance from a SObject that stores it.
232
233     :type  sobj: SObject
234     :param sobj: the SObject from which to read the :class:`ExchangeVariables`
235                  instance.
236
237     :return: the newly created :class:`ExchangeVariables` instance.
238
239     """
240     (found, attr) = sobj.FindAttribute("AttributeParameter")
241     if not found:
242         return None
243     refEntry = None
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)],
249             refEntry)
250
251 ## Load an \b ExchangeVariables instance from an XML file.
252 #
253 #  \param filepath (string) path of the XML file to load.
254 #
255 #  \return the newly created \b ExchangeVariables instance.
256 #  \ingroup study_exchange_vars
257 def loadExchangeVariablesFromXmlFile(filepath):
258     """
259     Load an :class:`ExchangeVariables` instance from an XML file.
260
261     :type  filepath: string
262     :param filepath: path of the XML file to load.
263
264     :return: the newly created :class:`ExchangeVariables` instance.
265
266     """
267     doc = parse(filepath)
268     top_el = doc.documentElement
269     # Check version
270     version = top_el.getAttribute("version")
271     if version != "6.4":
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"))
275                       for input_var_el
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"))
279                        for output_var_el
280                        in output_variables_el.getElementsByTagName("variable")]
281     return ExchangeVariables(input_var_list, output_var_list)