-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGRAMMAIRE_2.java
More file actions
211 lines (183 loc) · 6.67 KB
/
Copy pathGRAMMAIRE_2.java
File metadata and controls
211 lines (183 loc) · 6.67 KB
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
///EXO2///
import java.util.HashSet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Moore {
static private char[] alphabetIt;
static private EtatMoore[] colonnes;
public static Automate minimisation(Automate af) {
boolean premierAppel = true;
alphabetIt = new char[af.alphabet().size()];
colonnes = new EtatMoore[af.size()];
int compteur = 0;
for (Character c : af.alphabet()) alphabetIt[compteur++] = c.charValue();
compteur = 0;
for (Etat e : af) colonnes[compteur++] = new EtatMoore(e, alphabetIt.length);
if (Main.INFO) System.out.println("--- ETAPES MOORE ---");
do {
if (premierAppel) premierAppel = false;
else preparationEtape();
for (EtatMoore etape : colonnes) {
for (int j = 0; j < alphabetIt.length; j++) {
etape.transitions[j] = rechercheValeur(etape.etat, alphabetIt[j]);
}
}
bilan();
if (Main.INFO) print();
} while (!finMoore());
Automate minimale = new Automate();
HashSet<Integer> doublee = colonneDouble();
for (int i = 0; i < colonnes.length; i++) {
EtatMoore etape = colonnes[i];
if (!doublee.contains(Integer.valueOf(i))) {
minimale.ajouteEtatSeul(new Etat(etape.etat.isInit(), etape.etat.isTerm(), etape.bilan));
}
}
for (int k = 0; k < colonnes.length; k++) {
EtatMoore etape = colonnes[k];
if (!doublee.contains(Integer.valueOf(k))) {
for (int i = 0; i < alphabetIt.length; i++) {
if (etape.transitions[i] != -1) {
Etat etat = minimale.getEtat(etape.bilan);
etat.ajouteTransition(alphabetIt[i], minimale.getEtat(etape.transitions[i]));
}
}
}
}
return minimale;
}
private static HashSet<Integer> colonneDouble() {
HashSet<Integer> res = new HashSet<Integer>();
HashSet<Integer> index = new HashSet<Integer>();
for (int i = 0; i < colonnes.length; i++) {
if (!index.add(new Integer(colonnes[i].bilan))) {
res.add(new Integer(i));
}
}
return res;
}
private static void bilan() {
int compteur = 1;
boolean succes = false;
for (int i = 0; i < colonnes.length; i++) {
for (int j = 0; j < i; j++) {
if (bilanEgale(colonnes[i], colonnes[j])) {
colonnes[i].bilan = colonnes[j].bilan;
succes = true;
break;
}
}
if (!succes) colonnes[i].bilan = compteur++;
succes = false;
}
}
private static void preparationEtape() {
for (EtatMoore etape : colonnes) {
etape.motVide = etape.bilan;
}
}
private static boolean finMoore() {
for (EtatMoore etape : colonnes) {
if (etape.motVide != etape.bilan) return false;
}
return true;
}
private static boolean bilanEgale(EtatMoore a, EtatMoore b) {
if (a.motVide != b.motVide) return false;
for (int i = 0; i < a.transitions.length; i++) {
if (a.transitions[i] != b.transitions[i]) return false;
}
return true;
}
private static int rechercheValeur(Etat depart, char c) {
EnsEtat succ = depart.succ(c);
if (succ.isEmpty()) return -1;
Etat suivant = succ.iterator().next();
EtatMoore etapeDuSuivant = null;
for (EtatMoore etape : colonnes) {
if (etape.etat == suivant) {
etapeDuSuivant = etape;
break;
}
}
if (etapeDuSuivant == null) return -1;
return etapeDuSuivant.motVide;
}
private static void print() {
System.out.print(" ");
for (EtatMoore etape : colonnes) {
System.out.print(etape.etat.hashCode() + " ");
}
System.out.println();
System.out.print("1 ");
for (EtatMoore etape : colonnes) {
System.out.print(etape.motVide + " ");
}
System.out.println();
for (int i = 0; i < alphabetIt.length; i++) {
System.out.print(alphabetIt[i] + " ");
for (int j = 0; j < colonnes.length; j++) {
if (colonnes[j].transitions[i] == -1) System.out.print(colonnes[j].transitions[i] + " ");
else System.out.print(colonnes[j].transitions[i] + " ");
}
System.out.println();
}
System.out.print("=> ");
for (EtatMoore etape : colonnes) {
System.out.print(etape.bilan + " ");
}
System.out.println("\n");
}
}
class EtatMoore {
public Etat etat;
public int motVide;
public int bilan;
public int[] transitions;
public EtatMoore(Etat etat, int size) {
this.etat = etat;
this.motVide = (etat.isTerm()) ? 2 : 1;
this.bilan = 0;
this.transitions = new int[size];
}
}
public class Main {
public static void main(String[] args) {
try {
Automate afd = readAFDFromFile("input.txt");
Automate minimalAutomaton = Moore.minimisation(afd);
writeAutomateToFile("output.txt", minimalAutomaton);
} catch (IOException e) {
e.printStackTrace();
}
}
private static Automate readAFDFromFile(String filename) throws IOException {
Automate afd = new Automate();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(" ");
Etat source = new Etat(Integer.parseInt(parts[0]));
char symbol = parts[1].charAt(0);
Etat destination = new Etat(Integer.parseInt(parts[2]));
source.ajouteTransition(symbol, destination);
}
}
return afd;
}
private static void writeAutomateToFile(String filename, Automate automate) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
for (Etat etat : automate) {
for (char symbol : automate.alphabet()) {
EnsEtat successors = etat.succ(symbol);
if (!successors.isEmpty()) {
Etat destination = successors.iterator().next();
bw.write(etat.hashCode() + " " + symbol + " " + destination.hashCode() + "\n");
}
}
}
}
}