summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/DataProcessing/Dimension.py
blob: 0365ee24c6e27da2588525d01d7ec9bfaf4d92f4 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
'''
Created on Feb 27, 2012

@author: steger
'''
from Unit import UnitManager
from DataProcessing.MeasurementLevel import MeasurementLevel, Interval, Ratio
from DataProcessing.DataError import DimensionError

class DimensionManager(object):
    '''
    @summary: the dimension container
    @ivar dimenstions: the container of the known dimensions
    @type dimensions: dict(str: L{Dimension})
    @ivar unitmanager: reference to the unit manager
    @type unitmanager: L{UnitManager}
    '''    
    class Dimension(object):
        '''
        @summary: a skeleton class for all the dimensions handled by L{DimensionManager}
        @ivar manager: back reference to the dimension manager
        @type manager: L{DimensionManager}
        @ivar unitmanager: a reference to the unit manager
        @type unitmanager: L{UnitManager}
        @ivar reference: the unique identifier of the dimension
        @ivar name: the name of the dimension
        @type name: str
        @ivar unit: the default unit of the dimension
        @type unit: L{Unit}
        @ivar basin: the set of units which are valid for this dimension
        @type basin: set(L{Unit})
        '''
        def __init__(self, dimensionmanager, reference, name, unit, level):
            '''
            @summary: constructor
            @param dimensionmanager: reference to the dimension manager
            @type dimensionmanager: L{DimensionManager}  
            @param reference: the reference to the dimension
            @type reference: str
            @param unit: the default unit of the dimension
            @type unit: L{Unit}
            @param level: the measurement level of the dimension
            @type level: L{MeasurementLevel}
            @note: the level is not a class instance
            @raise L{DimensionError}: Wrong type of unit / Wrong type of level
            '''
            if not isinstance(unit, UnitManager.Unit):
                raise DimensionError("Wrong type of unit %s" % unit)
            try:
                if not issubclass(level, MeasurementLevel):
                    raise DimensionError("Wrong type of level %s" % level)
            except TypeError:
                raise DimensionError("Wrong type of level %s" % level)
            self._data = (dimensionmanager, reference, name, unit)
            self._level = level
        @property
        def manager(self):
            return self._data[0]
        @property
        def unitmanager(self):
            return self._data[0].unitmanager
        @property
        def reference(self):
            return self._data[1]
        @property
        def name(self):
            return self._data[2]
        @property
        def unit(self):
            return self._data[3]
        @property
        def basin(self):
            return self.unitmanager.getBasinByUnit(self.unit)
        def level(self, level):
            '''
            @summary: check measurement level against the given level
            @param level: measurement level
            @type level: L{MeasurementLevel}
            @return: True if the measurement level given as a parameter 
            is the same or looser than the level of the dimension
            @rtype: bool
            @raise L{DimensionError}: Wrong type of level
            '''
            if not issubclass(level, MeasurementLevel):
                raise DimensionError("Wrong type of level %s" % level)
            return issubclass(self._level, level)
        def __str__(self):
            return "%s [%s]" % (self.name, self.unit)
        def __eq__(self, d):
            if not isinstance(d, DimensionManager.Dimension):
                raise DimensionError("wrong type")
            return self._level == d._level and self.containsUnit(d.unit)
        def containsUnit(self, unit):
            '''
            @summary: checks if a given unit is in the basin of this dimension
            @param unit: the unit to check
            @type unit: L{Unit}
            @return: true if the unit is applicable for this dimension
            @rtype: bool
            '''
            return unit in self.unitmanager.getBasinByUnit(self.unit)
    
    class BaseDimension(Dimension): 
        '''
        @summary: a dimension axiom
        '''
        pass

    class DerivedDimension(Dimension): 
        '''
        @summary: a skeleton for  dimensions, which are deriving from other already known dimensions
        '''
        def ancestors(self):
            '''
            @summary: iterate over all ancestors this dimension is derived from
            @return: generator over ancestors
            @rtype: L{Dimension}
            '''
            for d in self._ancestor:
                yield d

    class DifferenceDimension(DerivedDimension):
        '''
        @summary: a dimension defined by subtracting two individuals of a known dimension 
        '''
        def __init__(self, dimensionmanager, reference, name, unit, derivedfrom):
            '''
            @summary: constructor
            @param dimensionmanager: reference to the dimension manager
            @type dimensionmanager: L{DimensionManager}  
            @param reference: the reference to the dimension
            @param unit: the default unit of the dimension
            @type unit: L{Unit}
            @param derivedfrom: the ancestor dimension this dimension is derived from
            @type derivedfrom: L{Dimension}
            @raise L{DimensionError}: Wrong type of derivedfrom
            '''
            if not isinstance(derivedfrom, DimensionManager.Dimension):
                raise DimensionError("Wrong type of derivedfrom")
            if not derivedfrom.level(Interval):
                raise DimensionError("Cannot subtract %s" % derivedfrom)
            DimensionManager.Dimension.__init__(self, dimensionmanager, reference, name, unit, Ratio)
            self._ancestor = derivedfrom
    
    class PowerDimension(DerivedDimension):
        '''
        @summary: a dimension defined by raising an existing dimension to a given power
        @ivar exponent: the power
        @type exponent: int
        '''
        def __init__(self, dimensionmanager, reference, name, unit, derivedfrom, exponent):
            '''
            @summary: constructor
            @param dimensionmanager: reference to the dimension manager
            @type dimensionmanager: L{DimensionManager}  
            @param reference: the reference to the dimension
            @param unit: the default unit of the dimension
            @type unit: L{Unit}
            @param derivedfrom: the ancestor dimension this dimension is derived from
            @type derivedfrom: L{Dimension}
            @param exponent: dimension is a derivative of the derivedfrom dimension, by raising to power exponent
            @type exponent: int
            @raise DimensionError: Wrong type of derivedfrom / Cannot power
            '''
            if not isinstance(derivedfrom, DimensionManager.Dimension):
                raise DimensionError("Wrong type of derivedfrom")
            if not derivedfrom.level(Ratio):
                raise DimensionError("Cannot power %s" % derivedfrom)
            DimensionManager.Dimension.__init__(self, dimensionmanager, reference, name, unit, Ratio)
            self._ancestor = (derivedfrom,)
            self._exponent = exponent
        @property
        def exponent(self):
            return self._exponent

    class ProductDimension(DerivedDimension):
        '''
        @summary: dimension defined by multiplying at least two known different dimensions
        '''
        def __init__(self, dimensionmanager, reference, name, unit, derivedfrom):
            '''
            @summary: constructor
            @param dimensionmanager: reference to the dimension manager
            @type dimensionmanager: L{DimensionManager}  
            @param reference: the reference to the dimension
            @param unit: the default unit of the dimension
            @type unit: L{Unit}
            @param derivedfrom: the set of dimensions that compose this dimension
            @type derivedfrom: tuple(L{Dimension})
            @raise L{DimensionError}: Wrong type of derivedfrom / ProductDimension is derived from more than 2 Dimensions / Cannot be a factor
            '''
            if not isinstance(derivedfrom, tuple):
                raise DimensionError("Wrong type of derivedfrom")
            if len(derivedfrom) < 2:
                raise DimensionError("ProductDimension is derived from more than 2 Dimensions, got %d instead" % len(derivedfrom))
            for d in derivedfrom:
                if not d.level(Ratio):
                    raise DimensionError("%s cannot be a factor" % d)
            DimensionManager.Dimension.__init__(self, dimensionmanager, reference, name, unit, Ratio)
            self._ancestor = derivedfrom

    class RatioDimension(DerivedDimension):
        '''
        @summary: dimension defined by dividing two known dimensions
        '''
        def __init__(self, dimensionmanager, reference, name, unit, derivedfrom):
            '''
            @summary: constructor
            @param dimensionmanager: reference to the dimension manager
            @type dimensionmanager: L{DimensionManager}  
            @param reference: the reference to the dimension
            @param unit: the default unit of the dimension
            @type unit: L{Unit}
            @param derivedfrom: the set of dimensions that compose this dimension
            @type derivedfrom: tuple(L{Dimension})
            @raise L{DimensionError}: Wrong type of derivedfrom / Cannot be a factor
            '''
            if not isinstance(derivedfrom, DimensionManager.Dimension):
                raise DimensionError("Wrong type of derivedfrom")
            if not derivedfrom.level(Ratio):
                raise DimensionError("%s cannot be a factor" % derivedfrom)
            DimensionManager.Dimension.__init__(self, dimensionmanager, reference, name, unit, Ratio)
            self._ancestor = (derivedfrom,)
    
    def __init__(self, unitmanager):
        '''
        @summary: constructor
        @param unitmanager: the unit manager needs to be referenced, to check the basins of a unit
        @type unitmanager: L{UnitManager}  
        '''
        self.dimensions = {}
        self.unitmanager = unitmanager
    
    def __len__(self):
        '''
        @summary: the number of dimension known by the L{DimensionManager}
        @return: the number of dimension known by the L{DimensionManager}
        @rtype: int
        '''
        return len(self.dimensions)
    
    def __iter__(self):
        '''
        @summary: an iterator over known dimensions
        @return: the next known dimension
        @rtype: L{Dimension}
        '''
        for d in self.dimensions.values():
            yield d
    
    def newBaseDimension(self, reference, name, unit, level):
        '''
        @summary: generate a new dimension
        @param reference: the reference to the dimension
        @type reference: str
        @param unit: the default unit of the dimension
        @type unit: L{Unit}
        @param level: the measurement level of the dimension
        @type level: L{MeasurementLevel}
        @note: the level is not a class instance
        @return: the new dimension
        @rtype: L{Dimension}
        @raise L{DimensionError}: Dimension with reference already exists / Wrong type of unit / Wrong type of level / Wrong type of dimension /
        Expecting derivedfrom set / Wrong number of derived from Dimensions
        '''
        if self.dimensions.has_key(reference):
            raise DimensionError("Dimension with reference %s already exists" % reference)
        dimension = self.BaseDimension(self, reference, name, unit, level)
        self.dimensions[reference] = dimension
        return dimension

    def newDerivedDimension(self, reference, name, unit, derivedfrom, dimtype, **kw):
        '''
        @summary: generate a new dimension
        @param reference: the reference to the dimension
        @param unit: the default unit of the dimension
        @type unit: L{Unit}
        @param derivedfrom: the set of dimensions that compose this dimension
        @type derivedfrom: tuple(L{Dimension}) or L{Dimension}
        @param dimtype: possible dimension types are L{DifferenceDimension}, L{PowerDimension}, L{ProductDimension}, L{RatioDimension}
        @note: dimtype parameter is not an instance, but a class scheme
        @type dimtype: L{Dimension}
        @return: the new dimension
        @rtype: L{Dimension}
        @keyword kw: L{PowerDimension} expects an integer valued parameter: exponent 
        @raise L{DimensionError}: Dimension with reference already exists / Wrong type of dimension
        '''
        if self.dimensions.has_key(reference):
            raise DimensionError("Dimension with reference %s already exists" % reference)
        if issubclass(dimtype, self.DifferenceDimension)or issubclass(dimtype, self.ProductDimension) or issubclass(dimtype, self.RatioDimension):
            dimension = dimtype(self, reference, name, unit, derivedfrom)
        elif issubclass(dimtype, self.PowerDimension):
            dimension = dimtype(self, reference, name, unit, derivedfrom, kw.get('exponent'))
        else:
            raise DimensionError("Wrong type of dimension %s" % dimtype)
        self.dimensions[reference] = dimension
        return dimension
    
    def __getitem__(self, reference):
        '''
        @summary: look up the prefix in the DimensionManager based on its reference
        @param reference: the reference to the dimension
        @return: the dimension if found
        @rtype: L{Dimension}
        @raise L{DimensionError}: Dimension with reference not found
        '''
        if not self.dimensions.has_key(reference):
            raise DimensionError("Dimension with reference %s not found" % reference)
        return self.dimensions[reference]