summaryrefslogtreecommitdiff
path: root/prolog/problems/family_relations/sister_2/common.py
blob: 9be9e6dac5fc05cdbf9a259515db3f00b6a4162a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# coding=utf-8

id = 96
group = 'family_relations'
number = 3
visible = True
facts = 'family_relations'

solution = '''\
sister(X, Y) :-
  parent(P, X),
  parent(P, 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)

def hint(program):
    if '\=' not in program and '\==' not in program:
        return [{'id': 'x_y_must_be_different'}]
    return None