class AlphabetTestCase(unittest.TestCase): def setUp(self): self.a = Alphabet(['a', 'b']) def test_verification(self): self.assertTrue('b' in self.a) self.assertFalse('c' in self.a) def test_insertion(self): self.a.ajouter_symbole('c') self.assertTrue('c' in self.a) def test_supression(self): self.a.supprime_symbole('a') self.assertFalse('a' in self.a)
def faire_union(automate1: Automate, automate2: Automate): if not isinstance(automate1, Automate) or not isinstance( automate2, Automate): raise TypeError("L'addition se fais uniquement entre les automate") initial = GenerateurAutomate.etat_initial() final = GenerateurAutomate.etat_final() ancien_initial = [automate1.etat_initial, automate2.etat_initial] ancien_finaux = [*automate1.etats_finaux, *automate2.etats_finaux] nouveaux_etats = [*automate1.etats, *automate2.etats, initial, final] nouvelle_transition = [*automate1.transitions, *automate2.transitions] nouvel_alphabet = Alphabet( [*automate1.alphabet.list, *automate2.alphabet.list]) automate = Automate(nouvel_alphabet, nouveaux_etats, initial, [final], nouvelle_transition) for etat in ancien_initial: transition_initial = Transition(initial, '', etat) automate.ajoute_transition(transition_initial) for etat in ancien_finaux: transition_final = Transition(etat, '', final) automate.ajoute_transition(transition_final) return automate
def convertir_en_nfa(self): if self.type != Type.eAFN: raise TypeError( f'Conversion autorisee uniquement pour les {Type.eAFN} ') etat_initial = self.etat_initial etat_finaux = list() transition = list() fermeture_epsilon = {} for etat in self.etats: fermeture_epsilon[etat] = self.epsilon_fermeture(etat) for etat in self.etats: if self.etats_finaux.intersection(set(fermeture_epsilon[etat])): etat_finaux.append(etat) for symbole in self.alphabet.list: etats_destination = list() for etat_epsilon in fermeture_epsilon[etat]: etats_destination.extend( self.__etats_destination(etat_epsilon, symbole)) destination_resultat = list() for etat_destination in set(etats_destination): destination_resultat.extend( self.epsilon_fermeture(etat_destination)) for destination in set(destination_resultat): nouvelle_transition = Transition(etat, symbole, destination) transition.append(nouvelle_transition) resultat = Automate(Alphabet(self.alphabet.list), list(self.etats), self.etat_initial, etat_finaux, transition) resultat.parents = self.parents.copy() resultat.nom = self.nom return resultat
def a_partir_du_symbole(symbole): if not isinstance(symbole, str): raise TypeError('Un symbole dois etre de type str') alphabet = Alphabet([symbole]) initial = GenerateurAutomate.etat_initial() final = GenerateurAutomate.etat_final() transition = Transition(initial,symbole,final) return Automate(alphabet,[initial,final],initial,[final],[transition])
def a_partir_de(automate): etat = None if not automate.etat_initial else Etat( str(automate.etat_initial)) resultat = Automate(Alphabet(automate.alphabet.list), list(automate.etats), etat, list(automate.etats_finaux), list(automate.transitions)) resultat.definir_nom(automate.nom) resultat.parents = [*automate.parents] return resultat
def faire_la_concatenation(automate1: Automate, automate2: Automate): initial = automate1.etat_initial final = list(automate2.etats_finaux)[-1] for etat in automate1.etats_finaux: automate2.remplacer_etat(automate2.etat_initial, etat) nouveaux_etats = [*automate1.etats, *automate2.etats] nouvelle_transition = [*automate1.transitions, *automate2.transitions] nouvel_alphabet = Alphabet([*automate1.alphabet.list, *automate2.alphabet.list]) automate = Automate(nouvel_alphabet, nouveaux_etats, initial, [final], nouvelle_transition) return automate
def newDialog(self): text, ok = QtWidgets.QInputDialog.getText(self.window, 'Nouvel Automate', 'Entrez le nom') if ok: a = Automate(Alphabet([]), [], None, [], []) a.definir_nom(text) try: self.liste_automate[text] = a except: self.liste_automate[text + '(1)'] = a self.creation.ui.createBtn.setState() self.automate.copie_automate(a)
def union_automate(self, automate): automate_1 = self.completer() if isinstance(automate, Automate): automate_2 = Automate.a_partir_de(automate) for etat in automate_2.etats: if etat in self.etats: automate_2.remplacer_etat(etat, Automate.genere_etat()) automate_2 = automate.completer() new_alphabet = set() for i in automate_1.alphabet.list: new_alphabet.add(i) for i in automate_2.alphabet.list: new_alphabet.add(i) for etat in automate_1.etats_finaux: if not etat.nom: etat.definir_nom(automate_1.nom) for etat in automate_2.etats_finaux: if not etat.nom: etat.definir_nom(automate_2.nom) new_alphabet = Alphabet(new_alphabet) new_transition = automate_1.transitions.union( automate_2.transitions) new_etat = automate_1.etats.union(automate_2.etats) new_etat.add(Etat('start')) print( f"Etat automate {automate_1.etat_initial} {automate_2.etat_initial}" ) new_transition.add( Transition(Etat('start'), '', automate_1.etat_initial)) new_transition.add( Transition(Etat('start'), '', automate_2.etat_initial)) new_finaux = automate_1.etats_finaux.union(automate_2.etats_finaux) resultat = Automate(new_alphabet, new_etat, Etat('start'), new_finaux, new_transition).determiniser().minimiser() resultat.parents = [ *automate_1.parents, *automate_2.parents, automate_1, automate_2 ] resultat.nom = self.nom return resultat
def completer(self): if self.est_complet: return self if self.type != Type.AFD: return self.determiniser().completer() puit = Etat("PUIS") etats = list(self.etats) etats.append(puit) transitions = list(self.transitions) for etat in etats: for symbole in self.__alphabet.list: if not self.__etats_destination(etat, symbole): transitions.append(Transition(etat, symbole, puit)) resultat = Automate(Alphabet(self.alphabet.list), etats, self.etat_initial, self.etats_finaux.copy(), transitions) resultat.parents = self.parents.copy() resultat.nom = self.nom return resultat
def union_automate(self, automate): automate_1 = self.completer() if isinstance(automate, Automate): automate_2 = automate.completer() new_alphabet = set() for i in automate_1.alphabet.list: new_alphabet.add(i) for i in automate_2.alphabet.list: new_alphabet.add(i) new_alphabet = Alphabet(new_alphabet) new_transition = automate_1.transitions.union( automate_2.transitions) new_etat = automate_1.etats.union(automate_2.etats) new_etat.add(Etat('start')) new_transition.add( Transition(Etat('start'), '', automate_1.etat_initial)) new_transition.add( Transition(Etat('start'), '', automate_2.etat_initial)) new_finaux = automate_1.etats_finaux.union(automate_2.etats_finaux) return Automate(new_alphabet, new_etat, Etat('start'), new_finaux, new_transition)
def setUp(self): self.a = Alphabet(['a', 'b'])
text, ok = QtWidgets.QInputDialog.getText(self.window, 'Nouvel Automate', 'Entrez le nom') if ok: a = Automate(Alphabet([]), [], None, [], []) a.definir_nom(text) try: self.liste_automate[text] = a except: self.liste_automate[text + '(1)'] = a self.creation.ui.createBtn.setState() self.automate.copie_automate(a) alphabet = Alphabet(['1', '2', '3']) a = Etat('a') b = Etat('b') c = Etat('c') t1 = Transition(a, '1', b) t2 = Transition(a, '1', a) t3 = Transition(a, '2', b) t4 = Transition(b, '1', b) automata = Automate(alphabet, [a, b, c], a, [a, c], [t1, t2, t3, t4]) automata.definir_nom('Brains') def run_app(automate=automata): import sys app = QtWidgets.QApplication(sys.argv)
def __verifier_alphabet(self, alphabet): if isinstance(alphabet, Alphabet): return alphabet else: return Alphabet(alphabet)
return self.__etats @property def etat_initial(self): return self.__etat_initial @property def etats_finaux(self): return self.__etat_finaux @property def transitions(self): return self.__transitions if __name__ == '__main__': alphabet = Alphabet(['a', 'b']) alphabet2 = Alphabet(['m']) a = Etat('q1') b = Etat('q2') c = Etat('q3') e = Etat('q4') f = Etat('q5') t1 = Transition(a, 'a', b) t2 = Transition(b, 'b', c) t3 = Transition(c, 'a', b) t4 = Transition(e, 'm', f) automate = Automate(alphabet, [a, b, c], a, [b], [t1, t2, t3]) automate2 = Automate(alphabet2, [e, f], e, [f], [t4]) print(automate.alphabet)
nouvel_alphabet = Alphabet( [*automate1.alphabet.list, *automate2.alphabet.list]) automate = Automate(nouvel_alphabet, nouveaux_etats, initial, [final], nouvelle_transition) return automate @staticmethod def etat(): GenerateurAutomate.compteur_etat += 1 return Etat(f'Q{GenerateurAutomate.compteur_etat}') @staticmethod def etat_initial(): GenerateurAutomate.compteur_etat_initial += 1 return Etat(f'I{GenerateurAutomate.compteur_etat_initial}') @staticmethod def etat_final(): GenerateurAutomate.compteur_etat_final += 1 return Etat(f'F{GenerateurAutomate.compteur_etat_final}') if __name__ == '__main__': a = Automate(Alphabet(['1']), [Etat('a'), Etat('b')], Etat('a'), [Etat('b')], [Transition(Etat('a'), '1', Etat('b'))]) b = Automate(Alphabet(['2']), [Etat('c'), Etat('d')], Etat('c'), [Etat('d')], [Transition(Etat('c'), '2', Etat('d'))]) a.visualiser() b.visualiser() GenerateurAutomate.faire_kleen(a).visualiser()
def minimiser(self): if self.type != Type.AFD: return self.determiniser().minimiser() paire_etat = list() paire_traite = list() etats_temporaire = self.etats.copy() for etat in self.etats: etats_temporaire.remove(etat) for etat_temporaire in etats_temporaire: pair = {etat, etat_temporaire} paire_etat.append(frozenset(pair)) # Scanning pairs and marking a_state_has_been_marked = False for pair in paire_etat: if len(pair.intersection(self.etats_finaux)) == 1: print(f"paire chosi {pair}") paire_traite.append(frozenset(pair)) a_state_has_been_marked = True while a_state_has_been_marked: a_state_has_been_marked = False for pair in paire_etat: if pair not in paire_traite: local_pair = list(pair) for symbol in self.alphabet.list: paire_resultat = set() a = self.__etats_destination(local_pair[0], symbol) b = self.__etats_destination(local_pair[-1], symbol) print( f"paire {pair} : {local_pair[0]} sur {symbol} => {a} " ) print( f"paire {pair} : {local_pair[1]} sur {symbol} => {b} " ) if a and b: paire_resultat = set(a + b) if paire_resultat in paire_traite: print(f'nouvelle pair => {pair}') paire_traite.append(frozenset(pair)) a_state_has_been_marked = True break # Generating new set of states unmarked_pair = set(paire_etat).difference(set(paire_traite)) print(f'paire non traite {unmarked_pair}') etats_utiliser = set() new_states = set() nouveau_vers_ancien_etat = dict() for etat in self.etats: etat_ajouter = False etat_actuel = etat if etat in etats_utiliser: continue for pair in unmarked_pair: if etat_actuel in pair: etats_utiliser.update({x for x in pair}) print(f"etat utiliser = {etats_utiliser}") nouvel_etat = Etat(','.join([str(x) for x in pair])) nouveau_vers_ancien_etat[nouvel_etat] = pair new_states.add(nouvel_etat) etat_ajouter = True if not etat_ajouter: new_states.add(etat_actuel) nouveau_vers_ancien_etat[etat_actuel] = [etat_actuel] #print(f"nouveau_vers_ancien_etat") print(f"Nouvelle etats {new_states}") # Generation new set of transitions read_trans = list() new_transitions = set() new_initial_state = set() new_final_states = set() for state in new_states: if self.etat_initial in nouveau_vers_ancien_etat[state]: new_initial_state = state print(f"Nouvel etat initial {state}") for f_state in self.etats_finaux: if f_state in nouveau_vers_ancien_etat[state]: new_final_states.add(state) for transition in self.transitions: _from = transition.depart _to = transition.arrive _on = transition.symbole if (_from, _on) in read_trans: continue for state in new_states: if _from in nouveau_vers_ancien_etat[state]: result = self.__etats_destination(_from, _on) read_trans.append((_from, _on)) for result_state in result: for i in new_states: if result_state in nouveau_vers_ancien_etat[i]: trans = Transition(state, _on, i) new_transitions.add(trans) resultat = Automate(Alphabet(self.alphabet.list), new_states, new_initial_state, new_final_states, list(new_transitions)) resultat.parents = self.parents.copy() resultat.nom = self.nom return resultat
self.creation.ui.createBtn.setState() self.automate.copie_automate(a) def view_diff(self): nom = self.automate.nom self.window.setWindowTitle(self.automate.nom + ' - Automata Brain') print(f'Nom actuel {nom}') for x in self.liste_automate: print(f"Nom {x}") if x == nom: self.liste_automate[x].copie_automate(self.automate) print( [self.liste_automate[a].etat_initial for a in self.liste_automate]) alphabet = Alphabet([]) a = Etat('a') b = Etat('b') c = Etat('c') t1 = Transition(a, '1', b) t2 = Transition(a, '1', a) t3 = Transition(a, '2', b) t4 = Transition(b, '1', b) automata = Automate(alphabet, [], None, [], []) automata.definir_nom('Sans Nom') liste_automate = {'Sans Nom': Automate.a_partir_de(automata)} def run_app(automate=automata): import sys