-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOntoMermaid.py
339 lines (297 loc) · 9.52 KB
/
OntoMermaid.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 5 18:40:53 2023
@author: Flores Bakker
The Mermaid Vocabulary.
With the script mermaid.py one can transform OWL into Mermaid diagrams.
Usage:
1. Place an arbitrary ontology (as turtle file) in the input folder.
2. In the command prompt, run 'python mermaid.py'
3. Go to the output folder and grab your enriched turtle file, now including Mermaid labels.
4. Copypaste Mermaid labels in your html file and use the Mermaid javascript library (see documentation on the Mermaid site)
"""
import os
import pyshacl
import rdflib
from rdflib import Namespace
# Set the path to the desired standard directory.
directory_path = "C:/Users/smaxbehr/Documents/GitHub/Ontologien_AD/Markdown-Grapher/"
# namespace declaration
mermaid = Namespace("https://data.rijksfinancien.nl/mermaid/model/def/")
# Function to read a graph (as a string) from a file
def readGraphFromFile(file_path):
# Open each file in read mode
with open(file_path, 'r', encoding='utf-8') as file:
# Read the content of the file and append it to the existing string
file_content = file.read()
return file_content
# Function to write a graph to a file
def writeGraph(graph):
graph.serialize(destination=directory_path+"Output/"+filename_stem+"-mermaid.ttl", format="turtle")
# Function to call the PyShacl engine so that a RDF model of an HTML document can be serialized to HTML-code.
def iteratePyShacl(mermaid_generator, serializable_graph):
# call PyShacl engine and apply the HTML vocabulary to the serializable HTML document
pyshacl.validate(
data_graph=serializable_graph,
shacl_graph=mermaid_generator,
data_graph_format="turtle",
shacl_graph_format="turtle",
advanced=True,
inplace=True,
inference=None,
iterate_rules=False, #rather than setting this to true, it is better to do the iteration in the script as PyShacl seems to have some buggy behavior around iteration.
debug=False,
)
statusquery = serializable_graph.query('''
prefix mermaid: <https://data.rijksfinancien.nl/mermaid/model/def/>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix sh: <http://www.w3.org/ns/shacl#>
ASK
WHERE {
# Any OWL or RDFS entity that is not yet described in terms of the manchester syntax
{
$this a owl:Class.
}
UNION
{
$this a rdfs:Class.
}
UNION
{
$this rdfs:subClassOf []
}
UNION
{
$this owl:equivalentClass []
}
UNION
{
$this owl:unionOf []
}
UNION
{
$this owl:intersectionOf []
}
UNION
{
$this owl:complementOf []
}
UNION
{
$this owl:oneOf []
}
UNION
{
$this owl:allValuesFrom []
}
UNION
{
$this owl:someValuesFrom []
}
UNION
{
$this owl:hasValue []
}
UNION
{
$this owl:cardinality []
}
UNION
{
$this owl:maxCardinality []
}
UNION
{
$this owl:minCardinality []
}
UNION
{
$this rdf:type rdf:Property
}
UNION
{
$this rdf:type owl:DatatypeProperty.
}
UNION
{
$this rdf:type owl:ObjectProperty.
}
UNION
{
$this rdfs:subPropertyOf []
FILTER NOT EXISTS {$this rdf:type owl:AnnotationProperty}.
FILTER NOT EXISTS {$this rdf:type owl:InverseFunctionalProperty}
FILTER NOT EXISTS {$this rdf:type owl:FunctionalProperty}
}
UNION
{
$this owl:equivalentProperty [].
}
filter not exists {
$this mermaid:syntax 'CLASS'.
}
filter not exists {
$this mermaid:syntax 'CLASS'.
}
filter not exists {
$this mermaid:syntax 'SUBCLASSOF'.
}
filter not exists {
$this mermaid:syntax 'EQUIVALENTTO'.
}
filter not exists {
$this mermaid:syntax 'OR'.
}
filter not exists {
$this mermaid:syntax 'AND'.
}
filter not exists {
$this mermaid:syntax 'NOT'.
}
filter not exists {
$this mermaid:syntax '{}'.
}
filter not exists {
$this mermaid:syntax 'ONLY'.
}
filter not exists {
$this mermaid:syntax 'SOME'.
}
filter not exists {
$this mermaid:syntax 'VALUE'.
}
filter not exists {
$this mermaid:syntax 'EXACTLY'.
}
filter not exists {
$this mermaid:syntax 'MAX'.
}
filter not exists {
$this mermaid:syntax 'MIN'.
}
filter not exists {
$this mermaid:syntax 'RDF_PROPERTY'.
}
filter not exists {
$this mermaid:syntax 'DATATYPEPROPERTY'.
}
filter not exists {
$this mermaid:syntax 'OBJECTPROPERTY'.
}
filter not exists {
$this mermaid:syntax 'SUBPROPERTYOF'.
}
filter not exists {
$this mermaid:syntax 'EQUIVALENTPROPERTY'.
}
filter not exists {
$this mermaid:syntax 'OR-DATATYPE'.
}
filter not exists {
$this mermaid:syntax 'AND-DATATYPE'.
}
filter not exists {
$this mermaid:syntax 'NOT-DATATYPE'.
}
filter not exists {
$this mermaid:syntax '{}-DATATYPE'.
}
filter not exists {
$this mermaid:syntax 'EXACTLYQUALIFIED'.
}
filter not exists {
$this mermaid:syntax 'MAXQUALIFIED'.
}
filter not exists {
$this mermaid:syntax 'MINQUALIFIED'.
}
}
''')
resultquery = serializable_graph.query('''
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix sh: <http://www.w3.org/ns/shacl#>
prefix : <https://data.rijksfinancien.nl/mermaid/model/def/>
SELECT (GROUP_CONCAT(?label; separator="\\n") AS ?mermaid_code)
WHERE {
?element :label ?label.
minus {
?this owl:annotatedTarget ?target.
?target (:|!:)* ?element.
?element :label ?label
FILTER isBlank(?target)
FILTER isBlank(?element)}
}
ORDER BY ?mermaid_code
''')
# Check whether another iteration is needed. If every OWL and RDFS construct contains a mermaid:syntax statement, the processing is considered done.
for status in statusquery:
if status == True:
writeGraph(serializable_graph)
iteratePyShacl(mermaid_generator, serializable_graph)
else:
print ("File " + filename_stem+"-mermaid.ttl" + " created in output folder.")
writeGraph(serializable_graph)
for result in resultquery:
mermaid_code = result["mermaid_code"]
output_file_path = directory_path+"Output/"+filename_stem+"-mermaid.html"
# Create the HTML content with the Mermaid code
html_start = '''
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div><pre class="mermaid">
%%{
init: {
"flowchart":{
"useMaxWidth": 0
}
}
}%%
graph TB
classDef Datatype fill:#9c6,stroke:#9c6;
'''
html_graph = f'''
{mermaid_code}
'''
html_end = '''
</pre>
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
mermaid.initialize({ startOnLoad: true });
</script>
</div>
</body>
</html>
'''
html_content = html_start + html_graph + html_end
# Write the HTML content to the output file
with open(output_file_path, "w", encoding="utf-8") as file:
file.write(html_content)
print ("File " + filename_stem+"-mermaid.html" + " created in output folder.")
# loop through any turtle files in the input directory
for filename in os.listdir(directory_path+"Input"):
if filename.endswith(".ttl"):
file_path = os.path.join(directory_path+"Input", filename)
# Establish the stem of the file name for reuse in newly created files
filename_stem = os.path.splitext(filename)[0]
# Get the manchester syntax vocabulary and place it in a string
mermaid_generator = readGraphFromFile(directory_path+"Specification/mermaid.ttl")
# Get some ontology to be transformed from OWL to Manchester Syntax. The ontology needs to be placed in the input directory.
ontology_graph = readGraphFromFile(file_path)
# Join the manchester syntax and the ontology to be transformed into one big string.
serializable_graph_string = ontology_graph
# Create a graph of the string consisting of the manchester syntax and the ontology to be transformed
serializable_graph = rdflib.Graph().parse(data=serializable_graph_string , format="ttl")
serializable_graph.bind("mermaid", mermaid)
# Inform user
print ('\nCreating Mermaid diagram labels for file',filename, '...')
# Call the shacl engine with the HTML vocabulary and the document to be serialized
iteratePyShacl(mermaid_generator, serializable_graph)
# Inform user
print ('Done.')