From 500dae272daabdb5e9272d3e54111aea5e109c3b Mon Sep 17 00:00:00 2001 From: Aleksander Sadikov Date: Mon, 29 Feb 2016 20:56:12 +0100 Subject: Improved hints for ancestor/2. "Timeout results as False" ask_truthTO() function added. --- prolog/engine.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/prolog/engine.py b/prolog/engine.py index b023c9e..cebadea 100644 --- a/prolog/engine.py +++ b/prolog/engine.py @@ -211,6 +211,30 @@ def ask_truth(engine, query, timeout=1): # no error, query failed return False +def ask_truthTO(engine, query, timeout=1): + # Returns either True or False + # (Runtime) error is False! + # Timeout *is* handled: returned as False + + # Run the query. + try: + reply, output = ask(engine, query, timeout) + except socket.timeout as ex: + return False + + if 'error' in map(itemgetter(0), output): + return False + # TODO: Tim, this doesn't really work. It just proceeds and returns as a runtime error. + answer, error, more = process_answer(reply) + if error: + # query caused a runtime error + return False + elif answer: + return True + else: + # no error, query failed + return False + # reformats prolog's answer into a neater form def process_answer(reply): # When an engine is destroyed, a nested data object has the actual -- cgit v1.2.1