asp-chef-cli 0.5.3__tar.gz → 0.5.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: asp-chef-cli
3
- Version: 0.5.3
3
+ Version: 0.5.5
4
4
  Summary: A simple CLI to run ASP Chef recipes
5
5
  Author: Mario Alviano
6
6
  Author-email: mario.alviano@gmail.com
@@ -120,8 +120,9 @@ async def _(json):
120
120
  @endpoint(router, "/sdl/")
121
121
  async def _(json):
122
122
  program = json["program"]
123
+ minizinc = json["minizinc"]
123
124
  result = subprocess.check_output(
124
- ["./run.sh"],
125
+ ["./run.sh", "minizinc" if minizinc else "asp"],
125
126
  input=program.encode(),
126
127
  cwd="../SDL",
127
128
  )
@@ -227,6 +228,72 @@ async def _(json):
227
228
  return {"models": models}
228
229
 
229
230
 
231
+ pasta_process: Final[Dict[str, Optional[subprocess.Popen]]] = defaultdict(lambda: None)
232
+ pasta_path: Final[str | None] = shutil.which("pastasolver")
233
+
234
+
235
+ def pasta_terminate(uuid):
236
+ if pasta_process[uuid] is not None:
237
+ pasta_process[uuid].kill()
238
+
239
+
240
+ @endpoint(router, "/pasta/")
241
+ async def _(json):
242
+ global pasta_process
243
+
244
+ uuid = json["uuid"]
245
+ program = json["program"]
246
+ output_predicate = json["output_predicate"] or "__bounds__"
247
+ bound_multiplier = 0
248
+ try:
249
+ bound_multiplier = int(json["bound_multiplier"])
250
+ bound_multiplier = min(max(bound_multiplier, 0), 1_000_000)
251
+ except ValueError:
252
+ bound_multiplier = 0
253
+
254
+ timeout = json["timeout"]
255
+ if type(timeout) is not int or timeout < 1 or timeout >= 24 * 60 * 60:
256
+ timeout = 5
257
+
258
+ pasta_terminate(uuid)
259
+
260
+ cmd = ["/bin/timeout", str(timeout), pasta_path, "/tmp/a"]
261
+ pasta_process[uuid] = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
262
+ out, err = pasta_process[uuid].communicate(program.encode())
263
+ timeout_reached: Final = pasta_process[uuid].returncode == 124
264
+ pasta_process[uuid] = None
265
+
266
+ # report errors
267
+ output = out.decode()
268
+ lines = output.split('\n')
269
+ if any(line.startswith("Error") for line in lines):
270
+ raise ValueError(output)
271
+ if timeout_reached:
272
+ lines = [line for line in lines if not line.startswith("Sig term")]
273
+
274
+ lines = [line for line in lines if line]
275
+ lb, ub = None, None
276
+ if len(lines) == 1:
277
+ lb = ub = lines[0].split("Lower probability == upper probability for the query: ")[1]
278
+ elif len(lines) == 2:
279
+ lb = float(lines[0].split("Lower probability for the query: ")[1])
280
+ ub = float(lines[1].split("Upper probability for the query: ")[1])
281
+
282
+ if lb is None or ub is None:
283
+ raise ValueError("Could not parse output from PASTA solver:\n" + '\n'.join(lines))
284
+
285
+ if bound_multiplier:
286
+ lb = round(lb * bound_multiplier)
287
+ ub = round(ub * bound_multiplier)
288
+ else:
289
+ lb = f'real("{lb}")'
290
+ ub = f'real("{ub}")'
291
+
292
+ return {"models": [
293
+ [f'{output_predicate}({lb},{ub})']
294
+ ]}
295
+
296
+
230
297
  @endpoint(router, "/template/core-template/")
231
298
  async def _(json):
232
299
  if not json:
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "asp-chef-cli"
3
- version = "0.5.3"
3
+ version = "0.5.5"
4
4
  description = "A simple CLI to run ASP Chef recipes"
5
5
  authors = ["Mario Alviano <mario.alviano@gmail.com>"]
6
6
  readme = "README.md"
File without changes
File without changes