From 80881f3a6b05474fcd5afe7b3ed30cc7e75e4a92 Mon Sep 17 00:00:00 2001
From: Aleksander Sadikov <aleksander.sadikov@fri.uni-lj.si>
Date: Tue, 1 Mar 2016 01:09:07 +0100
Subject: Improved hints for ancestor/2 and descendant/2.

---
 prolog/problems/family_relations/ancestor_2/common.py |  3 ++-
 prolog/problems/family_relations/ancestor_2/sl.py     |  4 ++++
 .../problems/family_relations/descendant_2/common.py  | 19 ++++++++++---------
 prolog/problems/family_relations/descendant_2/sl.py   |  4 ++++
 4 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/prolog/problems/family_relations/ancestor_2/common.py b/prolog/problems/family_relations/ancestor_2/common.py
index d2e4f76..6690e00 100644
--- a/prolog/problems/family_relations/ancestor_2/common.py
+++ b/prolog/problems/family_relations/ancestor_2/common.py
@@ -29,6 +29,7 @@ hint_type = {
     'ancestor_to_oneself': Hint('ancestor_to_oneself'),
     'descendant_need_not_be_parent': Hint('descendant_need_not_be_parent'),
     'wrong_direction': Hint('wrong_direction'),
+    'timeout': Hint('timeout'),
 }
 
 test_cases = [
@@ -105,7 +106,7 @@ def hint(code, aux_code):
             return [{'id': 'descendant_need_not_be_parent'}]
 
     except socket.timeout as ex:
-        pass
+        return [{'id': 'timeout'}]
 
     finally:
         if engine_id:
diff --git a/prolog/problems/family_relations/ancestor_2/sl.py b/prolog/problems/family_relations/ancestor_2/sl.py
index 081732b..03e9230 100644
--- a/prolog/problems/family_relations/ancestor_2/sl.py
+++ b/prolog/problems/family_relations/ancestor_2/sl.py
@@ -63,6 +63,10 @@ Skratka, poskusi prevesti na "manjši" problem, npr. prednik v enem koraku manj
 <p>Si morda sprogramiral ravno obratno in zamenjal prednika s potomcem?
 <code>X</code> naj bo prednik od <code>Y</code> in ne obratno!</p>
 <p><img src="[%@resource Prolog_ancestor_02.svg%]" /></p>
+''',
+
+    'timeout': '''\
+<p>Je morda na delu potencialno neskončna rekurzija?</p>
 ''',
 }
 
diff --git a/prolog/problems/family_relations/descendant_2/common.py b/prolog/problems/family_relations/descendant_2/common.py
index d2a2dcb..4c7f8e3 100644
--- a/prolog/problems/family_relations/descendant_2/common.py
+++ b/prolog/problems/family_relations/descendant_2/common.py
@@ -29,6 +29,7 @@ hint_type = {
     'descendant_of_oneself': Hint('ancestor_to_oneself'),
     'descendant_need_not_be_parent': Hint('descendant_need_not_be_parent'),
     'wrong_direction': Hint('wrong_direction'),
+    'timeout': Hint('timeout'),
 }
 
 test_cases = [
@@ -84,28 +85,28 @@ def hint(code, aux_code):
         # missing/failed base case
         # TODO: how will ask_truth handle/return timeouts...
         # TODO: timeout is the same as fail in this particular case
-        if prolog.engine.ask_truth(engine_id,
+        if prolog.engine.ask_truthTO(engine_id,
             'parent(Y, X), \+ descendant(X, Y)'):
             return [{'id': 'base_case'}]
 
         # descendant of oneself
-        if prolog.engine.ask_truth(engine_id, 'descendant(X, X)'):
+        if prolog.engine.ask_truthTO(engine_id, 'descendant(X, X)'):
             return [{'id': 'descendant_of_oneself'}]
 
-        # X does not necessarily need to be a parent
-        if prolog.engine.ask_one(engine_id,
-            'descendant(X, _), \+ parent(X, _)') == 'false':
-            return [{'id': 'descendant_need_not_be_parent'}]
-
         # ancestor instead of descendant (wrong direction)
         # warning: knowledge base dependent
-        if prolog.engine.ask_truth(engine_id,
+        if prolog.engine.ask_truthTO(engine_id,
             'findall(_, descendant(william, X), L1), length(L1, 6), \
              findall(_, descendant(X, william), L2), length(L2, 2)'):
             return [{'id': 'wrong_direction'}]
 
+        # X does not necessarily need to be a parent
+        if prolog.engine.ask_one(engine_id,
+            'descendant(X, _), \+ parent(X, _)') == 'false':
+            return [{'id': 'descendant_need_not_be_parent'}]
+
     except socket.timeout as ex:
-        pass
+        return [{'id': 'timeout'}]
 
     finally:
         if engine_id:
diff --git a/prolog/problems/family_relations/descendant_2/sl.py b/prolog/problems/family_relations/descendant_2/sl.py
index 8774ad6..949280f 100644
--- a/prolog/problems/family_relations/descendant_2/sl.py
+++ b/prolog/problems/family_relations/descendant_2/sl.py
@@ -58,6 +58,10 @@ Skratka, poskusi prevesti na "manjši" problem, npr. potomec v enem koraku manj
 
     'descendant_need_not_be_parent': '''\
 <p>Potomec <code>X</code> pravzaprav ne rabi imeti otrok...</p>
+''',
+
+    'timeout': '''\
+<p>Je morda na delu potencialno neskončna rekurzija?</p>
 ''',
 
     'wrong_direction': '''\
-- 
cgit v1.2.1