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

Switch to clingo pypi package #123

Merged
merged 1 commit into from
Feb 6, 2022
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 setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def main():
# Antlr pinned to a specific version to avoid messages "ANTLR runtime and generated code versions disagree"
# messages. If we want to bump this up, we'll need to regenerate the grammar files with the new version.
'antlr4-python3-runtime==4.7.2',

# Clingo (gringo) bindings to the clingo solver
'clingo>=5.5.1',
],

extras_require={
Expand Down
13 changes: 5 additions & 8 deletions src/tarski/reachability/clingo_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import logging
import os
import sys
import shutil
import tempfile
from pathlib import Path
from collections import defaultdict

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


def run_clingo(lp):
gringo = shutil.which("gringo")
if gringo is None:
raise CommandNotFoundError("gringo")

with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as f:
_ = [print(str(r), file=f) for r in lp.rules]
_ = [print(str(r), file=f) for r in lp.directives]
theory_filename = f.name

logging.debug('Using gringo binary found in "{}"'.format(gringo))
errlog = ''
with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as f:
with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as stderr:
# Option "-t" enforces an easier-to-parse textual output. Warnings could also be supressed with
# option "-Wno-atom-undefined"
retcode = cmd.execute([gringo, "-t", theory_filename], stdout=f, stderr=stderr)
retcode = cmd.execute([sys.executable, os.path.join(Path(__file__).parent.absolute(), "gringo.py"), "--text", theory_filename], stdout=f, stderr=stderr)
model_filename = f.name

if retcode == 0:
return model_filename, theory_filename

Expand All @@ -38,7 +35,7 @@ def run_clingo(lp):
if retcode == -24: # i.e. SIGXCPU
raise OutOfTimeError(f"Gringo ran out of time. Full error log: {errlog}")

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


def parse_model(filename, symbol_mapping):
Expand Down
27 changes: 27 additions & 0 deletions src/tarski/reachability/gringo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python3
import sys
from clingo.application import Application, clingo_main

"""
A wrapper to emulate the default clingo app behavior
"""
class WrapperClingo(Application):
def __init__(self, name):
self.app_name = name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this attribute being used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That attribute should be program_name. That's a typo on my part. It is a member of the Application class.


def main(self, ctl, files):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Innocent question: do we need all this code if we're just simply using the default behavior of clingo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same question. Going by the documentation this seems to be the recommended way to get the default clingo app behavior https://potassco.org/clingo/python-api/current/clingo/application.html

Also, I checked it. The pip package doesn't include the gringo binary, instead it has a platform specific library with a python wrapper. So, we cannot nab the path to the binary.

"""
The default implementation from clingo documentation
Note- main(...) must be implemented
"""
for f in files:
ctl.load(f)
if not files:
ctl.load("-")
ctl.ground([("base", [])])
ctl.solve()

"""
run the clingo application in the default gringo mode
"""
clingo_main(WrapperClingo("gringo"), ["--mode", "gringo"]+sys.argv[1:])