summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/Semantics/InformationModel.py
blob: f43a4252ee2f85b28693ce2a9957267bf658acc8 (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
'''
Created on Aug 10, 2011

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

class IMError(Exception):
    pass

class Ontology(object):
 
    def __init__(self):

        plugin.register(
            'sparql', Processor,
            'rdfextras.sparql.processor', 'Processor')
        plugin.register(
            'sparql', Result,
            'rdfextras.sparql.query', 'SPARQLQueryResult')
        self._graph = Graph()
        self._graph.bind('owl', Namespace("http://www.w3.org/2002/07/owl#"))
    
    def load(self, prefix, owl_url, namespace_url = None):
        '''
        @summary: load owl file and bind name space
        @param prefix: an abbreviation of the name space to be used in sparql queries
        @type prefix: str
        @param owl_url: the location of the owl document to load
        @type owl_url: str
        @param namespace_url: the name space if None a # is added to the owl_url
        @type namespace_url: str or None
        '''
        if namespace_url is None:
            ns = Namespace("%s#" % namespace_url)
        else:
            ns = Namespace(namespace_url)
        try:
            self._graph += Graph().parse(source = owl_url)
        #except URLError:
        except:
            raise IMError("URLError: Cannot read model %s" % owl_url)
        try:
            self._graph.bind(prefix, 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 ipret(x):
        if not x:
            return None
        x = str(x)
        if len(x):
            return x
        else:
            return None

    @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 triples(self, spo_tuple):
        return self._graph.triples(spo_tuple)

    def ns(self, prefix):
        for p, ns in self._graph.namespaces():
            if p == prefix:
                return Namespace(ns)
        raise IMError("Unknown prefix: %s" % prefix)
    
    @property
    def ns_dict(self):
        return dict(self._graph.namespaces())

    def emptygraph(self):
        g = Graph()
        # bind name spaces
        for prefix, ns in self.ns_dict.iteritems():
            g.bind(prefix, ns)
        return g

    @property
    def g(self):
        g = Graph()
        g += self._graph
        for prefix, ns in self.ns_dict.iteritems():
            g.bind(prefix, ns)
        return g
    
#    @property
#    def graph(self):
#        return self._graph

    def dump(self):
        for t in self._graph.triples((None, None, None)):
            print t