-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.py
285 lines (216 loc) · 7.06 KB
/
app.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
from flask import (
Flask,
render_template,
request,
)
from kgcl_rdflib.apply.graph_transformer import apply_patch
# from kgcl.diff.example_kgcl_operations import generate_diff
import rdflib
from kgcl_schema.grammar import parse
import kgcl_rdflib.diff.diff_2_kgcl_single as single
import kgcl_rdflib.diff.diff_2_kgcl_existential as existential
import kgcl_rdflib.diff.diff_2_kgcl_triple_annotation as annotation
from kgcl_rdflib.diff.pretty_print_kgcl import render_instances
# from diff.example_kgcl_operations import generate_diff
from kgcl_rdflib.render_kgcl import render
def generate_diff():
pass
app = Flask(__name__)
# we could store changes in a data base - do we want to
@app.route("/", methods=["POST", "GET"])
def index():
examples = [
"rename",
"nodeCreation",
"nodeDeletionByLabel",
"nodeDeletionById",
"obsoleteByLabel",
"obsoleteById",
"unobsoleteById",
"nodeDeepening",
"nodeShallowing",
"move",
"changeRelationship",
"createSynonym",
"edgeCreation",
"edgeDeletion",
"addSubsumptionAxiom",
"deleteSubsumptionAxiom",
]
if request.method == "POST":
if "apply_changes" in request.form:
# get input graph from form
graph = request.form["graph"]
# get input kgcl statements from form
kgcl = request.form["kgcl"]
kgcl_transformation(graph, kgcl)
if "load_example" in request.form:
select = request.form.get("comp_select")
example = str(select)
f = open("examples/kgcl/" + example + "/graph.nt", "r")
graph = f.read()
f.close()
f = open("examples/kgcl/" + example + "/kgcl", "r")
kgcl = f.read()
f.close()
kgcl_transformation(graph, kgcl)
# parse KGCL input
kgcl_model = parse(kgcl)
# prepare parsed
kgcl_model_rendering = ""
for o in kgcl_model:
kgcl_model_rendering += render(o) + "\n"
# load transformed graph
f = open("examples/kgcl/tmp/transformation.nt", "r")
transformation = f.read()
f.close()
return render_template(
"index.html",
inputGraph=graph,
inputKGCL=kgcl,
parsedKGCL=kgcl_model_rendering,
outputGraph=transformation,
examples=examples,
)
else:
# load rename example by default
# load graph
f = open("examples/kgcl/rename/graph.nt", "r")
graph = f.read()
f.close()
# load kgcl
f = open("examples/kgcl/rename/kgcl", "r")
kgcl = f.read()
f.close()
kgcl_transformation(graph, kgcl)
# parse KGCL input
kgcl_model = parse(kgcl)
# prepare parsed
kgcl_model_rendering = ""
for o in kgcl_model:
kgcl_model_rendering += render(o) + "\n"
# load transformed graph
f = open("examples/kgcl/tmp/transformation.nt", "r")
transformation = f.read()
f.close()
# return render_template("index.html", examples=examples)
return render_template(
"index.html",
inputGraph=graph,
inputKGCL=kgcl,
parsedKGCL=kgcl_model_rendering,
outputGraph=transformation,
examples=examples,
)
@app.route("/diff", methods=["POST", "GET"])
def diff():
examples = [
"rename",
"classCreation",
"obsoletion",
"unobsoletion",
"move",
"edgeCreation",
"edgeDeletion",
]
# initialise variables
graph1 = ""
graph2 = ""
kgcl = ""
if request.method == "POST":
if "generate_diff" in request.form:
graph1 = request.form["graph1"]
graph2 = request.form["graph2"]
kgcl_diff(graph1, graph2)
if "load_example_diff" in request.form:
select = request.form.get("comp_select")
example = str(select)
f = open("examples/diff/" + example + "/graph1.nt", "r")
graph1 = f.read()
f.close()
f = open("examples/diff/" + example + "/graph2.nt", "r")
graph2 = f.read()
f.close()
kgcl_diff(graph1, graph2)
f = open("examples/kgcl/tmp/patch.kgcl", "r")
kgcl = f.read()
f.close()
return render_template(
"diff.html",
examples=examples,
kgclDiff=kgcl,
inputGraph1=graph1,
inputGraph2=graph2,
)
else:
# load rename by default
example = "rename"
f = open("examples/diff/" + example + "/graph1.nt", "r")
graph1 = f.read()
f.close()
f = open("examples/diff/" + example + "/graph2.nt", "r")
graph2 = f.read()
f.close()
kgcl_diff(graph1, graph2)
f = open("examples/kgcl/tmp/patch.kgcl", "r")
kgcl = f.read()
f.close()
return render_template(
"diff.html",
examples=examples,
kgclDiff=kgcl,
inputGraph1=graph1,
inputGraph2=graph2,
)
# def parse(input):
# return parsing.parse(input)
def kgcl_transformation(graph, kgcl):
# store graph as file
f = open("examples/kgcl/tmp/graph.nt", "w")
f.write(graph)
f.close()
# store kgcl statements
f = open("examples/kgcl/tmp/kgcl", "w")
f.write(kgcl)
f.close()
# parse KGCL input
parsed_statements = parse(kgcl)
# load input graph from file
g = rdflib.Graph()
g.load("examples/kgcl/tmp/graph.nt", format="nt")
# transform graph
apply_patch(parsed_statements, g)
# save graph
# TODO: get text representation of graph without writing to a file
g.serialize(destination="examples/kgcl/tmp/transformation.nt", format="nt")
def kgcl_diff(graph1, graph2):
# store graph as file
f = open("examples/kgcl/tmp/graph1.nt", "w")
f.write(graph1)
f.close()
f = open("examples/kgcl/tmp/graph2.nt", "w")
f.write(graph2)
f.close()
# generate diff
# load graphs
g1 = rdflib.Graph()
g2 = rdflib.Graph()
g1.load("examples/kgcl/tmp/graph1.nt", format="nt")
g2.load("examples/kgcl/tmp/graph2.nt", format="nt")
# compute diff
existential_summary = existential.generate_atomic_existential_commands(g1, g2)
triple_annotation_summary = annotation.generate_triple_annotation_commands(g1, g2)
single_triple_summary = single.generate_thin_triple_commands(g1, g2)
# get KGCL commands
kgcl_commands = existential_summary.get_commands()
kgcl_commands += triple_annotation_summary.get_commands()
kgcl_commands += single_triple_summary.get_commands()
# pretty printing of KGCL commands
kgcl_commands = render_instances(kgcl_commands, g1)
# write KGCL commands
with open("examples/kgcl/tmp/patch.kgcl", "w") as f:
for k in kgcl_commands:
f.write(k)
f.write("\n")
if __name__ == "__main__":
app.run(debug=True)