File: test_dataset.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 (134 lines) | stat: -rw-r--r-- 4,495 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
from rdflib import Namespace, Dataset, URIRef
from rdflib.namespace import RDF
import sys
from pathlib import Path

try:
    from rdflib.graph import ConjunctiveGraph
    # ConjunctiveGraph is going away in rdflib 8.0
except ImportError:
    ConjunctiveGraph = Dataset

sys.path.append(str(Path(__file__).parent.parent))
import owlrl

RELS = Namespace("http://example.org/relatives#")


def test_dataset():
    # create an RDF graph, load a simple OWL ontology and data
    d = Dataset()
    d.default_union = True
    g = d.default_context
    try:
        g.parse("relatives.ttl", format="turtle")
    except FileNotFoundError:
        # This test might be run from the parent directory root
        g.parse("test/relatives.ttl", format="turtle")

    # count no. rels:Person class instances, no inferencing, should find 14 results (one Child)
    cnt = 0
    for _ in d.subjects(predicate=RDF.type, object=RELS.Person):
        cnt += 1

    assert cnt == 14
    dest = d.graph(identifier=URIRef("urn:test:dest"))
    # count no. of hasGrandparent predicates, no inferencing, should find 0 results
    cnt = 0
    for _ in d.subject_objects(predicate=RELS.hasGrandparent):
        cnt += 1
    assert cnt == 0

    # expand the graph with OWL-RL semantics
    owlrl.DeductiveClosure(owlrl.OWLRL_Semantics).expand(d, destination=dest)

    # count no. rels:Person class instances, after inferencing, should find 1 result in the Dest graph
    cnt = 0
    for _ in dest.subjects(predicate=RDF.type, object=RELS.Person):
        cnt += 1

    assert cnt == 1

    # count no. rels:Person class instances, after inferencing, should find 15 results in the whole dataset
    cnt = 0
    for _ in d.subjects(predicate=RDF.type, object=RELS.Person):
        cnt += 1

    assert cnt == 15

    # count no. of hasGrandparent predicates, after inferencing, should find 7 results in the Dest graph
    cnt = 0
    for _ in dest.subject_objects(predicate=RELS.hasGrandparent):
        cnt += 1
    assert cnt == 7

    # count no. of hasGrandparent predicates, after inferencing, should find 7 results in the whole dataset
    cnt = 0
    for _ in d.subject_objects(predicate=RELS.hasGrandparent):
        cnt += 1
    assert cnt == 7

    # count no. of hasGrandparent predicates, after inferencing, should find 0 results in the default graph
    cnt = 0
    for _ in g.subject_objects(predicate=RELS.hasGrandparent):
        cnt += 1
    assert cnt == 0


def test_conjunctive_graph():
    # create an RDF graph, load a simple OWL ontology and data
    d = ConjunctiveGraph()
    g = d.default_context
    try:
        g.parse("relatives.ttl", format="turtle")
    except FileNotFoundError:
        # This test might be run from the parent directory root
        g.parse("test/relatives.ttl", format="turtle")

    # count no. rels:Person class instances, no inferencing, should find 14 results (one Child)
    cnt = 0
    for _ in d.subjects(predicate=RDF.type, object=RELS.Person):
        cnt += 1

    assert cnt == 14
    dest = d.get_context(URIRef("urn:test:dest"))
    # count no. of hasGrandparent predicates, no inferencing, should find 0 results
    cnt = 0
    for _ in d.subject_objects(predicate=RELS.hasGrandparent):
        cnt += 1
    assert cnt == 0

    # expand the graph with OWL-RL semantics
    owlrl.DeductiveClosure(owlrl.OWLRL_Semantics).expand(d, destination=dest)

    # count no. rels:Person class instances, after inferencing, should find 1 result in the Dest graph
    cnt = 0
    for _ in dest.subjects(predicate=RDF.type, object=RELS.Person):
        cnt += 1

    assert cnt == 1

    # count no. rels:Person class instances, after inferencing, should find 15 results in the whole dataset
    cnt = 0
    for _ in d.subjects(predicate=RDF.type, object=RELS.Person):
        cnt += 1

    assert cnt == 15

    # count no. of hasGrandparent predicates, after inferencing, should find 7 results in the Dest graph
    cnt = 0
    for _ in dest.subject_objects(predicate=RELS.hasGrandparent):
        cnt += 1
    assert cnt == 7

    # count no. of hasGrandparent predicates, after inferencing, should find 7 results in the whole dataset
    cnt = 0
    for _ in d.subject_objects(predicate=RELS.hasGrandparent):
        cnt += 1
    assert cnt == 7

    # count no. of hasGrandparent predicates, after inferencing, should find 0 results in the default graph
    cnt = 0
    for _ in g.subject_objects(predicate=RELS.hasGrandparent):
        cnt += 1
    assert cnt == 0