def diff_aut(self, automaton, suffix): """Renames the automaton's states. Arguments: automaton: the automaton to be manipulated. suffix: the chosen string to be added to the states. Returns: An automaton with different names for states. """ aux_aut = FiniteAutomaton( {state + suffix for state in automaton.states}, automaton.alphabet, {}, automaton.init_state + suffix, {frozenset([set(state).pop() + suffix]) for state in automaton.final_states}) for state in automaton.transitions: key = frozenset([set(state).pop() + suffix]) aux_aut.transitions[key] = {} for symbol in automaton.transitions[state]: aux_aut.transitions[key][symbol] = set() for element in automaton.transitions[state][symbol]: if isinstance(element, str): element = [element] aux_aut.transitions[key][symbol].add( frozenset([set(element).pop() + suffix])) return aux_aut
def closure_op(self, automatons): """Implements the regular expression Kleene star operator. Arguments: automatons: a list of automatons to be operated on. Returns: An automaton that will accept any non-negative number of repetitions of its original language. """ clsr_aut = FiniteAutomaton( {"initClsr"}, set(), {frozenset(["initClsr"]): {automatons[0].epsilon: set()}}, "initClsr", {frozenset(["initClsr"])}) e = clsr_aut.epsilon for each in automatons: clsr_aut.states |= each.states clsr_aut.alphabet |= each.alphabet clsr_aut.transitions[frozenset(["initClsr"])][e].add( frozenset([each.init_state])) clsr_aut.transitions.update(each.transitions) clsr_aut.final_states |= each.final_states for state in each.final_states: try: clsr_aut.transitions[state][e] = set() except KeyError: clsr_aut.transitions[state] = {} clsr_aut.transitions[state][e] = set() clsr_aut.transitions[state][e].add(frozenset(["initClsr"])) return self.add_transitions(clsr_aut)
def rename_aut(self, automaton): """Makes the automaton's states' names readable. Arguments: automaton: the automaton with confusing names. Returns: An automaton with normal names for states. """ new_aut = FiniteAutomaton(set(), automaton.alphabet, {}, "", set()) new_states = {} for i, j in zip(automaton.states, range(len(automaton.states))): new_states[i] = 'q' + str(j) for i in new_states: state = new_states[i] new_aut.states.add(state) if frozenset([i]) in automaton.final_states: final_state = set([new_states[i]]) new_aut.final_states.add(frozenset(final_state)) if i == automaton.init_state: new_aut.init_state = new_states[i] for i in automaton.transitions: new_key = frozenset([new_states[list(set(i))[0]]]) new_aut.transitions[new_key] = {} for j in automaton.transitions[i]: new_aut.transitions[new_key][j] = set() for k in automaton.transitions[i][j]: if isinstance(k, frozenset): if len(k) > 1: to_these_states = {new_states[i] for i in automaton.transitions[i][j]} new_aut.transitions[new_key][j] |= to_these_states else: to_this_state = set([new_states[list(set(k))[0]]]) new_aut.transitions[new_key][j] |= to_this_state else: to_this_state = set([new_states[k]]) new_aut.transitions[new_key][j] |= to_this_state return new_aut