Skip to content

Instantly share code, notes, and snippets.

@Minimuino
Created June 5, 2017 17:39
Show Gist options
  • Save Minimuino/1301d98f2881bbaea858fc925a984820 to your computer and use it in GitHub Desktop.
Save Minimuino/1301d98f2881bbaea858fc925a984820 to your computer and use it in GitHub Desktop.
python gringo module example
# -*- coding: utf-8 -*-
import gringo
UNSAFE_PROGRAM = {'name': 'base_unsafe',
'rule': 'p(X).'}
SAFE_PROGRAM = {'name': 'base_safe',
'rule': 'p(x).'}
# Model callback
def on_model(m):
print m
def print_result(result):
if result == gringo.SolveResult.UNKNOWN:
print 'UNKNOWN'
elif result == gringo.SolveResult.SAT:
print 'SAT'
elif result == gringo.SolveResult.UNSAT:
print 'UNSAT'
def main():
# Get solver instance
solver = gringo.Control()
# Add an UNSAFE rule
solver.add(UNSAFE_PROGRAM['name'], [], UNSAFE_PROGRAM['rule'])
# Try to ground and solve
try:
solver.ground([(UNSAFE_PROGRAM['name'], [])])
result = solver.solve(on_model=on_model)
print_result(result)
except RuntimeError, e:
print e
# Reset the solver in some way?
# Get a new solver instance?
solver = gringo.Control()
# Add a SAFE rule
solver.add(SAFE_PROGRAM['name'], [], SAFE_PROGRAM['rule'])
# Try to ground and solve
try:
solver.ground([(SAFE_PROGRAM['name'], [])])
result = solver.solve(on_model=on_model)
print_result(result)
except RuntimeError, e:
print e
# Reset the solver in some way?
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment