-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnl_2_owl.py
281 lines (242 loc) · 11.1 KB
/
nl_2_owl.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
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
from owlready2 import *
from types import new_class
from common import AtomicConcept, ConceptAssertion, JunctionConcept, RestrictionConcept
def restriction_concept_2_owl(onto, restr_concept):
"""Given a restriction complex concept in nl, creates its corresponding
owl class using owlready2.
Args:
arguments_list (list): the restriction concept arguments splitted in a list
polarity (str): polarity of the concept
Returns:
Class: the owlready class representing the concept
str: the name given of the owlready class
"""
with onto:
restriction = restr_concept.restriction
role_name = restr_concept.role_name
inner_concept, inner_concept_name = make_concept(onto, restr_concept.concept)
if restriction == "∀":
complex_concept_name = f"only_{role_name}_d_L_{inner_concept_name}_R"
complex_concept = new_class(complex_concept_name, (Thing,))
role = new_class(str(role_name), (ObjectProperty,))
role.domain = role.range = [Thing]
complex_concept.equivalent_to.append(role.only(inner_concept))
return complex_concept, complex_concept_name
elif restriction == "∃":
complex_concept_name = f"exists_{role_name}_d_L_{inner_concept_name}_R"
complex_concept = new_class(complex_concept_name, (Thing,))
role = new_class(str(role_name), (ObjectProperty,))
role.domain = role.range = [Thing]
complex_concept.equivalent_to.append(role.some(inner_concept))
return complex_concept, complex_concept_name
else:
quantifier = restr_concept.restriction.split()[0]
quantity = restr_concept.restriction.split()[1]
role_name = restr_concept.role_name
if quantifier == ">":
complex_concept_name = (
f"MT{quantity}_{role_name}_d_L_{inner_concept_name}_R"
)
complex_concept = new_class(str(complex_concept_name), (Thing,))
role = new_class(str(role_name), (ObjectProperty,))
role.domain = role.range = [Thing]
complex_concept.equivalent_to.append(
role.min(int(quantity) + 1, inner_concept)
)
return complex_concept, complex_concept_name
elif quantifier == "<":
complex_concept_name = (
f"LT{quantity}_{role_name}_d_L_{inner_concept_name}_R"
)
complex_concept = new_class(str(complex_concept_name), (Thing,))
role = new_class(str(role_name), (ObjectProperty,))
role.domain = role.range = [Thing]
complex_concept.equivalent_to.append(
role.max(int(quantity) - 1, inner_concept)
)
return complex_concept, complex_concept_name
elif quantifier == ">=":
complex_concept_name = (
f"AL{quantity}_{role_name}_d_L_{inner_concept_name}_R"
)
complex_concept = new_class(complex_concept_name, (Thing,))
role = new_class(str(role_name), (ObjectProperty,))
role.domain = role.range = [Thing]
complex_concept.equivalent_to.append(
role.min(int(quantity), inner_concept)
)
return complex_concept, complex_concept_name
elif quantifier == "<=":
complex_concept_name = (
f"AM{quantity}_{role_name}_d_L_{inner_concept_name}_R"
)
complex_concept = new_class(complex_concept_name, (Thing,))
role = new_class(str(role_name), (ObjectProperty,))
role.domain = role.range = [Thing]
complex_concept.equivalent_to.append(
role.max(int(quantity), inner_concept)
)
return complex_concept, complex_concept_name
elif quantifier == "=":
complex_concept_name = (
f"EQ{quantity}_{role_name}_d_L_{inner_concept_name}_R"
)
complex_concept = new_class(complex_concept_name, (Thing,))
role = new_class(str(role_name), (ObjectProperty,))
role.domain = role.range = [Thing]
complex_concept.equivalent_to.append(
role.exactly(int(quantity), inner_concept)
)
return complex_concept, complex_concept_name
def AtomicConcept_2_Owl(onto, concept):
with onto:
polarity = concept.polarity
concept_name = concept.concept_name
concept = None
if concept_name == "⊤":
if polarity != "+":
concept_name = "neg_Thing"
concept = Nothing
else:
concept_name = "pos_Thing"
concept = Thing
elif concept_name == "⊥":
if polarity != "+":
concept_name = "neg_Nothing"
concept = Thing
else:
concept_name = "pos_Nothing"
concept = Nothing
else:
if polarity != "+":
concept = new_class(f"pos_{concept_name}", (Thing,))
concept_name = f"neg_{concept_name}"
neg_complex_concept = new_class(concept_name, (Thing,))
neg_complex_concept.equivalent_to.append(Not(concept))
concept = neg_complex_concept
else:
concept_name = f"pos_{concept_name}"
concept = new_class(concept_name, (Thing,))
return concept, concept_name
def junction_concept_2_owl(onto, concept2make):
with onto:
relationship = concept2make.relationship
lhs_concept, lhs_concept_name = make_concept(onto, concept2make.lhs_concept)
rhs_concept, rhs_concept_name = make_concept(onto, concept2make.rhs_concept)
owl_concept_name = str()
if relationship == "⊓":
owl_concept_name = f"L_{lhs_concept_name}_R_and_L_{rhs_concept_name}_R"
owl_concept = new_class(owl_concept_name, (Thing,))
owl_concept.equivalent_to.append(lhs_concept & rhs_concept)
else:
owl_concept_name = f"L_{lhs_concept_name}_R_or_L_{rhs_concept_name}_R"
owl_concept = new_class(owl_concept_name, (Thing,))
owl_concept.equivalent_to.append(lhs_concept | rhs_concept)
return owl_concept, owl_concept_name
def make_concept(onto, concept2make):
"""Given a concept, create it's owl class usign owlready.
Args:
onto (owlready ontology): The ontology to which this concept will be added
concept2make (Concept): A concept to create it's owl representation using owlready.
Returns:
Class: the owlready class corresponding to the concept
"""
with onto:
if isinstance(concept2make, AtomicConcept):
owl_concept, owl_concept_name = AtomicConcept_2_Owl(onto, concept2make)
elif isinstance(concept2make, JunctionConcept):
owl_concept, owl_concept_name = junction_concept_2_owl(onto, concept2make)
elif isinstance(concept2make, RestrictionConcept): # Restriction concept
owl_concept, owl_concept_name = restriction_concept_2_owl(
onto, concept2make
)
else:
raise TypeError(
"Unexpected type for concept2make: {}".format(type(concept2make))
)
return owl_concept, owl_concept_name
SPECIAL_RANGE = 0
SPECIAL_DOMAIN = 1
def special_axiom(axiom):
if (
isinstance(axiom.LHS_concept, AtomicConcept)
and axiom.LHS_concept.concept_name == "⊤"
):
return True, SPECIAL_RANGE
if (
isinstance(axiom.LHS_concept, RestrictionConcept)
and axiom.LHS_concept.concept == "⊤"
):
return True, SPECIAL_DOMAIN
return False, None
def make_special_axiom(onto, special_axiom, special_type):
LHS_concept = special_axiom.LHS_concept
RHS_concept = special_axiom.RHS_concept
with onto:
if special_type == SPECIAL_RANGE:
role_name = RHS_concept.role_name
range_concept, _ = make_concept(onto, RHS_concept.concept)
role = new_class(str(role_name), (ObjectProperty,))
role.domain = [Thing]
role.range = [range_concept]
elif special_type == SPECIAL_DOMAIN:
role_name = LHS_concept.role_name
domain_concept, _ = make_concept(onto, RHS_concept)
role = new_class(str(role_name), (ObjectProperty,))
role.domain = [domain_concept]
role.range = [Thing]
def create_ontology(id, ABoxAssertions, TBoxAxioms):
"""Given the ABox & the TBox, create the corresponding ontology using owlready.
Args:
ABoxAssertions (list): List with the ABox assertions
TBoxAxioms (list): List with the TBox axioms
"""
onto = get_ontology("http://alcq.org/onto.owl")
onto.destroy(update_is_a=True, update_relation=True)
onto = get_ontology("http://alcq.org/onto.owl")
inds = set()
with onto:
### A B O X A S S E R T I O N S ###
for assertion in ABoxAssertions:
if isinstance(assertion, ConceptAssertion): # Class Assertion ##
assertion_concept = assertion.concept
indName = assertion.individual
inds.add(indName)
concept, _ = make_concept(onto, assertion_concept)
ind = Thing(str(indName))
ind.is_a.append(concept)
else: # Role Assertion ##
role_name = assertion.RoleName
leftIndName = assertion.Individual_l
rightIndName = assertion.Individual_r
inds.add(leftIndName)
inds.add(rightIndName)
role = new_class(str(role_name), (ObjectProperty,))
role.domain = [Thing]
role.range = [Thing]
leftInd = Thing(str(leftIndName))
rightInd = Thing(str(rightIndName))
role[leftInd].append(rightInd)
### T B O X A X I O M S ###
for axiom in TBoxAxioms:
special_check, special_type = special_axiom(axiom)
if special_check:
make_special_axiom(onto, axiom, special_type)
continue
lhs_concept, _ = make_concept(onto, axiom.LHS_concept)
rhs_concept, _ = make_concept(onto, axiom.RHS_concept)
lhs_concept.is_a.append(rhs_concept)
# We need to state this in OWA #
individuals = list(onto.individuals())
individualset = set([str(i).split(".")[1] for i in individuals])
if inds != individualset:
print("DIFFERENT INDIVIDUALS!!!\n\n")
print(inds)
print(individualset)
while 1:
pass
if len(individuals) >= 2:
AllDifferent(individuals)
onto.save(f"./ALCQ_ontology.owl", "rdfxml")
# onto.save(f"./Generated-Ontologies/ALCQ-Ontology-{id}.owl", "rdfxml")
onto.destroy(update_relation=True, update_is_a=True)