forked from dice-group/Ontolearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcept_generator.py
293 lines (243 loc) · 11.1 KB
/
concept_generator.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
282
283
284
285
286
287
288
289
290
291
292
293
# -----------------------------------------------------------------------------
# MIT License
#
# Copyright (c) 2024 Ontolearn Team
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# -----------------------------------------------------------------------------
"""A module to generate concepts."""
from typing import Iterable, List, Generator
from owlapy.class_expression import OWLObjectMaxCardinality, OWLObjectMinCardinality, OWLObjectSomeValuesFrom, \
OWLObjectAllValuesFrom, OWLObjectIntersectionOf, OWLObjectUnionOf, OWLThing, OWLNothing, OWLClass, \
OWLClassExpression, OWLObjectComplementOf, OWLObjectExactCardinality, OWLDataAllValuesFrom, OWLDataSomeValuesFrom, \
OWLDataHasValue, OWLObjectHasValue
from owlapy.owl_data_ranges import OWLDataRange
from owlapy.owl_individual import OWLIndividual
from owlapy.owl_literal import OWLLiteral
from owlapy.owl_property import OWLObjectPropertyExpression, OWLDataPropertyExpression
from ontolearn.utils import parametrized_performance_debugger
class ConceptGenerator:
"""A class that can generate some sorts of OWL Class Expressions."""
@parametrized_performance_debugger()
def negation_from_iterables(self, class_expressions: Iterable[OWLClassExpression]):
"""Negate a sequence of Class Expressions.
Args:
class_expressions: Iterable of class expressions to negate.
Returns:
Negated form of input.
{ x \\| ( x \\equv not s} """
for item in class_expressions:
assert isinstance(item, OWLClassExpression)
yield self.negation(item)
@staticmethod
def intersect_from_iterables(a_operands: Iterable[OWLClassExpression], b_operands: Iterable[OWLClassExpression]) \
-> Iterable[OWLObjectIntersectionOf]:
""" Create an intersection of each class expression in a_operands with each class expression in b_operands."""
assert isinstance(a_operands, Generator) is False and isinstance(b_operands, Generator) is False
seen = set()
for i in a_operands:
for j in b_operands:
if (i, j) in seen:
continue
i_and_j = OWLObjectIntersectionOf((i, j))
seen.add((i, j))
seen.add((j, i))
yield i_and_j
@staticmethod
def union_from_iterables(a_operands: Iterable[OWLClassExpression],
b_operands: Iterable[OWLClassExpression]) -> Iterable[OWLObjectUnionOf]:
""" Create an union of each class expression in a_operands with each class expression in b_operands."""
assert (isinstance(a_operands, Generator) is False) and (isinstance(b_operands, Generator) is False)
# TODO: if input sizes say 10^4, we can employ multiprocessing
seen = set()
for i in a_operands:
for j in b_operands:
if (i, j) in seen:
continue
i_and_j = OWLObjectUnionOf((i, j))
seen.add((i, j))
seen.add((j, i))
yield i_and_j
# noinspection PyMethodMayBeStatic
def intersection(self, ops: Iterable[OWLClassExpression]) -> OWLObjectIntersectionOf:
"""Create intersection of class expression.
Args:
ops: Operands of the intersection.
Returns:
Intersection with all operands (intersections are merged).
"""
# TODO CD: I would rather prefer def intersection(self, a: OWLClassExpression, b: OWLClassExpression). This is
# TODO CD: more advantages as one does not need to create a tuple of a list before intersection two expressions.
operands: List[OWLClassExpression] = []
for c in ops:
if isinstance(c, OWLObjectIntersectionOf):
operands.extend(c.operands())
else:
assert isinstance(c, OWLClassExpression)
operands.append(c)
# operands = _avoid_overly_redundant_operands(operands)
return OWLObjectIntersectionOf(operands)
# noinspection PyMethodMayBeStatic
def union(self, ops: Iterable[OWLClassExpression]) -> OWLObjectUnionOf:
"""Create union of class expressions.
Args:
ops: Operands of the union
Returns:
Union with all operands (unions are merged).
"""
operands: List[OWLClassExpression] = []
for c in ops:
if isinstance(c, OWLObjectUnionOf):
operands.extend(c.operands())
else:
assert isinstance(c, OWLClassExpression)
operands.append(c)
# operands = _avoid_overly_redundand_operands(operands)
return OWLObjectUnionOf(operands)
# noinspection PyMethodMayBeStatic
def existential_restriction(self, filler: OWLClassExpression, property: OWLObjectPropertyExpression) \
-> OWLObjectSomeValuesFrom:
"""Create existential restriction.
Args:
property: Property.
filler: Filler of the restriction.
Returns:
Existential restriction.
"""
assert isinstance(property, OWLObjectPropertyExpression)
return OWLObjectSomeValuesFrom(property=property, filler=filler)
# noinspection PyMethodMayBeStatic
def universal_restriction(self, filler: OWLClassExpression, property: OWLObjectPropertyExpression) \
-> OWLObjectAllValuesFrom:
"""Create universal restriction.
Args:
property: Property.
filler: Filler of the restriction.
Returns:
universal restriction
"""
assert isinstance(property, OWLObjectPropertyExpression)
return OWLObjectAllValuesFrom(property=property, filler=filler)
def has_value_restriction(self, individual: OWLIndividual, property: OWLObjectPropertyExpression) \
-> OWLObjectHasValue:
"""Create object has value restriction.
Args:
property: Property.
individual: Individual of the restriction.
Returns:
Object has value restriction.
"""
assert isinstance(property, OWLObjectPropertyExpression)
return OWLObjectHasValue(property=property, individual=individual)
def min_cardinality_restriction(self, filler: OWLClassExpression,
property: OWLObjectPropertyExpression, card: int) \
-> OWLObjectMinCardinality:
"""Create min cardinality restriction.
Args:
filler: Filler of the restriction.
property: Property.
card: Cardinality of the restriction.
Returns:
Min cardinality restriction.
"""
assert isinstance(property, OWLObjectPropertyExpression)
return OWLObjectMinCardinality(cardinality=card, property=property, filler=filler)
def max_cardinality_restriction(self, filler: OWLClassExpression,
property: OWLObjectPropertyExpression, card: int) \
-> OWLObjectMaxCardinality:
"""Create max cardinality restriction.
Args:
filler: Filler of the restriction.
property: Property.
card: Cardinality of the restriction.
Returns:
Max cardinality restriction.
"""
assert isinstance(property, OWLObjectPropertyExpression)
return OWLObjectMaxCardinality(cardinality=card, property=property, filler=filler)
def exact_cardinality_restriction(self, filler: OWLClassExpression,
property: OWLObjectPropertyExpression, card: int) \
-> OWLObjectExactCardinality:
"""Create exact cardinality restriction.
Args:
filler: Filler of the restriction.
property: Property.
card: Cardinality of the restriction.
Returns:
Exact cardinality restriction.
"""
assert isinstance(property, OWLObjectPropertyExpression)
return OWLObjectExactCardinality(cardinality=card, property=property, filler=filler)
@staticmethod
def data_existential_restriction(filler: OWLDataRange, property: OWLDataPropertyExpression) \
-> OWLDataSomeValuesFrom:
"""Create data existential restriction.
Args:
filler: Filler of the restriction.
property: Property.
Returns:
Data existential restriction.
"""
assert isinstance(property, OWLDataPropertyExpression)
return OWLDataSomeValuesFrom(property=property, filler=filler)
@staticmethod
def data_universal_restriction(filler: OWLDataRange, property: OWLDataPropertyExpression) \
-> OWLDataAllValuesFrom:
"""Create data universal restriction.
Args:
filler: Filler of the restriction.
property: Property.
Returns:
Data universal restriction.
"""
assert isinstance(property, OWLDataPropertyExpression)
return OWLDataAllValuesFrom(property=property, filler=filler)
@staticmethod
def data_has_value_restriction(value: OWLLiteral, property: OWLDataPropertyExpression) \
-> OWLDataHasValue:
"""Create data has value restriction.
Args:
value: Value of the restriction.
property: Property.
Returns:
Data has value restriction.
"""
assert isinstance(property, OWLDataPropertyExpression)
return OWLDataHasValue(property=property, value=value)
def negation(self, concept: OWLClassExpression) -> OWLClassExpression:
"""Create negation of a concept.
Args:
concept: Class expression.
Returns:
Negation of concept.
"""
if concept.is_owl_thing():
return self.nothing
elif isinstance(concept, OWLObjectComplementOf):
return concept.get_operand()
else:
return concept.get_object_complement_of()
@property
def thing(self) -> OWLClass:
"""OWL Thing."""
return OWLThing
@property
def nothing(self) -> OWLClass:
"""OWL Nothing."""
return OWLNothing