Skip to content

Commit

Permalink
Add webapp
Browse files Browse the repository at this point in the history
  • Loading branch information
ckindermann committed Oct 10, 2021
1 parent 3e4168b commit 96e119c
Show file tree
Hide file tree
Showing 67 changed files with 490 additions and 0 deletions.
304 changes: 304 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
from flask import (
Flask,
render_template,
request,
)
from kgcl.model.kgcl import (
NodeRename,
NodeObsoletion,
NodeUnobsoletion,
NodeDeletion,
NodeMove,
NodeDeepening,
NodeShallowing,
EdgeCreation,
EdgeDeletion,
PredicateChange,
NodeCreation,
ClassCreation,
NewSynonym,
RemovedNodeFromSubset,
)

import os
from kgcl.apply.graph_transformer import apply_patch

# from kgcl.diff.example_kgcl_operations import generate_diff
import rdflib
from kgcl.grammar.parser import parse

import diff.diff_2_kgcl_single as single
from diff.diff_2_kgcl_single import SingleTripleChangeSummary
import diff.diff_2_kgcl_existential as existential
from diff.diff_2_kgcl_existential import ExistentialChangeSummary
import diff.diff_2_kgcl_triple_annotation as annotation
from diff.diff_2_kgcl_triple_annotation import TripleAnnotationChangeSummary

# from diff.example_kgcl_operations import generate_diff
from 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",
"edgeCreation",
"edgeDeletion",
"addExistential",
"addSubsumptionAxiom",
"annotatedEdgeCreation",
"annotatedEdgeDeletion",
"changeRelationship",
"createSynonym",
"deleteExistential",
"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("diff/stats/all", "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()

# 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)
Empty file.
1 change: 1 addition & 0 deletions examples/diff/classCreation/graph2.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://purl.obolibrary.org/obo/UBERON_0017671> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
Empty file.
1 change: 1 addition & 0 deletions examples/diff/edgeCreation/graph2.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://purl.obolibrary.org/obo/UBERON_0005875> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://purl.obolibrary.org/obo/UBERON_4000142> .
1 change: 1 addition & 0 deletions examples/diff/edgeDeletion/graph1.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://purl.obolibrary.org/obo/UBERON_0005875> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://purl.obolibrary.org/obo/UBERON_4000142> .
Empty file.
1 change: 1 addition & 0 deletions examples/diff/move/graph1.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://purl.obolibrary.org/obo/UBERON_0005875> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://purl.obolibrary.org/obo/UBERON_0005879> .
1 change: 1 addition & 0 deletions examples/diff/move/graph2.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://purl.obolibrary.org/obo/UBERON_0005875> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://purl.obolibrary.org/obo/UBERON_4000142> .
3 changes: 3 additions & 0 deletions examples/diff/obsoletion/graph1.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<http://purl.obolibrary.org/obo/UBERON_4000142> <http://www.w3.org/2000/01/rdf-schema#label> "metaplastic ossification" .
<http://purl.obolibrary.org/obo/UBERON_4000142> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<http://purl.obolibrary.org/obo/UBERON_4000142> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://purl.obolibrary.org/obo/UBERON_0005879> .
3 changes: 3 additions & 0 deletions examples/diff/obsoletion/graph2.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<http://purl.obolibrary.org/obo/UBERON_4000142> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<http://purl.obolibrary.org/obo/UBERON_4000142> <http://www.w3.org/2000/01/rdf-schema#label> "obsolete metaplastic ossification" .
<http://purl.obolibrary.org/obo/UBERON_4000142> <http://www.w3.org/2002/07/owl#deprecated> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .
1 change: 1 addition & 0 deletions examples/diff/rename/graph1.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://purl.obolibrary.org/obo/UBERON_0007176> <http://www.w3.org/2000/01/rdf-schema#label> "superior angle of scapula" .
1 change: 1 addition & 0 deletions examples/diff/rename/graph2.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://purl.obolibrary.org/obo/UBERON_0007176> <http://www.w3.org/2000/01/rdf-schema#label> "inferior angle of scapula" .
3 changes: 3 additions & 0 deletions examples/diff/unobsoletion/graph1.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<http://purl.obolibrary.org/obo/UBERON_4000142> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<http://purl.obolibrary.org/obo/UBERON_4000142> <http://www.w3.org/2000/01/rdf-schema#label> "obsolete metaplastic ossification" .
<http://purl.obolibrary.org/obo/UBERON_4000142> <http://www.w3.org/2002/07/owl#deprecated> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .
2 changes: 2 additions & 0 deletions examples/diff/unobsoletion/graph2.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<http://purl.obolibrary.org/obo/UBERON_4000142> <http://www.w3.org/2000/01/rdf-schema#label> "metaplastic ossification" .
<http://purl.obolibrary.org/obo/UBERON_4000142> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
Empty file.
1 change: 1 addition & 0 deletions examples/kgcl/addExistential/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add <http://example.org/subject> SubClassOf <http://example.org/predicate> some <http://example.org/object>
Empty file.
1 change: 1 addition & 0 deletions examples/kgcl/addSubsumptionAxiom/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add <http://example.org/subclass> SubClassOf <http://example.org/superclass>
Empty file.
1 change: 1 addition & 0 deletions examples/kgcl/annotatedEdgeCreation/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create edge <<<http://example.org/subject> <http://example.org/predicate> <http://example.org/object>>> <http://example.org/annotationPredicate> <http://example.org/annotation>
5 changes: 5 additions & 0 deletions examples/kgcl/annotatedEdgeDeletion/graph.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_:N21088129e79d457c952ec747c2124281 <http://example.org/annotationPredicate> <http://example.org/annotation> .
_:N21088129e79d457c952ec747c2124281 <http://www.w3.org/2002/07/owl#annotatedProperty> <http://example.org/predicate> .
_:N21088129e79d457c952ec747c2124281 <http://www.w3.org/2002/07/owl#annotatedSource> <http://example.org/subject> .
_:N21088129e79d457c952ec747c2124281 <http://www.w3.org/2002/07/owl#annotatedTarget> <http://example.org/object> .
_:N21088129e79d457c952ec747c2124281 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Axiom> .
1 change: 1 addition & 0 deletions examples/kgcl/annotatedEdgeDeletion/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
delete edge <<<http://example.org/subject> <http://example.org/predicate> <http://example.org/object>>> <http://example.org/annotationPredicate> <http://example.org/annotation>
1 change: 1 addition & 0 deletions examples/kgcl/changeRelationship/graph.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://example.org/subclass> <http://example.org/partOf> <http://example.org/superclass> .
1 change: 1 addition & 0 deletions examples/kgcl/changeRelationship/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
change relationship between <http://example.org/subclass> and <http://example.org/superclass> from <http://example.org/partOf> to <http://example.org/hasPart>
1 change: 1 addition & 0 deletions examples/kgcl/createSynonym/graph.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://purl.obolibrary.org/obo/NCBITaxon_2> <http://www.w3.org/2000/01/rdf-schema#label> "Bacteria" .
5 changes: 5 additions & 0 deletions examples/kgcl/createSynonym/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create synonym 'Virus' for <http://purl.obolibrary.org/obo/NCBITaxon_2>
create exact synonym 'Eubacteria' for <http://purl.obolibrary.org/obo/NCBITaxon_2>
create related synonym 'Disease' for <http://purl.obolibrary.org/obo/NCBITaxon_2>
create narrow synonym 'Bacterium' for <http://purl.obolibrary.org/obo/NCBITaxon_2>
create broad synonym 'Infection'@en for <http://purl.obolibrary.org/obo/NCBITaxon_2>
Binary file added examples/kgcl/deleteExistential/.graph.nt.swp
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/kgcl/deleteExistential/graph.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
_:Ndb451c3aad3841c88d7042f29b20bea8 <http://www.w3.org/2002/07/owl#onProperty> <http://example.org/predicate> .
_:Ndb451c3aad3841c88d7042f29b20bea8 <http://www.w3.org/2002/07/owl#someValuesFrom> <http://example.org/object> .
_:Ndb451c3aad3841c88d7042f29b20bea8 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
<http://example.org/subject> <http://www.w3.org/2000/01/rdf-schema#subClassOf> _:Ndb451c3aad3841c88d7042f29b20bea8 .
1 change: 1 addition & 0 deletions examples/kgcl/deleteExistential/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
delete <http://example.org/subject> SubClassOf <http://example.org/predicate> some <http://example.org/object>
1 change: 1 addition & 0 deletions examples/kgcl/deleteSubsumptionAxiom/graph.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://example.org/targetClass> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.org/A> .
1 change: 1 addition & 0 deletions examples/kgcl/deleteSubsumptionAxiom/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
delete <http://example.org/targetClass> SubClassOf <http://example.org/A>
Empty file.
1 change: 1 addition & 0 deletions examples/kgcl/edgeCreation/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create edge <http://example.org/subject> <http://example.org/predicate> <http://example.org/object>
1 change: 1 addition & 0 deletions examples/kgcl/edgeDeletion/graph.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://example.org/subject> <http://example.org/predicate> <http://example.org/object> .
1 change: 1 addition & 0 deletions examples/kgcl/edgeDeletion/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
delete edge <http://example.org/subject> <http://example.org/predicate> <http://example.org/object>
1 change: 1 addition & 0 deletions examples/kgcl/move/graph.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://example.org/targetClass> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.org/A> .
1 change: 1 addition & 0 deletions examples/kgcl/move/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
move <http://example.org/targetClass> from <http://example.org/A> to <http://example.org/B>
Empty file.
2 changes: 2 additions & 0 deletions examples/kgcl/nodeCreation/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
create node <http://purl.obolibrary.org/obo/NCBITaxon_2> 'Bacteria'
create <http://purl.obolibrary.org/obo/NCBITaxon_4>
2 changes: 2 additions & 0 deletions examples/kgcl/nodeDeepening/graph.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<http://example.org/targetClass> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.org/superclass> .
<http://example.org/subclass> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.org/superclass> .
1 change: 1 addition & 0 deletions examples/kgcl/nodeDeepening/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deepen <http://example.org/targetClass> from <http://example.org/superclass> to <http://example.org/subclass>
Binary file added examples/kgcl/nodeDeletionById/.graph.nt.swp
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/kgcl/nodeDeletionById/graph.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<http://purl.obolibrary.org/obo/NCBITaxon_2> <http://www.w3.org/2000/01/rdf-schema#label> "Bacteria" .
<http://purl.obolibrary.org/obo/NCBITaxon_2> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://purl.obolibrary.org/obo/OBI_0100026> .
<http://purl.obolibrary.org/obo/NCBITaxon_2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<http://purl.obolibrary.org/obo/OBI_0100026> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
1 change: 1 addition & 0 deletions examples/kgcl/nodeDeletionById/kgcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
delete <http://purl.obolibrary.org/obo/NCBITaxon_2>
Loading

0 comments on commit 96e119c

Please sign in to comment.