File: test_classes.py

package info (click to toggle)
owlrl 7.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,024 kB
  • sloc: python: 2,988; javascript: 249; makefile: 12
file content (321 lines) | stat: -rw-r--r-- 6,704 bytes parent folder | download
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
309
310
311
312
313
314
315
316
317
318
319
320
321
"""
Test for OWL 2 RL/RDF rules from

    Table 6. The Semantics of Classes

https://www.w3.org/TR/owl2-profiles/#Reasoning_in_OWL_2_RL_and_RDF_Graphs_using_Rules
"""

from unittest import mock

from rdflib import Graph, Literal, RDF, OWL
import sys
from pathlib import Path

sys.path.append(str(Path(__file__).parent.parent))
import owlrl
from owlrl.Namespaces import ERRNS, T


def test_cls_maxc1():
    """
    Test cls-maxc1 rule for OWL 2 RL.

    If::

        T(?x, owl:maxCardinality, "0"^^xsd:nonNegativeInteger)
        T(?x, owl:onProperty, ?p)
        T(?u, rdf:type, ?x)
        T(?u, ?p, ?y)

    then::

        false
    """
    g = Graph()

    x = T.x
    p = T.p
    c = T.C
    u = T.u
    y = T.y

    g.add((x, OWL.maxCardinality, Literal(0)))
    g.add((x, OWL.onProperty, p))
    g.add((x, OWL.onClass, c))
    g.add((u, RDF.type, x))
    g.add((u, p, y))

    owlrl.DeductiveClosure(owlrl.OWLRL_Semantics).expand(g)

    result = next(g.objects(predicate=ERRNS.error))
    expected = Literal(
        "Erroneous usage of maximum cardinality with"
        " http://test.org/x and http://test.org/y"
    )
    assert expected == result


def test_cls_maxc2():
    """
    Test cls-maxc2 rule for OWL 2 RL.

    If::

        T(?x, owl:maxCardinality, "1"^^xsd:nonNegativeInteger)
        T(?x, owl:onProperty, ?p)
        T(?u, rdf:type, ?x)
        T(?u, ?p, ?y1)
        T(?u, ?p, ?y2)

    then::

        T(?y1, owl:sameAs, ?y2)
    """
    g = Graph()

    x = T.x
    p = T.p
    u = T.u
    y1 = T.y1
    y2 = T.y2

    g.add((x, OWL.maxCardinality, Literal(1)))
    g.add((x, OWL.onProperty, p))
    g.add((u, RDF.type, x))
    g.add((u, p, y1))
    g.add((u, p, y2))

    owlrl.DeductiveClosure(owlrl.OWLRL_Semantics).expand(g)

    assert (y1, OWL.sameAs, y2) in g


def test_cls_maxqc1():
    """
    Test cls-maxqc1 rule for OWL 2 RL.

    If::

        T(?x, owl:maxQualifiedCardinality, "0"^^xsd:nonNegativeInteger)
        T(?x, owl:onProperty, ?p)
        T(?x, owl:onClass, ?c)
        T(?u, rdf:type, ?x)
        T(?u, ?p, ?y)
        T(?y, rdf:type, ?c)

    then::

        false
    """
    g = Graph()

    g.add((T.x, OWL.maxQualifiedCardinality, Literal(0)))
    g.add((T.x, OWL.onProperty, T.p))
    g.add((T.x, OWL.onClass, T.C))
    g.add((T.u, RDF.type, T.x))
    g.add((T.u, T.p, T.y))
    g.add((T.y, RDF.type, T.C))

    owlrl.DeductiveClosure(owlrl.OWLRL_Semantics).expand(g)

    result = next(g.objects(predicate=ERRNS.error))
    expected = Literal(
        "Erroneous usage of maximum qualified cardinality with"
        " http://test.org/x, http://test.org/C and http://test.org/y"
    )
    assert expected == result


def test_cls_maxqc2():
    """
    Test cls-maxqc2 rule for OWL 2 RL.

    If::

        T(?x, owl:maxQualifiedCardinality, "0"^^xsd:nonNegativeInteger)
        T(?x, owl:onProperty, ?p)
        T(?x, owl:onClass, owl:Thing)
        T(?u, rdf:type, ?x)
        T(?u, ?p, ?y)

    then::

        false
    """
    g = Graph()

    x = T.x
    p = T.p
    u = T.u
    y = T.y

    g.add((x, OWL.maxQualifiedCardinality, Literal(0)))
    g.add((x, OWL.onProperty, p))
    g.add((x, OWL.onClass, OWL.Thing))
    g.add((u, RDF.type, x))
    g.add((u, p, y))

    owlrl.DeductiveClosure(owlrl.OWLRL_Semantics).expand(g)

    result = next(g.objects(predicate=ERRNS.error))
    expected = Literal(
        "Erroneous usage of maximum qualified cardinality with"
        + " http://test.org/x, http://www.w3.org/2002/07/owl#Thing and"
        + " http://test.org/y"
    )
    assert expected == result


def test_cls_maxqc3():
    """
    Test cls-maxqc3 rule for OWL 2 RL.

    If::

        T(?x, owl:maxQualifiedCardinality, "1"^^xsd:nonNegativeInteger)
        T(?x, owl:onProperty, ?p)
        T(?x, owl:onClass, ?c)
        T(?u, rdf:type, ?x)
        T(?u, ?p, ?y1)
        T(?y1, rdf:type, ?c)
        T(?u, ?p, ?y2)
        T(?y2, rdf:type, ?c)

    then::

        T(?y1, owl:sameAs, ?y2)
    """
    g = Graph()

    x = T.x
    p = T.p
    c = T.C
    u = T.u
    y1 = T.y1
    y2 = T.y2

    g.add((x, OWL.maxQualifiedCardinality, Literal(1)))
    g.add((x, OWL.onProperty, p))
    g.add((x, OWL.onClass, c))
    g.add((u, RDF.type, x))
    g.add((u, p, y1))
    g.add((y1, RDF.type, c))
    g.add((u, p, y2))
    g.add((y2, RDF.type, c))

    owlrl.DeductiveClosure(owlrl.OWLRL_Semantics).expand(g)

    assert (y1, OWL.sameAs, y2) in g


def test_cls_maxqc4():
    """
    Test cls-maxqc4 rule for OWL 2 RL.

    If::

        T(?x, owl:maxQualifiedCardinality, "1"^^xsd:nonNegativeInteger)
        T(?x, owl:onProperty, ?p)
        T(?x, owl:onClass, owl:Thing)
        T(?u, rdf:type, ?x)
        T(?u, ?p, ?y1)
        T(?u, ?p, ?y2)

    then::

        T(?y1, owl:sameAs, ?y2)
    """
    g = Graph()

    x = T.x
    p = T.p
    u = T.u
    y1 = T.y1
    y2 = T.y2

    g.add((x, OWL.maxQualifiedCardinality, Literal(1)))
    g.add((x, OWL.onProperty, p))
    g.add((x, OWL.onClass, OWL.Thing))
    g.add((u, RDF.type, x))
    g.add((u, p, y1))
    g.add((u, p, y2))

    owlrl.DeductiveClosure(owlrl.OWLRL_Semantics).expand(g)

    assert (y1, OWL.sameAs, y2) in g


def test_cls_avf():
    """
    Test for cls-avf rule for OWL 2 RL.

    If::

        T(?x, owl:allValuesFrom, ?y)
        T(?x, owl:onProperty, ?p)
        T(?u, rdf:type, ?x)
        T(?u, ?p, ?v)

    and type restriction *valid*, then::

        T(?v, rdf:type, ?y)
    """
    g = Graph()

    x = T.x
    p = T.p
    u = T.u
    y = T.y
    v = T.v

    g.add((x, OWL.allValuesFrom, y))
    g.add((x, OWL.onProperty, p))
    g.add((u, RDF.type, x))
    g.add((u, p, v))

    owlrl.DeductiveClosure(owlrl.OWLRL_Semantics).expand(g)

    assert (v, RDF.type, y) in g


@mock.patch.object(owlrl.OWLRL_Semantics, "restriction_typing_check")
def test_cls_avf_error(mock_rtc):
    """
    Test restriction type check for cls-avf rule for OWL 2 RL.

    If::

        T(?x, owl:allValuesFrom, ?y)
        T(?x, owl:onProperty, ?p)
        T(?u, rdf:type, ?x)
        T(?u, ?p, ?v)

    and type restriction *invalid*, then::

        false
    """
    g = Graph()

    x = T.x
    p = T.p
    u = T.u
    y = T.y
    v = T.v

    g.add((x, OWL.allValuesFrom, y))
    g.add((x, OWL.onProperty, p))
    g.add((u, RDF.type, x))
    g.add((u, p, v))

    # trigger invalid restriction type
    mock_rtc.return_value = False

    owlrl.DeductiveClosure(owlrl.OWLRL_Semantics).expand(g)

    result = next(g.objects(predicate=ERRNS.error))
    expected = Literal(
        "Violation of type restriction for allValuesFrom in http://test.org/p"
        " for datatype http://test.org/y on value http://test.org/v"
    )
    assert expected == result