def split_subformula_in_world(MG, world, subformula, active_graphs, args): if MG.debug: print("Splitting subformula within world from " + modalparser.readable_natural_form(subformula) + " to " + modalparser.readable_natural_form(args[0]) + " and " + modalparser.readable_natural_form(args[1])) MG.next_formulas[world].append(args[0]) MG.next_formulas[world].append(args[1])
def handle_or(self, subformula, world): if self.debug: print("Handling disjunction of " + str(modalparser.readable_natural_form(subformula[1])) + " and " + str(modalparser.readable_natural_form(subformula[2]))) phi = subformula[1] psi = subformula[2] return (split_formula_over_new_graph, [phi, psi])
def handle_and(self, subformula, world): if self.debug: print("Handling conjunction of " + str(modalparser.readable_natural_form(subformula[1])) + " and " + str(modalparser.readable_natural_form(subformula[2]))) phi = subformula[1] psi = subformula[2] return (split_subformula_in_world, [phi, psi])
def split_formula_over_new_graph(MG, world, subformula, active_graphs, args): if MG.debug: print("Splitting subformula over new graph from " + modalparser.readable_natural_form(subformula) + " to " + modalparser.readable_natural_form(args[0]) + " and " + modalparser.readable_natural_form(args[1])) newMG = ModalGraph(MG=MG, params=MG.params) MG.next_formulas[world].append(args[0]) newMG.formulas[world].append(args[1]) active_graphs.append(newMG) if MG.debug: print("After split, original graph kept formula " + modalparser.readable_natural_form(MG.next_formulas[world][-1]) + " and new graph handles formula " + modalparser.readable_natural_form(newMG.formulas[world][-1]))
def process_all_worlds(self): self.formulas_processed_this_iter = { i: [] for i in range(self.next_world_number) } active_graphs = [self] self.next_formulas = {i: [] for i in range(self.next_world_number)} for world, world_formulas in self.formulas.items(): if self.debug: print("Processing world " + str(world) + " with world formulas " + str(world_formulas)) for subformula in world_formulas: self.formulas_processed_this_iter[world].append(subformula) if self.debug: print("Processing subformula " + str(modalparser.readable_natural_form(subformula))) if isinstance(subformula, str): # subformula at world is reduced to an atomic proposition that must be set to true if self.debug: print("Setting " + subformula + " True") graph_still_valid = self.set_atom_true(world, subformula) if not graph_still_valid and self in active_graphs: if self.debug: print( "This invalidated the graph so it has been removed" ) active_graphs.remove(self) elif isinstance(subformula, tuple): #Operation(s) to process still operator = subformula[0] if operator == UnaryPred.NOT: action = self.handle_negation(subformula, world) elif operator == UnaryPred.BOX: action = self.handle_box(subformula, world) elif operator == UnaryPred.DIAM: action = self.handle_diamond(subformula, world) elif operator == BinaryPred.AND: action = self.handle_and(subformula, world) elif operator == BinaryPred.OR: action = self.handle_or(subformula, world) elif operator == BinaryPred.IMPL: action = self.handle_implication(subformula, world) else: raise ValueError("Operator " + str(operator) + " is not of a known type") action[0](self, world, subformula, active_graphs, action[1]) else: raise ValueError("Subformula " + str(subformula) + " is not of a known type") if self not in active_graphs: return active_graphs self.formulas = self.next_formulas if self.is_fully_processed(): active_graphs.remove(self) return active_graphs
def twice(MG, world, subformula, active_graphs, args): if self.debug: print("Creating graph trying to satisfy " + modalparser.readable_natural_form( (BinaryPred.AND, subformula[1], subformula[2]))) newMG = ModalGraph(MG=MG, params=MG.params) newMG.formulas[world].append( (BinaryPred.AND, subformula[1], subformula[2])) active_graphs.append(newMG) split_formula_over_new_graph(MG, world, subformula, active_graphs, args)
def enforce_formula_met_by_children(MG, world, subformula, active_graphs, args): if MG.debug: print("Adding a new enforcement rule " + str(args[0]) + " to world " + str(world)) MG.rules_for_children[world].append(args[0]) for child in [edge[1] for edge in MG.nxG.out_edges(world)]: if MG.debug: print("Applied new enforcement rule '" + modalparser.readable_natural_form(args[0]) + "'to child " + str(child)) MG.next_formulas[child].append(args[0])
def comp(MG, world, subformula, active_graphs, args): if MG.debug: print("Trying two possibilities to satisfy <>" + modalparser.readable_natural_form(subformula[1])) if MG.debug: print("Adding a new graph") newMG = ModalGraph(MG=MG, params=MG.params, debug=MG.debug) newMG.formulas[random.choice([ edge for edge in MG.nxG.out_edges(world) ])[1]].append(args[0]) active_graphs.append(newMG) add_new_world_with_subformula(MG, world, subformula, active_graphs, args)
def handle_implication(self, subformula, world): if self.debug: print("Handling implication of " + str(modalparser.readable_natural_form(subformula[1])) + " and " + str(modalparser.readable_natural_form(subformula[2]))) phi = (UnaryPred.NOT, subformula[1]) psi = subformula[2] def twice(MG, world, subformula, active_graphs, args): if self.debug: print("Creating graph trying to satisfy " + modalparser.readable_natural_form( (BinaryPred.AND, subformula[1], subformula[2]))) newMG = ModalGraph(MG=MG, params=MG.params) newMG.formulas[world].append( (BinaryPred.AND, subformula[1], subformula[2])) active_graphs.append(newMG) split_formula_over_new_graph(MG, world, subformula, active_graphs, args) return (twice, [phi, psi])
def add_edge(self, w1, w2): if (w1, w2) in [edge for edge in self.nxG.edges()]: return if self.debug: print("Adding edge from world " + str(w1) + " to world " + str(w2) + " and applying the following rules: " + str([ modalparser.readable_natural_form(item) for item in self.rules_for_children[w1] ])) if self.next_formulas: self.next_formulas[w2].extend(self.rules_for_children[w1]) self.nxG.add_edge(w1, w2) if self.symmetric: self.add_edge(w2, w1) if self.transitive: for edge in self.nxG.in_edges(w1): self.add_edge(edge[0], w2)
def replace_current_subformula(MG, world, subformula, active_graphs, args): if MG.debug: print("Replacing current subformula " + modalparser.readable_natural_form(subformula) + " in world with " + str(args[0])) MG.next_formulas[world].append(args[0])
def finished_subformula(MG, world, subformula, active_graphs, args): if MG.debug: print("Finished processing subformula " + modalparser.readable_natural_form(subformula))
def handle_box(self, subformula, world): if self.debug: print("Handling box of " + str(modalparser.readable_natural_form(subformula[1]))) return (enforce_formula_met_by_children, [subformula[1]])