Debian Codegraph @

FaCT++.Python/pyfactxx/coras/query.py

raw symbols SCIP
1 #
2 # Coras - OWL reasoning system
3 #
4 # Copyright (C) 2018 by Artur Wroblewski <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 """
21 Query engine.
22 """
23
24 import re
25 import logging
26
27 import rdflib.store
28 from pyfactxx.lib_factxx import ObjectRoleExpr, DataRoleExpr
29 from rdflib import RDF, OWL
30
31 from .util import dispatch
32
33 logger = logging.getLogger(__name__)
34
35 class QueryStore(rdflib.store.Store):
36 def __init__(self, tgraph, reasoner):
37 self._triples_graph = tgraph
38 self._reasoner = reasoner
39 self._uri_pattern = re.compile(r'\w+://')
40
41 # TODO: research the settings below; set all of them to true to
42 # allow loading N3 files at the moment
43 self.context_aware = True
44 self.formula_aware = True
45
46 def get_term(self, node_name, datatype=''):
47 if datatype == 'default':
48 return rdflib.util.from_n3(node_name)
49 elif datatype != '':
50 return rdflib.Literal(node_name, datatype=datatype)
51 elif self._uri_pattern.match(node_name):
52 return rdflib.URIRef(node_name)
53 else:
54 return rdflib.BNode(node_name)
55
56 def triples(self, pattern, context=None):
57 s, p, o = pattern
58
59 #ref_s = None if s is None else self._reasoner.individual(s)
60 #ref_p = None if p is None else self._reasoner.object_role(p)
61 #ref_o = None if o is None else self._reasoner.individual(o)
62
63 ref_s = "" if s is None else s
64 ref_p = "" if p is None else p
65 ref_o = "" if o is None else o
66
67 for subj, role, obj, datatype in self._reasoner.get_triples(ref_s, ref_p, ref_o):
68 yield ((self.get_term(subj), self.get_term(role), self.get_term(obj, datatype)), context)
69
70 def __len__(self, context=None):
71 return len(list(self.triples((None, None, None), context)))
72
73 def role_triples_(self, s, ref_p, context):
74 ref_s = self._reasoner.individual(s)
75 objects = self._reasoner.get_role_fillers(ref_s, ref_p)
76 return (((s, None, o), context) for o in objects)
77
78 def role_triples(self, s, p, context):
79 ref_p, _, fetch_values = self._property_type(p)
80 values = fetch_values(s, p)
81 return (((s, p, v), context) for v in values)
82
83 def remove(self, triple, context=None):
84 logger.warning('removal of triples not supported yet')
85
86 @dispatch
87 def _get_domains(self, p):
88 raise NotImplementedError('Unknown type of property: {}'.format(p))
89
90 @_get_domains.register(ObjectRoleExpr)
91 def _(self, p):
92 yield from self._reasoner.get_o_domain(p)
93
94 @_get_domains.register(DataRoleExpr)
95 def _(self, p):
96 yield from self._reasoner.get_d_domain(p)
97
98 def _get_instances(self, classes):
99 get = self._reasoner.get_instances
100 instances = {i for c in classes for i in get(c)}
101 yield from (rdflib.URIRef(i.name) for i in instances)
102
103 @dispatch
104 def _fetch_objects(self, p, s):
105 raise NotImplementedError('Unknown type of property: {}'.format(p))
106
107 @_fetch_objects.register(ObjectRoleExpr)
108 def _(self, p, s):
109 if __debug__:
110 logger.debug('fetching objects for ({}, {})'.format(s, p))
111 objects = self._reasoner.get_role_fillers(s, p)
112 yield from (o.name for o in objects)
113
114 @_fetch_objects.register(DataRoleExpr)
115 def _(self, p, s):
116 if __debug__:
117 logger.debug('fetching objects for ({}, {})'.format(s, p))
118 s = rdflib.URIRef(s.name)
119 p = rdflib.URIRef(p.name)
120 yield from self._triples_graph.objects(s, p)
121
122 def _detect_property(self, p):
123 assert p is not None
124
125 reasoner = self._reasoner
126 if self._is_datatype_property(p):
127 return reasoner.data_role(p)
128 elif self._is_object_property(p):
129 return reasoner.object_role(p)
130 else:
131 return None
132
133 def _is_datatype_property(self, p):
134 q = (p, RDF.type, OWL.DatatypeProperty)
135 return q in self._triples_graph
136
137 def _is_object_property(self, p):
138 q = (p, RDF.type, OWL.ObjectProperty)
139 return q in self._triples_graph
140
141 # vim: sw=4:et:ai