summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@fri.uni-lj.si>2016-01-05 19:11:33 +0100
committerTimotej Lazar <timotej.lazar@fri.uni-lj.si>2016-01-05 19:11:33 +0100
commit71787079e02aa7c9fbc5e714778b3e0ba42c474d (patch)
tree0a7bac177056082fa0fdcce94fef8c88a0e9a702
parent59a5430087e251a4f8a3eccdb2ef4ae5b9a2f775 (diff)
Factor common code out of PrologSession.{hint,run_for_user}
-rw-r--r--server/prolog_session.py45
1 files changed, 20 insertions, 25 deletions
diff --git a/server/prolog_session.py b/server/prolog_session.py
index bf037ad..8a5b6e3 100644
--- a/server/prolog_session.py
+++ b/server/prolog_session.py
@@ -113,28 +113,27 @@ class PrologSession(server.LanguageSession):
def hint(self, sid, problem_id, program):
session = server.user_session.get_session_by_id(sid)
- p = Problem.get(id=problem_id)
- language_module = load_language(p.language, 'common')
- problem_module = load_problem(p.language, p.group, p.identifier, 'common')
+ problem = Problem.get(id=problem_id)
- solved_problems = [pp for pp in CodeqUser.solved_problems(session.get_uid(), p.language)
- if pp != (p.group, p.identifier)]
+ 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
- passed, _ = problem_module.test(program, solved_problems)
+ passed, _ = problem_module.test(program, aux_code=aux_code)
if passed:
hints = [{'id': 'program_already_correct'}]
if not hints and hasattr(language_module, 'hint'):
- hints = language_module.hint(program, solved_problems)
+ hints = language_module.hint(program, aux_code=aux_code)
if not hints and hasattr(problem_module, 'hint'):
- hints = problem_module.hint(program, solved_problems)
+ hints = problem_module.hint(program, aux_code=aux_code)
if not hints and problem_id in _edits:
# Testing function for monkey.
def tester(code):
- passed, hints = problem_module.test(code, solved_problems)
+ passed, hints = problem_module.test(code, aux_code=aux_code)
return passed
solution, steps, fix_time, n_tested = monkey.fix(
program, _edits[problem_id], tester, timeout=5, debug=True)
@@ -154,9 +153,8 @@ class PrologSession(server.LanguageSession):
language_module = load_language(problem.language, 'common')
problem_module = load_problem(problem.language, problem.group, problem.identifier, 'common')
- dependencies = self._dependencies(session, problem, program)
- aux_code = ('\n' + solutions_for_problems(problem.language, dependencies) +
- '\n' + get_facts(problem.language, problem_module))
+ aux_code = self._aux_code(session.get_uid(), problem, program)
+
try:
passed, hints = problem_module.test(program, aux_code=aux_code)
if passed:
@@ -170,11 +168,14 @@ class PrologSession(server.LanguageSession):
return hints
# Return a string with definitions of aux. predicates used in [program]
- # (but only those that the user has already solved).
- def _dependencies(self, session, problem, 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)}
- return {p for p in CodeqUser.solved_problems(session.get_uid(), problem.language)
- if p[1] != problem.identifier and p[1] in used_predicate_identifiers}
+ 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.
@@ -191,16 +192,10 @@ class PrologSession(server.LanguageSession):
"""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.
"""
- p = Problem.get(id=problem_id)
- problem_module = load_problem(p.language, p.group, p.identifier, 'common')
-
- solved_problems = [pp for pp in CodeqUser.solved_problems(user_id, p.language)
- if pp != (p.group, p.identifier)]
- other_solutions = solutions_for_problems(p.language, solved_problems)
- problem_facts = get_facts(p.language, problem_module)
- code = program + '\n' + other_solutions + '\n' + problem_facts
+ problem = Problem.get(id=problem_id)
+ aux_code = self._aux_code(user_id, problem, program)
- messages, status, have_more = self.run(code)
+ messages, status, have_more = self.run(program+aux_code)
if status == 'ok':
more_messages, status, have_more = self.query(query)
messages.extend(more_messages)