def to_automaton(f) -> SymbolicDFA: # noqa: C901 """Translate to automaton.""" f = f.to_nnf() initial_state = frozenset({frozenset({PLAtomic(f)})}) states = {initial_state} final_states = set() transition_function = {} # type: Dict all_labels = f.find_labels() alphabet = powerset(all_labels) if f.delta({}, epsilon=True) == PLTrue(): final_states.add(initial_state) visited = set() # type: Set to_be_visited = {initial_state} while len(to_be_visited) != 0: for q in list(to_be_visited): to_be_visited.remove(q) for actions_set in alphabet: new_state = _make_transition( q, {label: True for label in actions_set}) if new_state not in states: states.add(new_state) to_be_visited.add(new_state) transition_function.setdefault(q, {})[actions_set] = new_state if new_state not in visited: visited.add(new_state) if _is_true(new_state): final_states.add(new_state) automaton = SymbolicAutomaton() state2idx = {} for state in states: state_idx = automaton.create_state() state2idx[state] = state_idx if state == initial_state: automaton.set_initial_state(state_idx) if state in final_states: automaton.set_accepting_state(state_idx, True) for source in transition_function: for symbol, destination in transition_function[source].items(): source_idx = state2idx[source] dest_idx = state2idx[destination] pos_expr = sympy.And(*map(sympy.Symbol, symbol)) neg_expr = sympy.And(*map(lambda x: sympy.Not(sympy.Symbol(x)), all_labels.difference(symbol))) automaton.add_transition( (source_idx, sympy.And(pos_expr, neg_expr), dest_idx)) determinized = automaton.determinize() minimized = determinized.minimize() return minimized
def setup_class(cls): """Set the test up.""" cls.dfa = SymbolicAutomaton() automaton = cls.dfa q0 = automaton.create_state() q1 = automaton.create_state() q2 = automaton.create_state() automaton.set_initial_state(q0) automaton.set_accepting_state(q2, True) automaton.add_transition((q0, "a", q0)) automaton.add_transition((q0, "b", q1)) automaton.add_transition((q0, "c", q2)) automaton.add_transition((q1, "a", q0)) automaton.add_transition((q1, "b", q1)) automaton.add_transition((q1, "c", q2)) automaton.add_transition((q2, "a", q0)) automaton.add_transition((q2, "b", q1)) automaton.add_transition((q2, "c", q2)) # dummy state and transitions to make it non-deterministic q3 = automaton.create_state() automaton.add_transition((q0, "a", q3)) automaton.add_transition((q1, "b", q3)) automaton.add_transition((q2, "c", q3)) automaton.add_transition((q3, "a", q0)) automaton.add_transition((q3, "b", q1)) automaton.add_transition((q3, "c", q2))
def setup_class(cls): """Set the test up.""" cls.automaton = SymbolicAutomaton() state_0 = 0 cls.automaton.set_initial_state(state_0) cls.automaton.set_accepting_state(state_0, True) cls.automaton.add_transition((state_0, BooleanTrue(), state_0))
def setup_class(cls): """Set the test up.""" cls.automaton = SymbolicAutomaton() assert len(cls.automaton.states) == 1 assert cls.automaton.states == {0} cls.new_state = cls.automaton.create_state()
def setup_class(cls): """Set the test up.""" cls.automaton = SymbolicAutomaton() state_0 = 0 state_1 = cls.automaton.create_state() cls.automaton.set_initial_state(state_0) cls.automaton.set_accepting_state(state_1, True) a = Symbol("a") cls.automaton.add_transition((state_0, a, state_1))
def setup_class(cls): """Set the tests up.""" cls.automaton = SymbolicAutomaton() state_0 = 0 state_1 = cls.automaton.create_state() state_2 = cls.automaton.create_state() cls.automaton.set_initial_state(state_0) cls.automaton.set_accepting_state(state_1, True) cls.automaton.add_transition((state_0, "x | y", state_1)) cls.automaton.add_transition((state_0, "x | z", state_2)) cls.completed = cls.automaton.complete()
def setup_class(cls): """Set the tests up.""" cls.automaton = SymbolicAutomaton() automaton = cls.automaton q0 = automaton.create_state() q1 = automaton.create_state() q2 = automaton.create_state() q3 = automaton.create_state() q4 = automaton.create_state() automaton.set_initial_state(q0) automaton.set_accepting_state(q3, True) automaton.set_accepting_state(q4, True) automaton.add_transition((q0, "a", q1)) automaton.add_transition((q0, "b", q2)) automaton.add_transition((q1, "c", q3)) automaton.add_transition((q2, "c", q3)) automaton.add_transition((q3, "c", q4)) automaton.add_transition((q4, "c", q4)) cls.minimized = automaton.minimize()
def converter(expected): expected = str(expected) expected = expected.split("\n") init_states = [] states = [] transitions = [] terminal_states = [] for i in range(len(expected)): if (("->" in expected[i]) and ("init" not in expected[i])): temp = re.split("->", expected[i]) temp[1] = temp[1].replace("[", "") temp[1] = temp[1].replace("]", "") temp[1] = temp[1].replace(";", "") temp[1] = temp[1].replace("label=", "-") temp2 = temp[1].split("-") if (int(temp[0]) not in states): states.append(int(temp[0])) if (int(temp2[0]) not in states): states.append(int(temp2[0])) transitions.append((int(temp[0]), str(temp2[1]), int(temp2[0]))) for i in range(len(expected)): if (("init" in expected[i])): for j in range(len(states)): state_string = str(states[j]) + ";" if (state_string in expected[i]): init_states.append(states[j]) if ("doublecircle" in expected[i]): for j in range(len(states)): state_string = " " + str(states[j]) + ";" if (state_string in expected[i]): terminal_states.append(states[j]) automaton = SymbolicAutomaton() #automaton = SymbolicDFA() state2idx = {} for i in range(len(states)): state_idx = automaton.create_state() state2idx[i] = state_idx if (states[i] in init_states): automaton.set_initial_state(state_idx) if (states[i] in terminal_states): automaton.set_accepting_state(state_idx, True) for i in range(len(transitions)): automaton.add_transition(transitions[i]) print("pre-determinize: ") determinized = automaton.determinize() print("pre-minimize: ") minimized = determinized.minimize() print("post-minimize: ") dfa = minimized #dfa = determinized #remove state 0 #print(automaton.states) #automaton.remove_state(0) return dfa
def setup_class(cls): """Set the test up.""" cls.automaton = SymbolicAutomaton() cls.automaton.set_initial_state(0) cls.automaton.set_accepting_state(0, True)
def test_to_graphviz(): """Test 'to_graphviz' method.""" automaton = SymbolicAutomaton() state_0 = automaton.create_state() state_1 = automaton.create_state() state_2 = automaton.create_state() automaton.set_initial_state(state_0) automaton.set_accepting_state(state_1, True) automaton.add_transition((state_0, "x | y", state_1)) automaton.add_transition((state_0, "x | z", state_2)) automaton.to_graphviz()
def setup_class(cls): """Set the test up.""" A, B, C = sympy.symbols("A B C") aut = SymbolicAutomaton() aut.create_state() aut.create_state() aut.create_state() aut.set_initial_state(3) aut.set_accepting_state(0, True) aut.set_accepting_state(1, True) aut.set_accepting_state(3, True) trfun = { 3: {0: A | ~B, 2: B & ~A}, 0: {1: BooleanTrue()}, 2: {2: BooleanTrue()}, 1: {1: BooleanTrue()}, } for s in trfun: for d, guard in trfun[s].items(): aut.add_transition((s, guard, d)) cls.automaton = aut cls.determinized = aut.determinize()
def setup_class(cls): """Set the test up.""" cls.automaton = SymbolicAutomaton()