Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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
Exemplo n.º 3
0
    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()
Exemplo n.º 4
0
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