-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_generator.py
83 lines (68 loc) · 3.01 KB
/
code_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
import rdflib
import os
import jinja2
from rdflib.namespace import OWL, RDF, RDFS, XSD
class FileManager:
def __init__(self, directory):
self.directory = directory
self.file = None
def open_file(self, file_name):
path = os.path.join(self.directory, file_name)
os.makedirs(os.path.dirname(path), exist_ok=True)
self.file = open(path, "w")
return ""
def write_and_close(self, data):
if self.file is None:
return
self.file.write(data)
self.file.close()
self.file = None
def iri_to_identifier(iri):
return str(iri) \
.replace("https://comp-rob2b.github.io/modelling-tutorial/robot#", "") \
.replace(":", "_").replace("-", "_").replace("<", "").replace(">", "") \
.replace("/", "_").replace(".", "_").replace("#", "_")
if __name__ == "__main__":
g = rdflib.ConjunctiveGraph()
TUTORIAL = "../modelling-tutorial/models"
g.parse(TUTORIAL + "/skeleton.json", format="json-ld")
g.parse(TUTORIAL + "/spatial-relations.json", format="json-ld")
g.parse(TUTORIAL + "/cartesian-coordinates.json", format="json-ld")
g.parse(TUTORIAL + "/joint-coordinates.json", format="json-ld")
g.parse(TUTORIAL + "/chain.json", format="json-ld")
g.parse(TUTORIAL + "/dynamics.json", format="json-ld")
g.parse(TUTORIAL + "/fpk-algorithm.json", format="json-ld")
COMP_ROB2B = "https://comp-rob2b.github.io"
GEOM_ENT = rdflib.Namespace(COMP_ROB2B + "/metamodels/geometry/structural-entities#")
GEOM_REL = rdflib.Namespace(COMP_ROB2B + "/metamodels/geometry/spatial-relations#")
GEOM_CRD = rdflib.Namespace(COMP_ROB2B + "/metamodels/geometry/coordinates#")
GEOM_OP = rdflib.Namespace(COMP_ROB2B + "/metamodels/geometry/spatial-operators#")
KC_ENT = rdflib.Namespace(COMP_ROB2B + "/metamodels/kinematic-chain/structural-entities#")
KC_STAT = rdflib.Namespace(COMP_ROB2B + "/metamodels/kinematic-chain/state#")
KC_OP = rdflib.Namespace(COMP_ROB2B + "/metamodels/kinematic-chain/operators#")
ALGO = rdflib.Namespace(COMP_ROB2B + "/metamodels/algorithm#")
QUDT = rdflib.Namespace("http://qudt.org/schema/qudt/")
fm = FileManager("gen/")
env = jinja2.Environment(loader=jinja2.FileSystemLoader("jinja/"),
lstrip_blocks=True, trim_blocks=True)
env.filters["file"] = fm.open_file
for template_name in env.list_templates():
template = env.get_template(template_name)
rendered = template.render({
"g": g,
"RDF": RDF,
"GEOM_ENT": GEOM_ENT,
"GEOM_REL": GEOM_REL,
"GEOM_CRD": GEOM_CRD,
"GEOM_OP": GEOM_OP,
"KC_ENT": KC_ENT,
"KC_STAT": KC_STAT,
"KC_OP": KC_OP,
"ALGO": ALGO,
"QUDT": QUDT,
"rdflib": rdflib,
"inv_path": rdflib.paths.inv_path,
"list": list,
"iri_to_identifier": iri_to_identifier
})
fm.write_and_close(rendered)