def test_intersection_dfa2(self):
        state0 = State(0)
        symb_a = Symbol("a")
        symb_b = Symbol("b")
        dfa = DeterministicFiniteAutomaton({state0}, {symb_a, symb_b},
                                           start_state=state0,
                                           final_states={state0})
        dfa.add_transition(state0, symb_a, state0)
        dfa.add_transition(state0, symb_b, state0)
        self.assertTrue(dfa.accepts([symb_a, symb_a, symb_b, symb_b]))

        ter_a = Terminal("a")
        ter_b = Terminal("b")
        var_s = Variable("S")
        var_s1 = Variable("S1")
        var_l = Variable("L")
        productions = {
            Production(var_s, [var_l, var_s1]),
            Production(var_l, [Epsilon()]),
            Production(var_s1, [ter_a, var_s1, ter_b]),
            Production(var_s1, [ter_b, var_s1, ter_a]),
            Production(var_s1, [])
        }
        cfg = CFG(productions=productions, start_symbol=var_s)
        self.assertTrue(cfg.contains([ter_a, ter_a, ter_b, ter_b]))
        self.assertFalse(cfg.contains([ter_a, ter_a, ter_b]))
        cfg_i = cfg.intersection(dfa)
        self.assertFalse(cfg_i.is_empty())
        self.assertTrue(cfg_i.contains([ter_a, ter_a, ter_b, ter_b]))
        self.assertTrue(cfg_i.contains([]))
    def test_intersection_with_epsilon(self):
        state0 = State(0)
        state1 = State(1)
        symb_a = Symbol("a")
        dfa = DeterministicFiniteAutomaton({state0, state1}, {symb_a},
                                           start_state=state0,
                                           final_states={state1})
        dfa.add_transition(state0, symb_a, state1)
        self.assertTrue(dfa.accepts([symb_a]))

        ter_a = Terminal("a")
        var_s = Variable("S")
        var_l = Variable("L")
        var_t = Variable("T")
        productions = {
            Production(var_s, [var_l, var_t]),
            Production(var_l, [Epsilon()]),
            Production(var_t, [ter_a])
        }
        cfg = CFG(productions=productions, start_symbol=var_s)
        self.assertFalse(cfg.is_empty())
        self.assertTrue(cfg.contains([ter_a]))

        cfg_temp = cfg.to_pda().to_cfg()
        self.assertFalse(cfg_temp.is_empty())
        self.assertTrue(cfg_temp.contains([ter_a]))

        cfg_temp = cfg.to_pda().to_final_state().to_empty_stack().to_cfg()
        self.assertFalse(cfg_temp.is_empty())
        self.assertTrue(cfg_temp.contains([ter_a]))

        cfg_i = cfg.intersection(dfa)
        self.assertFalse(cfg_i.is_empty())
    def _test_profiling_intersection(self):
        size = 50
        states = [State(i) for i in range(size * 2 + 1)]
        symb_a = Symbol("a")
        symb_b = Symbol("b")
        dfa = DeterministicFiniteAutomaton(states, {symb_a, symb_b},
                                           start_state=states[0],
                                           final_states={states[-1]})
        for i in range(size):
            dfa.add_transition(states[i], symb_a, states[i + 1])
        for i in range(size, size * 2):
            dfa.add_transition(states[i], symb_b, states[i + 1])

        ter_a = Terminal("a")
        ter_b = Terminal("b")
        var_s = Variable("S")
        var_s1 = Variable("S1")
        var_l = Variable("L")
        productions = [
            Production(var_s, [var_l, var_s1]),
            Production(var_l, [Epsilon()]),
            Production(var_s1, [ter_a, var_s1, ter_b]),
            Production(var_s1, [ter_b, var_s1, ter_a]),
            Production(var_s1, [])
        ]
        cfg = CFG(productions=productions, start_symbol=var_s)
        cfg_i = cfg.intersection(dfa)
        self.assertFalse(cfg_i.is_empty())
        self.assertTrue(cfg_i.contains([ter_a] * size + [ter_b] * size))
        self.assertFalse(cfg_i.contains([]))
    def dfa_normalize_states(dfa):
        res = DeterministicFiniteAutomaton()
        old_states = dfa._states
        states_map = {}
        for i, state in enumerate(old_states):
            states_map[state] = i

        old_transitions = dfa.to_dict()
        for vs, trs in old_transitions.items():
            for label, ve in trs.items():
                res.add_transition(
                    states_map[vs],
                    label,
                    states_map[ve],
                )
        start_states = map(
            lambda state: states_map[state],
            dfa.start_states,
        )
        final_states = map(
            lambda state: states_map[state],
            dfa.final_states,
        )
        for state in start_states:
            res.add_start_state(state)
        for state in final_states:
            res.add_final_state(state)
        return res, states_map
Exemple #5
0
def test_DFA_intersection():
    char_a = Symbol("a")
    char_b = Symbol("b")

    state_start = State(0)
    state_final = State(1)

    dfa_fst = DeterministicFiniteAutomaton()
    dfa_fst.add_start_state(state_start)
    dfa_fst.add_final_state(state_final)

    dfa_snd = DeterministicFiniteAutomaton()
    dfa_snd.add_start_state(state_start)
    dfa_snd.add_final_state(state_final)

    dfa_fst.add_transition(state_start, char_a, state_final)
    dfa_fst.add_transition(state_final, char_b, state_start)

    dfa_snd.add_transition(state_start, char_a, state_final)
    dfa_snd.add_transition(state_final, char_b, state_final)

    dfa_intersection = dfa_fst & dfa_snd

    assert not dfa_fst.accepts("abbbb")
    assert dfa_fst.accepts("ababa")
    assert dfa_fst.accepts("a")

    assert not dfa_snd.accepts("ababa")
    assert dfa_snd.accepts("abbbb")
    assert dfa_snd.accepts("a")

    assert not dfa_intersection.accepts("ababa")
    assert not dfa_intersection.accepts("abbbb")
    assert dfa_intersection.accepts("a")
def test_DFA_intersection():
    symb_a = Symbol("a")
    symb_b = Symbol("b")

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

    dfa1 = DeterministicFiniteAutomaton()
    dfa2 = DeterministicFiniteAutomaton()

    dfa1.add_start_state(state0)
    dfa2.add_start_state(state0)

    dfa1.add_final_state(state1)
    dfa2.add_final_state(state1)

    dfa1.add_transition(state0, symb_a, state1)
    dfa1.add_transition(state1, symb_b, state0)
    dfa2.add_transition(state0, symb_a, state1)
    dfa2.add_transition(state1, symb_b, state1)

    dfa_intersection = dfa1 & dfa2

    assert dfa1.accepts("ababa")
    assert dfa1.accepts("a")
    assert not dfa2.accepts("ababa")
    assert dfa2.accepts("abbbb")
    assert dfa2.accepts("a")
    assert not dfa1.accepts("abbbb")
    assert dfa_intersection.accepts("a")
    assert not dfa_intersection.accepts("ababa")
    assert not dfa_intersection.accepts("abbbb")
 def test_not_cyclic(self):
     dfa = DeterministicFiniteAutomaton()
     state0 = State(0)
     state1 = State(1)
     symb_a = Symbol('a')
     dfa.add_start_state(state0)
     dfa.add_transition(state0, symb_a, state1)
     self.assertTrue(dfa.is_acyclic())
 def test_big_minimize(self):
     dfa = DeterministicFiniteAutomaton()
     size = 1000
     symb = Symbol("s")
     dfa.add_start_state(State(0))
     dfa.add_final_state(State(size))
     for i in range(size):
         dfa.add_transition(State(i), symb, State(i + 1))
     dfa = dfa.minimize()
     self.assertEqual(len(dfa.states), size + 1)
     self.assertFalse(dfa.accepts([symb]))
def get_example0_bis():
    """ Gives a dfa """
    dfa = DeterministicFiniteAutomaton()
    dfa.add_start_state(0)
    dfa.add_final_state(2)
    dfa.add_final_state(3)
    dfa.add_transition(0, "a", 1)
    dfa.add_transition(1, "b", 1)
    dfa.add_transition(1, "c", 2)
    dfa.add_transition(1, "d", 3)
    return dfa
 def test_add_transition(self):
     """ Tests the addition of transitions
     """
     dfa = DeterministicFiniteAutomaton()
     self.assertEqual(len(dfa.states), 0)
     state0 = State("0")
     state1 = State("1")
     symb = Symbol("a")
     dfa.add_transition(state0, symb, state1)
     self.assertEqual(len(dfa.states), 2)
     self.assertEqual(len(dfa.symbols), 1)
     self.assertEqual(len(list(dfa._transition_function.get_edges())), 1)
 def to_dfa(self):
     result = DeterministicFiniteAutomaton(
         states=set(self.vertice_numbering_dictionary.keys()),
         start_state=State(self.start_vertices.pop()),
         final_states=set(map(lambda x: State(x), self.final_vertices)))
     for label, matrix in self.label_matrices.items():
         for i in range(matrix.nrows):
             for j in range(matrix.ncols):
                 if self.label_matrices[label][i, j]:
                     result.add_transition(State(i), Symbol(label),
                                           State(j))
     return result
Exemple #12
0
    def to_dfa(self):
        dfa = DeterministicFiniteAutomaton()
        states = [State(i) for i in range(self.n_vertices)]
        for i in self.start_vertices:
            dfa.add_start_state(states[i])

        for i in self.terminal_vertices:
            dfa.add_final_state(states[i])

        for label in self.labels():
            symbol = Symbol(label)
            fro, to, has_edge = self.get_by_label(label).to_lists()
            for i in range(len(fro)):
                if has_edge:
                    dfa.add_transition(states[fro[i]], symbol, states[to[i]])
        return dfa
class TestAutomata:
    symb_a = Symbol("a")
    symb_b = Symbol("b")
    symb_c = Symbol("c")
    symb_d = Symbol("d")

    def init_dfas(self):
        # DFA that accepts "ab" and "ac"
        self.dfa1 = DeterministicFiniteAutomaton()
        # DFA that accepts "ac" and "ad"
        self.dfa2 = DeterministicFiniteAutomaton()

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

        # Add a start state
        self.dfa1.add_start_state(state0)
        self.dfa2.add_start_state(state0)

        # Add two final states
        self.dfa1.add_final_state(state2)
        self.dfa1.add_final_state(state3)

        self.dfa2.add_final_state(state2)
        self.dfa2.add_final_state(state3)

        # Create transitions
        self.dfa1.add_transition(state0, self.symb_a, state1)
        self.dfa1.add_transition(state1, self.symb_b, state2)
        self.dfa1.add_transition(state1, self.symb_c, state3)

        self.dfa2.add_transition(state0, self.symb_a, state1)
        self.dfa2.add_transition(state1, self.symb_c, state2)
        self.dfa2.add_transition(state1, self.symb_d, state3)

    def test_dfa1(self):
        self.init_dfas()
        assert (self.dfa1.accepts([self.symb_a, self.symb_b]))
        assert (self.dfa1.accepts([self.symb_a, self.symb_c]))
        assert (not self.dfa1.accepts([self.symb_a, self.symb_d]))

    def test_dfa2(self):
        self.init_dfas()
        assert (not self.dfa2.accepts([self.symb_a, self.symb_b]))
        assert (self.dfa2.accepts([self.symb_a, self.symb_c]))
        assert (self.dfa2.accepts([self.symb_a, self.symb_d]))

    def test_dfa_inter(self):
        self.init_dfas()
        dfa_inter = self.dfa1 & self.dfa2

        # Intersection result should accept only "ac", but not "ab" or "ad"
        assert (not dfa_inter.accepts([self.symb_a, self.symb_b]))
        assert (dfa_inter.accepts([self.symb_a, self.symb_c]))
        assert (not dfa_inter.accepts([self.symb_a, self.symb_d]))
 def test_complement(self):
     """ Tests the complement operation """
     dfa = DeterministicFiniteAutomaton()
     symb_a = Symbol("a")
     symb_b = Symbol("b")
     state0 = State(0)
     state1 = State(1)
     state2 = State(2)
     dfa.add_start_state(state0)
     dfa.add_final_state(state2)
     dfa.add_transition(state0, symb_a, state1)
     dfa.add_transition(state1, symb_b, state2)
     dfa_comp = dfa.get_complement()
     self.assertTrue(dfa_comp.accepts([symb_a]))
     self.assertTrue(dfa_comp.accepts([symb_b]))
     self.assertTrue(dfa_comp.accepts([symb_b, symb_a]))
     self.assertTrue(dfa_comp.accepts([]))
     self.assertFalse(dfa_comp.accepts([symb_a, symb_b]))
def get_example0():
    """ Gives a dfa """
    dfa = DeterministicFiniteAutomaton()
    state0 = State(0)
    state1 = State(1)
    state2 = State(2)
    state3 = State(3)
    symb_a = Symbol("a")
    symb_b = Symbol("b")
    symb_c = Symbol("c")
    symb_d = Symbol("d")
    dfa.add_start_state(state0)
    dfa.add_final_state(state2)
    dfa.add_final_state(state3)
    dfa.add_transition(state0, symb_a, state1)
    dfa.add_transition(state1, symb_b, state1)
    dfa.add_transition(state1, symb_c, state2)
    dfa.add_transition(state1, symb_d, state3)
    return dfa
    def to_dfa(self):
        edges = list(zip(*self.matrix.to_lists()))

        dfa = DeterministicFiniteAutomaton()

        # Creation of the states
        state_vals = range(self.matrix.ncols)
        state_dict = {}
        for value in state_vals:
            state_dict[value] = State(value)
        for value in self.start_states:
            dfa.add_start_state(state_dict[value])
        for value in self.final_states:
            dfa.add_final_state(state_dict[value])

        # Create transitions
        for s, p, o in edges:
            dfa.add_transition(state_dict[s], Symbol(p), state_dict[o])

        return dfa
Exemple #17
0
def test_pda_intersection():
    dfa_1 = DeterministicFiniteAutomaton()
    dfa_2 = DeterministicFiniteAutomaton()
    dfa = DeterministicFiniteAutomaton()

    # Creation of the states
    state0 = State(0)
    state1 = State(1)
    state2 = State(2)
    state3 = State(3)

    # Creation of the symbols
    symb_a = Symbol("a")
    symb_b = Symbol("b")
    symb_c = Symbol("c")
    symb_d = Symbol("d")

    # Add a start state
    dfa_1.add_start_state(state0)
    dfa_2.add_start_state(state0)

    # Add two final states
    dfa_1.add_final_state(state2)
    dfa_1.add_final_state(state3)
    dfa_2.add_final_state(state2)

    # Create transitions
    dfa_1.add_transition(state0, symb_a, state1)
    dfa_1.add_transition(state1, symb_b, state1)
    dfa_1.add_transition(state1, symb_c, state2)
    dfa_1.add_transition(state1, symb_d, state3)
    dfa_2.add_transition(state0, symb_a, state1)
    dfa_2.add_transition(state1, symb_b, state1)
    dfa_2.add_transition(state1, symb_c, state2)
    dfa_2.add_transition(state0, symb_d, state1)

    # Check if a word is accepted
    dfa = dfa_1 & dfa_2
    assert dfa.accepts([symb_a, symb_b, symb_c])
    assert dfa.accepts([symb_a, symb_b, symb_b, symb_c])
    assert not dfa.accepts([symb_a, symb_b, symb_d])
    assert not dfa.accepts([symb_d, symb_b, symb_c])
 def test_big_minimize_reduce(self):
     dfa = DeterministicFiniteAutomaton()
     symb_0 = Symbol("0")
     symb_0_minus = Symbol("0-")
     symb_1 = Symbol("1")
     symb_1_minus = Symbol("1-")
     symb_star = Symbol("STAR")
     start = State("start")
     states = [State(str(x)) for x in range(10)]
     dfa.add_start_state(start)
     dfa.add_final_state(states[2])
     dfa.add_final_state(states[3])
     dfa.add_final_state(states[4])
     dfa.add_final_state(states[8])
     dfa.add_final_state(states[7])
     dfa.add_final_state(states[9])
     dfa.add_transition(start, symb_0, states[0])
     dfa.add_transition(states[1], symb_0, states[2])
     dfa.add_transition(states[2], symb_0, states[0])
     dfa.add_transition(states[3], symb_0, states[0])
     dfa.add_transition(states[4], symb_0, states[0])
     dfa.add_transition(states[8], symb_0, states[0])
     dfa.add_transition(states[7], symb_0, states[0])
     dfa.add_transition(states[9], symb_0, states[0])
     dfa.add_transition(states[0], symb_star, states[1])
     dfa.add_transition(states[5], symb_star, states[6])
     dfa.add_transition(states[7], symb_star, states[9])
     dfa.add_transition(states[2], symb_star, states[3])
     dfa.add_transition(states[4], symb_star, states[8])
     dfa.add_transition(states[1], symb_0_minus, states[5])
     dfa.add_transition(states[6], symb_0_minus, states[7])
     dfa.add_transition(states[3], symb_1_minus, states[4])
     self.assertFalse(
         dfa.accepts([
             "0", "STAR", "0-", "STAR", "0-", "0", "STAR", "0", "0", "STAR",
             "0-", "STAR", "0-", "1-"
         ]))
     dfa = dfa.minimize()
     self.assertFalse(
         dfa.accepts([
             "0", "STAR", "0-", "STAR", "0-", "0", "STAR", "0", "0", "STAR",
             "0-", "STAR", "0-", "1-"
         ]))
    def test_minimize_repetition(self):
        dfa = DeterministicFiniteAutomaton()
        symb_a = Symbol('a')
        symb_b = Symbol("b")
        symb_star = Symbol("star")
        states = [State(x) for x in range(9)]
        dfa.add_start_state(states[0])
        dfa.add_final_state(states[3])
        dfa.add_final_state(states[4])
        dfa.add_final_state(states[7])
        dfa.add_final_state(states[8])
        dfa.add_transition(states[0], symb_a, states[1])
        dfa.add_transition(states[1], symb_star, states[2])
        dfa.add_transition(states[2], symb_a, states[3])
        dfa.add_transition(states[3], symb_star, states[4])
        dfa.add_transition(states[3], symb_a, states[1])
        dfa.add_transition(states[3], symb_b, states[5])
        dfa.add_transition(states[4], symb_a, states[1])
        dfa.add_transition(states[4], symb_b, states[5])

        dfa.add_transition(states[0], symb_b, states[5])
        dfa.add_transition(states[5], symb_star, states[6])
        dfa.add_transition(states[6], symb_b, states[7])
        dfa.add_transition(states[7], symb_star, states[8])
        dfa.add_transition(states[7], symb_a, states[1])
        dfa.add_transition(states[7], symb_b, states[5])
        dfa.add_transition(states[8], symb_a, states[1])
        dfa.add_transition(states[8], symb_b, states[5])
        dfa = dfa.minimize()
        self.assertTrue(dfa.accepts([symb_a, symb_star, symb_a]))
 def test_epsilon_refused(self):
     dfa = DeterministicFiniteAutomaton()
     state0 = State(0)
     state1 = State(1)
     with self.assertRaises(InvalidEpsilonTransition):
         dfa.add_transition(state0, Epsilon(), state1)
