-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathonto_helpers.py
63 lines (50 loc) · 1.41 KB
/
onto_helpers.py
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
# onto_helpers.py
from owlready2 import *
def get_isolated_ontology(ontology_iri):
new_world = World()
return new_world.get_ontology(ontology_iri)
def delete_ontology(onto, close_world=True):
onto.destroy()
if close_world:
onto.world.close()
def make_triple(subj, prop, obj):
try:
if FunctionalProperty in prop.is_a:
# "not asserted" workaround
setattr(subj, prop.python_name, obj)
else:
prop[subj].append(obj)
except Exception as e:
print("Exception in make_triple: ", subj, prop, obj)
raise e
def remove_triple(subj, prop, obj):
try:
if FunctionalProperty in prop.is_a:
# "not removed" workaround
setattr(subj, prop.python_name, None)
else:
if obj in prop[subj]:
prop[subj].remove(obj)
except Exception as e:
print("Exception in remove_triple: ", subj, prop, obj)
raise e
def get_relation_object(subj, prop):
"""
Another way to retrieve 3rd element of stored triple.
Usage:
obj = get_relation_object(subj, prop)
Works when the following fails (this appears generally with FunctionalProperty'es):
obj = subj.prop
objs = prop[subj]
"""
d = dict(prop.get_relations())
return d.get(subj, None)
def get_relation_subject(prop, obj):
"""
Another way to retrieve 1st element of stored triple.
Usage:
subj = get_relation_subject(prop, obj)
(This may be good for InverseFunctionalProperty'es)
"""
d = dict((b,a) for a,b in prop.get_relations())
return d.get(obj, None)