Back to index

salome-kernel  6.5.0
study_exchange_vars.py
Go to the documentation of this file.
00001 # Copyright (C) 2011-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 #
00003 # This library is free software; you can redistribute it and/or
00004 # modify it under the terms of the GNU Lesser General Public
00005 # License as published by the Free Software Foundation; either
00006 # version 2.1 of the License.
00007 #
00008 # This library is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 # Lesser General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU Lesser General Public
00014 # License along with this library; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 #
00017 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 #
00019 
00020 """
00021 This module provides classes and functions to handle "Exchange Variables",
00022 i.e. description of variables to be exchanged between a supervision code and a
00023 computation code. These Exchange Variables can be stored in a SObject in
00024 Salome study.
00025 """
00026 
00027 from xml.dom.minidom import getDOMImplementation, parse
00028 from salome.kernel.studyedit import getStudyEditor
00029 
00030 DEFAULT_NAME = "Variables"
00031 INPUT_VAR_NAMES = "ExchangeVariables.InputVarNames"
00032 OUTPUT_VAR_NAMES = "ExchangeVariables.OutputVarNames"
00033 REF_ENTRY = "ExchangeVariables.RefEntry"
00034 
00035 class Variable:
00036     """
00037     This class describes a single variable. For now, it is only described by
00038     its name. Other attributes are reserved for future use.
00039     """
00040     
00041     def __init__(self, name, dimension = [], minValue = None, maxValue = None,
00042                  initialValue = None):
00043         self.name = name
00044         
00045         # Reserved for future use
00046         self.dimension = dimension
00047         self.minValue = minValue
00048         self.maxValue = maxValue
00049         self.initialValue = initialValue
00050 
00051 
00052 class ExchangeVariables:
00053     """
00054     This class describes "Exchange Variables", i.e. a structure containing all
00055     the necessary information to exchange variables between a supervision code
00056     and a computation code.
00057 
00058     .. attribute:: inputVarList
00059     
00060        This instance attribute is a list of :class:`Variable` objects,
00061        describing the input variables for the computation code.
00062 
00063     .. attribute:: outputVarList
00064     
00065        This instance attribute is a list of :class:`Variable` objects,
00066        describing the output variables for the computation code.
00067 
00068     .. attribute:: refEntry
00069 
00070        This instance attribute is optional and can be used to store a
00071        reference to a Salome entry containing an "Exchange Variable" SObject
00072        that was used to build this one (when the current object has been built
00073        by selecting variables of interest in a list of potential variables).
00074 
00075     """
00076     
00077     def __init__(self, inputVarList = [], outputVarList = [],
00078                  refEntry = None):
00079         self.inputVarList = inputVarList
00080         self.outputVarList = outputVarList
00081         self.refEntry = refEntry
00082 
00083     def saveToXmlFile(self, filepath):
00084         """
00085         Save this object to an XML file.
00086     
00087         :type  filepath: string
00088         :param filepath: path of the XML file.
00089     
00090         """
00091         doc = getDOMImplementation().createDocument(None, "variables", None)
00092         top_el = doc.documentElement
00093         top_el.setAttribute("version", "6.4")
00094         input_variables_el = doc.createElement("input_variables")
00095         top_el.appendChild(input_variables_el)
00096         output_variables_el = doc.createElement("output_variables")
00097         top_el.appendChild(output_variables_el)
00098         for input_var in self.inputVarList:
00099             input_var_el = doc.createElement("variable")
00100             input_var_el.setAttribute("name", input_var.name)
00101             input_variables_el.appendChild(input_var_el)
00102         for output_var in self.outputVarList:
00103             output_var_el = doc.createElement("variable")
00104             output_var_el.setAttribute("name", output_var.name)
00105             output_variables_el.appendChild(output_var_el)
00106         f = open(filepath, "w")
00107         f.write(doc.toprettyxml(indent = "  "))
00108         f.close()
00109 
00110 def createSObjectForExchangeVariables(fatherSobj, exchangeVariables,
00111                                       name = DEFAULT_NAME,
00112                                       icon = None, typeId = None):
00113     """
00114     Create a SObject to store an :class:`ExchangeVariables` instance.
00115 
00116     :type  fatherSobj: SObject
00117     :param fatherSobj: parent of the SObject to create.
00118 
00119     :type  exchangeVariables: :class:`ExchangeVariables`
00120     :param exchangeVariables: :class:`ExchangeVariables` instance to store in
00121                               Salome study.
00122 
00123     :type  name: string
00124     :param name: name of the SObject to create.
00125 
00126     :type  icon: string
00127     :param icon: icon of the SObject to create.
00128 
00129     :type  typeId: integer
00130     :param typeId: type of the SObject to create.
00131 
00132     :return: the newly created SObject.
00133 
00134     """
00135     studyId = fatherSobj.GetStudy()._get_StudyId()
00136     editor = getStudyEditor(studyId)
00137     sobj = editor.createItem(fatherSobj,
00138                              name = name,
00139                              icon = icon,
00140                              typeId = typeId)
00141     _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
00142 
00143 def updateSObjectForExchangeVariables(sobj, exchangeVariables,
00144                                       name = DEFAULT_NAME,
00145                                       icon = None, typeId = None):
00146     """
00147     Update an existing SObject storing an :class:`ExchangeVariables` instance.
00148 
00149     :type  sobj: SObject
00150     :param sobj: the SObject to update.
00151 
00152     See :func:`createSObjectForExchangeVariables` for the description of the
00153     other parameters.
00154 
00155     """
00156     studyId = sobj.GetStudy()._get_StudyId()
00157     editor = getStudyEditor(studyId)
00158     editor.setItem(sobj, name = name, icon = icon, typeId = typeId)
00159     editor.builder.RemoveAttribute(sobj, "AttributeParameter")
00160     _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
00161 
00162 def _setSObjectForExchangeVariables(editor, sobj, exchangeVariables):
00163     attr = editor.builder.FindOrCreateAttribute(sobj, "AttributeParameter")
00164     attr.SetStrArray(INPUT_VAR_NAMES,
00165                      [x.name for x in exchangeVariables.inputVarList])
00166     attr.SetStrArray(OUTPUT_VAR_NAMES,
00167                      [x.name for x in exchangeVariables.outputVarList])
00168     if exchangeVariables.refEntry is not None:
00169         attr.SetString(REF_ENTRY, exchangeVariables.refEntry)
00170 
00171 def getExchangeVariablesFromSObject(sobj):
00172     """
00173     Get an :class:`ExchangeVariables` instance from a SObject that stores it.
00174 
00175     :type  sobj: SObject
00176     :param sobj: the SObject from which to read the :class:`ExchangeVariables`
00177                  instance.
00178 
00179     :return: the newly created :class:`ExchangeVariables` instance.
00180 
00181     """
00182     (found, attr) = sobj.FindAttribute("AttributeParameter")
00183     if not found:
00184         return None
00185     refEntry = None
00186     if attr.IsSet(REF_ENTRY, 3):
00187         refEntry = attr.GetString(REF_ENTRY)
00188     return ExchangeVariables(
00189             [Variable(name) for name in attr.GetStrArray(INPUT_VAR_NAMES)],
00190             [Variable(name) for name in attr.GetStrArray(OUTPUT_VAR_NAMES)],
00191             refEntry)
00192 
00193 def loadExchangeVariablesFromXmlFile(filepath):
00194     """
00195     Load an :class:`ExchangeVariables` instance from an XML file.
00196 
00197     :type  filepath: string
00198     :param filepath: path of the XML file to load.
00199 
00200     :return: the newly created :class:`ExchangeVariables` instance.
00201 
00202     """
00203     doc = parse(filepath)
00204     top_el = doc.documentElement
00205     # Check version
00206     version = top_el.getAttribute("version")
00207     if version != "6.4":
00208         raise Exception(self.tr("Unsupported version: %s" % version))
00209     input_variables_el = top_el.getElementsByTagName("input_variables")[0]
00210     input_var_list = [Variable(input_var_el.getAttribute("name"))
00211                       for input_var_el
00212                       in input_variables_el.getElementsByTagName("variable")]
00213     output_variables_el = top_el.getElementsByTagName("output_variables")[0]
00214     output_var_list = [Variable(output_var_el.getAttribute("name"))
00215                        for output_var_el
00216                        in output_variables_el.getElementsByTagName("variable")]
00217     return ExchangeVariables(input_var_list, output_var_list)