Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added string model parsing in clingo wrapper #110

Merged
merged 6 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/tarski/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ class OutOfMemoryError(TarskiError):
class OutOfTimeError(TarskiError):
pass

class ArgumentError(TarskiError):
pass


# class WrongTermUsageError(TarskiError):
# def __init__(self):
Expand Down
2 changes: 1 addition & 1 deletion src/tarski/grounding/lp_grounding.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _solve_lp(self):
if self.model is None:
lp, tr = create_reachability_lp(self.problem, self.do_ground_actions, self.include_variable_inequalities)
model_filename, theory_filename = run_clingo(lp)
self.model = parse_model(model_filename, tr)
self.model = parse_model(filename=model_filename, symbol_mapping=tr)

# Remove the input and output files for Gringo
silentremove(model_filename)
Expand Down
39 changes: 23 additions & 16 deletions src/tarski/reachability/clingo_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import tempfile
from collections import defaultdict

from ..errors import CommandNotFoundError, ExternalCommandError, OutOfMemoryError, OutOfTimeError
from ..errors import CommandNotFoundError, ExternalCommandError, OutOfMemoryError, OutOfTimeError, ArgumentError
from ..utils import command as cmd


Expand Down Expand Up @@ -40,22 +40,29 @@ def run_clingo(lp):

raise ExternalCommandError(f"Unknown Gringo error. Gringo exited with code {retcode}. Full error log: {errlog}")

def parse_model(*, filename=None, content=None, symbol_mapping):
if filename and not content:
with open(filename, "r") as f:
return _parse_model(f, symbol_mapping)
elif content and not filename:
return _parse_model(content.splitlines(), symbol_mapping)
else:
raise ArgumentError(f"Cannot have both filename and content as arguments.")

def parse_model(filename, symbol_mapping):
def _parse_model(lines, symbol_mapping):
tr = symbol_mapping
model = defaultdict(set)
with open(filename, "r") as f:
for line in f:
data = line.rstrip(' \n.').rstrip(')')
components = data.split('(')
if len(components) == 1:
symbol = tr.back(components[0])
model[symbol].add(())
elif len(components) == 2:
symbol, arguments = components
model[tr.back(symbol)].add(tuple(tr.back(s) for s in arguments.split(',')))
else:
# No nested terms expected, so there should be at most 2 components
raise RuntimeError('Unexpected line "{}" in Clingo solution file'.format(line))

for line in lines:
data = line.rstrip(' \n.').rstrip(')')
components = data.split('(')
if len(components) == 1:
symbol = tr.back(components[0])
model[symbol].add(())
elif len(components) == 2:
symbol, arguments = components
model[tr.back(symbol)].add(tuple(tr.back(s) for s in arguments.split(',')))
else:
# No nested terms expected, so there should be at most 2 components
raise RuntimeError('Unexpected line "{}" in Clingo solution file'.format(line))

return model