def test_creation(self):
     """ Test the creation of nfa
     """
     nfa = NondeterministicFiniteAutomaton()
     self.assertIsNotNone(nfa)
     states = [State(x) for x in range(10)]
     nfa = NondeterministicFiniteAutomaton(start_state=states)
     self.assertIsNotNone(nfa)
Esempio n. 2
0
def get_dfa_from_functions(functions, query):
    transition_function, states, symbols, final_states = get_transition_function_and_states_and_symbols(
        functions, query)
    nfa = NondeterministicFiniteAutomaton(states, symbols, transition_function,
                                          {State("Start")}, final_states)
    dfa = nfa.to_deterministic()
    #print("We have:", dfa.get_number_states(), "states")
    #print("Minimize")
    dfa = dfa.minimize()
    return dfa
Esempio n. 3
0
def get_enfa_from_functions(functions):
    transition_function, states, symbols, final_states = get_transition_function_and_states_and_symbols_non_reduced(
        functions)
    enfa = NondeterministicFiniteAutomaton(states, symbols,
                                           transition_function,
                                           {State("Start")}, final_states)
    return enfa
    def test_2(self):
        ndfa = NondeterministicFiniteAutomaton()

        state0 = State(0)
        state1 = State(1)
        state2 = State(2)
        state3 = State(3)

        ndfa.add_start_state(state0)

        ndfa.add_final_state(state2)
        ndfa.add_final_state(state3)

        ndfa.add_transition(state0, self.symb_a, state1)
        ndfa.add_transition(state1, self.symb_b, state2)
        ndfa.add_transition(state1, self.symb_c, state3)

        self.run_test(2, ndfa=ndfa, starts_spec=True, ans_spec=True)
    def get_fa(self, start_states, final_states):
        fa = NondeterministicFiniteAutomaton()

        fa.add_transitions(self.edges)
        if start_states is None:
            start_states = list(range(self.size))
        for ss in start_states:
            fa.add_start_state(ss)
        if final_states is None:
            final_states = list(range(self.size))
        for fs in final_states:
            fa.add_final_state(fs)
        return fa
 def to_nfa(self, start_states: Indices, final_states: Indices):
     self.start_states = start_states
     self.final_states = final_states
     nfa = NondeterministicFiniteAutomaton()
     states = [State(idx) for idx in range(self.vertices_num)]
     symbols = {label: Symbol(label) for label in self.label_to_bool_matrix}
     for start_idx in start_states:
         nfa.add_start_state(states[start_idx])
     for end_idx in final_states:
         nfa.add_final_state(states[end_idx])
     for label, matrix in self.label_to_bool_matrix.items():
         for i, j, _ in zip(*matrix.to_lists()):
             nfa.add_transition(states[i], symbols[label], states[j])
     return nfa
    def graph_to_nfa(cls, start_states, final_states,
                     transitions: List[Tuple[int, str, int]]):
        nfa = NondeterministicFiniteAutomaton()

        for start_state in start_states:
            nfa.add_start_state(State(start_state))

        for final_state in final_states:
            nfa.add_final_state(State(final_state))

        for st_from, label, st_to in transitions:
            nfa.add_transition(State(st_from), Symbol(label), State(st_to))

        return nfa
    def test_1(self):
        ndfa = NondeterministicFiniteAutomaton()

        state0 = State(0)
        state1 = State(1)

        ndfa.add_start_state(state0)
        ndfa.add_start_state(state1)
        ndfa.add_final_state(state1)
        ndfa.add_final_state(state0)

        ndfa.add_transition(state0, self.symb_a, state1)

        self.run_test(1, ndfa=ndfa, ans_spec=True)
 def test_deterministic(self):
     """ Tests the deterministic transformation """
     nfa = NondeterministicFiniteAutomaton()
     state0 = State("q0")
     state1 = State("q1")
     state2 = State("q2")
     symb0 = Symbol(0)
     symb1 = Symbol(1)
     nfa.add_start_state(state0)
     nfa.add_final_state(state1)
     nfa.add_transition(state0, symb0, state0)
     nfa.add_transition(state0, symb0, state1)
     nfa.add_transition(state0, symb1, state0)
     nfa.add_transition(state1, symb1, state2)
     dfa = nfa.to_deterministic()
     self.assertEqual(len(dfa.states), 3)
     self.assertEqual(dfa.get_number_transitions(), 6)
 def test_accepts(self):
     """ Tests the acceptance of nfa
     """
     nfa = NondeterministicFiniteAutomaton()
     state0 = State(0)
     state1 = State(1)
     state2 = State(2)
     state3 = State(3)
     state4 = State(4)
     symb_a = Symbol("a")
     symb_b = Symbol("b")
     symb_c = Symbol("c")
     symb_d = Symbol("d")
     nfa.add_start_state(state0)
     nfa.add_final_state(state4)
     nfa.add_final_state(state3)
     nfa.add_transition(state0, symb_a, state1)
     nfa.add_transition(state1, symb_b, state1)
     nfa.add_transition(state1, symb_c, state2)
     nfa.add_transition(state1, symb_d, state3)
     nfa.add_transition(state1, symb_c, state4)
     nfa.add_transition(state1, symb_b, state4)
     self.assertFalse(nfa.is_deterministic())
     self.assertTrue(nfa.accepts([symb_a, symb_b, symb_c]))
     self.assertTrue(nfa.accepts([symb_a, symb_b, symb_b, symb_b, symb_c]))
     self.assertTrue(nfa.accepts([symb_a, symb_b, symb_d]))
     self.assertTrue(nfa.accepts([symb_a, symb_d]))
     self.assertTrue(nfa.accepts([symb_a, symb_b, symb_b, symb_b, symb_b]))
     self.assertFalse(nfa.accepts([symb_a, symb_c, symb_d]))
     self.assertFalse(nfa.accepts([symb_d, symb_c, symb_d]))
     self.assertFalse(nfa.accepts([]))
     self.assertFalse(nfa.accepts([symb_c]))
     nfa.add_start_state(state1)
     self.assertFalse(nfa.is_deterministic())
     self.assertTrue(nfa.accepts([symb_c]))
     nfa.remove_start_state(state1)
     dfa = nfa.to_deterministic()
     self.assertTrue(dfa.is_deterministic())
     self.assertTrue(dfa.accepts([symb_a, symb_b, symb_c]))
     self.assertTrue(dfa.accepts([symb_a, symb_b, symb_b, symb_b, symb_c]))
     self.assertTrue(dfa.accepts([symb_a, symb_b, symb_d]))
     self.assertTrue(dfa.accepts([symb_a, symb_d]))
     self.assertTrue(dfa.accepts([symb_a, symb_b, symb_b, symb_b, symb_b]))
     self.assertFalse(dfa.accepts([symb_a, symb_c, symb_d]))
     self.assertFalse(dfa.accepts([symb_d, symb_c, symb_d]))
     self.assertFalse(dfa.accepts([]))
     self.assertFalse(dfa.accepts([symb_c]))
 def test_remove_initial(self):
     """ Test the remove of initial state
     """
     nfa = NondeterministicFiniteAutomaton()
     state0 = State(0)
     state1 = State(1)
     symb_a = Symbol("a")
     nfa.add_transition(state0, symb_a, state1)
     nfa.add_start_state(state0)
     nfa.add_final_state(state1)
     self.assertTrue(nfa.is_deterministic())
     self.assertTrue(nfa.accepts([symb_a]))
     self.assertEqual(nfa.remove_start_state(state1), 0)
     self.assertTrue(nfa.accepts([symb_a]))
     self.assertEqual(nfa.remove_start_state(state0), 1)
     self.assertFalse(nfa.accepts([symb_a]))
 def test_epsilon_refused(self):
     dfa = NondeterministicFiniteAutomaton()
     state0 = State(0)
     state1 = State(1)
     with self.assertRaises(InvalidEpsilonTransition):
         dfa.add_transition(state0, Epsilon(), state1)
