summaryrefslogtreecommitdiffstats
path: root/Monitoring/src/main/python/Semantics/Query.py
blob: 93c3a2cc90e56b8293e9c86771aac0e0c6e7efcb (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
'''
Created on Feb 21, 2012

@author: steger
'''
from DataProcessing.Parameter import ParameterList
from Resource.resource import resource as coreresource
from DataProcessing.DataFormatter import JsonFormatter, DumbFormatter

class SingleQuery(object):
    '''
    @summary: represents a (feature, resource) pair, representing what and where to measure
    The requested output format is also stored here 
    Optionally some measurement specific parameters can be added and post processing can be applied
    '''
    def __init__(self):
        self._feature = None
        self._resource = None
        self._samplechain = None
        self._formatter = None
        self._parameters = ParameterList()

    def _get_resource(self):
        return self._resource
    def _set_resource(self, (resourceid, resource)):
        if not isinstance(resource, coreresource):
            raise Exception("%s is not a resource type" % resource)
        self._resource = (resourceid, resource)
    
    def _get_feature(self):
        return self._feature
    def _set_feature(self, feature):
        self._feature = feature

    def _get_samplechain(self):
        return self._samplechain
    def _set_samplechain(self, samplechain):
        self._samplechain = samplechain

    def _get_formatter(self):
        return self._formatter
    def _set_formatter(self, uri_formatter):
        if str(uri_formatter).endswith("Formatter_JSON"):
            self._formatter = JsonFormatter
        elif str(uri_formatter).endswith("Formatter_CSV"):
            self._formatter = DumbFormatter
        else:
            raise Exception("%s is not a formatter type" % uri_formatter)

    def _get_paramlist(self):
        return self._parameters
    
    def addParameter(self, parameter):
        self._parameters.append(parameter)


    samplechain = property(_get_samplechain,_set_samplechain,None)

    formatter = property(_get_formatter,_set_formatter,None)

    resource = property(_get_resource,_set_resource,None)

    feature = property(_get_feature,_set_feature,None)

    paramlist = property(_get_paramlist,None,None)
class QueryBundle(object):
    '''
    @summary: represents a collection of SingleQueries
    '''
    def __init__(self):
        self.atoms = {}

    def __len__(self):
        return len(self.atoms)

    def has_key(self, key):
        return self.atoms.has_key(key)

    def __iter__(self):
        for q in self.atoms.itervalues():
            yield q
    
    def getResource(self, resourceid):
        for q in self:
            if q.resource[0] == resourceid:
                return q.resource[1]
        return None

    def add(self, reference, q):
        if self.atoms.has_key(reference):
            raise Exception("Duplicate MonitoringQuery entry")
        if not isinstance(q, SingleQuery):
            raise Exception("Wrong type")
        self.atoms[reference] = q





#class BundleQueryBundle(QueryBundle):
#    def newQuery(self, key, feature, samplechain, resource, formatter):
#        if self.atoms.has_key(key):
#            raise Exception("Atomic query %s exists" % key)
#        Q = MonitorQuery()
#        Q.resource = resource 
#        Q.feature = feature
#        Q.samplechain = samplechain
#        Q.formatter = formatter
#        self.atoms[key] = Q
            
#    def addParameter(self, key, parameter):
#        if not self.atoms.has_key(key):
#            raise Exception("Atomic query %s does not exist" % key)
#        self.atoms[key].addParameter(parameter)
    

#class AggregatorQuery(Query):
#    def __init__(self):
#        Query.__init__(self)
#        self._processid = None
    
#    def _get_processid(self):
#        return self._processid
#    def _set_processid(self, processid):
#        self._processid = processid



#class SampleQuery(QueryBundle):
#    def newQuery(self, key, processid, feature, samplechain, formatter):
#        if self.atoms.has_key(key):
#            raise Exception("Atomic query %s exists" % key)
#        Q = AggregatorQuery()
#        Q.processid = processid
#        Q.feature = feature
#        Q.samplechain = samplechain
#        Q.formatter = formatter
#        self.atoms[key] = Q

#processid = property(_get_processid,_set_processid,None)