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
|
from test.data import context0, context1
import pytest
from rdflib import OWL, RDFS, BNode, Graph, Literal, Namespace, URIRef, Variable
from rdflib.extras.infixowl import (
ACE_NS,
BooleanClass,
Class,
Individual,
Property,
max,
)
from rdflib.util import first
EXNS = Namespace("http://example.org/vocab/")
PZNS = Namespace(
"http://www.co-ode.org/ontologies/pizza/2005/10/18/classified/pizza.owl#"
)
@pytest.fixture(scope="function")
def graph():
g = Graph(identifier=EXNS.context0)
g.bind("ex", EXNS)
Individual.factoryGraph = g
yield g
del g
def test_class_instantiation(graph):
name = EXNS.Man
assert isinstance(name, URIRef)
nounannotations = ("Man", "Men")
nameannotation = Literal("Man")
c = Class(
identifier=name,
subClassOf=None,
equivalentClass=None,
disjointWith=None,
complementOf=None,
graph=graph,
skipOWLClassMembership=False,
comment=Literal("This is a Man"),
nounAnnotations=nounannotations,
nameAnnotation=nameannotation,
nameIsLabel=True,
)
c.serialize(graph)
res = graph.serialize(format="ttl")
assert res == (
"@prefix ace: <http://attempto.ifi.uzh.ch/ace_lexicon#> .\n"
"@prefix ex: <http://example.org/vocab/> .\n"
"@prefix owl: <http://www.w3.org/2002/07/owl#> .\n"
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
"\n"
"ace:CN_pl a owl:AnnotationProperty .\n"
"\n"
"ace:CN_sg a owl:AnnotationProperty .\n"
"\n"
"ace:PN_sg a owl:AnnotationProperty .\n"
"\n"
"ace:TV_pl a owl:AnnotationProperty .\n"
"\n"
"ace:TV_sg a owl:AnnotationProperty .\n"
"\n"
"ace:TV_vbg a owl:AnnotationProperty .\n"
"\n"
"ex:Man a owl:Class ;\n"
' rdfs:label "Man" ;\n'
' ace:CN_pl "Men" ;\n'
' ace:CN_sg "Man" ;\n'
' ace:PN_sg "Man" ;\n'
' rdfs:comment "This is a Man" .\n'
"\n"
)
def test_class_hash():
b = Class(OWL.Restriction)
c = Class(OWL.Restriction)
assert len(set([b, c])) == 1
def test_class_and(graph):
# Construct an anonymous class description consisting of the
# intersection of this class and 'other' and return it
# Chaining 3 intersections
female = Class(EXNS.Female, graph=graph)
human = Class(EXNS.Human, graph=graph)
youngperson = Class(EXNS.YoungPerson, graph=graph)
youngwoman = female & human & youngperson
assert str(youngwoman) == "ex:YoungPerson THAT ( ex:Female AND ex:Human )"
assert isinstance(youngwoman, BooleanClass) is True
assert isinstance(youngwoman.identifier, BNode) is True
def test_class_getparents(graph):
# computed attributes that returns a generator over taxonomic 'parents'
# by disjunction, conjunction, and subsumption
dad = Class(EXNS.Dad)
brother = Class(EXNS.Brother)
sister = Class(EXNS.Sister)
sibling = brother | sister
sibling.identifier = EXNS.Sibling
assert len(sibling) == 2
assert str(sibling) == "( ex:Brother OR ex:Sister )"
assert (
str(first(brother.parents))
== "Class: ex:Sibling EquivalentTo: ( ex:Brother OR ex:Sister )"
)
parent = Class(EXNS.Parent)
male = Class(EXNS.Male)
father = parent & male
father.identifier = EXNS.Father
father.equivalentClass = [dad]
female = Class(EXNS.Female)
mother = parent & female
mother.identifier = EXNS.Mother
assert len(list(father.parents)) == 3
assert (
str(list(father.parents))
== "[Class: ex:Dad , Class: ex:Parent , Class: ex:Male ]"
)
# Restriction(EXNS.isChild, graph=g, allValuesFrom=EXNS.Sibling)
Property(EXNS.hasParent, graph=graph) << max >> Literal(1)
assert male.isPrimitive() is True
assert brother.isPrimitive() is True
assert sibling.isPrimitive() is False
assert list(sibling.subSumpteeIds()) == []
assert str(brother.__repr__(full=True)) == "Class: ex:Brother "
assert graph.serialize(format="ttl") == (
"@prefix ex: <http://example.org/vocab/> .\n"
"@prefix owl: <http://www.w3.org/2002/07/owl#> .\n"
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n"
"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n"
"\n"
"ex:Father a owl:Class ;\n"
" owl:equivalentClass ex:Dad ;\n"
" owl:intersectionOf ( ex:Parent ex:Male ) .\n"
"\n"
"ex:Mother a owl:Class ;\n"
" owl:intersectionOf ( ex:Parent ex:Female ) .\n"
"\n"
"ex:Sibling a owl:Class ;\n"
" owl:unionOf ( ex:Brother ex:Sister ) .\n"
"\n"
"ex:Brother a owl:Class .\n"
"\n"
"ex:Dad a owl:Class .\n"
"\n"
"ex:Female a owl:Class .\n"
"\n"
"ex:Male a owl:Class .\n"
"\n"
"ex:Sister a owl:Class .\n"
"\n"
"ex:hasParent a owl:ObjectProperty .\n"
"\n"
"ex:Parent a owl:Class .\n"
"\n"
"[] a owl:Restriction ;\n"
" owl:maxCardinality 1 ;\n"
" owl:onProperty ex:hasParent .\n"
"\n"
)
def test_class_serialize(graph):
father = Class(EXNS.Father)
sister = Class(EXNS.Sister)
parent = Class(EXNS.Parent)
male = Class(EXNS.Male)
nounannotations = ("Man", "Men")
nameannotation = Literal("Man")
owlc = Class(
EXNS.test,
subClassOf=[parent],
equivalentClass=[male],
disjointWith=[father],
complementOf=sister,
graph=graph,
skipOWLClassMembership=False,
comment=Literal("This is a Man"),
nounAnnotations=nounannotations,
nameAnnotation=nameannotation,
nameIsLabel=True,
)
g1 = Graph(identifier=context1)
owlc.serialize(g1)
assert len(g1) > 0
owlc.setupNounAnnotations(None)
owlc.setupNounAnnotations(["Man", "Men"])
owlc.extent = None
owlc.extent = [context1]
assert list(owlc.extent) == [context1]
pred = RDFS.comment
pred = ACE_NS.CN_pl
tgt = (EXNS.test, pred, Literal("Men"))
assert tgt in owlc.graph
assert list(owlc._get_annotation(pred)) == [
Literal("Men"),
Literal("['Man', 'Men']"),
]
assert owlc.extentQuery == (
Variable("CLASS"),
URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
URIRef("http://example.org/vocab/test"),
)
assert str(owlc.__invert__()) == "Some Class DisjointWith ( NOT ex:Sister )\n"
assert owlc.__repr__(full=True) == (
"Class: ex:test \n"
" ## A Defined Class (Man) ##\n"
" This is a Man\n"
" SubClassOf: ex:Parent . \n"
" EquivalentTo: ex:Male . \n"
" DisjointWith ex:Father\n"
" ex:Sister\n"
)
assert owlc.complementOf == sister
owlc.complementOf = None
owlc.complementOf = father
with pytest.raises(Exception, match="2"):
assert owlc.complementOf == sister
def test_class_nameislabel():
g = Graph(identifier=context0)
g.bind("ex", EXNS)
Individual.factoryGraph = g
nameannotation = Literal("Man")
owlc = Class(
EXNS.test,
graph=g,
comment=Literal("This is a Man"),
nameAnnotation=nameannotation,
nameIsLabel=True,
)
assert list(owlc.annotation) == [Literal("Man")]
assert g.serialize(format="ttl") == (
"@prefix ace: <http://attempto.ifi.uzh.ch/ace_lexicon#> .\n"
"@prefix ex: <http://example.org/vocab/> .\n"
"@prefix owl: <http://www.w3.org/2002/07/owl#> .\n"
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
"\n"
"ace:CN_pl a owl:AnnotationProperty .\n"
"\n"
"ace:CN_sg a owl:AnnotationProperty .\n"
"\n"
"ace:PN_sg a owl:AnnotationProperty .\n"
"\n"
"ace:TV_pl a owl:AnnotationProperty .\n"
"\n"
"ace:TV_sg a owl:AnnotationProperty .\n"
"\n"
"ace:TV_vbg a owl:AnnotationProperty .\n"
"\n"
"ex:test a owl:Class ;\n"
' rdfs:label "Man" ;\n'
' ace:PN_sg "Man" ;\n'
' rdfs:comment "This is a Man" .\n'
"\n"
)
def test_class_nameisnotlabel():
g = Graph(identifier=context0)
g.bind("ex", EXNS)
Individual.factoryGraph = g
nameannotation = Literal("Man")
owlc = Class(
EXNS.test,
graph=g,
comment=Literal("This is a Man"),
nameAnnotation=nameannotation,
)
assert list(owlc.annotation) == []
assert g.serialize(format="ttl") == (
"@prefix ace: <http://attempto.ifi.uzh.ch/ace_lexicon#> .\n"
"@prefix ex: <http://example.org/vocab/> .\n"
"@prefix owl: <http://www.w3.org/2002/07/owl#> .\n"
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
"\n"
"ace:CN_pl a owl:AnnotationProperty .\n"
"\n"
"ace:CN_sg a owl:AnnotationProperty .\n"
"\n"
"ace:PN_sg a owl:AnnotationProperty .\n"
"\n"
"ace:TV_pl a owl:AnnotationProperty .\n"
"\n"
"ace:TV_sg a owl:AnnotationProperty .\n"
"\n"
"ace:TV_vbg a owl:AnnotationProperty .\n"
"\n"
"ex:test a owl:Class ;\n"
' ace:PN_sg "Man" ;\n'
' rdfs:comment "This is a Man" .\n'
"\n"
)
assert (EXNS.test, RDFS.label, nameannotation) not in g
|