| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 
 |  
import java.io.BufferedReader;
import java.io.FileReader;
 
class Automate{
 
    private Etat[] tabEtat;
 
 
    public Automate(){
        tabEtat = new Etat[0];
    }
 
    public Automate(Etat[] tab){
        tabEtat= tab;
    }
 
    public Automate(String MonAutomate){
        try{
            BufferedReader entree = new BufferedReader(new FileReader(MonAutomate));
            String ligne = entree.readLine(); // lit premiere ligne du fichier MonAutomate.txt
            String[] tab = ligne.split(" ");	// creer un tableau tab en divisant la ligne
 
            this.tabEtat = new Etat[Integer.parseInt(tab[0])];
            for(int i = 0 ; i < this.tabEtat.length ; i++){
                this.tabEtat[i] = new Etat();
            }
            Etat temp;			// Etat = temp
            Transition tmp;		// Transition = tmp
            ligne = entree.readLine();		// ligne = premiere ligne lue 
            while(ligne!=null){			// a condition que la ligne ne soit pas vide
                tab = ligne.split(" ");		// on divise String ligne en un tableau tab le tout séparé par le caractere " "
                if(tab[0].equals("initial"))	// si le string a la position 0 du tableau tab est equivalent a un String d'etat initial
                    this.tabEtat[Integer.parseInt(tab[1])].initial = true;		//
                if(tab[0].equals("terminal"))
                    this.tabEtat[Integer.parseInt(tab[1])-1].terminal = true;
                if(tab.length==3){													// si dimension = 3
                    temp = tabEtat[Integer.parseInt(tab[0])];						// etat reviens a position 0
                    tmp = new Transition(tabEtat[Integer.parseInt(tab[2])], tab[1].substring(0)); // transition devient nouvelle transition 
                    temp.ajoutTrans(tmp);
                }
                ligne = entree.readLine();		// si ligne vide on lit la suivante
            }
            entree.close();
        } catch (Exception e){
            System.out.println("Erreur  le lecture : " +e.getMessage());
            e.printStackTrace();
            /*System.out.println("Probleme.  Causes possibles :\n "
                    + "  - ouverture du fichier\n"
                    + "  - Votres fichier est-il au bon format ? \n"
                    + "  (Existe-t-il ?  Etes-vous dans le bon repertoire ?)\n"
                    + "  - lecture\n  (Est-il au bon format ?)\n"
                    + "  - fermeture (??)\n");*/
            System.exit(1);
 
        }
 
        //Temporairement on "chargera" la liste des états directement dans le code
        Etat e0 = new Etat();
        Etat e1 = new Etat();
        Etat e2 = new Etat();
        Etat e3 = new Etat();
        Etat e4 = new Etat();
        Etat e5 = new Etat();
 
        e0.initial = true;
        e5.terminal = true;
 
        e0.ajoutTrans(new Transition(e1, "Le lion "));
        e0.ajoutTrans(new Transition(e1, "Le leopard "));
        e0.ajoutTrans(new Transition(e1, "Le buffle "));
        e0.ajoutTrans(new Transition(e1, "Le zebre "));
        e0.ajoutTrans(new Transition(e1, "La mouche "));
        e0.ajoutTrans(new Transition(e1, "La fleur "));
        e1.ajoutTrans(new Transition(e2, " mange "));
        e1.ajoutTrans(new Transition(e2, " boit "));
        e1.ajoutTrans(new Transition(e2, " dort "));
        e2.ajoutTrans(new Transition(e3, " de la viande "));
        e2.ajoutTrans(new Transition(e3, " de l' eau "));
        e2.ajoutTrans(new Transition(e3, " de l' herbe "));
        e2.ajoutTrans(new Transition(e3, "  "));
        e3.ajoutTrans(new Transition(e4, " le matin "));
        e3.ajoutTrans(new Transition(e4, " le midi "));
        e3.ajoutTrans(new Transition(e4, " le soir "));
        e3.ajoutTrans(new Transition(e4, "  "));
        e4.ajoutTrans(new Transition(e5, " dans la foret."));
        e4.ajoutTrans(new Transition(e5, " au bord du lac."));
        e4.ajoutTrans(new Transition(e5, " dans la savane."));
        e4.ajoutTrans(new Transition(e5, " "));
 
        tabEtat[0] = e0;
        tabEtat[1] = e1;
        tabEtat[2] = e2;
        tabEtat[3] = e3;
        tabEtat[4] = e4;
        tabEtat[5] = e5;
    }
 
 
 
    Etat etat(int i){
        return tabEtat[i];
    }
 
    /**
     *
     * @param e
     * @return un entier correspondant  l'indice de l'etat e dans le tableau
     */
    int indiceEtat(Etat e){
        if(tabEtat[0] == null) return -1;
        for(int i= 0; i<tabEtat.length; i++){
            if(tabEtat[i]==e) return i;
        }
        return -1;
    }
 
    void ajoutEtat(Etat etat){
        if(tabEtat.length == 0){
            Etat[] temp= {etat};
            tabEtat= temp;
        }
 
        else{
            Etat[] newTab= new Etat[tabEtat.length+1];
            for(int i=0; i<tabEtat.length; i++)
                newTab[i]= tabEtat[i];
            newTab[tabEtat.length]= etat;
            tabEtat= newTab;
        }
    }
 
    void ajoutTrans(Etat ei,Etat ef, String groupe){
        ei.ajoutTrans(new Transition(ef,groupe));
    }
 
    public Etat etatInit(){						// etat initial
        if(tabEtat[0] == null) return null;		// si il n y a rien a l ' etat 0 on ne renvoie rien 
        for( int i= 0; i<tabEtat.length; i++){			// pour i=0 et < longueur tableau d etats alors on incremente jusqu a lgth
            if(tabEtat[i].initial) return tabEtat[i]; // si 
        }
        return null;
    }
 
    /**
     *
     * @param mot
     * @return true si la chaine @mot est reconnue par l'automate
     */
    public boolean estReconnu(String groupe){
        Etat etatCourant=etatInit();
        for(int i=0;i<groupe.length();i++){
            if(etatCourant.cible(groupe.substring(i))!= null){
                //System.out.print(indiceEtat(etatCourant) + " + " + mot.charAt(i) + " --> ");
                etatCourant=etatCourant.cible(groupe.substring(i));
                //System.out.println(indiceEtat(etatCourant));
            } else{
                //System.out.println(indiceEtat(etatCourant) + " + " + mot.charAt(i) + " --> pas de transition pour cette lettre");
                return false;
            }
        }
        if(etatCourant.terminal){
            return true;
        }
        return false;
    }
 
    public boolean verifier(String phrase) {
 
    	for(int i = 0; i<tabEtat.length; i++) { 
    		Etat e = tabEtat[i];
    		System.out.print(e.initial+" ");
    		System.out.print(e.terminal+" ");
    		ListeTransition l = e.listTrans;
    		System.out.println(l);
    	}
 
    	boolean isValide = false;		// n est pas Valide
    	String[] tab = phrase.split("@");	// tab recoit la phrase saisie splité par le caractere @
    	Etat tmp = this.etatInit();			// 
    	for (int i = 0; i<tab.length; i++) {	// pour i = 0 et inferieur a la longueur du tableau
    		String groupe = tab[i];
    		if (tmp.existeTrans(groupe)) {
    			tmp = tmp.cible(groupe);
    		}
    		isValide = false;
    	}
    	if (tmp.terminal) {
    		isValide = true;
    	}
    	return isValide;
    }
 
} | 
Partager