From 004e1074407b6a0d53d2e0627baa838915932f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=CC=88zgu=CC=88r=20Akgu=CC=88n?= Date: Thu, 2 Mar 2023 09:08:46 +0000 Subject: [PATCH] Use --solutions-in-one-file --- .github/workflows/CI.yml | 7 ++++++- conjure/conjure.py | 8 ++++---- conjure/conjurehelper.py | 23 ++++++++++++----------- conjure/conjuremagics.py | 6 +----- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 7a8bc7a..6626703 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,6 +1,11 @@ + on: - pull_request: + workflow_dispatch: # can be triggered manually push: + branches: + - main # run for the main branch + pull_request: # and for PRs + # other branches that want testing must create a PR jobs: test: diff --git a/conjure/conjure.py b/conjure/conjure.py index 0336e05..4fcbb02 100644 --- a/conjure/conjure.py +++ b/conjure/conjure.py @@ -19,11 +19,11 @@ def solve(self, args: str, code: str, shell_params: dict) -> dict: else: raise Exception("Variable {0} is not defined".format(p)) temp_params_file = conjurehelper.create_params_file(params) - shell_output = Popen(["conjure solve -ac " + temp_essence_file + " " + temp_params_file + - " --output-format=json --solver=chuffed " + args, ], shell=True, stdout=PIPE, stderr=PIPE) + shell_output = Popen(["conjure solve " + temp_essence_file + " " + temp_params_file + + " --output-format=json --solutions-in-one-file --solver=chuffed " + args, ], shell=True, stdout=PIPE, stderr=PIPE) else: - shell_output = Popen(["conjure solve -ac " + temp_essence_file + - " --output-format=json --solver=chuffed " + args, ], shell=True, stdout=PIPE, stderr=PIPE) + shell_output = Popen(["conjure solve " + temp_essence_file + + " --output-format=json --solutions-in-one-file --solver=chuffed " + args, ], shell=True, stdout=PIPE, stderr=PIPE) _, error = shell_output.communicate() if(error): diff --git a/conjure/conjurehelper.py b/conjure/conjurehelper.py index 0d09cda..fee48e3 100644 --- a/conjure/conjurehelper.py +++ b/conjure/conjurehelper.py @@ -40,18 +40,19 @@ def create_params_file(self, params={}) -> str: return self.create_temp_file("param.json", tempstr) def read_solution_json_file(self) -> dict: - solutions = [] try: - if os.path.isdir('./conjure-output'): - files = sorted(os.listdir('./conjure-output')) - for f in files: - if f.endswith('.json'): - with open('./conjure-output/' + f) as file: - solutions.append(json.loads(file.read())) - except: - raise Exception('Error while reading json solution file(s).') - - return {"conjure_solutions": solutions} + for p in os.listdir('conjure-output'): + if p.endswith('solutions.json'): + with open("conjure-output/" + p) as f: + filecontent = f.read() + # there is a bug in Conjure's latest release... + if filecontent.strip() == "]": + return {"conjure_solutions": []} + else: + with open("conjure-output/" + p) as f: + return {'conjure_solutions': json.load(f)} + except Exception as e: + raise Exception('Error while reading json solution file.') def clean_tmp_files(self) -> None: # remove conjure-output-folder diff --git a/conjure/conjuremagics.py b/conjure/conjuremagics.py index fbfa42b..86a75fc 100644 --- a/conjure/conjuremagics.py +++ b/conjure/conjuremagics.py @@ -55,17 +55,13 @@ def conjure(self, args, code): try: if code not in self.conjure_models: # we won't add code to models if the code is already there self.conjure_models.append(code) - resultdict = conjure.solve(args, '\n'.join( - self.conjure_models), dict(self.shell.user_ns)) + resultdict = conjure.solve(args, '\n'.join(self.conjure_models), dict(self.shell.user_ns)) except Exception as err: self.conjure_models.pop() print("{}: {}".format(type(err).__name__, err), file=sys.stderr) return - # assign results to notebook environment - for key, value in resultdict.items(): - self.shell.user_ns[key] = value if len(resultdict['conjure_solutions']) == 1: # assign results of single solution to notebook environment for key, value in resultdict['conjure_solutions'][0].items():