Esempio n. 13
0
def test_dfa_nfa_intersection():
    nfa1 = NondeterministicFiniteAutomaton()
    state0 = finite_automaton.State(0)
    state1 = finite_automaton.State(1)
    state2 = finite_automaton.State(2)
    state3 = finite_automaton.State(3)
    nfa1.add_transition(state0, symb_a, state1)
    nfa1.add_transition(state0, symb_c, state2)
    nfa1.add_transition(state1, symb_a, state1)
    nfa1.add_transition(state1, symb_b, state2)
    nfa1.add_transition(state2, symb_a, state0)
    nfa1.add_transition(state0, symb_c, state3)
    nfa1.add_transition(state3, symb_a, state1)
    nfa1.add_start_state(state0)
    nfa1.add_final_state(state1)

    s = ("((((b.a)|(((a|(b.c))|(c.a)).(((b.c))*." +
         "(b.a)))))*.(((a|(b.c))|(c.a)).((b.c))*))")
    r = Regex(s)
    dfa = r.to_epsilon_nfa().to_deterministic().minimize()
    dnfa = dfa.get_intersection(nfa1)
    assert (dnfa.accepts([symb_a]))
    assert (dnfa.accepts([symb_c, symb_a]))
    assert (dnfa.accepts([symb_a, symb_b, symb_a, symb_a]))
    assert (not dnfa.accepts([symb_c, symb_b]))
    assert (not dnfa.accepts([symb_a, symb_a]))
    assert (not dnfa.accepts([symb_b]))
