/
clingo-server.tex
66 lines (59 loc) · 2.15 KB
/
clingo-server.tex
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
% ----------------------------------------------------------------------
\begin{frame}{\textit{clingo-server}}
\begin{itemize}
\item \structure{Idea} \ run \clingo\ as a server and use it via a Web API
\item \structure{API methods} \
\begin{itemize}
\item create a solver
\item register a theory
\item add logic programs
\item initiate grounding
\item set value of external atoms
\item initiate solving (with assumptions)
\item poll models
\item resume solving
\item finalize search
\end{itemize}
\item \structure{Applications} \ running ASP-based systems in the cloud
\end{itemize}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}[fragile,shrink]{Example}{Minimal usage}
\begin{lstlisting}
# Create solver
response = requests.get("http://my-clingo-server.de:8000/create")
# Upload logic program / facts
with open("queens.lp", "rb") as f:
response = requests.post(
"http://my-clingo-server.de:8000/add",
data=f.read(),
headers={"Content-Type": "text/plain; charset=utf-8 "},
)
# Initiate grounding
response = requests.post(
"http://my-clingo-server.de:8000/ground",
data=io.StringIO('{"base": []}').read(),
headers={"Content-Type": "application/json; charset=utf-8 "},
)
# Initiate solving
response = requests.get("http://my-clingo-server.de:8000/solve")
# Poll models
response = requests.get("http://my-clingo-server.de:8000/model", timeout=1)
if response.json() == "Running":
print("No model yet ... ")
if "Model" in response.json():
model = response.json()["Model"]
print("Model found:")
print(bytes(model).decode("utf-8"))
if response.json() == "Done":
print("Search finished, no more models.")
# Close solve handle
response = requests.get("http://my-clingo-server.de:8000/close")
\end{lstlisting}
\end{frame}
% ----------------------------------------------------------------------
%
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../../main"
%%% End: