diff options
-rw-r--r-- | server/prolog_session.py | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/server/prolog_session.py b/server/prolog_session.py index cb9fd19..bf037ad 100644 --- a/server/prolog_session.py +++ b/server/prolog_session.py @@ -19,12 +19,14 @@ 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 db.models import CodeqUser, Problem -from . import problems +from server.problems import get_facts, load_language, load_problem, solutions_for_problems __all__ = ['PrologSession'] @@ -112,8 +114,8 @@ 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 = problems.load_language(p.language, 'common') - problem_module = problems.load_problem(p.language, p.group, p.identifier, 'common') + language_module = load_language(p.language, 'common') + problem_module = load_problem(p.language, p.group, p.identifier, 'common') solved_problems = [pp for pp in CodeqUser.solved_problems(session.get_uid(), p.language) if pp != (p.group, p.identifier)] @@ -148,19 +150,15 @@ class PrologSession(server.LanguageSession): def test(self, sid, problem_id, program): session = server.user_session.get_session_by_id(sid) - p = Problem.get(id=problem_id) - - language_module = problems.load_language(p.language, 'common') - group_module = problems.load_group(p.language, p.group, 'common') - problem_module = problems.load_problem(p.language, p.group, p.identifier, 'common') - - solved_problems = [pp for pp in CodeqUser.solved_problems(session.get_uid(), p.language) - if pp[0] in group_module.allowed_groups and pp != (p.group, p.identifier)] - aux_code = ('\n' + problems.solutions_for_problems('prolog', solved_problems) + - '\n' + problems.get_facts('prolog', problem_module)) + problem = Problem.get(id=problem_id) + 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)) try: - passed, hints = problem_module.test(program, aux_code) + passed, hints = problem_module.test(program, aux_code=aux_code) if passed: session.update_solution(problem_id, done=True) except AttributeError as ex: @@ -171,6 +169,13 @@ class PrologSession(server.LanguageSession): 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). + def _dependencies(self, session, problem, program): + 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} + # 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): @@ -187,12 +192,12 @@ class PrologSession(server.LanguageSession): problems and the given program, and ask a query. """ p = Problem.get(id=problem_id) - problem_module = problems.load_problem(p.language, p.group, p.identifier, 'common') + 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 = problems.solutions_for_problems(p.language, solved_problems) - problem_facts = problems.get_facts(p.language, problem_module) + 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 messages, status, have_more = self.run(code) @@ -204,6 +209,7 @@ class PrologSession(server.LanguageSession): 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')) |