Esempio n. 14
0
def test_nfa_intersection():
    nfa1 = NondeterministicFiniteAutomaton()
    state0 = finite_automaton.State(0)
    state1 = finite_automaton.State(1)
    state2 = finite_automaton.State(2)
    state3 = finite_automaton.State(3)
    nfa1.add_transition(state0, symb_a, state1)
    nfa1.add_transition(state0, symb_c, state2)
    nfa1.add_transition(state1, symb_a, state1)
    nfa1.add_transition(state1, symb_b, state2)
    nfa1.add_transition(state2, symb_a, state0)
    nfa1.add_transition(state0, symb_c, state3)
    nfa1.add_transition(state3, symb_a, state1)
    nfa1.add_start_state(state0)
    nfa1.add_final_state(state1)

    nfa2 = NondeterministicFiniteAutomaton()
    nfa2.add_transition(state0, symb_a, state1)
    nfa2.add_transition(state0, symb_b, state2)
    nfa2.add_transition(state1, symb_b, state2)
    nfa2.add_transition(state2, symb_c, state1)
    nfa2.add_transition(state2, symb_a, state0)
    nfa2.add_transition(state0, symb_c, state3)
    nfa2.add_transition(state3, symb_a, state1)
    nfa2.add_start_state(state0)
    nfa2.add_final_state(state1)
    new_nfa = nfa1.get_intersection(nfa2)
    assert (new_nfa.accepts([symb_a]))
    assert (new_nfa.accepts([symb_c, symb_a]))
    assert (new_nfa.accepts([symb_a, symb_b, symb_a, symb_a]))
    assert (not new_nfa.accepts([symb_c, symb_b]))
    assert (not new_nfa.accepts([symb_a, symb_a]))
    assert (not new_nfa.accepts([symb_b]))
Esempio n. 15
0
def test_intersection_dfa_nfa():
    dfa = str_to_dfa('1 0* 1*')

    symb_0 = finite_automaton.Symbol('0')
    symb_1 = finite_automaton.Symbol('1')
    state_1 = finite_automaton.State(1)
    state_2 = finite_automaton.State(2)
    state_3 = finite_automaton.State(3)

    nfa = NondeterministicFiniteAutomaton()
    nfa.add_transition(state_1, symb_1, state_2)
    nfa.add_transition(state_1, symb_0, state_2)
    nfa.add_transition(state_2, symb_0, state_2)
    nfa.add_transition(state_1, symb_1, state_3)
    nfa.add_transition(state_2, symb_1, state_3)
    nfa.add_final_state(state_3)
    nfa.add_start_state(state_1)
    assert not nfa.accepts('1011')

    ia = dfa.get_intersection(nfa)
    assert ia.accepts('101')
    assert not ia.accepts('1011')
Esempio n. 16
0
def test_a_intersection_nfa():
    symb_0 = finite_automaton.Symbol('0')
    symb_1 = finite_automaton.Symbol('1')
    state_1_1 = finite_automaton.State(1)
    state_2_1 = finite_automaton.State(2)
    state_3_1 = finite_automaton.State(3)

    nfa1 = NondeterministicFiniteAutomaton()
    nfa1.add_transition(state_1_1, symb_1, state_2_1)
    nfa1.add_transition(state_1_1, symb_0, state_2_1)
    nfa1.add_transition(state_2_1, symb_0, state_2_1)
    nfa1.add_transition(state_1_1, symb_1, state_3_1)
    nfa1.add_transition(state_2_1, symb_1, state_3_1)
    nfa1.add_final_state(state_3_1)
    nfa1.add_start_state(state_1_1)
    assert not nfa1.accepts('1011')

    state_1_2 = finite_automaton.State(1)
    state_2_2 = finite_automaton.State(2)
    state_3_2 = finite_automaton.State(3)
    state_4_2 = finite_automaton.State(4)

    nfa2 = NondeterministicFiniteAutomaton()
    nfa2.add_transition(state_1_2, symb_1, state_2_2)
    nfa2.add_transition(state_2_2, symb_0, state_3_2)
    nfa2.add_transition(state_3_2, symb_1, state_3_2)
    nfa2.add_transition(state_3_2, symb_1, state_4_2)
    nfa2.add_transition(state_1_2, symb_1, state_4_2)
    nfa2.add_transition(state_1_2, symb_0, state_4_2)
    nfa2.add_final_state(state_4_2)
    nfa2.add_start_state(state_1_2)
    assert nfa2.accepts('1011')

    nfa3 = nfa1.get_intersection(nfa2)
    assert nfa3.accepts('1')
    assert nfa3.accepts('101')
    assert not nfa3.accepts('1011')