Skip to content

Instantly share code, notes, and snippets.

@dariusf
Created October 31, 2017 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dariusf/2eaf2c1c0211981d437ebd08315ee3bf to your computer and use it in GitHub Desktop.
Save dariusf/2eaf2c1c0211981d437ebd08315ee3bf to your computer and use it in GitHub Desktop.
Make clingo output JSON
#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.
#!/bin/bash
clear
./clingo -n 0 --outf=3 pp.lp test.lp
# why does clingo not exit cleanly...
true
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