1 2 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
| import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class automate
{
private Vector new_transitions = new Vector();
private Vector e_traite = new Vector();
/**
* vecteur pour la sauvegarde des étas non encore traités dans la déterminisation .
* @see #determiniser()
*/
private Vector e_n_traite = new Vector();
/**
* Etat initial .
*/
private String etat_init="";
/**
* Ensemble d'alphabets .
*/
private String alph="";
/**
* Ensemble d'états finaux .
*/
private String etat_final;
/**
* Ensemble de nouveaux états finaux aprés déterminisation.
*/
private String nv_etf="";
/**
* Ensemble des etas resultantes en partatant d'un etat et un alphabet .
*/
private String edest="";
/**
* descrripteur du fichier de sauvegarde de l'automate déterministe.
*/
private PrintWriter dest;
/**
* Séparateur Systeme .
*/
private String separateur = System.getProperty("file.separator");
/**
* Répertoire courant .
*/
private String rep_courant = System.getProperty("user.dir");
/**
* Fichier de sauvegarde de l'automate déterministe.
*/
private String f_dest;
/**
* Construit un nouveau automate .
* @param etat_init etat initial .
* @param alph ensemble d'alphabets .
* @param etat_final etat(s) finaux .
* @param f_dest représente ou l'automate(apres determination) va etre enregistré .
* @exception IOException si input/output declanche une exeption .
*/
public automate(String etat_init,String alph,String etat_final,String f_dest)
{
try{
this.etat_init=etat_init;
this.alph=alph;
this.etat_final=etat_final;
e_n_traite.addElement(etat_init);
this.f_dest=f_dest;
dest = new PrintWriter(new FileWriter(f_dest));
determiniser();
}
catch ( IOException e )
{
System.out.println(e.getMessage());
}
}
/**
* Construit un nouveau automate .
* @param nom_fich le fichier ou on va extraire les caractéristique de l'automate .
* @exception IOException si input/output declanche une exeption .
* @exception NullPointerException si la structure du fichier ne respecte pas la strucure automate .
*/
public automate(String nom_fich)
{
try {
BufferedReader entree = new BufferedReader(new FileReader(nom_fich));
this.f_dest=nom_fich;
String ligneLue = null;
while(true)
{
ligneLue = entree.readLine();
if((ligneLue == null)||(ligneLue.charAt(0)=='#')) break;
}
ligneLue = ligneLue.substring(1);
StringTokenizer tok = new StringTokenizer(ligneLue,":");
tok.nextToken();
alph=tok.nextToken();
etat_init=tok.nextToken();
nv_etf=tok.nextToken();
}
catch ( IOException e )
{
System.out.println(e.getMessage());
}
catch (NullPointerException e)
{
}
}
/**
* Déterminiser l'automate .
*/
public void determiniser() throws IOException
{
String trans;
String etat="";
Vector etf = new Vector();
while(e_n_traite.isEmpty()==false)
{
StringTokenizer alphabet = new StringTokenizer(alph,",");
int nb = alphabet.countTokens();
for(int i=0;i<nb;i++)
{
trans= alphabet.nextToken();
etat=(String)e_n_traite.firstElement();
StringTokenizer etats = new StringTokenizer(etat,",");
int nbr = etats.countTokens();
for(int j=0;j<nbr;j++)
{
String etati=etats.nextToken();
if(contient2(etat_final,etati)==true&& i==0)
{
if (nv_etf.equals("")){
etf.addElement(etat);
nv_etf=nv_etf+etat;
}
else if (contains(etf,etat)==false){
etf.addElement(etat);
nv_etf=nv_etf+";"+etat;
}
}
seek_efinal(etati,trans);
}
if(edest.equals("")){
new_transitions.addElement(etat+":"+trans+":q");
dest.println(etat+":"+trans+":q");
}
else{
new_transitions.addElement(etat+":"+trans+":"+edest);
dest.println(etat+":"+trans+":"+edest);
}
if(edest.equals("")==false)
if( (contains(e_traite,edest)==false) && (contains(e_n_traite,edest)== false))
e_n_traite.addElement(edest);
edest="";
}
e_traite.addElement(etat);
e_n_traite.removeElement(etat);
}
String et="";
for(int i=0;i<e_traite.size();i++)
if(i==0)
et=et+e_traite.elementAt(i);
else
et=et+";"+e_traite.elementAt(i);
dest.println("#"+et+":"+alph+":"+etat_init+":"+nv_etf);
dest.close();
new File(System.getProperty("user.dir")+System.getProperty("file.separator")+"temp.tmp").delete();
}
/**
* Recherche les etat resultant à partir d'un états initial aprés lecture d'un alphabet dans le nouveau automate .
* @param etati etat de départ .
* @param trans l'alphabet à utiliser .
*/
private void seek_efinal(String etati,String trans) throws IOException
{
BufferedReader entree = new BufferedReader(new FileReader(System.getProperty("user.dir")+System.getProperty("file.separator")+"temp.tmp"));
while(true)
{
String ligneLue = entree.readLine();
if(ligneLue == null) break;
StringTokenizer tok = new StringTokenizer(ligneLue,":");
if (etati.equals(tok.nextToken()) && trans.equals(tok.nextToken()))
if (edest.equals("")){
edest=edest+tok.nextToken();}
else
{
String dest=tok.nextToken();
if(edest.indexOf(dest)==-1)
edest=edest+","+dest;
}
}
entree.close();
}
/**
* Détermine l'appartenance de etat dans l'ensemble des etats dans le vecteur etf .
* @param etf vecteur qui contient des elements de type String .
* @param etat l'état à rechercher l'appartenance dans le vecteur etat .
* @return true si etat appartient à etf , false sinon
*/
private boolean contains(Vector etf,String etat){
for (int i=0 ;i<etf.size();i++)
{
if (contient((String)etf.elementAt(i),etat) == true)
return true;
}
return false;
}
private boolean appartient(String et_f,String etat_f)
{
StringTokenizer tok = new StringTokenizer(etat_f,";");
int nb = tok.countTokens();
for(int i=0;i<nb;i++)
if(contient(et_f,tok.nextToken()))
return true;
return false;
}
/**
* @return le fichier correspondant à l'automate.
* @see #automate(String)
*/
public void setFdest(String f_dest){
try{
this.f_dest=f_dest;
dest = new PrintWriter(new FileWriter(f_dest));
e_n_traite.addElement(etat_init);
}
catch ( IOException e )
{
System.out.println(e.getMessage());
}
}
public String getFdest(){
return f_dest;
}
public String getEtatInitial(){
return etat_init;
}
public void setEtatInitial(String etat_i){
this.etat_init=etat_i;
}
public String getEtatFinal(){
return etat_final;
}
public String getEtatsfinaux(){
return nv_etf;
}
public void setEtatFinal(String etat_final){
this.etat_final=etat_final;
}
public void setAlphabets(String alph){
this.alph=alph;
}
public Vector getEtats(){
return e_traite;
}
public Vector getNewTransitions(){
return new_transitions;
} |
Partager