-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelService.jl
169 lines (144 loc) · 3.9 KB
/
ModelService.jl
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
module ModelService
using UUIDs
using Genie
using Genie.Requests
using Genie.Renderer.Json
using JSON: json, parse
using Catlab
using Catlab.CategoricalAlgebra
using Catlab.Programs
using Catlab.WiringDiagrams
using AlgebraicPetri
using ModelingToolkit
using Latexify
import YAML: load
using SwagUI
import SwaggerMarkdown: build, @swagger, OpenAPI, DOCS
include("./ASKEMPetriNets.jl")
using .ASKEMPetriNets
export start!
# heatlhcheck
@swagger """
/:
get:
summary: Healthcheck
description: A basic healthcheck for the model service
responses:
'200':
description: Returns notice that service has started
"""
route("/") do
return json("model-service running")
end
# convert petri to latex
#
# This expects a json body of a petri ascet:
#
# {
# S: [ ...]
# T: [ ...]
# I: [ ...]
# O: [ ...]
# }
#
@swagger """
/api/petri-to-latex:
post:
summary: ACSet to LaTex
description: Convert an ACSet into a LaTeX string by ways of ODESystem
requestBody:
description: Arguments to pass into conversion
required: true
content:
application/json:
schema:
type: object
properties:
S:
type: array
T:
type: array
I:
type: array
O:
type: array
responses:
'200':
description: Returns LaTex string
"""
route("/api/petri-to-latex", method = POST) do
payload = jsonpayload()
model = parse_json_acset(LabelledPetriNet, json(payload))
model_odesys = ODESystem(model)
model_latex = latexify(model_odesys)
println(model_latex.s)
return model_latex.s
end
route("/api/petri-to-latex-json", method = POST) do
payload = jsonpayload()
model = parse_json_acset(LabelledPetriNet, json(payload))
model_odesys = ODESystem(model)
model_latex = latexify(model_odesys)
println(model_latex.s)
return json(
Dict([
(:latex, model_latex.s)
])
)
end
@swagger """
/api/stratify:
post:
summary: Stratify
description: Given two typed AMR Petrinets, perform stratification and return result as AMR Petrinet
requestBody:
description: Arguments to pass into conversion
required: true
content:
application/json:
schema:
type: object
properties:
baseModel:
type: object
strataModel:
type: object
responses:
'200':
description: Returns new model
"""
route("/api/stratify", method=POST) do
# payload = jsonpayload()
rawPayload = rawpayload()
jsonData = parse(rawPayload)
baseModel = jsonData["baseModel"]
strataModel = jsonData["strataModel"]
x = TypedASKEMPetriNet(ASKEMPetriNet(baseModel))
y = TypedASKEMPetriNet(ASKEMPetriNet(strataModel))
result = StratifiedASKEMPetriNet(x, y)
return json(result)
end
# Generate swagger-ui docs
info = Dict("title" => "Model Service", "version" => "0.1.0")
openAPI = OpenAPI("3.0.0", info)
openAPI.paths = load(join(DOCS)) # NOTE: Has to be done manually because it's broken in SwaggerMarkdown
documentation = build(openAPI)
route("/docs") do
render_swagger(documentation)
end
function start!()
println("Starting model service")
# Configuration
# FIXME: Remove this later when quarkus API sever is fully configured to do forwarding/proxying routes
Genie.config.cors_headers["Access-Control-Allow-Origin"] = "*"
Genie.config.cors_headers["Access-Control-Allow-Headers"] = "Content-Type"
Genie.config.cors_headers["Access-Control-Allow-Methods"] ="GET,POST,PUT,DELETE,OPTIONS"
Genie.config.cors_allowed_origins = ["*"]
Genie.Configuration.config!(
cors_allowed_origins = ["*"]
)
# Start the API
up(8888, "0.0.0.0", async = false)
println("Model service started!!")
end
end # module