-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiof_render.py
More file actions
252 lines (228 loc) · 10.9 KB
/
iof_render.py
File metadata and controls
252 lines (228 loc) · 10.9 KB
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
# -*- coding: utf-8 -*-
import types
from typing import Union, Optional
from owlready2 .disjoint import AllDisjoint
from owlready2.class_construct import LogicalClassConstruct, Or, And, Not, Inverse, \
Construct, Restriction, OneOf, ConstrainedDatatype, PropertyChain
from owlready2.entity import EntityClass, ThingClass
from owlready2.annotation import AnnotationPropertyClass
from owlready2.prop import PropertyClass, ObjectPropertyClass, DataPropertyClass, ReasoningPropertyClass, \
SymmetricProperty, AsymmetricProperty, TransitiveProperty, FunctionalProperty, ReflexiveProperty, \
IrreflexiveProperty
from owlready2.namespace import owl, Ontology
import owlready2.base
import re
import treelib as tl
from ontologies import MTConnect, Core
_DL_SYNTAX = types.SimpleNamespace(
SUBCLASS="⊑",
EQUIVALENT_TO="↔",
NOT="¬",
DISJOINT_WITH="⊑" + " " + "¬",
EXISTS="∃",
FORALL="∀",
IN="∈",
MIN="≥",
EQUAL="=",
NOT_EQUAL="≠",
MAX="≤",
INVERSE="⁻",
AND="⊓",
TOP="⊤",
BOTTOM="⊥",
OR="⊔",
COMP="∘",
WEDGE="⋀",
CONDITIONAL="⊑",
IMPLIES="←",
COMMA=",",
SELF="self",
)
_FACETS = {
"length": "length",
"min_length": "minLength",
"max_length": "maxLength",
"pattern": "pattern",
"max_inclusive": "\u2264",
"max_exclusive": "\u003c",
"min_inclusive": "\u2265",
"min_exclusive": "\u003e",
"total_digits": "totalDigits",
"fraction_digits": "fractionDigits",
}
Namespaces = {
"http://purl.obolibrary.org/obo/": "bfo",
"https://spec.industrialontologies.org/ontology/construct/": "constr",
"https://spec.industrialontologies.org/ontology/annotation/": "av",
"https://www.omg.org/spec/Commons/Designators/": "des",
"https://onto.mtconnect.org/ontology/construct/": "mtc",
"http://qudt.org/vocab/unit/": "unit"
}
def _render_short_name(concept) -> str:
ns = Namespaces.get(concept.namespace.base_iri, '_')
name = concept.name
if re.match(r'[A-Z]+_[0-9]+', name):
name = re.sub(r"[ ]", "_", str(concept.label.first()))
return f"{ns}:{name}"
def dl_render_terminology(onto: Ontology, show_disjoint: bool = True, show_domain: bool = True, show_range: bool = True, show_inverse: bool = True, show_characteristics: bool = False) -> dict:
s = dict()
if onto.annotation_properties() and (props := list(onto.annotation_properties())):
s['Annotation properties'] = { prop.name : dl_render_prop(prop)
for prop in props }
if onto.data_properties() and (props := list(onto.data_properties())):
s['Data properties'] = { prop.name :
dl_render_prop(prop, show_domain=show_domain, show_range=show_range, show_inverse=show_inverse, show_characteristics=show_characteristics)
for prop in props }
if onto.object_properties() and (props := list(onto.object_properties())):
s['Object properties'] = { prop.name :
dl_render_prop(prop, show_domain=show_domain, show_range=show_range, show_inverse=show_inverse, show_characteristics=show_characteristics)
for prop in props }
if onto.classes() and (classes := list(onto.classes())):
tree = tl.Tree()
for klass in classes:
dl_render_class(klass, show_disjoint=show_disjoint, tree = tree)
s['Classes'] = tree
return s
def dl_render_class(klass: ThingClass, show_disjoint: bool = True, tree: tl.Tree = None) -> list:
def add_class(klass, tree, data = None):
if str(klass) in tree: return
parent = klass.__base__
if str(parent) not in tree:
if parent == owl.Thing:
tree.create_node(_render_name(parent), str(parent))
else:
dl_render_class(parent, show_disjoint=show_disjoint, tree=tree)
tree.create_node(_render_name(klass), str(klass), parent = str(parent), data={ 'axioms': data, 'class': klass })
s = []
parent = None
if klass.equivalent_to:
s.extend(
[("%s %s %s" % (dl_render_concept_str(klass), _DL_SYNTAX.EQUIVALENT_TO, dl_render_concept_str(_))) for _ in
klass.equivalent_to])
if klass.is_a:
s.extend([("%s %s %s" % (dl_render_concept_str(klass), _DL_SYNTAX.SUBCLASS, dl_render_concept_str(_))) for _ in
klass.is_a])
# Pick up general class axioms
axioms = [_[0] for _ in klass.namespace.world.sparql("SELECT * { ?x rdfs:subClassOf ?? }", [klass]) if not isinstance(_[0], ThingClass) ]
if axioms:
s.extend([("%s %s %s" % (dl_render_concept_str(_), _DL_SYNTAX.CONDITIONAL, dl_render_concept_str(klass))) for _ in axioms])
if not s:
s.append("%s %s %s" % (dl_render_concept_str(klass), _DL_SYNTAX.SUBCLASS, _DL_SYNTAX.TOP))
if show_disjoint:
for disjoint in klass.disjoints():
s.extend(dl_render_disjoint(disjoint, klass))
if tree is not None: add_class(klass, tree, s)
return s
def dl_render_prop(prop: PropertyClass, show_domain: bool = True, show_range: bool = True, show_inverse: bool = True, show_characteristics: bool = False) -> list:
s = []
if prop.is_a:
s.extend([("%s %s %s" % (dl_render_concept_str(prop), _DL_SYNTAX.SUBCLASS, dl_render_concept_str(_))) for _ in
prop.is_a if _.namespace is not owl])
if prop.domain and show_domain:
s.extend([("domain: %s" % (dl_render_concept_str(_))) for _ in prop.domain])
if prop.range and show_range:
s.extend([("range: %s" % (dl_render_concept_str(_))) for _ in prop.range])
if prop.inverse_property and show_inverse:
s.append("inverse: %s" % (dl_render_concept_str(prop.inverse_property)))
if show_characteristics:
if SymmetricProperty in prop.is_a:
s.append('Symmetric')
if AsymmetricProperty in prop.is_a:
s.append('Asymmetric')
if TransitiveProperty in prop.is_a:
s.append('Transitive')
if FunctionalProperty in prop.is_a:
s.append('Functional')
if ReflexiveProperty in prop.is_a:
s.append('Reflexive')
if IrreflexiveProperty in prop.is_a:
s.append('Irreflexive')
return s
def dl_render_disjoint(disjoint: AllDisjoint, klass: Optional[ThingClass] = None) -> list:
if klass is None:
return [dl_render_disjoint(disjoint, _) for _ in disjoint.entities]
if klass in disjoint.entities:
return ["%s %s %s" % (dl_render_concept_str(And([klass, _])), _DL_SYNTAX.SUBCLASS, _DL_SYNTAX.BOTTOM)
for _ in disjoint.entities if _ is not klass]
def _render_name(concept) -> str:
formatted = _render_short_name(concept)
if concept.iri.startswith('https://onto.mtconnect.org') and concept.iri.split('/')[3] != 'ontology':
formatted = f"<a href='{concept.iri.split('/')[3]}.html#{concept.name}'>{formatted}</a>"
elif concept.namespace == MTConnect:
formatted = f"<a href='MTConnect.html#{concept.name}'>{formatted}</a>"
elif concept.namespace.name == 'Core':
formatted = f"<a href='{concept.iri}'>{formatted}</a>"
return formatted
def dl_render_concept_str(concept: Union[Construct, EntityClass]) -> str:
# print(f"Rendering {concept} {type(concept)}")
this = dl_render_concept_str
if concept is None:
return _DL_SYNTAX.BOTTOM
if isinstance(concept, ThingClass):
if concept is owl.Thing:
return _DL_SYNTAX.TOP
if concept is owl.Nothing:
return _DL_SYNTAX.BOTTOM
return _render_name(concept)
if isinstance(concept, PropertyClass):
return _render_name(concept)
if isinstance(concept, LogicalClassConstruct):
s = []
for x in concept.Classes:
if isinstance(x, LogicalClassConstruct):
s.append("(" + this(x) + ")")
else:
s.append(this(x))
if isinstance(concept, Or):
return (" %s " % _DL_SYNTAX.OR).join(s)
if isinstance(concept, And):
return (" %s " % _DL_SYNTAX.AND).join(s)
if isinstance(concept, Not):
return "%s %s" % (_DL_SYNTAX.NOT, this(concept.Class))
if isinstance(concept, Inverse):
return "%s%s" % (this(concept.property), _DL_SYNTAX.INVERSE)
if isinstance(concept, Restriction):
# type map
# SOME:
# ONLY:
# VALUE:
# HAS_SELF:
# EXACTLY:
# MIN:
# MAX:
if concept.type == owlready2.base.SOME:
oparan, cparen = ["(", ")"] if isinstance(concept.value, LogicalClassConstruct) else ["", ""]
return "%s %s .%s%s%s" % (_DL_SYNTAX.EXISTS, this(concept.property), oparan, this(concept.value), cparen)
if concept.type == owlready2.base.ONLY:
oparan, cparen = ["(", ")"] if isinstance(concept.value, LogicalClassConstruct) else ["", ""]
return "%s %s .%s%s%s" % (_DL_SYNTAX.FORALL, this(concept.property), oparan, this(concept.value), cparen)
if concept.type == owlready2.base.VALUE:
return "%s %s .{%s}" % (_DL_SYNTAX.EXISTS, this(concept.property), concept.value.name if isinstance(concept.value, owl.Thing) else concept.value)
if concept.type == owlready2.base.HAS_SELF:
return "%s %s .%s" % (_DL_SYNTAX.EXISTS, this(concept.property), _DL_SYNTAX.SELF)
if concept.type == owlready2.base.EXACTLY:
return "%s %s %s .%s" % (_DL_SYNTAX.EQUAL, concept.cardinality, this(concept.property), this(concept.value))
if concept.type == owlready2.base.MIN:
return "%s %s %s .%s" % (_DL_SYNTAX.MIN, concept.cardinality, this(concept.property), this(concept.value))
if concept.type == owlready2.base.MAX:
return "%s %s %s .%s" % (_DL_SYNTAX.MAX, concept.cardinality, this(concept.property), this(concept.value))
if isinstance(concept, OneOf):
return "{%s}" % (" %s " % _DL_SYNTAX.OR).join("%s" % (_.name if isinstance(_, owl.Thing) else _) for _ in concept.instances)
if isinstance(concept, ConstrainedDatatype):
s = []
for k in _FACETS:
v = getattr(concept, k, None)
if not v is None:
s.append("%s %s" % (_FACETS[k], v))
return "%s[%s]" % (concept.base_datatype.__name__, (" %s " % _DL_SYNTAX.COMMA).join(s))
if isinstance(concept, PropertyChain):
return (" %s " % _DL_SYNTAX.COMP).join(this(_) for _ in concept.properties)
if concept in owlready2.base._universal_datatype_2_abbrev:
iri = owlready2.base._universal_abbrev_2_iri.get(owlready2.base._universal_datatype_2_abbrev.get(concept))
if iri.startswith("http://www.w3.org/2001/XMLSchema#"):
return "xsd:" + iri[33:]
hash, slash = iri.rindex('#'), iri.rindex('/')
return iri[max(hash, slash)+1:]
if owlready2.rdfs_datatype in [_.storid for _ in concept.is_a]: # rdfs:Datatype
return concept.name
raise NotImplemented