-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastapp.py
177 lines (144 loc) · 6.64 KB
/
fastapp.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
from fastapi import FastAPI, File, Form, UploadFile, HTTPException
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import subprocess
import rdflib
import os
import sys
import shutil
import uvicorn
from fastapi.responses import FileResponse
from fastapi import Response
from fastapi.staticfiles import StaticFiles
import tempfile
from rdflib import Graph
from pyshacl import validate
import multiprocessing as mp
from scoop.main import main
from scoop.rdf_graph_load import load_rdf_graph
from scoop.SCOOP.shape_integration_priority import ShapeIntegrationPriority
from scoop.SCOOP.shape_integration_priority_r import ShapeIntegrationPriorityR
from scoop.SCOOP.shape_integration_all import ShapeIntegrationAll
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/assets", StaticFiles(directory="static/assets"), name="assets")
app.mount("/examples", StaticFiles(directory="static/examples"), name="examples")
app.mount("/images", StaticFiles(directory="static/images"), name="images")
@app.get("/")
async def index():
# Return the index.html file
return FileResponse("static/index.html", media_type="text/html")
@app.get("/video")
async def video():
# Return the index.html file
return FileResponse("static/video.html", media_type="text/html")
class TranslationRequest(BaseModel):
rmlData: list = []
owlData: list = []
xsdData: list = []
mode : str = None
priority : str = None
@app.post("/translate")
async def translate(request_data: TranslationRequest):
try:
# create temp folder
temp_folder = tempfile.mkdtemp()
# make subfolders
inputrml_folder = os.path.join(temp_folder, "inputrml")
os.makedirs(inputrml_folder)
inputowl_folder = os.path.join(temp_folder, "inputowl")
os.makedirs(inputowl_folder)
inputxsd_folder = os.path.join(temp_folder, "inputxsd")
os.makedirs(inputxsd_folder)
tempshacl_folder = os.path.join(temp_folder, "tempshacl")
os.makedirs(tempshacl_folder)
tempoutput_folder = os.path.join(temp_folder, "output")
os.makedirs(tempoutput_folder)
output_file = os.path.join(tempoutput_folder, "output.ttl")
args = ['-ot', output_file, '--tempshacl_folder', tempshacl_folder, '--mode', request_data.mode]
priority = ["rml", "ontology", "xsd"]
if request_data.priority:
priority = request_data.priority.split(" ")
if request_data.mode == "priorityR":
priority = [i.replace("-r","") for i in priority if "-r" in i]
elif request_data.mode == "priority":
priority = [i for i in priority if "-r" not in i]
args.append('--priority')
args.extend(priority)
if request_data.rmlData!=[]:
for index, data in enumerate(request_data.rmlData):
g, rdf_format = load_rdf_graph(data)
if rdf_format:
rml_file = os.path.join(inputrml_folder, f"rml{index}.ttl")
g.serialize(destination=rml_file, format='ttl')
else:
# unrecognized format
return JSONResponse(content={"error": "Invalid RML format"}, status_code=400)
args.extend(['-m', inputrml_folder, '-xr', inputrml_folder])
if request_data.owlData != []:
for index, data in enumerate(request_data.owlData):
g, rdf_format = load_rdf_graph(data)
if rdf_format:
owl_file = os.path.join(inputowl_folder, f"owl{index}.ttl")
g.serialize(destination=owl_file, format='ttl')
else:
# unrecognized format
return JSONResponse(content={"error": "Invalid Ontology format"}, status_code=400)
args.extend(['-o', inputowl_folder])
if request_data.xsdData!=[]:
for index, data in enumerate(request_data.xsdData):
xsd_file = os.path.join(inputxsd_folder, f"xsd{index}.xsd")
open(xsd_file, 'w', encoding='utf-8').write(data)
args.extend(['-x', inputxsd_folder])
main(args)
output = rdflib.Graph().parse(output_file, format="turtle")
shutil.rmtree(temp_folder)
return JSONResponse(content={"shacl_output": output.serialize(format="turtle")})
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
class IntegrationRequest(BaseModel):
shacl1Data: str = None
shacl2Data: str = None
mode : str = None
@app.post("/integrate")
async def integrate(request_data: IntegrationRequest):
try:
if request_data.shacl1Data and request_data.shacl2Data:
temp_folder = tempfile.mkdtemp()
output_file = os.path.join(temp_folder, "output.ttl")
priority = ["SHACL Shapes 1", "SHACL Shapes 2"]
shapes_graph = []
for p in priority:
if p=="SHACL Shapes 1" and request_data.shacl1Data:
shapes_graph.append(rdflib.Graph().parse(data=request_data.shacl1Data, format="turtle"))
elif p=="SHACL Shapes 2" and request_data.shacl2Data:
shapes_graph.append(rdflib.Graph().parse(data=request_data.shacl2Data, format="turtle"))
shapes_graph[0] = (shapes_graph[0], "")
shapes_graph[1] = (shapes_graph[1], "owl")
if request_data.mode == "all":
shIn = ShapeIntegrationAll(shapes_graph, output_file)
elif request_data.mode == "priority":
shIn = ShapeIntegrationPriority(shapes_graph, output_file)
elif request_data.mode == "priority_R":
shIn = ShapeIntegrationPriorityR(shapes_graph, output_file)
shacl_graph = shIn.integration()
shutil.rmtree(temp_folder)
else:
if request_data.shacl1Data:
shacl_graph = rdflib.Graph().parse(data=request_data.shacl1Data, format="turtle")
elif request_data.shacl2Data:
shacl_graph = rdflib.Graph().parse(data=request_data.shacl2Data, format="turtle")
return JSONResponse(content={"shacl_output_integ": shacl_graph.serialize(format="turtle")})
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
if __name__ == "__main__":
uvicorn.run(app, port=8000)