-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasp_wrapper.py
58 lines (42 loc) · 1.4 KB
/
asp_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from clingo import Control
from predicate_parser import PredicateParser, PredicateLexer
lexer = PredicateLexer()
parser = PredicateParser()
def parse_answer(code):
return parser.parse(lexer.tokenize(code))
class Program:
# -- Fields --
# rule_list
def __init__(self, code):
self.code = code
self.answer_sets = []
self.errors = []
def logger(self, code, message):
self.errors.append(message)
def __on_model(self, model):
answer_set = []
for atom in model.symbols(atoms="True"):
answer_set.append(parse_answer(str(atom)))
self.answer_sets.append(answer_set)
def solve(self):
ctl = Control(logger=self.logger)
ctl.configuration.solve.models = 0 # to obtain all answer sets
try:
ctl.add("base", [], self.code)
except RuntimeError:
print(self.errors)
exit()
ctl.ground([("base", [])])
ctl.solve(on_model=self.__on_model)
return self.answer_sets
# given an ASP program, solve it and print the answer sets
def solve(code):
program = Program(code)
return program.solve()
# solve an ASP program given as a file and print the outpupt
def solve_file(filename):
with open(filename, 'r') as myfile:
code = myfile.read()
solve(code)
if __name__ == '__main__':
print(solve("mortal(X) :- man(X). man(socrates)."))