Created
October 31, 2017 11:33
-
-
Save dariusf/2eaf2c1c0211981d437ebd08315ee3bf to your computer and use it in GitHub Desktop.
Make clingo output JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#script (python) | |
from __future__ import print_function | |
import clingo | |
import json | |
def to_json(symbol): | |
s = {} | |
if symbol.type == clingo.SymbolType.Number: | |
s['value'] = symbol.number | |
elif symbol.type == clingo.SymbolType.Function: | |
s['name'] = symbol.name | |
if symbol.arguments: | |
s['args'] = [to_json(a) for a in symbol.arguments] | |
else: | |
assert False, 'unrecognised type ' + symbol.type | |
return s | |
def on_model(m): | |
# Just the shown terms; see https://potassco.org/clingo/python-api/current/clingo.html#Model | |
symbols = m.symbols(False, False, True, False, False, False) | |
print(json.dumps([to_json(s) for s in symbols])) | |
def main(prg): | |
prg.ground([("base",[])]) | |
# https://sourceforge.net/p/potassco/mailman/message/35495025/ | |
# https://github.com/potassco/clingo/tree/master/examples/clingo/enum-assumption | |
prg.use_enumeration_assumption = False | |
prg.solve(on_model=on_model) | |
#end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
clear | |
./clingo -n 0 --outf=3 pp.lp test.lp | |
# why does clingo not exit cleanly... | |
true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
thing(a;b;c). | |
blah(1). | |
func(f(a)). | |
1 { p(A) : thing(A) } 1. | |
% this will cause unsat | |
% :- blah(1). | |
#show p/1. | |
#show blah/1. | |
#show func/1. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment