summaryrefslogtreecommitdiff
path: root/prolog/engine.py
blob: 43991d4768f2ad01b5966400c5ec6f9fb2e06346 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/python3

# CodeQ: an online programming tutor.
# Copyright (C) 2015 UL FRI
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import collections
import html
import http.client
import json
from operator import itemgetter
import re
import time
import urllib

def strip_html(text):
    return html.unescape(re.sub(r'</?[a-z]+[^>]*>', '', text))

# Create a new pengine and initialize it with [code]. Return engine ID and a
# list of messages from Prolog.
def create(code='', timeout=10):
    opts = {'format': 'json-html', 'destroy': False, 'src_text': code}
    reply, output = request('POST', '/pengine/create', body=json.dumps(opts), timeout=timeout)
    return reply.get('id'), output

def ask(engine, query, timeout=10):
    # Strip trailing whitespace and periods from the query.
    query = query.rstrip().rstrip('.')
    return send(engine, 'ask(({}),[])'.format(query), timeout=timeout)

def next(engine, n=1, timeout=10):
    return send(engine, 'next({})'.format(n), timeout=timeout)

def stop(engine, timeout=10):
    return send(engine, 'stop', timeout=timeout)

def destroy(engine):
    params = urllib.parse.urlencode({'ids': engine})
    try:
        # We don't care about the answer here, so don't wait for it.
        request('GET', '/pengine/destroy_all?' + params, timeout=0.01)
    except:
        pass

def send(engine, event, timeout=10):
    params = urllib.parse.urlencode({
        'id': engine,
        'event': event,
        'format': 'json-html'})
    return request('GET', path='/pengine/send?' + params, timeout=timeout)

# Return the main reply and pull potential output replies.
address, port = '127.0.0.1', 3030  # TODO put this somewhere sane
def request(method, path, body=None, timeout=10):
    headers = {'Content-Type': 'application/json;charset=utf-8'}
    messages = []
    try:
        conn = http.client.HTTPConnection(address, port, timeout=timeout)
        conn.request(method, path, body, headers=headers)
        while True:
            response = conn.getresponse()
            if response.status != http.client.OK:
                raise Exception('server returned {}'.format(response.status))

            reply = json.loads(response.read().decode('utf-8'))
            if isinstance(reply, dict) and reply.get('event') == 'output':
                messages.append(_get_message(reply))

                # Pull the next output. These requests should return instantly
                # as no additional processing needs to be done in the pengine.
                params = urllib.parse.urlencode({
                    'id': reply['id'],
                    'format': 'json-html'})
                conn.request('GET', '/pengine/pull_response?' + params, headers=headers)
            else:
                return reply, messages
    finally:
        conn.close()

# Strip boilerplate from Prolog messages … ugly.
def _get_message(reply):
    data = strip_html(reply['data']).strip()
    # Prepend the message with formatted location.
    # NOTE in the final version we probably want to return the location object
    where = ''
    if reply['message'] in ('error', 'warning'):
        if 'location' in reply:
            loc = reply['location']
            where += 'near line ' + str(loc['line'])
            if 'ch' in loc:
                where += ', character ' + str(loc['ch'])
            where += ': '
    # Strip HTML and pengine IDs from the messages.
    text = strip_html(data)
    text = re.sub(r"pengine://[0-9]*/src:([0-9]*:)* ", '', text)
    text = re.sub(r"'[0-9]{10,}':", '', text)
    return reply['message'], where + text

# Return the value of variable [name] in the JSON object returned by Prolog.
def get_var(data, name):
    for binding in data['variables']:
        if name in binding['variables']:
            return strip_html(binding['value'])
    return None

# Return a string describing variable bindings and residuals in the JSON object
# returned by Prolog.
def pretty_vars(data):
    result = []
    for binding in data['variables']:
        var_list = binding['variables']
        value = binding['value']
        result.append(' = '.join(var_list) + ' = ' + strip_html(value))
    if 'residuals' in data:
        result += [strip_html(b) for b in data['residuals']]
    return ',\n'.join(result) if result else 'true'

# Run [query] in pengine with ID [engine] and check whether all solutions
# specified by [answers] (a list of binding dictionaries) are returned. This
# function succeeds if [query] finds each solution in [answers] at least once
# within [timeout] seconds, and fails when it finds any other solution.
def check_answers(engine, query, answers, timeout=10):
    seen = []
    # Return false (only) if there is no expected answer where values of all
    # specified variables match values in [answer].
    def check_answer(answer):
        if not answer:
            return True
        bindings, constraints = answer
        for expected in answers:
            if all(bindings.get(var) == val for var, val in expected.items()):
                if expected not in seen:
                    seen.append(expected)
                return True
        return False

    start = time.monotonic()
    try:
        # Limit inferences for each solution to curb unbounded recursion.
        limited = '''(call_with_inference_limit(({}), 100000, _ILR),
                      _ILR \= inference_limit_exceeded)'''.format(query)
        # Run the query.
        reply, output = ask(engine, limited, timeout)
        answer, error, more = process_answer(reply)
        if not check_answer(answer):
            return False

        # Continue while there are more potential answers and time remaining.
        while more:
            real_timeout = timeout - (time.monotonic()-start)
            if real_timeout <= 0:
                break
            reply, output = next(engine, timeout=real_timeout)
            answer, error, more = process_answer(reply)
            if not check_answer(answer):
                return False
    except:
        pass
    return len(seen) == len(answers)

# Run [query] in the pengine with id [engine] and return the first answer only
# found within [timeout] seconds.
# used for quick hint debugging by Sasha
def ask_one(engine, query, timeout=1):
    # Returns either an error message, true, false, timeout (see below), or bindings
    # Timeout is "returned" as an unhandled exception -- deliberately so

    # Run the query.
    reply, output = ask(engine, query, timeout)
    if 'error' in map(itemgetter(0), output):
        return 'query syntax error'
        # 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 'runtime error: ' + error[-1]
    elif answer:
        # no error, query satisfied
        if not answer[0]:       # empty bindings
            return 'true'
        else:
            return answer[0]    # for now return just bindings without constraints
    else:
        # no error, query failed
        return 'false'

def ask_truth(engine, query, timeout=1):
    # Returns either True or False
    # (Runtime) error is False!
    # Timeout is an unhandled exception -- deliberately so

    # Run the query.
    reply, output = ask(engine, query, timeout)
    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
        # query result.
        if reply['event'] == 'destroy':
            reply = reply['data']

        if reply['event'] == 'success':
            # Return a dictionary {var: value} and a list of constraints (as
            # strings) from the JSON object returned by Prolog.
            data = reply['data'][0]
            bindings = {}
            for binding in data['variables']:
                value = strip_html(binding['value'])
                for name in binding['variables']:
                    bindings[name] = value
            constraints = [strip_html(r) for r in data.get('residuals', [])]
            return (bindings, constraints), None, reply['more']
        elif reply['event'] == 'failure':
            return None, None, False
        elif reply['event'] == 'error':
            # Remove potential module name (engine ID) from the error message.
            error = ('error', reply['data'].replace("'{}':".format(reply['id']), ''))
            return None, error, False

# Basic sanity check.
if __name__ == '__main__':
    engine, messages = create('b(Y). a(X) :- {X > 3}, (X = 5 ; {X > 4}).')
    print(engine, messages)
    if engine is not None:
        reply = ask(engine, 'a(X)', timeout=1.0)
        print(reply)
        destroy(engine)