diff options
Diffstat (limited to 'prolog/problems/family_relations')
-rw-r--r-- | prolog/problems/family_relations/sister_2/common.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/prolog/problems/family_relations/sister_2/common.py b/prolog/problems/family_relations/sister_2/common.py index 084aca6..2c9528e 100644 --- a/prolog/problems/family_relations/sister_2/common.py +++ b/prolog/problems/family_relations/sister_2/common.py @@ -13,3 +13,35 @@ sister(X, Y) :- female(X), X \== Y. ''' + +import prolog.engine +import server.problems + +def test(program): + # Test queries and expected answers (values of X). + queries = [ + ('sister(melanie, X)', set(['andrew'])), + ('sister(X, andrew)', set(['melanie'])), + ('sister(X, _)', set(['michelle', 'daniela', 'patricia', 'luana', 'sally', 'melanie', 'nevia', 'vanessa', 'anna'])), + ] + + code = program + '\n' + server.problems.load_facts('prolog', facts).facts + + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if not engine_id: + return 0, len(queries) + + n_correct = 0 + for query, solution in queries: + answers, error = prolog.engine.ask_all(engine_id, query=query, timeout=1.0) + if answers and answers[-1] != 'timed out': + values_x = set([ans.get('X') for ans, constraints in answers]) + if values_x == solution: + n_correct += 1 + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + return n_correct, len(queries) |