summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/Semantics/UnitModel.py
blob: e09a22a62d773fcb2752f3b9eb7d2da556ce20e0 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
'''
Created on Feb 12, 2012

@author: steger
'''
from DataProcessing.Prefix import PrefixManager
from DataProcessing.Unit import UnitManager
from DataProcessing.Dimension import DimensionManager
from DataProcessing.MeasurementLevel import lut_level

class UnitModel(object):
    '''
    @summary: an interface to infer prefix, unit and dimension related information from the model
    '''
    
    def __init__(self, ontology):
        '''
        @summary: constructor
        @param ontology: the basic knowledge
        @type ontology: Ontology 
        '''
        self.ontology = ontology
        self.pm = PrefixManager()
        self.um = UnitManager()
        self.dm = DimensionManager(self.um)

        # infer and store prefixes
        for (p_reference, p_symbol, base, exponent) in self.inferPrefixes():
            self.pm.newPrefix( self.ontology._tail(p_reference), p_symbol, base, exponent )
        
        # infer basic units
        for u_reference, u_symbol in self.inferBaseUnits():
            self.storeBasicUnit(u_reference, u_symbol)
        for u_reference, u_symbol, _, _ in self.inferPowerUnits():
            self.storeBasicUnit(u_reference, u_symbol)
        for u_reference, u_symbol, _ in self.inferProductUnits():
            self.storeBasicUnit(u_reference, u_symbol)
        for u_reference, u_symbol, derivedfrom, scale, offset in self.inferLinearTransformedUnits():
            self.storeLinearTransformedUnit(u_reference, u_symbol, derivedfrom, scale, offset)
        for u_reference, u_symbol, derivedfrom, expr_fwd, expr_inv in self.inferRegexpTransformedUnits():
            uref = self.ontology._tail(u_reference)
            ancestor = self.um[ self.ontology._tail(derivedfrom) ]
            self.um.addRegexpTransformedUnit(uref, u_symbol, ancestor, expr_fwd, expr_inv)

        # infer dimensions
        #FIXME: if there is a reference loop an error is raised...
        for d_reference, u_reference, level in self.inferBaseDimensions():
            dref = self.ontology._tail(d_reference)
            uref = self.ontology._tail(u_reference)
            lref = self.ontology._tail(level)
            level = lut_level[lref]
            unit = self.um[uref]
            self.dm.newBaseDimension(dref, dref, unit, level)
        for d_reference, u_reference, d_derivedfrom in self.inferDifferenceDimensions():
            dref = self.ontology._tail(d_reference)
            uref = self.ontology._tail(u_reference)
            daref = self.ontology._tail(d_derivedfrom)
            unit = self.um[uref]
            derivedfrom = self.dm[daref]
            self.dm.newDerivedDimension(dref, dref, unit, derivedfrom, self.dm.DifferenceDimension)
        for d_reference, u_reference, d_derivedfrom, exponent in self.inferPowerDimensions():
            dref = self.ontology._tail(d_reference)
            uref = self.ontology._tail(u_reference)
            daref = self.ontology._tail(d_derivedfrom)
            unit = self.um[uref]
            derivedfrom = self.dm[daref]
            self.dm.newDerivedDimension(dref, dref, unit, derivedfrom, self.dm.PowerDimension, exponent = exponent)
        for d_reference, u_reference, d_derivedfrom in self.inferProductDimensions():
            dref = self.ontology._tail(d_reference)
            uref = self.ontology._tail(u_reference)
            unit = self.um[uref]
            derivedfrom = tuple( self.dm[self.ontology._tail(x)] for x in d_derivedfrom )
            self.dm.newDerivedDimension(dref, dref, unit, derivedfrom, self.dm.ProductDimension)
        for d_reference, u_reference, d_derivedfrom in self.inferRatioDimensions():
            dref = self.ontology._tail(d_reference)
            uref = self.ontology._tail(u_reference)
            daref = self.ontology._tail(d_derivedfrom)
            unit = self.um[uref]
            derivedfrom = self.dm[daref]
            self.dm.newDerivedDimension(dref, dref, unit, derivedfrom, self.dm.RatioDimension)

    def storeBasicUnit(self, u_reference, u_symbol):
        uref = self.ontology._tail(u_reference)
        bu = self.um.newBasicUnit(uref, u_symbol)
        for p_reference in self.inferPossiblePrefixesOf(u_reference):
            p = self.pm[ self.ontology._tail(p_reference) ]
            puref = "%s_%s" % (p.reference, uref)
            symbol = "%s%s" % (p.symbol, bu.symbol)
            self.um.addLinearTransformedUnit(puref, symbol, bu, p.scale)

    def storeLinearTransformedUnit(self, u_reference, u_symbol, derivedfrom, scale, offset):
        uref = self.ontology._tail(u_reference)
        ancestor = self.um[ self.ontology._tail(derivedfrom) ]
        bu = self.um.addLinearTransformedUnit(uref, u_symbol, ancestor, scale, offset)
        for p_reference in self.inferPossiblePrefixesOf(u_reference):
            p = self.pm[ self.ontology._tail(p_reference) ]
            puref = "%s_%s" % (p.reference, uref)
            symbol = "%s%s" % (p.symbol, bu.symbol)
            self.um.addLinearTransformedUnit(puref, symbol, bu, p.scale)
    
    def inferPrefixes(self):
        '''
        @summary: iterate over all prefixes defined in the model.
        @return: a generator of the prefix details: (reference, symbol, base, exponent)
        @rtype: (URIRef, str, int, int) 
        @todo: in case the unit:base is not present in a Prefix individual, 
        we should fall back to the restriction on the base defined for the given sibling of the Prefix.  
        This sibling is referenced ?basegroup in the query.
        '''
        q = """
SELECT ?prefix ?symbol ?base ?exponent
WHERE {
 ?prefix a owl:NamedIndividual ;
         a ?basegroup ;
         unit:exponent ?exponent ;
         unit:base ?base .
 ?basegroup rdfs:subClassOf unit:Prefix .
 OPTIONAL {
  ?prefix unit:symbol ?symbol .
 }
}
        """
        for uri_prefix, symbol, base, exponent in self.ontology.query(q):
            if symbol is None:
                yield uri_prefix, self.ontology._tail(uri_prefix), int(base), int(exponent)
            else:
                yield uri_prefix, str(symbol), int(base), int(exponent)

    def inferPrefixSymbolOf(self, prefixuri):
        '''
        @summary: generates an short written form of a unit prefix if unit:symbol is present in the model,
        otherwise an abbreviation is derived from the tail of the uri (the reference name to the individual).
        @param prefixuri: the uri reference to the unit prefix
        @type prefixuri: URIRef
        @return: the short form
        @rtype: str   
        '''
        try:
            _, _, symbol = self.ontology.triples((prefixuri, self.ontology.ns('unit')['symbol'], None)).next()
            return str(symbol)
        except StopIteration:
            return self.ontology._tail(prefixuri)
    

    def inferBaseUnits(self):
        '''
        @summary: iterate over all BaseUnits defined in the model.
        @return: a generator of the unit details: (reference, symbol)
        @rtype: (URIRef, str) 
        '''
        q = """
SELECT ?unit ?symbol
WHERE {
 ?unit a owl:NamedIndividual ;
       a unit:BaseUnit .
 OPTIONAL {
  ?unit unit:symbol ?symbol .
 }
} 
        """
        for uri_unit, symbol in self.ontology.query(q):
            if symbol is None:
                yield uri_unit, self.ontology._tail(uri_unit)
            else:
                yield uri_unit, str(symbol)

    def inferPowerUnits(self):
        '''
        @summary: iterate over all PowerUnits defined in the model.
        @return: a generator of the unit details: (reference, symbol, powerof, exponent)
        @rtype: (URIRef, str, URIRef, int) 
        '''
        q = """
SELECT ?unit ?symbol ?powerof ?exponent
WHERE {
 ?unit a owl:NamedIndividual ;
       a unit:PowerUnit ;
       unit:exponent ?exponent ;
       unit:derivedFrom ?powerof .
 OPTIONAL {
  ?unit unit:symbol ?symbol .
 }
} 
        """
        for uri_unit, symbol, uri_powerof, exponent in self.ontology.query(q):
            if symbol is None:
                yield uri_unit, self.ontology._tail(uri_unit), uri_powerof, int(exponent)
            else:
                yield uri_unit, str(symbol), uri_powerof, int(exponent)

    def inferProductUnits(self):
        '''
        @summary: iterate over all ProductUnits defined in the model.
        @return: a generator of the unit details: (reference, symbol, productof)
        @rtype: (URIRef, str, set(URIRef)) 
        '''
        q = """
SELECT ?unit ?symbol ?productof
WHERE {
 ?unit a owl:NamedIndividual ;
       a unit:ProductUnit ;
       unit:derivedFrom ?productof
 OPTIONAL {
  ?unit unit:symbol ?symbol .
 }
} 
        """
        container = {}
        for uri_unit, symbol, uri_productof in self.ontology.query(q):
            if symbol is None:
                key = uri_unit, self.ontology_tail(uri_unit)
            else:
                key = uri_unit, str(symbol)
            if not container.has_key(key):
                container[key] = set()
            container[key].add(uri_productof)
        for (uri_unit, symbol), productof in container.iteritems():
            yield uri_unit, symbol, productof

    def inferLinearTransformedUnits(self):
        '''
        @summary: iterate over all LinearTransformedUnits defined in the model.
        @return: a generator of the unit details: (reference, symbol, derivedfrom, scale, offset)
        @rtype: (URIRef, str, URIRef, float, float) 
        '''
        q = """
SELECT ?unit ?symbol ?scale ?offset ?derivedfrom
WHERE {
 ?unit a owl:NamedIndividual ;
       a unit:LinearTransformedUnit ;
       unit:derivedFrom ?derivedfrom ;
       unit:scale ?scale .
 OPTIONAL {
  ?unit unit:offset ?offset .
 }
 OPTIONAL {
  ?unit unit:symbol ?symbol .
 }
} 
        """
        for uri_unit, symbol, scale, offset, uri_derivedfrom in self.ontology.query(q):
            if offset is None:
                offset = 0
            else:
                offset = self.ontology._float(offset)
            if symbol is None:
                yield uri_unit, self.ontology._tail(uri_unit), uri_derivedfrom, self.ontology._float(scale), offset
            else:
                yield uri_unit, str(symbol), uri_derivedfrom, self.ontology._float(scale), offset

    def inferRegexpTransformedUnits(self):
        '''
        @summary: iterate over all RegexpTransformedUnits defined in the model.
        @return: a generator of the unit details: (reference, symbol, derivedfrom, expr_fwd, expr_inv)
        @rtype: (URIRef, str, URIRef, str, str) 
        '''
        q = """
SELECT ?unit ?symbol ?derivedfrom ?fwd ?inv
WHERE {
 ?unit a owl:NamedIndividual ;
       a unit:RegexpTransformedUnit ;
       unit:derivedFrom ?derivedfrom ;
       unit:forwardExpression ?fwd ;
       unit:inverseExpression ?inv .
 OPTIONAL {
  ?unit unit:symbol ?symbol .
 }
} 
        """
        for uri_unit, symbol, uri_derivedfrom, expr_fwd, expr_inv in self.ontology.query(q):
            if symbol is None:
                yield uri_unit, self.ontology._tail(uri_unit), uri_derivedfrom, str(expr_fwd), str(expr_inv)
            else:
                yield uri_unit, str(symbol), uri_derivedfrom, str(expr_fwd), str(expr_inv)
        
    def inferPossiblePrefixesOf(self, uri_unit):
        '''
        @summary: extract possible prefixes for the given unit
        @param unit: reference to the unit
        @type unit: URIRef
        @return: a generator over the references of the possible unit prefixes
        @rtype: URIRef 
        '''
        for _, _, uri_prefix in self.ontology.triples((uri_unit, self.ontology.ns('unit')['possiblePrefix'], None)):
            yield uri_prefix

    def inferBaseDimensions(self):
        '''
        @summary: extract BaseDimensions and their corresponding units from the model
        @return: a generator of the BaseDimension details: (reference, unit, level)
        @rtype: (URIRef, URIRef, str) 
        '''
        q = """
SELECT ?dimension ?unit ?level
WHERE {
 ?dimension rdfs:subClassOf unit:BaseDimension ;
            rdfs:subClassOf ?constraint ;
            rdfs:subClassOf ?level .
 ?constraint owl:onProperty unit:defaultUnit ;
            owl:hasValue ?unit .
 FILTER regex(?level, "Level") .
}
        """
        for uri_dimension, uri_unit, level in self.ontology.query(q):
            yield uri_dimension, uri_unit, level

    def inferDifferenceDimensions(self):
        '''
        @summary: extract DifferenceDimensions and their corresponding units from the model
        @return: a generator of the DifferenceDimension details: (reference, unit, derivedfrom)
        @rtype: (URIRef, URIRef, URIRef) 
        '''
        q = """
SELECT ?dimension ?unit ?derivedFrom
WHERE {
 ?dimension rdfs:subClassOf unit:DifferenceDimension ;
            rdfs:subClassOf ?constraint1 ;
            rdfs:subClassOf ?constraint2 .
 ?constraint1 owl:onProperty unit:defaultUnit ;
              owl:hasValue ?unit .
 ?constraint2 owl:onProperty unit:derivedFrom ;
              owl:onClass ?derivedFrom .
}
        """
        for uri_dimension, uri_unit, uri_derivedfrom in self.ontology.query(q):
            yield uri_dimension, uri_unit, uri_derivedfrom

    def inferPowerDimensions(self):
        '''
        @summary: extract PowerDimensions and their corresponding units from the model
        @return: a generator of the PowerDimension details: (reference, unit, derivedfrom, exponent)
        @rtype: (URIRef, URIRef, URIRef, int) 
        '''
        q = """
SELECT ?dimension ?unit ?derivedFrom ?exponent
WHERE {
 ?dimension rdfs:subClassOf unit:PowerDimension ;
            rdfs:subClassOf ?constraint1 ;
            rdfs:subClassOf ?constraint2 ;
            rdfs:subClassOf ?constraint3 .
 ?constraint1 owl:onProperty unit:defaultUnit ;
              owl:hasValue ?unit .
 ?constraint2 owl:onProperty unit:derivedFrom ;
              owl:onClass ?derivedFrom .
 ?constraint3 owl:onProperty unit:exponent ;
              owl:hasValue ?exponent .
}
        """
        for uri_dimension, uri_unit, uri_derivedfrom, exponent in self.ontology.query(q):
            yield uri_dimension, uri_unit, uri_derivedfrom, int(exponent)

    def inferProductDimensions(self):
        '''
        @summary: extract ProductDimensions and their corresponding units from the model
        @return: a generator of the ProductDimension details: (reference, unit, set of derivedfrom references)
        @rtype: (URIRef, URIRef, tuple(URIRef)) 
        '''
        q = """
SELECT ?dimension ?unit ?derivedFrom
WHERE {
 ?dimension rdfs:subClassOf unit:ProductDimension ;
            rdfs:subClassOf ?constraint1 ;
            rdfs:subClassOf ?constraint2 .
 ?constraint1 owl:onProperty unit:defaultUnit ;
              owl:hasValue ?unit .
 ?constraint2 owl:onProperty unit:derivedFrom ;
              owl:onClass ?derivedFrom .
}
        """
        container = {}
        for uri_dimension, uri_unit, uri_derivedfrom in self.ontology.query(q):
            if not container.has_key(uri_dimension):
                container[uri_dimension] = (uri_unit, set())
            container[uri_dimension][1].add(uri_derivedfrom)
        for uri_dimension, (uri_unit, set_derivedfrom) in container.iteritems():
            yield uri_dimension, uri_unit, tuple(set_derivedfrom)

    def inferRatioDimensions(self):
        '''
        @summary: extract RatioDimensions and their corresponding units from the model
        @return: a generator of the RatioDimension details: (reference, unit, derivedfrom)
        @rtype: (URIRef, URIRef, URIRef) 
        '''
        q = """
SELECT ?dimension ?unit ?derivedFrom
WHERE {
 ?dimension rdfs:subClassOf unit:RatioDimension ;
            rdfs:subClassOf ?constraint1 ;
            rdfs:subClassOf ?constraint2 .
 ?constraint1 owl:onProperty unit:defaultUnit ;
              owl:hasValue ?unit .
 ?constraint2 owl:onProperty unit:derivedFrom ;
              owl:onClass ?derivedFrom .
}
        """
        for uri_dimension, uri_unit, uri_derivedfrom in self.ontology.query(q):
            yield uri_dimension, uri_unit, uri_derivedfrom