summaryrefslogtreecommitdiffstats
path: root/Monitoring/src/main/python/DataProcessing/Parameter.py
blob: d0f20268b5d341b810a04e95d356506e704af4bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from __future__ import with_statement
'''
Created on Oct 20, 2011

@author: steger, jozsef
@organization: ELTE
@contact: steger@complex.elte.hu
'''

from DataProcessing.Dimension import DimensionManager
from DataProcessing.DataError import ParameterError

class Parameter(object):
    '''
    @author: steger, jozsef
    @summary: 
    This class represents the control parameters of a monitoring task. 
    '''

    def __init__(self, name, valuetype, unitmanager, dimension, default = None):
        '''
        @summary: Constructor
        @param name: the name of the parameter
        @type name: str
        @param valuetype: the type of the parameter (used when reading value information)
        @type valuetype: type
@fixme: docs
        @param default: the preset unit aware value of the parameter 
        @type default: a tuple of value and unit
        '''
        self.um = unitmanager
        if not isinstance(dimension, DimensionManager.Dimension):
            raise ParameterError("wrong type of dimension")
        self._data = (name, valuetype, dimension)
        self._value = None
        if default is not None:
            self.value = default

    def __str__(self):
        if self._value is None:
            return "%s (%s)" % (self.name, self.dimension)
        else:
            return "%s (%s) = %s [%s] as %s" % (self.name, self.dimension.name, self._value[0], self._value[1], self.valuetype)
 
    def _get_name(self):
        return self._data[0]

    def _get_valuetype(self):
        return self._data[1]
    
    def _get_dimension(self):
        return self._data[2]

    def _get_value(self):
        return self._value
    def _set_value(self, value):
        _, unit = value
        if not self.dimension.containsUnit(unit):
            raise ParameterError("Unit %s is not in the basin of the dimension %s" % (unit, self.dimension))
        self._value = tuple(value)
    def _del_value(self):
        self._value = None
    
    def copy(self):
        return Parameter(name = self.name, valuetype = self.valuetype, unitmanager = self.um, dimension = self.dimension, default = self.value)
    
    def convert(self, unit):
        '''
        @summary: returns the value of the given parameter in the required unit
        @param unit: the requested unit, which must adhere to the unit model of this parameter
        @type unit: Unit
        @return: the parameter value represented in the requested units
        @rtype: 
        @raise ParameterError: Unit not in dimension basin / Unit is not initialized
        '''
        if not self.dimension.containsUnit(unit):
            raise ParameterError("Unit %s is not in the basin of the dimension %s" % (unit, self.dimension))
        if self._value is None:
            raise ParameterError("%s is not initialized" % self)
        val, un = self._value
        if unit == un:
            return self.valuetype(val)
        else:
            return self.valuetype( self.um.convert(value = val, from_unit = un, to_unit = unit) )
    
    def convertToReferencedUnit(self, unitreference):
        '''
        @summary: returns the parameter value in units, where the unit is referenced
        @param unitreference: the reference to the requested unit, which must adhere to the unit model of this parameter
        @type unit: str
        '''
        return self.convert( self.um[unitreference] )




























    valuetype = property(_get_valuetype,None,None)

    name = property(_get_name,None,None)

    value = property(_get_value,_set_value,_del_value)

    dimension = property(_get_dimension,None,None)
