| 1 |
from pathlib import Path |
| 2 |
import subprocess |
| 3 |
import json |
| 4 |
import sys |
| 5 |
import os |
| 6 |
from tempfile import NamedTemporaryFile |
| 7 |
from contextlib import contextmanager |
| 8 |
|
| 9 |
|
| 10 |
@contextmanager |
| 11 |
def named_temp_file(*args, **kwargs): |
| 12 |
# Workaround for temp files on Windows |
| 13 |
temp = NamedTemporaryFile(delete=False, *args, **kwargs) |
| 14 |
try: |
| 15 |
yield temp |
| 16 |
finally: |
| 17 |
temp.close() |
| 18 |
os.unlink(temp.name) |
| 19 |
|
| 20 |
|
| 21 |
def test_json_stream(): |
| 22 |
from minizinc import default_driver, Driver |
| 23 |
expected = [ |
| 24 |
{"type": "trace", "section": "default", "message": "foo\n"}, |
| 25 |
{"type": "comment", "comment": "% This is a comment\n"}, |
| 26 |
{"type": "statistics", "statistics": {"foo": 123, "bar": "string"}}, |
| 27 |
{ |
| 28 |
"type": "solution", |
| 29 |
"output": {"dzn": "x = 1;\ny = 1;\n"}, |
| 30 |
"sections": ["dzn"], |
| 31 |
}, |
| 32 |
{ |
| 33 |
"type": "solution", |
| 34 |
"output": {"dzn": "x = 1;\ny = 2;\n"}, |
| 35 |
"sections": ["dzn"], |
| 36 |
}, |
| 37 |
{ |
| 38 |
"type": "solution", |
| 39 |
"output": {"dzn": "x = 1;\ny = 3;\n"}, |
| 40 |
"sections": ["dzn"], |
| 41 |
}, |
| 42 |
{ |
| 43 |
"type": "solution", |
| 44 |
"output": {"dzn": "x = 2;\ny = 3;\n"}, |
| 45 |
"sections": ["dzn"], |
| 46 |
}, |
| 47 |
{ |
| 48 |
"type": "solution", |
| 49 |
"output": {"dzn": "x = 3;\ny = 3;\n"}, |
| 50 |
"sections": ["dzn"], |
| 51 |
}, |
| 52 |
{"type": "status", "status": "OPTIMAL_SOLUTION"}, |
| 53 |
] |
| 54 |
|
| 55 |
here = Path(__file__).resolve().parent |
| 56 |
assert isinstance(default_driver, Driver) |
| 57 |
model_file = here / "test_json_stream.mzn" |
| 58 |
with named_temp_file(suffix=".msc", mode="w", encoding="utf-8") as fp: |
| 59 |
json.dump( |
| 60 |
{ |
| 61 |
"name": "Test solver", |
| 62 |
"version": "1.0", |
| 63 |
"id": "org.minizinc.test_solver", |
| 64 |
"executable": [ |
| 65 |
Path(sys.executable).resolve().as_posix(), |
| 66 |
Path(__file__).resolve().as_posix(), |
| 67 |
], |
| 68 |
}, |
| 69 |
fp, |
| 70 |
) |
| 71 |
fp.close() |
| 72 |
p = subprocess.run( |
| 73 |
[ |
| 74 |
default_driver._executable, |
| 75 |
model_file, |
| 76 |
"--json-stream", |
| 77 |
"--solver", |
| 78 |
fp.name, |
| 79 |
], |
| 80 |
stdin=None, |
| 81 |
stdout=subprocess.PIPE, |
| 82 |
stderr=subprocess.PIPE, |
| 83 |
) |
| 84 |
assert p.returncode == 0 |
| 85 |
actual = [json.loads(l) for l in p.stdout.splitlines(False)] |
| 86 |
assert expected == actual |
| 87 |
|
| 88 |
|
| 89 |
if __name__ == "__main__": |
| 90 |
# Dummy solver output |
| 91 |
print("% This is a comment") |
| 92 |
print("%%%mzn-stat: foo=123") |
| 93 |
print('%%%mzn-stat: bar="string"') |
| 94 |
print("%%%mzn-stat-end") |
| 95 |
obj = 0 |
| 96 |
for x in range(1, 4): |
| 97 |
for y in range(1, 4): |
| 98 |
if x + y > obj: |
| 99 |
obj = x + y |
| 100 |
print(f"x = {x};") |
| 101 |
print(f"y = {y};") |
| 102 |
print("----------") |
| 103 |
print("==========") |