summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@araneo.org>2014-04-25 15:56:13 +0200
committerAleš Smodiš <aless@guru.si>2015-08-11 14:26:00 +0200
commit170fb9c4cb46464360cafe2d4ced1267746b372a (patch)
treed42822661c8a4f536646f716c590f2b8bb9e9e20
parentcd572b9b2de73eea8e83b028c7b4c3906ab60ab2 (diff)
Add call(predicate, arguments) to PrologEngine
Also disable error messages printed by swipl library.
-rwxr-xr-xprolog/engine.py51
1 files changed, 25 insertions, 26 deletions
diff --git a/prolog/engine.py b/prolog/engine.py
index 7f91a30..bd67d86 100755
--- a/prolog/engine.py
+++ b/prolog/engine.py
@@ -84,13 +84,14 @@ class PrologEngine(object):
raise EnvironmentError('Could not initialize Prolog environment.'
'PL_initialise returned {0}'.format(result))
- # construct needed predicates
+ # construct some predicates
self.p = {
'assertz': PL_predicate(b'assertz', 2, None),
'erase': PL_predicate(b'erase', 1, None),
'call_with_depth_limit': PL_predicate(b'call_with_depth_limit', 3, None),
'call_with_time_limit': PL_predicate(b'call_with_time_limit', 2, None),
'consult': PL_predicate(b'consult', 1, None),
+ 'message_to_string': PL_predicate(b'message_to_string', 2, None),
'read_term_from_atom': PL_predicate(b'read_term_from_atom', 3, None),
'safe_goal': PL_predicate(b'safe_goal', 1, None),
'set_prolog_stack': PL_predicate(b'set_prolog_stack', 2, None)
@@ -101,15 +102,19 @@ class PrologEngine(object):
self.refs = {}
self.max_index = 0
- PL_call_predicate(None, self.err_flags, self.p['set_prolog_stack'],
- Termv([Term('global'), Term('limit(2*10**9)')]).ref)
- PL_call_predicate(None, self.err_flags, self.p['set_prolog_stack'],
- Termv([Term('local'), Term('limit(2*10**9)')]).ref)
+ # increase memory limits
+ self.call('set_prolog_stack', [Term('global'), Term('limit(2*10**9)')])
+ self.call('set_prolog_stack', [Term('local'), Term('limit(2*10**9)')])
+
+ # discard messages from swipl library
+ self.load('message_hook(_, _, _). ')
+
+ def call(self, name, args):
+ return PL_call_predicate(None, self.err_flags,
+ self.p[name], Termv(args).ref) == 1
def consult(self, filename):
- args = Termv([Term(Atom(filename))])
- out = PL_call_predicate(None, self.err_flags, self.p['consult'], args.ref)
- return out == 1
+ return self.call('consult', [Term(Atom(filename))])
def load(self, program, module=None):
tokens = tokenize(program)
@@ -124,14 +129,13 @@ class PrologEngine(object):
if module != None:
rule = module + ':(' + rule + ')'
- args = Termv([Term(rule), Term('Ref')])
- out = PL_call_predicate(None, self.err_flags, self.p['assertz'], args.ref)
- refs.append(args[1])
+ Ref = Term('Ref')
+ if self.call('assertz', [Term(rule), Ref]):
+ refs.append(Ref)
except:
# unload already loaded rules (hacky, mustfix, todo)
for ref in refs:
- args = Termv([ref])
- out = PL_call_predicate(None, self.err_flags, self.p['erase'], args.ref)
+ self.call('erase', [ref])
return None
self.max_index += 1
@@ -140,8 +144,7 @@ class PrologEngine(object):
def unload(self, index):
for ref in self.refs[index]:
- args = Termv([ref])
- out = PL_call_predicate(None, self.err_flags, self.p['erase'], args.ref)
+ self.call('erase', [ref])
del self.refs[index]
def query(self, q, module=None):
@@ -154,14 +157,12 @@ class PrologEngine(object):
options = Term([Term(('variable_names', Termv([var_list])))])
# parse term
- if not PL_call_predicate(None, self.err_flags, self.p['read_term_from_atom'],
- Termv([Term(Atom(q)), goal, options]).ref):
- sys.stderr.write('WARNING: Could not read term from {}\n'.format(q))
+ if not self.call('read_term_from_atom', [Term(Atom(q)), goal, options]):
+ sys.stderr.write('Warning: Could not read term from {}\n'.format(q))
return None
# check if goal is safe with currently loaded rules
- if not PL_call_predicate(None, self.err_flags, self.p['safe_goal'],
- Termv([goal]).ref):
- sys.stderr.write('WARNING: Unsafe goal: {}\n'.format(q))
+ if not self.call('safe_goal', [goal]):
+ sys.stderr.write('Warning: Unsafe goal: {}\n'.format(q))
return None
solutions = []
@@ -185,11 +186,9 @@ class PrologEngine(object):
if ex != None:
ex = Term(ref=PL_exception(qid))
if ex != None:
- message_to_string = PL_predicate(b'message_to_string', 2, None)
- args = Termv([ex, Term()])
-
- if PL_call_predicate(None, self.err_flags, message_to_string, args.ref):
- sys.stderr.write('Error: ' + str(args[1]) + '\n')
+ Msg = Term('Msg')
+ if self.call('message_to_string', [ex, Msg]):
+ sys.stderr.write('Error: ' + str(Msg) + '\n')
else:
sys.stderr.write('Unknown error\n')
break