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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
from test.data import TEST_DATA_DIR, context0
import pytest
from rdflib import OWL, RDF, RDFS, XSD, Graph, Literal, Namespace, URIRef, logger
from rdflib.extras.infixowl import (
AllClasses,
AllProperties,
BooleanClass,
Class,
ClassNamespaceFactory,
CommonNSBindings,
ComponentTerms,
DeepClassClear,
EnumeratedClass,
GetIdentifiedClasses,
Individual,
Infix,
MalformedClassError,
Property,
Restriction,
classOrTerm,
exactly,
generateQName,
max,
min,
only,
some,
value,
)
EXNS = Namespace("http://example.org/vocab/")
PZNS = Namespace(
"http://www.co-ode.org/ontologies/pizza/2005/10/18/classified/pizza.owl#"
)
def test_infix_operators_dunders():
def fn(other, arg=""):
return None
i = Infix(fn)
assert i.__rlshift__("foo") is not None
assert i.__rshift__("foo") is None
assert i.__rmatmul__("foo") is not None
assert i.__matmul__("foo") is None
assert i.__call__("foo", "baz") is None
def test_infix_operators():
g = Graph()
g.bind("ex", EXNS)
# Broken since forever
with pytest.raises(Exception, match="Only URIRefs or Paths can be in paths!"):
EXNS.hasParent | some | Class(EXNS.Physician, graph=g)
# Will break when RDF Star syntax is added
res = EXNS.hasParent << some >> Class(EXNS.Physician, graph=g)
assert str(res) == "( ex:hasParent SOME ex:Physician )"
res = EXNS.hasParent @ some @ Class(EXNS.Physician, graph=g)
assert str(res) == "( ex:hasParent SOME ex:Physician )"
def test_generateqname():
g = Graph()
g.bind("ex", EXNS)
assert (
generateQName(g, URIRef("http://example.org/vocab/ontology")) == "ex:ontology"
)
def test_classorterm():
term = URIRef("http://example.org/vocab/D")
assert classOrTerm(Class(EXNS.D)) == term
assert classOrTerm(term) == term
def test_getidentifiedclasses():
g = Graph()
g.bind("ex", PZNS)
g.parse(TEST_DATA_DIR / "owl" / "pizza.owl", format="xml")
assert len(list(GetIdentifiedClasses(g))) == 97
class Foo:
pass
def test_common_ns_bindings():
g = Graph(bind_namespaces=None)
CommonNSBindings(g, additionalNS={"ex": EXNS})
def test_allclasses():
g = Graph()
g.bind("ex", PZNS)
g.parse(TEST_DATA_DIR / "owl" / "pizza.owl", format="xml")
for c in AllClasses(g):
assert isinstance(c, Class)
def test_check_allclasses():
from test.data import bob, michel, tarek
g = Graph()
g.bind("ex", PZNS)
g.add((tarek, RDF.type, OWL.Class))
g.add((michel, RDF.type, OWL.Class))
g.add((bob, RDF.type, OWL.Class))
assert set(g.subjects(predicate=RDF.type, object=OWL.Class)) == {
URIRef("urn:example:bob"),
URIRef("urn:example:michel"),
URIRef("urn:example:tarek"),
}
def test_check_allproperties():
g = Graph()
g.bind("ex", PZNS)
g.parse(TEST_DATA_DIR / "owl" / "pizza.owl", format="xml")
for p in AllProperties(g):
assert isinstance(p, Property)
EXCL = ClassNamespaceFactory(EXNS)
def test_classnamespacefactory():
leg = EXCL.Leg
x = EXCL.__getitem__(leg.identifier)
assert x is not None
def test_componentterms():
g = Graph()
g.bind("ex", PZNS)
Individual.factoryGraph = g
g.parse(TEST_DATA_DIR / "owl" / "pizza.owl", format="xml")
owlcls = list(AllClasses(g))[-1]
assert isinstance(owlcls, Class)
for c in ComponentTerms(owlcls):
assert isinstance(c, Class)
def test_componentterms_extended():
g = Graph()
g.bind("ex", PZNS)
Individual.factoryGraph = g
isPartOf = Property(EXNS.isPartOf) # noqa: N806
g.add((isPartOf.identifier, RDF.type, OWL.TransitiveProperty))
hasLocation = Property(EXNS.hasLocation, subPropertyOf=[isPartOf]) # noqa: N806
g.add((hasLocation.identifier, RDFS.subPropertyOf, isPartOf.identifier))
leg = EXCL.Leg
knee = EXCL.Knee
joint = EXCL.Joint
kneeJoint = EXCL.KneeJoint # noqa: N806
legStructure = EXCL.LegStructure # noqa: N806
kneeJoint.equivalentClass = [joint & (isPartOf @ some @ knee)]
structure = EXCL.Structure
legStructure.equivalentClass = [structure & (isPartOf @ some @ leg)]
structure += leg
structure += joint
locatedInLeg = hasLocation @ some @ leg # noqa: N806
locatedInLeg += knee # noqa: N806
for c in ComponentTerms(knee):
assert isinstance(c, Class)
for c in ComponentTerms(legStructure):
assert isinstance(c, Class)
logger.debug(f"G\n{g.serialize(format='ttl')}")
def test_raise_malformedclasserror():
e = MalformedClassError("Malformed owl:Restriction")
assert str(repr(e)) == "Malformed owl:Restriction"
with pytest.raises(MalformedClassError, match="Malformed owl:Restriction"):
raise e
def test_owlrdfproxylist():
g = Graph()
g.bind("ex", EXNS)
Individual.factoryGraph = g
ogbujiBros = EnumeratedClass( # noqa: N806
EXNS.ogbujiBros, members=[EXNS.chime, EXNS.uche, EXNS.ejike]
)
ogbujiSiblings = EnumeratedClass( # noqa: N806
EXNS.ogbujiBros, members=[EXNS.uche, EXNS.chime, EXNS.ejike]
)
assert ogbujiBros == ogbujiSiblings
assert EXNS.chime in ogbujiBros
fred = EXNS.fred
bert = EXNS.bert
assert fred not in ogbujiBros
ogbujiBros.append(fred)
ogbujiBros += bert
assert ogbujiBros[1] == EXNS.uche
assert len(ogbujiBros) == 5
del ogbujiBros[4]
assert len(ogbujiBros) == 4
ogbujiBros[4] = fred
assert len(ogbujiBros) == 5
male = Class(EXNS.Male, graph=g)
female = Class(EXNS.Female, graph=g)
human = Class(EXNS.Human, graph=g)
youngPerson = Class(EXNS.YoungPerson, graph=g) # noqa: N806
youngWoman = female & human & youngPerson # noqa: N806
assert isinstance(youngWoman, BooleanClass)
res = ogbujiBros.__eq__(youngWoman)
assert res is False
youngMan = male & human & youngPerson # noqa: N806
res = youngMan.__eq__(youngWoman)
assert res is False
def test_deepclassclear():
g = Graph()
g.bind("ex", EXNS)
Individual.factoryGraph = g
classB = Class(EXNS.B) # noqa: N806
classC = Class(EXNS.C) # noqa: N806
classD = Class(EXNS.D) # noqa: N806
classE = Class(EXNS.E) # noqa: N806
classF = Class(EXNS.F) # noqa: N806
anonClass = EXNS.someProp << some >> classD # noqa: N806
assert str(anonClass) == "( ex:someProp SOME ex:D )"
assert str(classF) == "Class: ex:F "
classF += anonClass # noqa: N806
assert str(list(anonClass.subClassOf)) == "[Class: ex:F ]"
classA = classE | classF | anonClass # noqa: N806
classG = Class(EXNS.G, complementOf=classA) # noqa: N806
classB += classA
classA.equivalentClass = [Class()]
classA.complementOf = classG
classB.subClassOf = [EXNS.someProp << some >> classC]
assert str(classA) == "( ex:E OR ex:F OR ( ex:someProp SOME ex:D ) )"
DeepClassClear(classA)
assert str(classA) == "( )"
assert list(anonClass.subClassOf) == []
assert str(classB) == "Class: ex:B SubClassOf: ( ex:someProp SOME ex:C )"
otherClass = classD | anonClass # noqa: N806
assert str(otherClass) == "( ex:D OR ( ex:someProp SOME ex:D ) )"
DeepClassClear(otherClass)
assert str(otherClass) == "( )"
otherClass.delete()
assert list(g.triples((otherClass.identifier, None, None))) == []
def test_booleanclassextenthelper():
g = Graph()
g.bind("ex", EXNS)
Individual.factoryGraph = g
fire = Class(EXNS.Fire)
water = Class(EXNS.Water)
tc1 = BooleanClass(members=[fire, water])
assert str(tc1) == "( ex:Fire AND ex:Water )"
tc2 = BooleanClass(operator=OWL.unionOf, members=[fire, water])
assert str(tc2) == "( ex:Fire OR ex:Water )"
def test_changeoperator():
# Converts a unionOf / intersectionOf class expression into one
# that instead uses the given operator
g = Graph(identifier=context0)
g.bind("ex", EXNS)
Individual.factoryGraph = g
fire = Class(EXNS.Fire)
water = Class(EXNS.Water)
testclass = BooleanClass(members=[fire, water])
assert str(testclass) == "( ex:Fire AND ex:Water )"
testclass.changeOperator(OWL.unionOf)
assert repr(testclass) == "( ex:Fire OR ex:Water )"
try:
testclass.changeOperator(OWL.unionOf)
except Exception as e:
assert str(e) == "The new operator is already being used!"
def test_cardinality_zero():
g = Graph()
g.bind("ex", EXNS)
prop = Property(EXNS.someProp, baseType=OWL.DatatypeProperty)
r = Restriction(
prop, graph=g, cardinality=Literal(0, datatype=XSD.nonNegativeInteger)
)
assert str(r) == "( ex:someProp EQUALS 0 )"
def test_textual_infix_operators():
g = Graph()
g.bind("ex", EXNS)
Individual.factoryGraph = g
isPartOf = Property(EXNS.isPartOf) # noqa: N806
g.add((isPartOf.identifier, RDF.type, OWL.TransitiveProperty))
knee = EXCL.Knee
x = isPartOf @ some @ knee
assert x is not None
y = isPartOf @ only @ knee
assert y is not None
y = isPartOf @ exactly @ Literal("1", datatype=XSD.integer)
y = isPartOf @ min @ Literal("1", datatype=XSD.integer)
y = isPartOf @ max @ Literal("1", datatype=XSD.integer)
y = isPartOf @ value @ Literal("1", datatype=XSD.integer)
|