-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
243 lines (215 loc) Β· 7 KB
/
client.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
import argparse
import io
import json
import sys
import time
import traceback
import requests
server = "http://localhost:8000/"
conf_fail = '{\
"tester": {\
"share": ""\
}\
}'
conf = '{\
"tester": {\
"solver": [],\
"configuration": "auto",\
"share": "auto",\
"learn_explicit": "0",\
"sat_prepro": "no"\
},\
"solve": {\
"solve_limit": "umax,umax",\
"parallel_mode": "1,compete",\
"global_restarts": "no",\
"distribute": "conflict,global,4,4194303",\
"integrate": "gp,1024,all",\
"enum_mode": "auto",\
"project": "no",\
"models": "0",\
"opt_mode": "opt"\
},\
"asp": {\
"trans_ext": "dynamic",\
"eq": "3",\
"backprop": "0",\
"supp_models": "0",\
"no_ufs_check": "0",\
"no_gamma": "0",\
"eq_dfs": "0",\
"dlp_old_map": "0"\
},\
"solver": [],\
"configuration": "auto",\
"share": "auto",\
"learn_explicit": "0",\
"sat_prepro": "no",\
"stats": "0",\
"parse_ext": "false",\
"parse_maxsat": "false"\
}'
def main():
try:
parser = argparse.ArgumentParser(description="Test the clingo server")
parser.add_argument("-i", "--input", required=True, help="logic program")
parser.add_argument(
"-c",
"--conf",
action="store_true",
required=False,
help="set and get configuration",
)
parser.add_argument(
"-s", "--stats", action="store_true", required=False, help="get statistics"
)
parser.add_argument(
"--assume",
action="store_true",
required=False,
help="assume queen(3,1) to be true",
)
parser.add_argument(
"--pigeons",
action="store_true",
required=False,
help="add parmeters for the pigeon example holes=3 pigeons=2",
)
parser.add_argument(
"--external",
action="store_true",
required=False,
help="assign external atom `enable` with True in the external example",
)
parser.add_argument(
"--theory-dl", action="store_true", required=False, help="load DL theory"
)
parser.add_argument(
"--theory-con",
action="store_true",
required=False,
help="load clingcon theory",
)
args = parser.parse_args()
response = requests.get(server)
print(response.text)
# create solver
response = requests.get(server + "create")
if response.status_code == 500:
print("Solver already running ...")
print("Shutting down old solver ...")
response = requests.get(server + "close")
response = requests.get(server + "create")
print(response.text)
# register theory
if args.theory_dl:
response = requests.get(server + "register_dl_theory")
print(response.text)
if args.theory_con:
response = requests.get(server + "register_con_theory")
print(response.text)
# add logic program
with open(args.input, "rb") as f:
response = requests.post(
server + "add",
data=f.read(),
headers={"Content-Type": "text/plain; charset=utf-8 "},
)
print(response.text)
# set configuration
if args.conf:
response = requests.post(
server + "set_configuration",
data=io.StringIO(conf).read(),
headers={"Content-Type": "application/json; charset=utf-8 "},
)
print(response.text)
# get configuration
response = requests.get(server + "configuration")
dictionary = response.json()
json_formatted_str = json.dumps(dictionary, indent=2)
print("Configuration:", json_formatted_str)
# ground
part = '{"base": []}'
if args.pigeons:
part = '{"pigeon": ["3", "2"]}'
response = requests.post(
server + "ground",
data=io.StringIO(part).read(),
headers={"Content-Type": "application/json; charset=utf-8 "},
)
print(response.text)
if args.external:
# set external atom 'enable' to True
# works with external_test.lp
assignment = '{"literal": "enable", "truth_value": "True"}'
response = requests.post(
server + "assign_external",
data=io.StringIO(assignment).read(),
headers={"Content-Type": "application/json; charset=utf-8 "},
)
print(response.text)
# solve with assumptions
if args.assume:
assumptions = '[["queen(3,1)",true]]' # works with queens.lp
else:
assumptions = "[]"
response = requests.post(
server + "solve_with_assumptions",
data=io.StringIO(assumptions).read(),
headers={"Content-Type": "application/json; charset=utf-8 "},
)
print(response.text)
poll_models()
if args.external:
# release external atom 'enable'
# works with external_test.lp
atom = '"enable"'
response = requests.post(
server + "release_external",
data=io.StringIO(atom).read(),
headers={"Content-Type": "application/json; charset=utf-8 "},
)
print(response.text)
# get statistics
if args.stats:
response = requests.get(server + "statistics")
dictionary = response.json()
json_formatted_str = json.dumps(dictionary, indent=2)
print("Statistics:", json_formatted_str)
except Exception as e:
print(e)
traceback.print_exception(*sys.exc_info())
return 1
def poll_models():
"""poll for models"""
count = 0
while True:
response = requests.get(server + "model", timeout=1)
if response.status_code == 200:
json_response = response.json()
if json_response == "Running":
print("No model yet ... waiting 10 seconds.")
time.sleep(10)
elif json_response == "Done":
print("Search finished, no more models.")
break
elif "Model" in json_response:
model = json_response["Model"]
count += 1
print("Model", count, ":")
print(bytes(model).decode("utf-8"))
response = requests.get(server + "resume")
print(response.text)
else:
print("Error unexpected response to model/ request")
print(json_response)
exit()
else:
print("ServerError")
print(response.text)
break
response = requests.get(server + "close")
print(response.text)
if __name__ == "__main__":
sys.exit(main())