forked from ngonimombeshora/ontologydocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·87 lines (72 loc) · 4.07 KB
/
build.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
#!/usr/bin/env python3
import sys
import subprocess
from os import listdir, remove, rename
from os.path import isfile, join, splitext, isdir
from shutil import copytree
from rdflib import Graph
from rdflib.namespace import OWL, RDF
# GitHub Pages blows up if PyLODE is included in the repo, strangely
pylodePath = "Applications/pyLODE-2.8.3/pylode/cli.py"
owl2vowlPath = "bin/owl2vowl.jar"
ontologyFileEndings = ["owl", "rdf", "ttl", "nq"]
if (len(sys.argv) != 2):
print("Usage: './build.py OntologyDirectory', e.g., './build.py 3.1.3'")
sys.exit()
ontologyPath = sys.argv[1]
allFiles = [f for f in listdir(ontologyPath) if isfile(join(ontologyPath, f))]
ontologyFiles = [f for f in allFiles if splitext(f)[1].strip('.') in ontologyFileEndings]
# Copy webvowl skeleton
if not isdir(f"{ontologyPath}/webvowl"):
copytree("webvowl", f"{ontologyPath}/webvowl")
# Set up unioned ontology graph for full
unionedOntology = Graph()
unionedOntology.parse(f"{ontologyPath}/full.rdf")
# For each ontology except full,:
for inputFile in ontologyFiles:
if not inputFile.startswith("full"):
inputFilePath = f"{ontologyPath}/{inputFile}"
# 1. Generate PyLODE docs
moduleName, fEnding = splitext(inputFile)
outputHtmlFileName = f"{ontologyPath}/{moduleName}.html"
subprocess.run([pylodePath, "-i", inputFilePath, "-o", outputHtmlFileName])
# 2. Merge into temp full graph (excluding lantmäteriet module)
moduleGraph = Graph()
moduleGraph.parse(inputFilePath)
moduleOntologyUris = list(moduleGraph.subjects(RDF.type, OWL.Ontology))
if not inputFile.startswith("lantmäteriet"):
for (s, p, o) in moduleGraph:
if not s in moduleOntologyUris:
unionedOntology.add((s, p, o))
# 3. Generate and graft WebVOWL visualization (except for metadata file)
if not inputFile.startswith("metadata"):
# This is to prevent the visualization from including the imports closure
tempOntologyFilePath = inputFilePath + "-TEMP"
moduleGraph.remove((None, OWL.imports, None))
moduleGraph.serialize(destination=tempOntologyFilePath, format="xml")
outputJsonFilePath = f"{ontologyPath}/webvowl/data/{moduleName}.json"
subprocess.run(["java", "-jar", owl2vowlPath, "-file", tempOntologyFilePath, "-output", outputJsonFilePath])
remove(tempOntologyFilePath)
# Graft on HTML <iframe>s
htmlFile = open(outputHtmlFileName, "rt")
htmlContent = htmlFile.read()
htmlFile.close()
htmlContent = htmlContent.replace('<div style="width:500px; height:50px; background-color: lightgrey; border:solid 2px grey; padding:10px;margin-bottom:5px; text-align:center;">Pictures say 1,000 words</div>', f'<iframe src="webvowl/index.html#{moduleName}" width="100%" height="800"></iframe>')
htmlFile = open(outputHtmlFileName, "wt")
htmlFile.write(htmlContent)
htmlFile.close()
# 4. Export unioned full ontology to temp file, run pylode, kill temp file
rename(f"{ontologyPath}/full.rdf", f"{ontologyPath}/fullTemp.rdf")
unionedOntology.serialize(destination=f"{ontologyPath}/full.rdf", format="xml")
subprocess.run([pylodePath, "-i", f"{ontologyPath}/full.rdf", "-o", f"{ontologyPath}/full.html"])
remove(f"{ontologyPath}/full.rdf")
rename(f"{ontologyPath}/fullTemp.rdf", f"{ontologyPath}/full.rdf")
# 5. Generate WebVOWL visualization on full file and graft it
subprocess.run(["java", "-jar", owl2vowlPath, "-file", f"{ontologyPath}/full.rdf", "-output", f"{ontologyPath}/webvowl/data/full.json"])
htmlFile = open(f"{ontologyPath}/full.html", "rt")
htmlContent = htmlFile.read()
htmlFile.close()
htmlContent = htmlContent.replace('<div style="width:500px; height:50px; background-color: lightgrey; border:solid 2px grey; padding:10px;margin-bottom:5px; text-align:center;">Pictures say 1,000 words</div>', f'<iframe src="webvowl/index.html#full" width="100%" height="800"></iframe>')
htmlFile = open(f"{ontologyPath}/full.html", "wt")
htmlFile.write(htmlContent)
htmlFile.close()