From c799df6ea970c1335035544c1b2028072b22ebb0 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Mon, 22 Dec 2014 19:15:34 +0100 Subject: Add support for per-problem fact libraries Each fact library is loaded at most once, so different problems can share the same library (e.g. family-relation problems). --- prolog/engine.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/prolog/engine.py b/prolog/engine.py index 8ad8bfa..c859b67 100644 --- a/prolog/engine.py +++ b/prolog/engine.py @@ -117,8 +117,7 @@ class PrologEngine(object): self.answers = {} # Loads the correct solution [code] to problem [pid]. - # TODO handle library loading. - def load_solution(self, pid, code): + def load_solution(self, pid, code, facts=None): module = 'solution{}'.format(pid) fid = PL_open_foreign_frame() @@ -126,6 +125,10 @@ class PrologEngine(object): for rule in prolog.util.split(code): self.call('assertz/1', [Term('{}:({})'.format(module, rule))]) predicates.add('{}:{}'.format(module, self.predicate_indicator(rule))) + if facts: + for rule in prolog.util.split(facts): + self.call('assertz/1', [Term(rule)]) + predicates.add(self.predicate_indicator(rule)) self.call('compile_predicates/1', [Term([Term(p) for p in predicates])]) PL_discard_foreign_frame(fid) -- cgit v1.2.1