]> SALOME platform Git repositories - modules/kernel.git/blob - src/KERNEL_PY/kernel/parametric/study_exchange_vars.py
Salome HOME
4b15c1695f9ffc2c49c02ecf9f0625e88cb0b43b
[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 """
21 This module provides classes and functions to handle "Exchange Variables",
22 i.e. description of variables to be exchanged between a supervision code and a
23 computation code. These Exchange Variables can be stored in a SObject in
24 Salome study.
25 """
26
27 from xml.dom.minidom import getDOMImplementation, parse
28 from salome.kernel.studyedit import getStudyEditor
29
30 DEFAULT_NAME = "Variables"
31 INPUT_VAR_NAMES = "ExchangeVariables.InputVarNames"
32 OUTPUT_VAR_NAMES = "ExchangeVariables.OutputVarNames"
33 REF_ENTRY = "ExchangeVariables.RefEntry"
34
35 class Variable:
36     """
37     This class describes a single variable. For now, it is only described by
38     its name. Other attributes are reserved for future use.
39     """
40     
41     def __init__(self, name, dimension = [], minValue = None, maxValue = None,
42                  initialValue = None):
43         self.name = name
44         
45         # Reserved for future use
46         self.dimension = dimension
47         self.minValue = minValue
48         self.maxValue = maxValue
49         self.initialValue = initialValue
50
51
52 class ExchangeVariables:
53     """
54     This class describes "Exchange Variables", i.e. a structure containing all
55     the necessary information to exchange variables between a supervision code
56     and a computation code.
57
58     .. attribute:: inputVarList
59     
60        This instance attribute is a list of :class:`Variable` objects,
61        describing the input variables for the computation code.
62
63     .. attribute:: outputVarList
64     
65        This instance attribute is a list of :class:`Variable` objects,
66        describing the output variables for the computation code.
67
68     .. attribute:: refEntry
69
70        This instance attribute is optional and can be used to store a
71        reference to a Salome entry containing an "Exchange Variable" SObject
72        that was used to build this one (when the current object has been built
73        by selecting variables of interest in a list of potential variables).
74
75     """
76     
77     def __init__(self, inputVarList = [], outputVarList = [],
78                  refEntry = None):
79         self.inputVarList = inputVarList
80         self.outputVarList = outputVarList
81         self.refEntry = refEntry
82
83     def saveToXmlFile(self, filepath):
84         """
85         Save this object to an XML file.
86     
87         :type  filepath: string
88         :param filepath: path of the XML file.
89     
90         """
91         doc = getDOMImplementation().createDocument(None, "variables", None)
92         top_el = doc.documentElement
93         top_el.setAttribute("version", "6.4")
94         input_variables_el = doc.createElement("input_variables")
95         top_el.appendChild(input_variables_el)
96         output_variables_el = doc.createElement("output_variables")
97         top_el.appendChild(output_variables_el)
98         for input_var in self.inputVarList:
99             input_var_el = doc.createElement("variable")
100             input_var_el.setAttribute("name", input_var.name)
101             input_variables_el.appendChild(input_var_el)
102         for output_var in self.outputVarList:
103             output_var_el = doc.createElement("variable")
104             output_var_el.setAttribute("name", output_var.name)
105             output_variables_el.appendChild(output_var_el)
106         f = open(filepath, "w")
107         f.write(doc.toprettyxml(indent = "  "))
108         f.close()
109
110 def createSObjectForExchangeVariables(fatherSobj, exchangeVariables,
111                                       name = DEFAULT_NAME,
112                                       icon = None, typeId = None):
113     """
114     Create a SObject to store an :class:`ExchangeVariables` instance.
115
116     :type  fatherSobj: SObject
117     :param fatherSobj: parent of the SObject to create.
118
119     :type  exchangeVariables: :class:`ExchangeVariables`
120     :param exchangeVariables: :class:`ExchangeVariables` instance to store in
121                               Salome study.
122
123     :type  name: string
124     :param name: name of the SObject to create.
125
126     :type  icon: string
127     :param icon: icon of the SObject to create.
128
129     :type  typeId: integer
130     :param typeId: type of the SObject to create.
131
132     :return: the newly created SObject.
133
134     """
135     studyId = fatherSobj.GetStudy()._get_StudyId()
136     editor = getStudyEditor(studyId)
137     sobj = editor.createItem(fatherSobj,
138                              name = name,
139                              icon = icon,
140                              typeId = typeId)
141     _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
142
143 def updateSObjectForExchangeVariables(sobj, exchangeVariables,
144                                       name = DEFAULT_NAME,
145                                       icon = None, typeId = None):
146     """
147     Update an existing SObject storing an :class:`ExchangeVariables` instance.
148
149     :type  sobj: SObject
150     :param sobj: the SObject to update.
151
152     See :func:`createSObjectForExchangeVariables` for the description of the
153     other parameters.
154
155     """
156     studyId = sobj.GetStudy()._get_StudyId()
157     editor = getStudyEditor(studyId)
158     editor.setItem(sobj, name = name, icon = icon, typeId = typeId)
159     editor.builder.RemoveAttribute(sobj, "AttributeParameter")
160     _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
161
162 def _setSObjectForExchangeVariables(editor, sobj, exchangeVariables):
163     attr = editor.builder.FindOrCreateAttribute(sobj, "AttributeParameter")
164     attr.SetStrArray(INPUT_VAR_NAMES,
165                      [x.name for x in exchangeVariables.inputVarList])
166     attr.SetStrArray(OUTPUT_VAR_NAMES,
167                      [x.name for x in exchangeVariables.outputVarList])
168     if exchangeVariables.refEntry is not None:
169         attr.SetString(REF_ENTRY, exchangeVariables.refEntry)
170
171 def getExchangeVariablesFromSObject(sobj):
172     """
173     Get an :class:`ExchangeVariables` instance from a SObject that stores it.
174
175     :type  sobj: SObject
176     :param sobj: the SObject from which to read the :class:`ExchangeVariables`
177                  instance.
178
179     :return: the newly created :class:`ExchangeVariables` instance.
180
181     """
182     (found, attr) = sobj.FindAttribute("AttributeParameter")
183     if not found:
184         return None
185     refEntry = None
186     if attr.IsSet(REF_ENTRY, 3):
187         refEntry = attr.GetString(REF_ENTRY)
188     return ExchangeVariables(
189             [Variable(name) for name in attr.GetStrArray(INPUT_VAR_NAMES)],
190             [Variable(name) for name in attr.GetStrArray(OUTPUT_VAR_NAMES)],
191             refEntry)
192
193 def loadExchangeVariablesFromXmlFile(filepath):
194     """
195     Load an :class:`ExchangeVariables` instance from an XML file.
196
197     :type  filepath: string
198     :param filepath: path of the XML file to load.
199
200     :return: the newly created :class:`ExchangeVariables` instance.
201
202     """
203     doc = parse(filepath)
204     top_el = doc.documentElement
205     # Check version
206     version = top_el.getAttribute("version")
207     if version != "6.4":
208         raise Exception(self.tr("Unsupported version: %s" % version))
209     input_variables_el = top_el.getElementsByTagName("input_variables")[0]
210     input_var_list = [Variable(input_var_el.getAttribute("name"))
211                       for input_var_el
212                       in input_variables_el.getElementsByTagName("variable")]
213     output_variables_el = top_el.getElementsByTagName("output_variables")[0]
214     output_var_list = [Variable(output_var_el.getAttribute("name"))
215                        for output_var_el
216                        in output_variables_el.getElementsByTagName("variable")]
217     return ExchangeVariables(input_var_list, output_var_list)