Exemple #21
0
def home(request):
    if request.method == "POST":
        states = request.POST['States']
        ini_states = request.POST['ini_State']
        fin_states = request.POST['fin_State']
        symbols = request.POST['Symbols']
        transition_states = request.POST['tran_State']
        test_string = request.POST['test_String']
    else : 
        return render(request, 'home/home.html')

    dfa = DeterministicFiniteAutomaton()

    # Creation of the states
    input_states = states.split()
    input_states = list(map(int, input_states))
    actual_states = list(map(State, input_states))

    def convert_symb(a):
        converted_symb = []
        for i in a:
            converted_symb.append(Symbol(i))
        return converted_symb
        
    # Creation of the symbols
    input_symbs = symbols.split()
    actual_symbs = convert_symb(input_symbs)

    # Add a start state
    input_start_state = ini_states.split()
    input_start_state = list(map(int, input_start_state))
    for i in input_start_state:
        dfa.add_start_state(actual_states[i])

    # Add two final states
    input_final_state = fin_states.split()
    input_final_state = list(map(int, input_final_state))
    for i in input_final_state:
        dfa.add_final_state(actual_states[i])


    # Create transitions
    user_tran_in = transition_states.split()
    i = 0
    while(i < len(user_tran_in)):
        
        a = int(user_tran_in[i])
        i += 1
        b = int(user_tran_in[i])
        i += 1
        c = int(user_tran_in[i])
        i += 1
        dfa.add_transition(actual_states[a], actual_symbs[b], actual_states[c])

    user = test_string.split()
    user_string = convert_symb(user)
    output = dfa.accepts(user_string)

    content = {
        't1' : output
    }

    return render(request, 'home/home.html', content)