summaryrefslogtreecommitdiff
path: root/server/prolog_session.py
blob: 121f80289e33b246ac6086483faa84da13c54e53 (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
# 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 operator
import os.path
import pickle
import threading

from db.models import CodeqUser, Problem
from db.util import make_identifier
import monkey
import prolog.engine
from prolog.util import used_predicates
import server
import server.user_session
from server.problems import get_facts, load_language, load_problem, solutions_for_problems

__all__ = ['PrologSession']

def format_prolog_output(reply, output):
    messages = [text for text in map(operator.itemgetter(1), output)]
    # When an engine is destroyed, a nested data object has the actual query result.
    event = reply['event']
    if event == 'destroy':
        reply = reply['data']
        event = reply['event']

    if event == 'success':
        messages.append(prolog.engine.pretty_vars(reply['data'][0]))
        return messages, 'ok', True and reply['more']
    if event == 'failure':
        messages.append('false')
        return messages, 'ok', False
    if event == 'error':
        # Remove potential module name (engine ID) from the error message.
        messages.append('error: ' + reply['data'].replace("'{}':".format(reply['id']), ''))
        return messages, 'error', False

    return messages, 'ok', False  # TODO: is it possible to reach this return statement?


class PrologSession(server.LanguageSession):
    """Abstracts a Prolog session.
    Only public methods are available to the outside world due to the use of multiprocessing managers.
    Therefore prefix any private methods with an underscore (_).
    No properties are accessible; use getters and setters instead.
    Values are passed by value instead of by reference (deep copy!).
    """
    def __init__(self):
        self._access_lock = threading.Lock()
        self._engine_id = None
        self._problem_id = -1
        self._sent_hints = []

    def run(self, code):
        with self._access_lock:
            if self._engine_id is not None:
                prolog.engine.stop(self._engine_id)
                self._engine_id = None
            engine_id, output = prolog.engine.create(code=code)
            if not engine_id:
                raise Exception('System error: could not create a prolog engine')
            self._engine_id = engine_id
            messages = [text for text in map(operator.itemgetter(1), output)]
            status = 'error' if 'error' in map(operator.itemgetter(0), output) else 'ok'
            return messages, status, False

    def query(self, query):
        with self._access_lock:
            if self._engine_id is None:
                return ['Prolog is not running'], 'error', False
            try:
                return format_prolog_output(*prolog.engine.ask(self._engine_id, query))
            except Exception as e:
                return [str(e)], 'error', False

    def step(self):
        with self._access_lock:
            if self._engine_id is None:
                return ['Prolog is not running'], 'error', False
            try:
                return format_prolog_output(*prolog.engine.next(self._engine_id))
            except Exception as e:
                return [str(e)], 'error', False

    def destroy(self):  # this method was previously named: end()
        """Stops the Prolog engine."""
        with self._access_lock:
            if self._engine_id is not None:
                prolog.engine.destroy(self._engine_id)
                self._engine_id = None
                self._problem_id = -1
            return [], 'ok', False

    def __del__(self):
        #  no locking needed if GC is removing us, as there cannot be any concurrent access by definition
        if hasattr(self, '_engine_id') and (self._engine_id is not None):
            prolog.engine.destroy(self._engine_id)
            self._engine_id = None

    def hint(self, sid, problem_id, program):
        session = server.user_session.get_session_by_id(sid)
        problem = Problem.get(id=problem_id)

        language_module = load_language(problem.language, 'common')
        problem_module = load_problem(problem.language, problem.group, problem.identifier, 'common')
        aux_code = self._aux_code(session.get_uid(), problem, program)

        hints = []
        try:
            # check if the program is already correct
            n_correct, n_all, _ = problem_module.test(program, aux_code=aux_code)
            if n_correct == n_all:
                hints = [{'id': 'program_already_correct'}]

            if not hints and hasattr(language_module, 'hint'):
                hints = language_module.hint(program, aux_code=aux_code)
            if not hints and hasattr(problem_module, 'hint'):
                hints = problem_module.hint(program, aux_code=aux_code)
            if not hints and problem_id in _edits:
                # Testing function for monkey.
                def tester(code):
                    n_correct, n_all, _ = problem_module.test(code, aux_code=aux_code)
                    return n_correct, n_all
                solution, steps, fix_time, n_tested = monkey.fix(
                        program, _edits[problem_id], tester, timeout=5, debug=True)
                if solution and steps:
                    hints = [{'id': 'monkey_main'}] + monkey.fix_hints(program, steps)
            if not hints:
                hints = [{'id': 'no_hint'}]
        except Exception as ex:
            hints = [{'id': 'system_error', 'args': {'message': str(ex)}}]

        self._instantiate_and_save_hints(language_module, problem_module, hints)
        return hints

    def test(self, sid, problem_id, program):
        session = server.user_session.get_session_by_id(sid)
        problem = Problem.get(id=problem_id)

        language_module = load_language(problem.language, 'common')
        problem_module = load_problem(problem.language, problem.group, problem.identifier, 'common')
        aux_code = self._aux_code(session.get_uid(), problem, program)

        try:
            n_correct, n_all, hints = problem_module.test(program, aux_code=aux_code)
            if n_correct == n_all:
                session.update_solution(problem_id, done=True)
        except AttributeError as ex:
            hints = [{'id': 'system_error', 'args': {'message': 'test function does not exist'}}]
        except Exception as ex:
            hints = [{'id': 'system_error', 'args': {'message': str(ex)}}]

        self._instantiate_and_save_hints(language_module, problem_module, hints)
        return hints

    # Return a string with definitions of aux. predicates used in [program]
    # (but only those that the user has already solved) and any required facts.
    def _aux_code(self, user_id, problem, program):
        problem_module = load_problem(problem.language, problem.group, problem.identifier, 'common')
        used_predicate_identifiers = {make_identifier(name) for name in used_predicates(program)}
        dependencies = {p for p in CodeqUser.solved_problems(user_id, problem.language)
                          if p[1] != problem.identifier and p[1] in used_predicate_identifiers}
        return ('\n' + solutions_for_problems(problem.language, dependencies) +
                '\n' + get_facts(problem.language, problem_module))

    # Add hint parameters (such as message index) based on hint class. Append
    # the finalized hints to the list of sent hints.
    def _instantiate_and_save_hints(self, language_mod, problem_mod, hints):
        with self._access_lock:
            for hint in hints:
                for mod in [language_mod, problem_mod]:
                    if hasattr(mod, 'hint_type') and hint['id'] in mod.hint_type:
                        hint_type = mod.hint_type[hint['id']]
                        hint_type.instantiate(hint, self._sent_hints)
            self._sent_hints.extend(hints)

    def run_for_user(self, user_id, problem_id, program, query):
        """A "shorthand" method to start a Prolog session, load correct solutions of all user's solved
        problems and the given program, and ask a query.
        """
        problem = Problem.get(id=problem_id)
        aux_code = self._aux_code(user_id, problem, program)

        messages, status, have_more = self.run(program+aux_code)
        if status == 'ok':
            more_messages, status, have_more = self.query(query)
            messages.extend(more_messages)
        self._problem_id = problem_id
        return messages, status, have_more

server.language_session_handlers['prolog'] = lambda user_session, problem_id, language_identifier, group_identifier, problem_identifier: PrologSession()

# Load edit data.
try:
    _edits, _submissions, _queries = pickle.load(
        open(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'edits.pickle'), 'rb'))
except:
    _edits = {}
    _submissions = {}
    _queries = {}