예제 #1
0
    def with_var_map(self, new_var_map):
        alphabets = list(range(len(new_var_map)))
        for v, (idx, alphabet) in new_var_map.items():
            alphabets[idx] = alphabet

        if len(alphabets) == 0:
            new_alphabet = set()
        else:
            new_alphabet = set(' '.join(syms)
                               for syms in it.product(*alphabets))

        new_transitions = {}
        for (src, sym), dsts in self.aut['transitions'].items():
            new_syms = list(range(len(new_var_map)))

            # Copy over the old symbols
            syms = sym.split(' ')
            for v, (idx, alphabet) in self.var_map.items():
                # If a symbol isn't in the new map, just drop it
                if v in new_var_map:
                    new_syms[new_var_map[v][0]] = [syms[idx]]

            for v, (idx, alphabet) in new_var_map.items():
                if not v in self.var_map:
                    new_syms[idx] = alphabet

            for new_sym in it.product(*new_syms):
                new_transitions[(src, ' '.join(new_sym))] = dsts

        # Rename all states because PySimpleAutomata can't union automata with the same state names...
        # TODO: Probably very inefficient.
        return NFA.rename_nfa_states(
            {
                'alphabet': new_alphabet,
                'states': self.aut['states'],
                'initial_states': self.aut['initial_states'],
                'accepting_states': self.aut['accepting_states'],
                'transitions': new_transitions
            }, FiniteAutomaton.fresh_ap())
예제 #2
0
 def test_rename_nfa_states(self):
     """ Tests a correct NFA states renaming """
     automata_IO.nfa_to_dot(NFA.rename_nfa_states(self.nfa_1, 'TOP_'),
                            'nfa_renamed_1', 'tests/outputs')