summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/Semantics/QueryInterpreter.py
blob: 28a2775f26b15941f836c73059cd7871c731beed (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
'''
Created on Feb 21, 2012

@author: steger
'''

from Semantics.Query import QueryBundle, SingleQuery, SingleSampleQuery,\
    SingleConditionQuery
from Resource.node import node
from Resource.interface import interface
from DataProcessing.Aggregator import Max, Min, Percentile, Mean, Deviation
from DataProcessing.Sampler import Tail, Head
from DataProcessing.Parameter import ParameterList
from DataProcessing.Bool import IsPositive, IsNegative

class QueryInterpreter(object):
    '''
    classdocs
    '''
    samplesource = 'UnmodifiedExtractOfFeatureSamples'
    lut_skeleton = {
        'Maximum': Max,
        'Minimum': Min,
        'Percentile': Percentile,
        'Average': Mean,
        'Variance': Deviation,
        'Tail': Tail,
        'Head': Head
    }
    lut_condition = {
        'IsPositive': IsPositive,
        'IsNegative': IsNegative,
        #FIXME: IsNotNegative, IsNotPositive
        'AndExpression': '',
        'OrExpression': '',
    }

    def __init__(self, model):
        '''
        @summary: constructor
        @param model: the task model to resolve the tools
        @type model: TaskModel
        '''
        self.model = model
    
    def getUnitOfDimension(self, ref_dim):
        return self.model.dm[ref_dim].unit

    def getUnit(self, uri_prefix, uri_unit):
        if uri_prefix is None:
            uref = self.model._tail(uri_unit)
        else:
            uref = "%s_%s" % (self.model._tail(uri_prefix), self.model._tail(uri_unit))
        return self.um[uref]

    @property
    def myns(self):
        return self.model.ontology.ns_dict

    def inferInterfacesOf(self, qgraph, uri_node):
        q = """
SELECT ?ifin ?address ?unit ?prefix
WHERE {
 <%s> core:hasInboundInterface ?ifin ;
    core:hasOutboundInterface ?ifout .
 ?ifin a core:Interface ;
       core:hasIPv4Address ?addressobj .
 ?ifout a core:Interface ;
        core:hasIPv4Address ?addressobj .
 ?addressobj a owl:NamedIndividual ;
             a unit:IPAddress ;
             unit:hasValue ?address .
 OPTIONAL {
  ?addressobj unit:hasUnit ?unit .
 }
 OPTIONAL {
  ?addressobj unit:hasPrefix ?prefix .
 }
}
        """ % uri_node
        for uri_ifin, address, uri_unit, uri_prefix in qgraph.query(q, initNs = self.myns):
            name = self.model.ontology._tail(uri_ifin)
            iface = interface(name, resourceid = uri_ifin)
            if uri_unit is not None:
                iface.address = str(address), self.getUnit(uri_prefix, uri_unit)
            else:
                iface.address = str(address), self.getUnitOfDimension('IPAddress')
            iface.direction = iface.EGRESS | iface.INGRESS
            #FIXME: this info should come from the model
            iface.interface = "eth0"
            iface.ispublic = True
            yield iface
        #TODO: similarly look up uni directional interfaces of the node and yield them as well

    def inferBundleQueries(self, qgraph):
        '''
        @summary: 
        '''
        q = """
SELECT ?query ?resource ?feature ?sample ?formatter
WHERE {
 ?query a owl:NamedIndividual ;
        a query:BundleQuery ;
        feature:hasFeature ?feature ;
        stat:hasSample ?sample ;
        query:hasResource ?resource ;
        query:hasFormatter ?formatter .
}
        """
        Q = QueryBundle()
        for uri_query, uri_resource, uri_feature, uri_sample, uri_formatter in qgraph.query(q, initNs = self.myns):
            r = Q.getResource(uri_resource)
            if r is None: 
                r = self.translateResource(qgraph, uri_resource)
            sq = SingleQuery()
            sq.feature = uri_feature
            sq.resource = (uri_resource, r)
            sq.formatter = uri_formatter
            sq.samplechain = self.inferSampleChain(qgraph, uri_sample)
            for p in self.inferParameters(qgraph, uri_query):
                sq.addParameter(parameter = p)
            Q.add(uri_query, sq)
        return Q

    def inferSampleManipulationQueries(self, qgraph):
        '''
        @summary: 
        '''
        q = """
SELECT ?query ?resource ?feature ?sourceid ?sample ?formatter
WHERE {
 ?query a owl:NamedIndividual ;
        a query:SampleManipulationQuery ;
        query:hasResource ?resource ;
        feature:hasFeature ?feature ;
        query:hasProcessid ?sourceid ;
        stat:hasSample ?sample ;
        query:hasFormatter ?formatter .
}
        """
        Q = QueryBundle()
        for uri_query, uri_resource, uri_feature, uri_sourceid, uri_sample, uri_formatter in qgraph.query(q, initNs = self.myns):
            r = Q.getResource(uri_resource)
            if r is None: 
                r = self.translateResource(qgraph, uri_resource)
            aq = SingleSampleQuery()
            aq.feature = uri_feature
            aq.resource = (uri_resource, r)
            aq.formatter = uri_formatter
            aq.samplechain = self.inferSampleChain(qgraph, uri_sample)
            aq.sourceid = str(uri_sourceid)
            Q.add(uri_query, aq)
        return Q

    def inferConditionQueries(self, qgraph):
        '''
        @summary: 
        '''
        q = """
SELECT ?query ?resource ?sourceid ?feature ?cond 
WHERE {
 ?query a owl:NamedIndividual ;
        a query:ConditionQuery ;
        query:hasResource ?resource ;
        query:hasProcessid ?sourceid ;
        feature:hasFeature ?feature ;
        stat:hasCondition ?cond ;
}
        """
        Q = QueryBundle()
        for uri_query, uri_resource, uri_sourceid, uri_feature, uri_cond in qgraph.query(q, initNs = self.myns):
            r = Q.getResource(uri_resource)
            if r is None: 
                r = self.translateResource(qgraph, uri_resource)
            C, op = self.inferCondition(qgraph, uri_cond)
            print uri_query, uri_resource, uri_sourceid, uri_cond
            
            cq = SingleConditionQuery()
            cq.feature = uri_feature
            cq.resource = (uri_resource, r)
            cq.operation = op
            cq.conditiontype = C
            cq.sourceid = str(uri_sourceid)
            Q.add(uri_query, cq)
        return Q

    def inferCondition(self, qgraph, uri_cond):
        q = """
SELECT ?what ?sample
WHERE {
 <%s> a owl:NamedIndividual ;
      a ?what ;
      stat:hasSample ?sample ;
}
        """ % uri_cond
        for what, uri_sample in qgraph.query(q, initNs = self.myns):
            tail = self.model.ontology._tail(what)
            if tail in [ 'NamedIndividual' ]:
                continue
            C = self.lut_condition[tail]
            if C in [IsPositive, IsNegative]: #FIXME: IsNotNegative, IsNotPositive
                return C, self.inferLineraCombinedSample(qgraph, uri_sample)
            else:
                print "BALHE"
                raise Exception("QI NOT IMPLMENTED")

    def inferLineraCombinedSample(self, qgraph, uri_sample):
        q = """
SELECT ?sample ?factor
WHERE {
 <%s> a owl:NamedIndividual ;
      a stat:LinearCombinedSample ;
      stat:hasTerm ?term .
 ?term stat:hasSample ?sample .
 OPTIONAL {
  ?term stat:hasScale ?factor .
 }
}
        """ % uri_sample
        terms = []
        for uri_sample, factor in qgraph.query(q, initNs = self.myns):
            try:
                factor = float(factor)
            except:
                factor = 1
            op = self.inferSampleChain(qgraph, uri_sample)
            terms.append( ( factor, op) ) 
        return terms
    
    def inferSampleChain(self, qgraph, uri_sample):
        tail = self.model.ontology._tail(uri_sample)
        if tail == self.samplesource:
            return []
        q = """
SELECT ?nextsample ?sampleop
WHERE {
 <%s> a owl:NamedIndividual ;
      stat:hasSample ?nextsample ;
      a ?sampleop
}
        """ % uri_sample
        for uri_sample_next, uri_sampleop in qgraph.query(q, initNs = self.myns):
            tail = self.model.ontology._tail(uri_sampleop)
            if tail in [ 'NamedIndividual' ]:
                continue
            op = self.inferSampleChain(qgraph, uri_sample_next)
            break
        skeleton = self.lut_skeleton[tail]
        parlist = ParameterList([ p for p in self.inferParameters(qgraph, uri_sample) ])
        op.append( (skeleton, parlist) )
        return op

    def inferParameters(self, qgraph, uri_query):
        q = """
SELECT ?name ?type ?dim ?defval ?unit ?prefix
WHERE {
 <%s> param:hasParameter ?par .
 ?par a owl:NamedIndividual ;
      param:paramName ?name ;
      param:hasType ?type ;
      a ?dim .
 OPTIONAL {
  ?par unit:hasValue ?defval .
  OPTIONAL {
   ?par unit:hasUnit ?unit .
  }
  OPTIONAL {
   ?par unit:hasPrefix ?prefix .
  }
 }
}
        """ % uri_query
        for uri_name, uri_type, uri_dim, uri_default, uri_unit, uri_prefix in qgraph.query(q, initNs = self.myns):
            tail = self.model.ontology._tail(uri_dim)
#FIXME: query should include the filter, but rdflib has a bug and only the spelt out form would work
# FILTER ( ?dim != owl:NamedIndividual ) 
# FILTER ( ?dim != query:QueryParameter ) 
# FILTER ( ?dim != stat:SampleOperatorParameter ) 
#
# like:
# FILTER ( ?dim != <http://www.w3.org/2002/07/owl#NamedIndividual> ) 
            if tail in [ 'QueryParameter', 'SOP_tail', 'SOP_head', 'SOP_order', 'NamedIndividual' ]:
                continue
            yield self.model.translateParameter(str(uri_name), uri_dim, uri_unit, uri_prefix, uri_type, uri_default)
    
    def translateResource(self, qgraph, uri_resource):
        resource_name = self.model.ontology._tail(uri_resource)
        q = """
SELECT ?resourcetype
WHERE {
 <%s> a owl:NamedIndividual ;
      a core:Resource ;
      a ?resourcetype ;
}
        """ % uri_resource
        for uri_rtype, in qgraph.query(q, initNs = self.myns):
            tail = self.model.ontology._tail(uri_rtype)
            if tail in [ 'Resource', 'NamedIndividual' ]:
                continue
            if tail == "Node":
                r = node(name = resource_name, resourceid = uri_resource)
                for iface in self.inferInterfacesOf(qgraph, uri_resource):
                    r.addinterface(iface)
                return r
            else:
                print "WW: unhandled rtype", uri_rtype
                continue