class ParameterList(object):
    '''
    @author: steger, jozsef
    @summary: 
    This class represents a list of control parameters of a monitoring task. 
    '''

    def __init__(self, parameterlist = []):
        '''
        @summary: Constructor
        @param parameterlist: a list of parameters to handle together
        @type parameterlist: list(Parameter) or ParameterList
        '''
        self.parameter = {}
        self.extend(parameterlist)
    
    def __str__(self):
        '''
        '''
        return "<ParameterList> [%s\n\t]" % "\n\t\t".join([ "%s," % (p) for p in self.parameter.values() ])
    
    def __len__(self):
        '''
        @summary: return the size of the parameter list
        @return: the size of the parameter list
        @rtype: integer
        '''
        return len(self.parameter)
    
    def __iter__(self):
        '''
        @summary: provide an iterator over all the parameter elements
        @return: the next parameter
        @rtype: Parameter
        '''
        for p in self.parameter.values():
            yield p
    
    def append(self, p):
        '''
        @summary: append a new Parameter to the parameter list. If a wrong type of parameter is given, silently discard it. 
        In case a parameter with the same name exists overwrite its value only.
        @param p: a new parameter to add or an existing parameter to update former values 
        @type p: Parameter
        '''
        if not isinstance(p, Parameter):
            print "WW: %s is not a parameter" % str(p)
            return
        if self.has_key(p.name):
            print "WW: parameter with name %s is updated" % p.name
            self.parameter[p.name].value = p.value
        else:
            self.parameter[p.name] = p
    
    def has_key(self, name):
        '''
        @summary: Check if a parameter with a given name is already in the list
        @param name: the name of the parameter looking for
        @type name: str
        '''
        return self.parameter.has_key(name)
    
    def get(self, name, unit):
        '''
        @summary: Read the parameter pointed by a given name in the required unit
        @param name: the name of the parameter
        @type name: str
        @param unit: the target unit the caller wants the named parameter to be expressed in
        @type unit: Unit
        @raise ParameterError: no such parameter name
        '''
        if not self.has_key(name):
            raise ParameterError("No Parameter with name: %s" % name)
        return self.parameter[name].convert(unit)

    def getInReferencedUnits(self, name, unitreference):
        '''
        @summary: Read the parameter pointed by a given name in the required unit
        @param name: the name of the parameter
        @type name: str
        @param unitreference: the target unit the caller wants the named parameter to be expressed in
        @type unitreference: str
        @raise ParameterError: no such parameter name
        '''
        if not self.has_key(name):
            raise ParameterError("No Parameter with name: %s" % name)
        return self.parameter[name].convertToReferencedUnit(unitreference)
    
    def update(self, name, value, unit):
        '''
        @summary: reset the value of the parameter with the given name
        @param name: the name of the parameter to update
        @type name: str
        @param value: the new value
        @type value: depends on the Parameter.type
        @param unit: the new unit
        @type unit: Unit
        '''
        self.parameter[name].value = value, unit

    def updateInReferencedUnits(self, name, value, unitreference):
        '''
        @summary: reset the value of the parameter with the given name
        @param name: the name of the parameter to update
        @type name: str
        @param value: the new value
        @type value: depends on the Parameter.type
        @param unitreference: the new unit
        @type unitreference: str
        '''
        p = self.parameter[name]
        p.value = value, p.um[unitreference]
        
    def update_by_list(self, p_updating):
        '''
        @summary: update parameter list with matching elements of another parameter list
        @param p_updating: parameter list, whose matching elements update the element of this list
        @type p_updating: ParameterList
        @raise ParameterError: wrong argument type 
        '''
        if not isinstance(p_updating, ParameterList):
            raise ParameterError("wrong argument type")
        for name in p_updating.parameter_names():
            if self.has_key(name):
                v = p_updating.parameter[name].value
                if v is not None:
                    self.parameter[name].value = v
    
    def clear(self):
        '''
        @summary: Empty the parameter list
        '''
        self.parameter.clear()
    
    def copy(self):
        return ParameterList( map(lambda p: p.copy(), self) )
    
    def extend(self, parameterlist):
        '''
        @summary: extends this parameter list with the items of another parameter list
        @param paramlist: the list of parameter items to extend with
        @type paramlist: ParameterList 
        '''
        for p in parameterlist:
            self.append(p)
        
    def parameter_names(self):
        '''
        @summary: List the names of the currently hold parameters
        @return: list of Parameter.name
        @rtype: list 
        '''
        return self.parameter.keys()
    
    def formkeyvaldict(self):
        return dict( [ (name, p.value[0]) for (name, p) in self.parameter.iteritems() ] )