summaryrefslogtreecommitdiffstats
path: root/Monitoring/src/main/python/Semantics/InformationModel.py.old
blob: 9de648e3057166622da26acf19964ea169bd7c2e (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
'''
Created on Aug 10, 2011

@author: steger
'''
from urllib2 import URLError
from rdflib import Graph, Namespace, URIRef, plugin
from rdflib.query import Processor, Result

class IMError(Exception):
    pass

class Ontology(object):
    ontology = { 
        'owl': (None, "http://www.w3.org/2002/07/owl#"),
        'unit': ('unit.owl', "http://fp7-novi.eu/unit.owl#"),
        'param': ('monitoring_parameters.owl', "http://fp7-novi.eu/monitoring_parameter.owl#"),
        'feature': ('monitoring_features.owl', "http://fp7-novi.eu/monitoring_features.owl#"),
        'task': ('monitoring_task.owl', "http://fp7-novi.eu/monitoring_task.owl#"),
        'query': (None, "http://fp7-novi.eu/monitoring_query.owl#"),   #('monitoring_query.owl', ...)
        'conf': (None, "http://fp7-novi.eu/config.owl#"),
        'stat': (None, 'http://fp7-novi.eu/monitoring_stat.owl#'),
        'core': ('novi-im.owl', "http://fp7-novi.eu/im.owl#"),
    }
 
    def __init__(self, baseurl, config_owl):

        plugin.register(
            'sparql', Processor,
            'rdfextras.sparql.processor', 'Processor')
        plugin.register(
            'sparql', Result,
            'rdfextras.sparql.query', 'SPARQLQueryResult')

        
        self.baseurl = baseurl
        self.graph = Graph()
        # load owl files and bind name spaces
        try:
            self.graph += Graph().parse(source = config_owl)
        except URLError:
            raise IMError("URLError: Cannot read model %s" % config_owl)
        for prefix, (owl, ns) in self.ontology.iteritems():
            if owl:
                url = "%s/%s" % (self.baseurl, owl)
                try:
                    self.graph += Graph().parse(source = url)
                except URLError:
                    raise IMError("URLError: Cannot read model %s" % url)
            try:
                self.graph.bind(prefix, Namespace(ns))
            except:
                pass

    @staticmethod
    def _float(f):
        if '.' in f or 'e' in f or 'E' in f:
            return float(f)
        else:
            return int(f)

    @staticmethod
    def _tail(uriref):
        if not isinstance(uriref, URIRef):
            raise IMError("Wrong uriref %s" % uriref)
        return str(uriref).split("#")[-1]
    
    def query(self, query):
        return self.graph.query(query, initNs = dict(self.graph.namespaces()))

    def ns(self, prefix):
        for p, ns in self.graph.namespaces():
            if p == prefix:
                return Namespace(ns)
        raise IMError("Unknown prefix: %s" % prefix)
    
    def dump(self):
        for t in self.graph.triples((None, None, None)):
            print t