def update_paths_branches(calc_relations, curr_path, branches, known_variables): new_paths = [] ignoreset = set(curr_path.ignorelist) changed = False for notation in curr_path.new_notation: for calc_rel in calc_relations: if (not (calc_rel.right_part in curr_path.ignorelist) and not (set(calc_rel.left_part).issubset(ignoreset)) and calc_rel.right_part == notation): new_paths.append( calc.Path( calc_rel.left_part, curr_path.ignorelist + curr_path.new_notation, curr_path.indexes + [calc_relations.index(calc_rel)], curr_path.id)) branches.append( (curr_path.id, curr_path.indexes[len(curr_path.indexes) - 1], calc_rel.get_id())) changed = True if changed == False: #больше не можем продвинуться вперед, а нужных переменных не достигли. тогда это тупик, надо #удалить соответствующие ветки if (len( set(curr_path.new_notation).difference( set(known_variables))) > 0): branches = filter(lambda x: x[0] != curr_path.id, branches) return new_paths, branches
def calculate_dependencies(calc_relations, notations, student_formula): dependencies = [] _calc_relations, student_leftmost_notations, student_rightmost_notations = get_preprocessed_relations_and_notations( calc_relations, student_formula, notations) solution_paths = get_possible_solution_paths( _calc_relations, student_leftmost_notations, calc.Path(student_rightmost_notations, student_rightmost_notations, [])) if (len(solution_paths) == 0): solution_paths = get_new_paths_by_combining_calc_relations( _calc_relations, student_rightmost_notations) while (len(solution_paths) > 0): new_dependencies = get_dependencies_from_paths(_calc_relations, solution_paths) if (len(new_dependencies) > 0): dependencies.extend(new_dependencies) new_solution_paths = get_filtered_solution_paths(solution_paths) else: new_solution_paths = get_new_paths_by_expanding_existing_paths( _calc_relations, solution_paths, student_leftmost_notations) #if (len(new_solution_paths) == 0): TO DO: add code when we get necessary test cases solution_paths = get_updated_solution_paths(new_solution_paths) return dependencies
def init_subtree_paths(calc_relations, sought_variable): branches, fst_paths = [], [] for calc_rel in calc_relations: if calc_rel.right_part == sought_variable: branches.append([calc_rel.get_id()]) fst_paths.append( calc.Path(calc_rel.left_part, [sought_variable], [calc_relations.index(calc_rel)])) fst_subtree = SubTree(0, branches) return fst_subtree, fst_paths
def get_possible_solution_paths(_calc_relations, student_leftmost_notations, path): possible_solution_paths = [] for _cr in _calc_relations: if (not (_cr.right_part in path.ignorelist) and len(filter(lambda x: x in path.new_notation, _cr.left_part)) == len(path.new_notation)): path = calc.Path([_cr.right_part], path.ignorelist + path.new_notation, path.indexes + [_calc_relations.index(_cr)]) #NB! we suggest that left part of student's formula consists of only one notation. path.check_if_found(student_leftmost_notations[0]) possible_solution_paths.append(path) return possible_solution_paths
def get_new_paths_by_combining_calc_relations(_calc_relations, student_rightmost_notations): solution_paths = [] filtered_calc_relations = get_relations_intersecting_with_student_rightmost_notations( _calc_relations, student_rightmost_notations) _calc_sublists = util.SmartSublist(len(filtered_calc_relations), 2, len(filtered_calc_relations), filtered_calc_relations).get_sublists() _calc_sublists.sort(key=len, reverse=False) _calc_subsets = map(set, _calc_sublists) applied_sets = util.AppliedSets() for i in range(len(_calc_subsets)): if applied_sets.check(_calc_subsets[i]) == False: _calc_left_parts = [] selected_calc_ids = _calc_subsets[i] for s in selected_calc_ids: _calc_left_parts.extend( util.get_calc_relation_by_id(_calc_relations, s).left_part) #we need to use calc_relations whose left parts don't intersect and all together are equal to student_rightmost_notations if (len( filter(lambda x: x in student_rightmost_notations, set(_calc_left_parts))) == len( student_rightmost_notations)): _calc_right_parts, _calc_ids = [], [] for s in selected_calc_ids: _calc_right_parts.append( util.get_calc_relation_by_id(_calc_relations, s).right_part) _calc_ids.append(s) solution_paths.append( calc.Path(_calc_right_parts, student_rightmost_notations, _calc_ids)) applied_sets.update(selected_calc_ids) return solution_paths