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
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
27 from xml.dom.minidom import getDOMImplementation, parse
28 from salome.kernel.studyedit import getStudyEditor
30 DEFAULT_NAME = "Variables"
31 INPUT_VAR_NAMES = "ExchangeVariables.InputVarNames"
32 OUTPUT_VAR_NAMES = "ExchangeVariables.OutputVarNames"
33 REF_ENTRY = "ExchangeVariables.RefEntry"
37 This class describes a single variable. For now, it is only described by
38 its name. Other attributes are reserved for future use.
41 def __init__(self, name, dimension = [], minValue = None, maxValue = None,
45 # Reserved for future use
46 self.dimension = dimension
47 self.minValue = minValue
48 self.maxValue = maxValue
49 self.initialValue = initialValue
52 class ExchangeVariables:
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.
58 .. attribute:: inputVarList
60 This instance attribute is a list of :class:`Variable` objects,
61 describing the input variables for the computation code.
63 .. attribute:: outputVarList
65 This instance attribute is a list of :class:`Variable` objects,
66 describing the output variables for the computation code.
68 .. attribute:: refEntry
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).
77 def __init__(self, inputVarList = [], outputVarList = [],
79 self.inputVarList = inputVarList
80 self.outputVarList = outputVarList
81 self.refEntry = refEntry
83 def saveToXmlFile(self, filepath):
85 Save this object to an XML file.
87 :type filepath: string
88 :param filepath: path of the XML file.
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 = " "))
110 def createSObjectForExchangeVariables(fatherSobj, exchangeVariables,
112 icon = None, typeId = None):
114 Create a SObject to store an :class:`ExchangeVariables` instance.
116 :type fatherSobj: SObject
117 :param fatherSobj: parent of the SObject to create.
119 :type exchangeVariables: :class:`ExchangeVariables`
120 :param exchangeVariables: :class:`ExchangeVariables` instance to store in
124 :param name: name of the SObject to create.
127 :param icon: icon of the SObject to create.
129 :type typeId: integer
130 :param typeId: type of the SObject to create.
132 :return: the newly created SObject.
135 studyId = fatherSobj.GetStudy()._get_StudyId()
136 editor = getStudyEditor(studyId)
137 sobj = editor.createItem(fatherSobj,
141 _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
143 def updateSObjectForExchangeVariables(sobj, exchangeVariables,
145 icon = None, typeId = None):
147 Update an existing SObject storing an :class:`ExchangeVariables` instance.
150 :param sobj: the SObject to update.
152 See :func:`createSObjectForExchangeVariables` for the description of the
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)
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)
171 def getExchangeVariablesFromSObject(sobj):
173 Get an :class:`ExchangeVariables` instance from a SObject that stores it.
176 :param sobj: the SObject from which to read the :class:`ExchangeVariables`
179 :return: the newly created :class:`ExchangeVariables` instance.
182 (found, attr) = sobj.FindAttribute("AttributeParameter")
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)],
193 def loadExchangeVariablesFromXmlFile(filepath):
195 Load an :class:`ExchangeVariables` instance from an XML file.
197 :type filepath: string
198 :param filepath: path of the XML file to load.
200 :return: the newly created :class:`ExchangeVariables` instance.
203 doc = parse(filepath)
204 top_el = doc.documentElement
206 version = top_el.getAttribute("version")
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"))
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"))
216 in output_variables_el.getElementsByTagName("variable")]
217 return ExchangeVariables(input_var_list, output_var_list)