fuzzy-dl-owl2 1.0.4__py3-none-any.whl → 1.0.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fuzzy_dl_owl2/fuzzydl/fuzzydl_to_owl2.py +224 -61
- fuzzy_dl_owl2/fuzzydl/util/__init__.py +1 -0
- fuzzy_dl_owl2/fuzzyowl2/fuzzyowl2.py +22 -16
- fuzzy_dl_owl2/fuzzyowl2/fuzzyowl2_to_fuzzydl.py +3 -4
- fuzzy_dl_owl2/fuzzyowl2/owl_types/modified_property.py +7 -1
- fuzzy_dl_owl2/fuzzyowl2/parser/__init__.py +2 -1
- fuzzy_dl_owl2/fuzzyowl2/parser/owl2_parser.py +22 -15
- fuzzy_dl_owl2/fuzzyowl2/parser/owl2_xml_parser.py +226 -0
- fuzzy_dl_owl2/fuzzyowl2/util/__init__.py +1 -0
- fuzzy_dl_owl2/fuzzyowl2/util/constants.py +138 -52
- fuzzy_dl_owl2/fuzzyowl2/util/fuzzy_xml.py +107 -0
- {fuzzy_dl_owl2-1.0.4.dist-info → fuzzy_dl_owl2-1.0.6.dist-info}/METADATA +2 -2
- {fuzzy_dl_owl2-1.0.4.dist-info → fuzzy_dl_owl2-1.0.6.dist-info}/RECORD +15 -13
- {fuzzy_dl_owl2-1.0.4.dist-info → fuzzy_dl_owl2-1.0.6.dist-info}/LICENSE +0 -0
- {fuzzy_dl_owl2-1.0.4.dist-info → fuzzy_dl_owl2-1.0.6.dist-info}/WHEEL +0 -0
|
@@ -6,11 +6,7 @@ import typing
|
|
|
6
6
|
|
|
7
7
|
import pyparsing as pp
|
|
8
8
|
|
|
9
|
-
from fuzzy_dl_owl2.fuzzydl.concept.qowa_concept import QowaConcept
|
|
10
9
|
from fuzzy_dl_owl2.fuzzydl.knowledge_base import KnowledgeBase
|
|
11
|
-
from fuzzy_dl_owl2.fuzzydl.modifier.linear_modifier import LinearModifier
|
|
12
|
-
from fuzzy_dl_owl2.fuzzydl.modifier.modifier import Modifier
|
|
13
|
-
from fuzzy_dl_owl2.fuzzydl.modifier.triangular_modifier import TriangularModifier
|
|
14
10
|
from fuzzy_dl_owl2.fuzzydl.query.query import Query
|
|
15
11
|
from fuzzy_dl_owl2.fuzzydl.util import utils
|
|
16
12
|
from fuzzy_dl_owl2.fuzzydl.util.config_reader import ConfigReader
|
|
@@ -18,14 +14,17 @@ from fuzzy_dl_owl2.fuzzydl.util.util import Util
|
|
|
18
14
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.choquet_concept import ChoquetConcept
|
|
19
15
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.concept_definition import ConceptDefinition
|
|
20
16
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.fuzzy_datatype import FuzzyDatatype
|
|
17
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.fuzzy_modifier import FuzzyModifier
|
|
21
18
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.fuzzy_nominal_concept import FuzzyNominalConcept
|
|
22
19
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.left_shoulder_function import (
|
|
23
20
|
LeftShoulderFunction,
|
|
24
21
|
)
|
|
25
22
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.linear_function import LinearFunction
|
|
23
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.linear_modifier import LinearModifier
|
|
26
24
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.modified_concept import ModifiedConcept
|
|
27
25
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.modified_property import ModifiedProperty
|
|
28
26
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.owa_concept import OwaConcept
|
|
27
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.qowa_concept import QowaConcept
|
|
29
28
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.quasi_sugeno_concept import QsugenoConcept
|
|
30
29
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.right_shoulder_function import (
|
|
31
30
|
RightShoulderFunction,
|
|
@@ -33,6 +32,7 @@ from fuzzy_dl_owl2.fuzzyowl2.owl_types.right_shoulder_function import (
|
|
|
33
32
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.sugeno_concept import SugenoConcept
|
|
34
33
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.trapezoidal_function import TrapezoidalFunction
|
|
35
34
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.triangular_function import TriangularFunction
|
|
35
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.triangular_modifer import TriangularModifier
|
|
36
36
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.weighted_concept import WeightedConcept
|
|
37
37
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.weighted_max_concept import WeightedMaxConcept
|
|
38
38
|
from fuzzy_dl_owl2.fuzzyowl2.owl_types.weighted_min_concept import WeightedMinConcept
|
|
@@ -66,13 +66,13 @@ def _parse_fuzzy_datatype(tokens: pp.ParseResults) -> FuzzyDatatype:
|
|
|
66
66
|
return tokens
|
|
67
67
|
|
|
68
68
|
|
|
69
|
-
def _parse_modifier_function(tokens: pp.ParseResults) ->
|
|
69
|
+
def _parse_modifier_function(tokens: pp.ParseResults) -> FuzzyModifier:
|
|
70
70
|
Util.debug(f"_parse_modifier_function -> {tokens}")
|
|
71
71
|
list_tokens: list = tokens.as_list()
|
|
72
72
|
if list_tokens[0] == FuzzyOWL2Keyword.LINEAR:
|
|
73
|
-
return LinearModifier(
|
|
73
|
+
return LinearModifier(list_tokens[1])
|
|
74
74
|
elif list_tokens[0] == FuzzyOWL2Keyword.TRIANGULAR:
|
|
75
|
-
return TriangularModifier(
|
|
75
|
+
return TriangularModifier(list_tokens[1], list_tokens[2], list_tokens[3])
|
|
76
76
|
return tokens
|
|
77
77
|
|
|
78
78
|
|
|
@@ -103,11 +103,9 @@ def _parse_fuzzy_nominal(tokens: pp.ParseResults) -> ConceptDefinition:
|
|
|
103
103
|
def _parse_weighted_complex_concept(tokens: pp.ParseResults) -> ConceptDefinition:
|
|
104
104
|
Util.debug(f"_parse_weighted_complex_concept -> {tokens}")
|
|
105
105
|
list_tokens: list = tokens.as_list()
|
|
106
|
-
assert isinstance(list_tokens[1]
|
|
107
|
-
isinstance(a, WeightedConcept) for a in list_tokens[1]
|
|
108
|
-
)
|
|
106
|
+
assert all(isinstance(a, WeightedConcept) for a in list_tokens[1:])
|
|
109
107
|
wc: list[WeightedConcept] = [
|
|
110
|
-
typing.cast(WeightedConcept, w) for w in list_tokens[1]
|
|
108
|
+
typing.cast(WeightedConcept, w) for w in list_tokens[1:]
|
|
111
109
|
]
|
|
112
110
|
if list_tokens[0] == FuzzyOWL2Keyword.WEIGHTED_MAXIMUM:
|
|
113
111
|
return WeightedMaxConcept(wc)
|
|
@@ -221,7 +219,10 @@ class FuzzyOwl2Parser(object):
|
|
|
221
219
|
concept = pp.Forward()
|
|
222
220
|
|
|
223
221
|
modified_role_concept = (
|
|
224
|
-
FuzzyOWL2Keyword.
|
|
222
|
+
FuzzyOWL2Keyword.CONCEPT.get_value().suppress()
|
|
223
|
+
+ FuzzyOWL2Keyword.TYPE.get_value().suppress()
|
|
224
|
+
+ FuzzyOWL2Keyword.EQUAL.get_value().suppress()
|
|
225
|
+
+ FuzzyOWL2Keyword.MODIFIED.get_value().suppress()
|
|
225
226
|
+ FuzzyOWL2Keyword.MODIFIER.get_value().suppress()
|
|
226
227
|
+ FuzzyOWL2Keyword.EQUAL.get_value().suppress()
|
|
227
228
|
+ variables
|
|
@@ -231,13 +232,18 @@ class FuzzyOwl2Parser(object):
|
|
|
231
232
|
).add_parse_action(_parse_modified_concept)
|
|
232
233
|
|
|
233
234
|
weighted_concept = (
|
|
234
|
-
|
|
235
|
+
open_tag
|
|
236
|
+
+ FuzzyOWL2Keyword.CONCEPT.get_value().suppress()
|
|
237
|
+
+ FuzzyOWL2Keyword.TYPE.get_value().suppress()
|
|
238
|
+
+ FuzzyOWL2Keyword.EQUAL.get_value().suppress()
|
|
239
|
+
+ FuzzyOWL2Keyword.WEIGHTED.get_value().suppress()
|
|
235
240
|
+ FuzzyOWL2Keyword.DEGREE_VALUE.get_value().suppress()
|
|
236
241
|
+ FuzzyOWL2Keyword.EQUAL.get_value().suppress()
|
|
237
242
|
+ numbers
|
|
238
243
|
+ FuzzyOWL2Keyword.BASE.get_value().suppress()
|
|
239
244
|
+ FuzzyOWL2Keyword.EQUAL.get_value().suppress()
|
|
240
245
|
+ variables
|
|
246
|
+
+ single_close_tag
|
|
241
247
|
).add_parse_action(_parse_weighted_concept)
|
|
242
248
|
|
|
243
249
|
weights = (
|
|
@@ -280,7 +286,6 @@ class FuzzyOwl2Parser(object):
|
|
|
280
286
|
+ close_tag
|
|
281
287
|
)
|
|
282
288
|
|
|
283
|
-
weighted_list_concept = close_tag + weights + concepts
|
|
284
289
|
q_owa_concept = (
|
|
285
290
|
FuzzyOWL2Keyword.QUANTIFIER.get_value().suppress()
|
|
286
291
|
+ FuzzyOWL2Keyword.EQUAL.get_value().suppress()
|
|
@@ -318,7 +323,9 @@ class FuzzyOwl2Parser(object):
|
|
|
318
323
|
| FuzzyOWL2Keyword.SUGENO.get_value()
|
|
319
324
|
| FuzzyOWL2Keyword.QUASI_SUGENO.get_value()
|
|
320
325
|
)
|
|
321
|
-
+
|
|
326
|
+
+ close_tag
|
|
327
|
+
+ weights
|
|
328
|
+
+ concepts
|
|
322
329
|
).add_parse_action(_parse_integral_concept)
|
|
323
330
|
| FuzzyOWL2Keyword.Q_OWA.get_value().suppress() + q_owa_concept
|
|
324
331
|
)
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import traceback
|
|
5
|
+
import typing
|
|
6
|
+
from xml.etree import ElementTree
|
|
7
|
+
|
|
8
|
+
import pyparsing as pp
|
|
9
|
+
|
|
10
|
+
from fuzzy_dl_owl2.fuzzydl.util.config_reader import ConfigReader
|
|
11
|
+
from fuzzy_dl_owl2.fuzzydl.util.util import Util
|
|
12
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.choquet_concept import ChoquetConcept
|
|
13
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.fuzzy_nominal_concept import FuzzyNominalConcept
|
|
14
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.left_shoulder_function import (
|
|
15
|
+
LeftShoulderFunction,
|
|
16
|
+
)
|
|
17
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.linear_function import LinearFunction
|
|
18
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.linear_modifier import LinearModifier
|
|
19
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.modified_concept import ModifiedConcept
|
|
20
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.modified_property import ModifiedProperty
|
|
21
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.owa_concept import OwaConcept
|
|
22
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.qowa_concept import QowaConcept
|
|
23
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.quasi_sugeno_concept import QsugenoConcept
|
|
24
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.right_shoulder_function import (
|
|
25
|
+
RightShoulderFunction,
|
|
26
|
+
)
|
|
27
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.sugeno_concept import SugenoConcept
|
|
28
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.trapezoidal_function import TrapezoidalFunction
|
|
29
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.triangular_function import TriangularFunction
|
|
30
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.triangular_modifer import TriangularModifier
|
|
31
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.weighted_concept import WeightedConcept
|
|
32
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.weighted_max_concept import WeightedMaxConcept
|
|
33
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.weighted_min_concept import WeightedMinConcept
|
|
34
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.weighted_sum_concept import WeightedSumConcept
|
|
35
|
+
from fuzzy_dl_owl2.fuzzyowl2.owl_types.weighted_sum_zero_concept import (
|
|
36
|
+
WeightedSumZeroConcept,
|
|
37
|
+
)
|
|
38
|
+
from fuzzy_dl_owl2.fuzzyowl2.util.constants import FuzzyOWL2Keyword
|
|
39
|
+
from fuzzy_dl_owl2.fuzzyowl2.util.fuzzy_xml import FuzzyXML
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class FuzzyOwl2XMLParser(object):
|
|
43
|
+
|
|
44
|
+
@staticmethod
|
|
45
|
+
def get_caseless_attrib(attrib: dict[str, str], key: str) -> typing.Optional[str]:
|
|
46
|
+
keys = [k for k in attrib if k.lower() == key.lower()]
|
|
47
|
+
return attrib.get(keys[0]) if len(keys) > 0 else None
|
|
48
|
+
|
|
49
|
+
@staticmethod
|
|
50
|
+
def parse_string(
|
|
51
|
+
instring: str,
|
|
52
|
+
) -> pp.ParseResults:
|
|
53
|
+
root: ElementTree.Element = ElementTree.fromstring(instring)
|
|
54
|
+
Util.debug(f"XML PARSER -> {FuzzyXML.to_str(root)}")
|
|
55
|
+
assert root.tag == FuzzyOWL2Keyword.FUZZY_OWL_2
|
|
56
|
+
|
|
57
|
+
annotation_type: str = root.attrib.get(
|
|
58
|
+
FuzzyOWL2Keyword.FUZZY_TYPE.get_str_value()
|
|
59
|
+
)
|
|
60
|
+
if annotation_type == FuzzyOWL2Keyword.CONCEPT:
|
|
61
|
+
child = root.find(FuzzyOWL2Keyword.CONCEPT.get_tag_name())
|
|
62
|
+
concept_type = child.attrib.get(FuzzyOWL2Keyword.TYPE.get_str_value())
|
|
63
|
+
if concept_type == FuzzyOWL2Keyword.MODIFIED:
|
|
64
|
+
return ModifiedConcept(
|
|
65
|
+
child.attrib.get(FuzzyOWL2Keyword.MODIFIER.get_str_value()),
|
|
66
|
+
child.attrib.get(FuzzyOWL2Keyword.BASE.get_str_value()),
|
|
67
|
+
)
|
|
68
|
+
elif concept_type == FuzzyOWL2Keyword.WEIGHTED:
|
|
69
|
+
return WeightedConcept(
|
|
70
|
+
float(
|
|
71
|
+
child.attrib.get(FuzzyOWL2Keyword.DEGREE_VALUE.get_str_value())
|
|
72
|
+
),
|
|
73
|
+
child.attrib.get(FuzzyOWL2Keyword.BASE.get_str_value()),
|
|
74
|
+
)
|
|
75
|
+
elif concept_type in (
|
|
76
|
+
FuzzyOWL2Keyword.WEIGHTED_MINIMUM,
|
|
77
|
+
FuzzyOWL2Keyword.WEIGHTED_MAXIMUM,
|
|
78
|
+
FuzzyOWL2Keyword.WEIGHTED_SUM,
|
|
79
|
+
FuzzyOWL2Keyword.WEIGHTED_SUMZERO,
|
|
80
|
+
):
|
|
81
|
+
wc: list[WeightedConcept] = [
|
|
82
|
+
WeightedConcept(
|
|
83
|
+
float(
|
|
84
|
+
inner_child.attrib.get(
|
|
85
|
+
FuzzyOWL2Keyword.DEGREE_VALUE.get_str_value()
|
|
86
|
+
)
|
|
87
|
+
),
|
|
88
|
+
inner_child.attrib.get(FuzzyOWL2Keyword.BASE.get_str_value()),
|
|
89
|
+
)
|
|
90
|
+
for inner_child in child
|
|
91
|
+
if inner_child.tag == FuzzyOWL2Keyword.CONCEPT.get_tag_name()
|
|
92
|
+
and inner_child.attrib.get(FuzzyOWL2Keyword.TYPE.get_str_value())
|
|
93
|
+
== FuzzyOWL2Keyword.WEIGHTED
|
|
94
|
+
]
|
|
95
|
+
if concept_type == FuzzyOWL2Keyword.WEIGHTED_MAXIMUM:
|
|
96
|
+
return WeightedMaxConcept(wc)
|
|
97
|
+
elif concept_type == FuzzyOWL2Keyword.WEIGHTED_MINIMUM:
|
|
98
|
+
return WeightedMinConcept(wc)
|
|
99
|
+
elif concept_type == FuzzyOWL2Keyword.WEIGHTED_SUM:
|
|
100
|
+
return WeightedSumConcept(wc)
|
|
101
|
+
elif concept_type == FuzzyOWL2Keyword.WEIGHTED_SUMZERO:
|
|
102
|
+
return WeightedSumZeroConcept(wc)
|
|
103
|
+
elif concept_type in (
|
|
104
|
+
FuzzyOWL2Keyword.OWA,
|
|
105
|
+
FuzzyOWL2Keyword.CHOQUET,
|
|
106
|
+
FuzzyOWL2Keyword.SUGENO,
|
|
107
|
+
FuzzyOWL2Keyword.QUASI_SUGENO,
|
|
108
|
+
):
|
|
109
|
+
weights: list[float] = [
|
|
110
|
+
float(weight.text)
|
|
111
|
+
for weight in child.find(
|
|
112
|
+
FuzzyOWL2Keyword.WEIGHTS.get_tag_name()
|
|
113
|
+
).findall(FuzzyOWL2Keyword.WEIGHT.get_tag_name())
|
|
114
|
+
]
|
|
115
|
+
concepts: list[str] = [
|
|
116
|
+
name.text
|
|
117
|
+
for name in child.find(
|
|
118
|
+
FuzzyOWL2Keyword.CONCEPT_NAMES.get_tag_name()
|
|
119
|
+
).findall(FuzzyOWL2Keyword.NAME.get_tag_name())
|
|
120
|
+
]
|
|
121
|
+
if concept_type == FuzzyOWL2Keyword.OWA:
|
|
122
|
+
return OwaConcept(weights, concepts)
|
|
123
|
+
elif concept_type == FuzzyOWL2Keyword.CHOQUET:
|
|
124
|
+
return ChoquetConcept(weights, concepts)
|
|
125
|
+
elif concept_type == FuzzyOWL2Keyword.SUGENO:
|
|
126
|
+
return SugenoConcept(weights, concepts)
|
|
127
|
+
elif concept_type == FuzzyOWL2Keyword.QUASI_SUGENO:
|
|
128
|
+
return QsugenoConcept(weights, concepts)
|
|
129
|
+
elif concept_type == FuzzyOWL2Keyword.Q_OWA:
|
|
130
|
+
quantifier = child.attrib.get(
|
|
131
|
+
FuzzyOWL2Keyword.QUANTIFIER.get_str_value()
|
|
132
|
+
)
|
|
133
|
+
concepts: list[str] = [
|
|
134
|
+
name.text
|
|
135
|
+
for name in child.find(
|
|
136
|
+
FuzzyOWL2Keyword.CONCEPT_NAMES.get_tag_name()
|
|
137
|
+
).findall(FuzzyOWL2Keyword.NAME.get_tag_name())
|
|
138
|
+
]
|
|
139
|
+
return QowaConcept(quantifier, concepts)
|
|
140
|
+
elif concept_type == FuzzyOWL2Keyword.NOMINAL:
|
|
141
|
+
return FuzzyNominalConcept(
|
|
142
|
+
float(
|
|
143
|
+
child.attrib.get(FuzzyOWL2Keyword.DEGREE_DEF.get_str_value())
|
|
144
|
+
),
|
|
145
|
+
child.attrib.get(FuzzyOWL2Keyword.INDIVIDUAL.get_str_value()),
|
|
146
|
+
)
|
|
147
|
+
elif annotation_type == FuzzyOWL2Keyword.DATATYPE:
|
|
148
|
+
child = root.find(FuzzyOWL2Keyword.DATATYPE.get_tag_name())
|
|
149
|
+
datatype_type = child.attrib.get(FuzzyOWL2Keyword.TYPE.get_str_value())
|
|
150
|
+
|
|
151
|
+
if datatype_type == FuzzyOWL2Keyword.LEFT_SHOULDER:
|
|
152
|
+
return LeftShoulderFunction(
|
|
153
|
+
float(child.attrib.get(FuzzyOWL2Keyword.A.get_str_value())),
|
|
154
|
+
float(child.attrib.get(FuzzyOWL2Keyword.B.get_str_value())),
|
|
155
|
+
)
|
|
156
|
+
elif datatype_type == FuzzyOWL2Keyword.RIGHT_SHOULDER:
|
|
157
|
+
return RightShoulderFunction(
|
|
158
|
+
float(child.attrib.get(FuzzyOWL2Keyword.A.get_str_value())),
|
|
159
|
+
float(child.attrib.get(FuzzyOWL2Keyword.B.get_str_value())),
|
|
160
|
+
)
|
|
161
|
+
elif datatype_type == FuzzyOWL2Keyword.LINEAR:
|
|
162
|
+
return LinearFunction(
|
|
163
|
+
float(child.attrib.get(FuzzyOWL2Keyword.A.get_str_value())),
|
|
164
|
+
float(child.attrib.get(FuzzyOWL2Keyword.B.get_str_value())),
|
|
165
|
+
)
|
|
166
|
+
elif datatype_type == FuzzyOWL2Keyword.TRIANGULAR:
|
|
167
|
+
return TriangularFunction(
|
|
168
|
+
float(child.attrib.get(FuzzyOWL2Keyword.A.get_str_value())),
|
|
169
|
+
float(child.attrib.get(FuzzyOWL2Keyword.B.get_str_value())),
|
|
170
|
+
float(child.attrib.get(FuzzyOWL2Keyword.C.get_str_value())),
|
|
171
|
+
)
|
|
172
|
+
elif datatype_type == FuzzyOWL2Keyword.TRAPEZOIDAL:
|
|
173
|
+
return TrapezoidalFunction(
|
|
174
|
+
float(child.attrib.get(FuzzyOWL2Keyword.A.get_str_value())),
|
|
175
|
+
float(child.attrib.get(FuzzyOWL2Keyword.B.get_str_value())),
|
|
176
|
+
float(child.attrib.get(FuzzyOWL2Keyword.C.get_str_value())),
|
|
177
|
+
float(child.attrib.get(FuzzyOWL2Keyword.D.get_str_value())),
|
|
178
|
+
)
|
|
179
|
+
elif annotation_type == FuzzyOWL2Keyword.MODIFIER:
|
|
180
|
+
child = root.find(FuzzyOWL2Keyword.MODIFIER.get_tag_name())
|
|
181
|
+
mod_type = child.attrib.get(FuzzyOWL2Keyword.TYPE.get_str_value())
|
|
182
|
+
if mod_type == FuzzyOWL2Keyword.LINEAR:
|
|
183
|
+
return LinearModifier(
|
|
184
|
+
float(child.attrib.get(FuzzyOWL2Keyword.C.get_str_value())),
|
|
185
|
+
)
|
|
186
|
+
elif mod_type == FuzzyOWL2Keyword.TRIANGULAR:
|
|
187
|
+
return TriangularModifier(
|
|
188
|
+
float(child.attrib.get(FuzzyOWL2Keyword.A.get_str_value())),
|
|
189
|
+
float(child.attrib.get(FuzzyOWL2Keyword.B.get_str_value())),
|
|
190
|
+
float(child.attrib.get(FuzzyOWL2Keyword.C.get_str_value())),
|
|
191
|
+
)
|
|
192
|
+
elif annotation_type == FuzzyOWL2Keyword.AXIOM:
|
|
193
|
+
child = root.find(FuzzyOWL2Keyword.DEGREE_DEF.get_tag_name())
|
|
194
|
+
return float(
|
|
195
|
+
child.attrib.get(FuzzyOWL2Keyword.DEGREE_VALUE.get_str_value())
|
|
196
|
+
)
|
|
197
|
+
elif annotation_type == FuzzyOWL2Keyword.ONTOLOGY:
|
|
198
|
+
child = root.find(FuzzyOWL2Keyword.FUZZY_LOGIC.get_tag_name())
|
|
199
|
+
return child.attrib.get(FuzzyOWL2Keyword.LOGIC.get_str_value())
|
|
200
|
+
elif annotation_type == FuzzyOWL2Keyword.ROLE:
|
|
201
|
+
child = root.find(FuzzyOWL2Keyword.ROLE.get_tag_name())
|
|
202
|
+
assert (
|
|
203
|
+
child.attrib.get(FuzzyOWL2Keyword.TYPE.get_str_value())
|
|
204
|
+
== FuzzyOWL2Keyword.MODIFIED
|
|
205
|
+
)
|
|
206
|
+
return ModifiedProperty(
|
|
207
|
+
child.attrib.get(FuzzyOWL2Keyword.MODIFIER.get_str_value()),
|
|
208
|
+
child.attrib.get(FuzzyOWL2Keyword.BASE.get_str_value()),
|
|
209
|
+
)
|
|
210
|
+
else:
|
|
211
|
+
raise ValueError
|
|
212
|
+
|
|
213
|
+
@staticmethod
|
|
214
|
+
def load_config(*args) -> None:
|
|
215
|
+
ConfigReader.load_parameters(os.path.join(os.getcwd(), "CONFIG.ini"), args)
|
|
216
|
+
|
|
217
|
+
@staticmethod
|
|
218
|
+
def main(annotation: str, *args) -> typing.Any:
|
|
219
|
+
try:
|
|
220
|
+
FuzzyOwl2XMLParser.load_config(*args)
|
|
221
|
+
return FuzzyOwl2XMLParser.parse_string(annotation)
|
|
222
|
+
except FileNotFoundError as e:
|
|
223
|
+
Util.error(f"Error: File {args[0]} not found.")
|
|
224
|
+
except Exception as e:
|
|
225
|
+
Util.error(e)
|
|
226
|
+
Util.error(traceback.format_exc())
|
|
@@ -26,63 +26,149 @@ class ConceptType(enum.StrEnum):
|
|
|
26
26
|
return self.name
|
|
27
27
|
|
|
28
28
|
|
|
29
|
+
# class FuzzyOWL2Keyword(enum.Enum):
|
|
30
|
+
# OPEN_TAG = pp.Word("<")
|
|
31
|
+
# CLOSE_TAG = pp.Word(">")
|
|
32
|
+
# SINGLE_CLOSE_TAG = pp.Word("/>")
|
|
33
|
+
# SLASH = pp.Word("/")
|
|
34
|
+
# ONTOLOGY = pp.CaselessKeyword("ontology")
|
|
35
|
+
# FUZZY_OWL_2 = pp.CaselessKeyword("fuzzyOwl2")
|
|
36
|
+
# FUZZY_LABEL = pp.CaselessKeyword("fuzzyLabel")
|
|
37
|
+
# FUZZY_TYPE = pp.CaselessKeyword("fuzzytype")
|
|
38
|
+
# FUZZY_LOGIC = pp.CaselessKeyword("FuzzyLogic")
|
|
39
|
+
# TYPE = pp.CaselessKeyword("type")
|
|
40
|
+
# LOGIC = pp.CaselessKeyword("logic")
|
|
41
|
+
# DATATYPE = pp.CaselessKeyword("datatype")
|
|
42
|
+
# CONCEPT = pp.CaselessKeyword("concept")
|
|
43
|
+
# ROLE = pp.CaselessKeyword("role")
|
|
44
|
+
# AXIOM = pp.CaselessKeyword("axiom")
|
|
45
|
+
# DEGREE_DEF = pp.CaselessKeyword("degree")
|
|
46
|
+
# DEGREE_VALUE = pp.CaselessKeyword("value")
|
|
47
|
+
# MODIFIED = pp.CaselessKeyword("modified")
|
|
48
|
+
# WEIGHTED = pp.CaselessKeyword("weighted")
|
|
49
|
+
# NOMINAL = pp.CaselessKeyword("nominal")
|
|
50
|
+
# INDIVIDUAL = pp.CaselessKeyword("individual")
|
|
51
|
+
# WEIGHTED_MAXIMUM = pp.CaselessKeyword("weightedMaximum")
|
|
52
|
+
# WEIGHTED_MINIMUM = pp.CaselessKeyword("weightedMinimum")
|
|
53
|
+
# WEIGHTED_SUM = pp.CaselessKeyword("weightedSum")
|
|
54
|
+
# WEIGHTED_SUMZERO = pp.CaselessKeyword("weightedSumZero")
|
|
55
|
+
# OWA = pp.CaselessKeyword("owa")
|
|
56
|
+
# Q_OWA = pp.CaselessKeyword("qowa")
|
|
57
|
+
# CHOQUET = pp.CaselessKeyword("choquet")
|
|
58
|
+
# SUGENO = pp.CaselessKeyword("sugeno")
|
|
59
|
+
# QUASI_SUGENO = pp.CaselessKeyword("quasisugeno")
|
|
60
|
+
# MODIFIER = pp.CaselessKeyword("modifier")
|
|
61
|
+
# BASE = pp.CaselessKeyword("base")
|
|
62
|
+
# CONCEPT_NAMES = pp.CaselessKeyword("names")
|
|
63
|
+
# NAME = pp.CaselessKeyword("name")
|
|
64
|
+
# WEIGHT = pp.CaselessKeyword("weight")
|
|
65
|
+
# WEIGHTS = pp.CaselessKeyword("weights")
|
|
66
|
+
# QUANTIFIER = pp.CaselessKeyword("quantifier")
|
|
67
|
+
# CRISP = pp.CaselessKeyword("crisp")
|
|
68
|
+
# LEFT_SHOULDER = pp.CaselessKeyword("leftshoulder")
|
|
69
|
+
# RIGHT_SHOULDER = pp.CaselessKeyword("rightshoulder")
|
|
70
|
+
# TRIANGULAR = pp.CaselessKeyword("triangular")
|
|
71
|
+
# TRAPEZOIDAL = pp.CaselessKeyword("trapezoidal")
|
|
72
|
+
# LINEAR = pp.CaselessKeyword("linear")
|
|
73
|
+
# A = pp.CaselessKeyword("a")
|
|
74
|
+
# B = pp.CaselessKeyword("b")
|
|
75
|
+
# C = pp.CaselessKeyword("c")
|
|
76
|
+
# D = pp.CaselessKeyword("d")
|
|
77
|
+
# LUKASIEWICZ = pp.CaselessKeyword("lukasiewicz")
|
|
78
|
+
# GOEDEL = pp.CaselessKeyword("goedel")
|
|
79
|
+
# ZADEH = pp.CaselessKeyword("zadeh")
|
|
80
|
+
# PRODUCT = pp.CaselessKeyword("product")
|
|
81
|
+
# EQUAL = pp.Word("=")
|
|
82
|
+
# LES = pp.CaselessKeyword("les")
|
|
83
|
+
# LEQ = pp.CaselessKeyword("leq")
|
|
84
|
+
# GEQ = pp.CaselessKeyword("geq")
|
|
85
|
+
# GRE = pp.CaselessKeyword("gre")
|
|
86
|
+
|
|
87
|
+
# def get_name(self) -> str:
|
|
88
|
+
# return re.sub(r"[\"\']+", "", self.value.name.lower())
|
|
89
|
+
|
|
90
|
+
# def get_value(self) -> typing.Union[pp.CaselessKeyword, pp.Word]:
|
|
91
|
+
# return self.value
|
|
92
|
+
|
|
93
|
+
# def get_str_value(self) -> str:
|
|
94
|
+
# return str(self.value).replace('"', "").replace("'", "")
|
|
95
|
+
|
|
96
|
+
# def get_tag_name(self) -> str:
|
|
97
|
+
# return self.get_str_value().capitalize()
|
|
98
|
+
|
|
99
|
+
# def __eq__(self, value: object) -> bool:
|
|
100
|
+
# if isinstance(value, str):
|
|
101
|
+
# return self.get_name() == value.lower()
|
|
102
|
+
# elif isinstance(value, pp.CaselessKeyword):
|
|
103
|
+
# return self.get_name() == value.name.lower()
|
|
104
|
+
# elif isinstance(value, FuzzyOWL2Keyword):
|
|
105
|
+
# return self.get_name() == value.get_name()
|
|
106
|
+
# raise NotImplementedError
|
|
107
|
+
|
|
108
|
+
# def __repr__(self) -> str:
|
|
109
|
+
# return self.name
|
|
110
|
+
|
|
111
|
+
# def __str__(self) -> str:
|
|
112
|
+
# return self.name
|
|
113
|
+
|
|
114
|
+
|
|
29
115
|
class FuzzyOWL2Keyword(enum.Enum):
|
|
30
116
|
OPEN_TAG = pp.Word("<")
|
|
31
117
|
CLOSE_TAG = pp.Word(">")
|
|
32
118
|
SINGLE_CLOSE_TAG = pp.Word("/>")
|
|
33
119
|
SLASH = pp.Word("/")
|
|
34
|
-
ONTOLOGY = pp.
|
|
35
|
-
FUZZY_OWL_2 = pp.
|
|
36
|
-
FUZZY_LABEL = pp.
|
|
37
|
-
FUZZY_TYPE = pp.
|
|
38
|
-
FUZZY_LOGIC = pp.
|
|
39
|
-
TYPE = pp.
|
|
40
|
-
LOGIC = pp.
|
|
41
|
-
DATATYPE = pp.
|
|
42
|
-
CONCEPT = pp.
|
|
43
|
-
ROLE = pp.
|
|
44
|
-
AXIOM = pp.
|
|
45
|
-
DEGREE_DEF = pp.
|
|
46
|
-
DEGREE_VALUE = pp.
|
|
47
|
-
MODIFIED = pp.
|
|
48
|
-
WEIGHTED = pp.
|
|
49
|
-
NOMINAL = pp.
|
|
50
|
-
INDIVIDUAL = pp.
|
|
51
|
-
WEIGHTED_MAXIMUM = pp.
|
|
52
|
-
WEIGHTED_MINIMUM = pp.
|
|
53
|
-
WEIGHTED_SUM = pp.
|
|
54
|
-
WEIGHTED_SUMZERO = pp.
|
|
55
|
-
OWA = pp.
|
|
56
|
-
Q_OWA = pp.
|
|
57
|
-
CHOQUET = pp.
|
|
58
|
-
SUGENO = pp.
|
|
59
|
-
QUASI_SUGENO = pp.
|
|
60
|
-
MODIFIER = pp.
|
|
61
|
-
BASE = pp.
|
|
62
|
-
CONCEPT_NAMES = pp.
|
|
63
|
-
NAME = pp.
|
|
64
|
-
WEIGHT = pp.
|
|
65
|
-
WEIGHTS = pp.
|
|
66
|
-
QUANTIFIER = pp.
|
|
67
|
-
CRISP = pp.
|
|
68
|
-
LEFT_SHOULDER = pp.
|
|
69
|
-
RIGHT_SHOULDER = pp.
|
|
70
|
-
TRIANGULAR = pp.
|
|
71
|
-
TRAPEZOIDAL = pp.
|
|
72
|
-
LINEAR = pp.
|
|
73
|
-
A = pp.
|
|
74
|
-
B = pp.
|
|
75
|
-
C = pp.
|
|
76
|
-
D = pp.
|
|
77
|
-
LUKASIEWICZ = pp.
|
|
78
|
-
GOEDEL = pp.
|
|
79
|
-
ZADEH = pp.
|
|
80
|
-
PRODUCT = pp.
|
|
120
|
+
ONTOLOGY = pp.Keyword("ontology")
|
|
121
|
+
FUZZY_OWL_2 = pp.Keyword("fuzzyOwl2")
|
|
122
|
+
FUZZY_LABEL = pp.Keyword("fuzzyLabel")
|
|
123
|
+
FUZZY_TYPE = pp.Keyword("fuzzyType")
|
|
124
|
+
FUZZY_LOGIC = pp.Keyword("FuzzyLogic")
|
|
125
|
+
TYPE = pp.Keyword("type")
|
|
126
|
+
LOGIC = pp.Keyword("logic")
|
|
127
|
+
DATATYPE = pp.Keyword("datatype")
|
|
128
|
+
CONCEPT = pp.Keyword("concept")
|
|
129
|
+
ROLE = pp.Keyword("role")
|
|
130
|
+
AXIOM = pp.Keyword("axiom")
|
|
131
|
+
DEGREE_DEF = pp.Keyword("degree")
|
|
132
|
+
DEGREE_VALUE = pp.Keyword("value")
|
|
133
|
+
MODIFIED = pp.Keyword("modified")
|
|
134
|
+
WEIGHTED = pp.Keyword("weighted")
|
|
135
|
+
NOMINAL = pp.Keyword("nominal")
|
|
136
|
+
INDIVIDUAL = pp.Keyword("individual")
|
|
137
|
+
WEIGHTED_MAXIMUM = pp.Keyword("weightedMaximum")
|
|
138
|
+
WEIGHTED_MINIMUM = pp.Keyword("weightedMinimum")
|
|
139
|
+
WEIGHTED_SUM = pp.Keyword("weightedSum")
|
|
140
|
+
WEIGHTED_SUMZERO = pp.Keyword("weightedSumZero")
|
|
141
|
+
OWA = pp.Keyword("owa")
|
|
142
|
+
Q_OWA = pp.Keyword("qowa")
|
|
143
|
+
CHOQUET = pp.Keyword("choquet")
|
|
144
|
+
SUGENO = pp.Keyword("sugeno")
|
|
145
|
+
QUASI_SUGENO = pp.Keyword("quasisugeno")
|
|
146
|
+
MODIFIER = pp.Keyword("modifier")
|
|
147
|
+
BASE = pp.Keyword("base")
|
|
148
|
+
CONCEPT_NAMES = pp.Keyword("names")
|
|
149
|
+
NAME = pp.Keyword("name")
|
|
150
|
+
WEIGHT = pp.Keyword("weight")
|
|
151
|
+
WEIGHTS = pp.Keyword("weights")
|
|
152
|
+
QUANTIFIER = pp.Keyword("quantifier")
|
|
153
|
+
CRISP = pp.Keyword("crisp")
|
|
154
|
+
LEFT_SHOULDER = pp.Keyword("leftshoulder")
|
|
155
|
+
RIGHT_SHOULDER = pp.Keyword("rightshoulder")
|
|
156
|
+
TRIANGULAR = pp.Keyword("triangular")
|
|
157
|
+
TRAPEZOIDAL = pp.Keyword("trapezoidal")
|
|
158
|
+
LINEAR = pp.Keyword("linear")
|
|
159
|
+
A = pp.Keyword("a")
|
|
160
|
+
B = pp.Keyword("b")
|
|
161
|
+
C = pp.Keyword("c")
|
|
162
|
+
D = pp.Keyword("d")
|
|
163
|
+
LUKASIEWICZ = pp.Keyword("lukasiewicz")
|
|
164
|
+
GOEDEL = pp.Keyword("goedel")
|
|
165
|
+
ZADEH = pp.Keyword("zadeh")
|
|
166
|
+
PRODUCT = pp.Keyword("product")
|
|
81
167
|
EQUAL = pp.Word("=")
|
|
82
|
-
LES = pp.
|
|
83
|
-
LEQ = pp.
|
|
84
|
-
GEQ = pp.
|
|
85
|
-
GRE = pp.
|
|
168
|
+
LES = pp.Keyword("les")
|
|
169
|
+
LEQ = pp.Keyword("leq")
|
|
170
|
+
GEQ = pp.Keyword("geq")
|
|
171
|
+
GRE = pp.Keyword("gre")
|
|
86
172
|
|
|
87
173
|
def get_name(self) -> str:
|
|
88
174
|
return re.sub(r"[\"\']+", "", self.value.name.lower())
|
|
@@ -99,7 +185,7 @@ class FuzzyOWL2Keyword(enum.Enum):
|
|
|
99
185
|
def __eq__(self, value: object) -> bool:
|
|
100
186
|
if isinstance(value, str):
|
|
101
187
|
return self.get_name() == value.lower()
|
|
102
|
-
elif isinstance(value, pp.
|
|
188
|
+
elif isinstance(value, pp.Keyword):
|
|
103
189
|
return self.get_name() == value.name.lower()
|
|
104
190
|
elif isinstance(value, FuzzyOWL2Keyword):
|
|
105
191
|
return self.get_name() == value.get_name()
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
import typing
|
|
5
|
+
from xml.dom import minidom
|
|
6
|
+
from xml.etree.ElementTree import Element, tostring
|
|
7
|
+
|
|
8
|
+
from fuzzy_dl_owl2.fuzzyowl2.util.constants import FuzzyOWL2Keyword
|
|
9
|
+
from pyowl2.abstracts.class_expression import OWLClassExpression
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class FuzzyXML(object):
|
|
13
|
+
|
|
14
|
+
@staticmethod
|
|
15
|
+
def build_main_xml(fuzzy_type: str) -> Element:
|
|
16
|
+
return Element(
|
|
17
|
+
FuzzyOWL2Keyword.FUZZY_OWL_2.get_str_value(),
|
|
18
|
+
attrib={FuzzyOWL2Keyword.FUZZY_TYPE.get_str_value(): fuzzy_type},
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
@staticmethod
|
|
22
|
+
def build_logic_xml(logic: str, attrib: dict[str, str] = dict()) -> Element:
|
|
23
|
+
full_attrib = {FuzzyOWL2Keyword.LOGIC.get_str_value(): logic}
|
|
24
|
+
if attrib:
|
|
25
|
+
full_attrib.update(attrib)
|
|
26
|
+
return Element(
|
|
27
|
+
FuzzyOWL2Keyword.FUZZY_LOGIC.get_tag_name(),
|
|
28
|
+
attrib=full_attrib,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
@staticmethod
|
|
32
|
+
def build_datatype_xml(
|
|
33
|
+
datatype_type: str, attrib: dict[str, str] = dict()
|
|
34
|
+
) -> Element:
|
|
35
|
+
full_attrib = {FuzzyOWL2Keyword.TYPE.get_str_value(): datatype_type}
|
|
36
|
+
if attrib:
|
|
37
|
+
full_attrib.update(attrib)
|
|
38
|
+
return Element(
|
|
39
|
+
FuzzyOWL2Keyword.DATATYPE.get_tag_name(),
|
|
40
|
+
attrib=full_attrib,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
@staticmethod
|
|
44
|
+
def build_modifier_xml(
|
|
45
|
+
modifier_type: str, attrib: dict[str, str] = dict()
|
|
46
|
+
) -> Element:
|
|
47
|
+
full_attrib = {FuzzyOWL2Keyword.TYPE.get_str_value(): modifier_type}
|
|
48
|
+
if attrib:
|
|
49
|
+
full_attrib.update(attrib)
|
|
50
|
+
return Element(
|
|
51
|
+
FuzzyOWL2Keyword.MODIFIER.get_tag_name(),
|
|
52
|
+
attrib=full_attrib,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
@staticmethod
|
|
56
|
+
def build_degree_xml(
|
|
57
|
+
value: typing.Union[int, float], attrib: dict[str, str] = dict()
|
|
58
|
+
) -> Element:
|
|
59
|
+
full_attrib = {FuzzyOWL2Keyword.DEGREE_VALUE.get_str_value(): str(value)}
|
|
60
|
+
if attrib:
|
|
61
|
+
full_attrib.update(attrib)
|
|
62
|
+
return Element(
|
|
63
|
+
FuzzyOWL2Keyword.DEGREE_DEF.get_tag_name(),
|
|
64
|
+
attrib=full_attrib,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
@staticmethod
|
|
68
|
+
def build_concept_xml(
|
|
69
|
+
concept_type: str, attrib: dict[str, str] = dict()
|
|
70
|
+
) -> Element:
|
|
71
|
+
full_attrib = {FuzzyOWL2Keyword.TYPE.get_str_value(): concept_type}
|
|
72
|
+
if attrib:
|
|
73
|
+
full_attrib.update(attrib)
|
|
74
|
+
return Element(
|
|
75
|
+
FuzzyOWL2Keyword.CONCEPT.get_tag_name(),
|
|
76
|
+
attrib=full_attrib,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
@staticmethod
|
|
80
|
+
def build_weights_xml(weights: list[float]) -> Element:
|
|
81
|
+
element = Element(FuzzyOWL2Keyword.WEIGHTS.get_tag_name())
|
|
82
|
+
for w in weights:
|
|
83
|
+
curr = Element(FuzzyOWL2Keyword.WEIGHT.get_tag_name())
|
|
84
|
+
curr.text = str(w)
|
|
85
|
+
element.append(curr)
|
|
86
|
+
return element
|
|
87
|
+
|
|
88
|
+
@staticmethod
|
|
89
|
+
def build_names_xml(concepts: list[OWLClassExpression]) -> Element:
|
|
90
|
+
element = Element(FuzzyOWL2Keyword.CONCEPT_NAMES.get_tag_name())
|
|
91
|
+
for c in concepts:
|
|
92
|
+
curr = Element(FuzzyOWL2Keyword.NAME.get_tag_name())
|
|
93
|
+
curr.text = str(c)
|
|
94
|
+
element.append(curr)
|
|
95
|
+
return element
|
|
96
|
+
|
|
97
|
+
@staticmethod
|
|
98
|
+
def to_str(element: Element) -> str:
|
|
99
|
+
return re.sub(
|
|
100
|
+
"\n+",
|
|
101
|
+
"\n",
|
|
102
|
+
"\n".join(
|
|
103
|
+
minidom.parseString(tostring(element, encoding="unicode", method="xml"))
|
|
104
|
+
.toprettyxml()
|
|
105
|
+
.split("\n")[1:]
|
|
106
|
+
),
|
|
107
|
+
)
|