Salome HOME
Added doc for salome.kernel.varlist module
[modules/kernel.git] / src / KERNEL_PY / kernel / varlist.py
1 #  Copyright (C) 2011  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.
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
18 #  email : webmaster.salome@opencascade.com
19 #
20
21 """
22 This module provides classes and functions to handle "Exchange Variables",
23 i.e. description of variables to be exchanged between a supervision code and a
24 computation code. These Exchange Variables can be stored in a SObject in
25 Salome study.
26 """
27
28 from 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
84 def createSObjectForExchangeVariables(fatherSobj, exchangeVariables,
85                                       name = DEFAULT_NAME,
86                                       icon = None, typeId = None):
87     """
88     Create a SObject to store an :class:`ExchangeVariables` instance.
89
90     :type  fatherSobj: SObject
91     :param fatherSobj: parent of the SObject to create.
92
93     :type  exchangeVariables: :class:`ExchangeVariables`
94     :param exchangeVariables: :class:`ExchangeVariables` instance to store in
95                               Salome study.
96
97     :type  name: string
98     :param name: name of the SObject to create.
99
100     :type  icon: string
101     :param icon: icon of the SObject to create.
102
103     :type  typeId: integer
104     :param typeId: type of the SObject to create.
105
106     :return: the newly created SObject.
107
108     """
109     studyId = fatherSobj.GetStudy()._get_StudyId()
110     editor = getStudyEditor(studyId)
111     sobj = editor.createItem(fatherSobj,
112                              name = name,
113                              icon = icon,
114                              typeId = typeId)
115     _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
116
117 def updateSObjectForExchangeVariables(sobj, exchangeVariables,
118                                       name = DEFAULT_NAME,
119                                       icon = None, typeId = None):
120     """
121     Update an existing SObject storing an :class:`ExchangeVariables` instance.
122
123     :type  sobj: SObject
124     :param sobj: the SObject to update.
125
126     See :func:`createSObjectForExchangeVariables` for the description of the
127     other parameters.
128
129     """
130     studyId = sobj.GetStudy()._get_StudyId()
131     editor = getStudyEditor(studyId)
132     editor.setItem(sobj, name = name, icon = icon, typeId = typeId)
133     editor.builder.RemoveAttribute(sobj, "AttributeParameter")
134     _setSObjectForExchangeVariables(editor, sobj, exchangeVariables)
135
136 def _setSObjectForExchangeVariables(editor, sobj, exchangeVariables):
137     attr = editor.builder.FindOrCreateAttribute(sobj, "AttributeParameter")
138     attr.SetStrArray(INPUT_VAR_NAMES,
139                      [x.name for x in exchangeVariables.inputVarList])
140     attr.SetStrArray(OUTPUT_VAR_NAMES,
141                      [x.name for x in exchangeVariables.outputVarList])
142     if exchangeVariables.refEntry is not None:
143         attr.SetString(REF_ENTRY, exchangeVariables.refEntry)
144
145 def getExchangeVariablesFromSObject(sobj):
146     """
147     Get an :class:`ExchangeVariables` instance from a SObject that stores it.
148
149     :type  sobj: SObject
150     :param sobj: the SObject from which to read the :class:`ExchangeVariables`
151                  instance.
152
153     :return: the newly created :class:`ExchangeVariables` instance.
154
155     """
156     (found, attr) = sobj.FindAttribute("AttributeParameter")
157     if not found:
158         return None
159     refEntry = None
160     if attr.IsSet(REF_ENTRY, 3):
161         refEntry = attr.GetString(REF_ENTRY)
162     return ExchangeVariables(
163             [Variable(name) for name in attr.GetStrArray(INPUT_VAR_NAMES)],
164             [Variable(name) for name in attr.GetStrArray(OUTPUT_VAR_NAMES)],
165             refEntry)