-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiniteAutomata.java
More file actions
567 lines (499 loc) · 13.6 KB
/
FiniteAutomata.java
File metadata and controls
567 lines (499 loc) · 13.6 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/**
** Pontifícia Universidade Catolica de Minas Gerais
** Fundamentos Teoricos da Computacao - Ciencia da Computacao - ICEI
** Trabalho Pratico 01 - Professor Zenilton
** Alunos: Joao Paulo de Castro Bento Pereira
** Paulo Junio Reis Rodrigues
**Objetivo:
** Construir um conversor/Parser de Automato Finito Nao Deterministico para
** um Automato Finito Deterministico a ser tState no programa JFLAP V7.0
**
***/
import java.util.List;
import java.util.Queue;
import javax.lang.model.util.ElementScanner6;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
class State{
String name;
int id;
double x; /* x Position ( Standard 0.0 ) */
double y; /* y Position ( Standard 0.0 ) */
char type; /* C = Commom, I = Initial, F = Final */
/**
* Standard Constructor
*/
public State(){
this.name = "q";
this.id = 99;
this.x = 0.0;
this.y = 0.0;
this.type = 'c';
}
/**
* Alternative Constructor
*/
public State(String name, int id, double x, double y, char type){
this.name = name;
this.id = id;
this.x = x;
this.y = y;
this.type = type;
}
/*Getters and setters*/
public String getName(){return this.name;}
public int getId(){return this.id;}
public double getX(){return this.x;}
public double getY(){return this.y;}
public char getType(){return this.type;}
public void setName(String name){this.name=name;}
public void setId(int id){this.id=id;}
public void setX(double x){this.x=x;}
public void setY(double y){this.y=y;}
public void setType(char type){this.type=type;}
public String toString(){
return "State: " + this.name + "\n" +
" id: " + this.id + "\n" +
" x: " + this.x + "\n" +
" y: " + this.y + "\n" +
" Type: " + this.type + "\n";
}
public String toXML(){
if (this.type == 'I'){
return "\n\t\t<state id=\"" + this.id + "\"" + " name=\"" + this.name + "\"" + ">\n\t\t\t<x>" + this.x + "</x>\n\t\t\t<y>" + this.y + "</y>\n\t\t\t<initial/>\n\t\t</state>";
} else if (this.type == 'F'){
return "\n\t\t<state id=\"" + this.id + "\"" + " name=\"" + this.name + "\"" + ">\n\t\t\t<x>" + this.x + "</x>\n\t\t\t<y>" + this.y + "</y>\n\t\t\t<final/>\n\t\t</state>";
}else if(this.type == 'S'){
return "\n\t\t<state id=\"" + this.id + "\"" + " name=\"" + this.name + "\"" + ">\n\t\t\t<x>" + this.x + "</x>\n\t\t\t<y>" + this.y + "</y>\n\t\t\t<initial/>\n\t\t<final/>\n\t\t</state>";
}else{
return "\n\t\t<state id=\"" + this.id + "\"" + " name=\"" + this.name + "\"" + ">\n\t\t\t<x>" + this.x + "</x>\n\t\t\t<y>" + this.y + "</y>\n\t\t</state>";
}
}
}
class Transition{
State from; /* Current State*/
State to; /* Destination State */
char read; /* Input */
/**
* Standard Constructor
*/
public Transition(){
this.from = null;
this.to = null;
this.read = '9';
}
/**
* Alternative Constructor
*/
public Transition(State from, State to, char read){
this.from = from;
this.to = to;
this.read = read;
}
/* Getters and Setters */
public State getFrom(){return this.from;}
public State getTo(){return this.to;}
public char getRead(){return this.read;}
public void setFrom(State from){this.from = from;}
public void setTo(State to){this.to = to;}
public void setRead(char read){this.read = read;}
public String toString(){
return "Transition from " + this.from.getId() + " to " + this.to.getId() + " when input = " + this.read + "\n";
}
public String toXML(){
return "\n\t\t<transition>\n\t\t\t<from>" + this.from.getId() + "</from>\n\t\t\t<to>" + this.to.getId() + "</to>\n\t\t\t<read>" + this.read + "</read>\n\t\t</transition>";
}
}
class FiniteAutomata{
List<State> states; /*List of States*/
List<Transition> transitions; /*List of transitions*/
String alphabet;
String[][] transitionTable;
String type; /* FA = Standard, NFA = Nondeterministic Finite Automata, DFA = Deterministic Finite Automata */
List<String> statesNames;
int countStates;
/* Service functions */
/**
* Standard Constructor
*/
public FiniteAutomata(){
this.states = new ArrayList<>();
this.transitions = new ArrayList<>();
this.type = "NFA";
this.alphabet = "";
this.countStates = 0;
}
/**
* Alternative Constructor
* @param List<State> states - all automata states
* @param List<Transition> transitions - all automata transitions
* @param String type - automata type
* @param String alphabet - automata's alphabet
*/
public FiniteAutomata(List<State> states, List<Transition> transitions, String type, String alphabet){
this.states = states;
this.transitions = transitions;
this.type = type;
this.alphabet = alphabet;
this.countStates = 0;
}
/**
* Function to return the last id inside the list
*/
public int getLastId(){
return this.states.get(states.size()).id;
}
/**
* Function to generate the alphabet based on transitions
*/
public void generateAlphabet(){
for(Transition t : transitions){
if(!(this.alphabet.contains(t.read+"")))
alphabet += t.read;
}
}
/**
* State getter
* @param String id
*/
public State getState(String name){
for(State e : states)
if(e.getName().equals(name))
return e;
return null;
}
/**
* Getter to State
* @param char type
*/
public State getState(char type){
for(State e : states)
if(e.getType() == type)
return e;
return null;
}
/**
* State getter
* @param int id
*/
public State getState(int id){
for(State e : states)
if(e.getId() == id)
return e;
return null;
}
/**
* Getter of Initial States Only
*/
public List<State> getInitialStates(){
List<State> initials = new ArrayList<State>();
for(State e : states){
if(e.getType() == 'I' || e.getType() == 'S'){
initials.add(e);
System.out.println(e);
}
}
return initials;
}
/**
* Function to return all final states
*/
public List<State> getFinalStates(){
List<State> finals = new ArrayList<State>();
for(State e : states){
if(e.getType() == 'F' || e.getType() == 'S'){
finals.add(e);
}
}
return finals;
}
/**
* Alphabet Getter
*/
public String getAlphabet(){
if(this.alphabet.equals("")){
this.generateAlphabet();
return this.alphabet;
}else{
return this.alphabet;
}
}
/**
* Function to return the index of a letter in the alphabet
* @param char c - index
*/
public int getCharOnAlphabet(char c){
for(int i = 0; i < alphabet.length(); i++)
if(alphabet.charAt(i) == c)
return i;
return -1;
}
/**
* Transition Getter
* @param State s
*/
Transition getTransition(State s){
for(Transition t : transitions){
if(t.from.name.equals(s.name))
return t;
}
return null;
}
/**
* Transition Getter
* @param State s
* @param char c
*/
Transition getTransition(State s, char c){
for(Transition t :transitions){
if(t.from.name.equals(s.name) && t.read == c)
return t;
}
return null;
}
State getNextState(State s, char c){
for (Transition t : transitions){
if(t.from.name.equals(s.name) && t.read == c){
return t.to;
}
}
return null;
}
/**
* Function that returns the name of a state by the transition and previous state
* @param State s - State that points to returned state
* @param char c - what needs to be read
*/
String getNameOfTransitionState(State s, char c){
String name = "";
for (Transition t : transitions){
if(t.from.name.equals(s.name) && t.read == c){
name += "," + t.to.name;
}
}
// correcting the string name
if(!name.equals("") && name.charAt(0)==',')
name = name.substring(1, (name.length()));
return name;
}
/**
* Method to generate the transition table
*/
void createTransitionTable(){
//creating table of states/simbols
this.transitionTable = new String[this.states.size()][this.alphabet.length()];
for(int i = 0; i < this.states.size(); i++){
for(int j = 0; j < this.alphabet.length(); j++){
this.transitionTable[i][j] = getNameOfTransitionState(states.get(i), alphabet.charAt(j));
}
}
}
/**
* TransitionTable Getter
*/
String[][] getTransitionTable(){
if(this.transitionTable == null){
this.createTransitionTable();
return this.transitionTable;
}else{
return this.transitionTable;
}
}
/**
* Setter for Type
*/
public void setType(String type){
this.type = type;
}
public Boolean isDeterministic(){
int countOfTransitions = 0;
for(int i = 0; i < alphabet.length(); i++){
char c = alphabet.charAt(i);
for(State s : states){
for(Transition t : transitions){
if(t.from.name.equals(s.name) && t.read == c && countOfTransitions < 2){
countOfTransitions++;
if(countOfTransitions >= 2){
this.type = "NFA";
return false;
}
}
}
countOfTransitions = 0;
}
}
this.type = "DFA";
return true;
}
public String fixName(String name){
if(!name.equals("")){
if(name.charAt(0) == ','){
name = name.substring(1, name.length());
}
if(name.charAt(name.length()-1) == ','){
name = name.substring(0,name.length()-1);
}
}
return name;
}
public boolean hasState(String name){
for(int i = 0; i < states.size(); i++){
State tmp = states.get(i);
if(tmp.name.equals(name)){
return true;
}
}
return false;
}
/* end of service area */
/**
* Function to return the name of the next State Name
* @param
*/
public String nextState(State s, char c){
//state name can be : "0", "0,1", "0,1,2"...
String[] strSplit = s.name.split(",");
String name = "";
for(int i = 0; i < strSplit.length; i++){
if(!strSplit[i].equals("")){
int a = Integer.parseInt(strSplit[i]);
int b = this.getCharOnAlphabet(c);
name += "," + this.transitionTable[a][b];
}
}
return this.fixName(name);
}
/**
* Function to convert a Non deterministic finite automata into a Deterministic one
*/
public FiniteAutomata convert( ){
//Tests if its aready a Deterministic Finite Automata
if(this.type.equals("NFA")){
List<State> new_f = this.getFinalStates(); // List of final States of the NFA
List<State> new_i = this.getInitialStates(); // List of Initial States of the NFA
List<Transition> new_t = new ArrayList<Transition>(); // List of new Transitions
FiniteAutomata dfa = new FiniteAutomata(new_i, new_t, "DFA", this.getAlphabet());
int size = dfa.states.size(); // control variable for looping trought convertion
// for each state
for(int k = 0; k < size; k++ ){
State x = dfa.states.get(k);
//for each char on alphabet
for(int i = 0; i < dfa.alphabet.length(); i++){
char a = dfa.alphabet.charAt(i);
// passing actual state to recieve the new state transiction
String name = nextState(x, a);
State y;
if(dfa.hasState(name) == false){
// avoiding duplicated states for not having a error state
// if(name.charAt(0) != ','){
char type = 'c';
//Testing if a final state
for(State e : new_f){
String[] tmp = name.split(",");
for(int p = 0; p < tmp.length; p++){
if(e.name.contains(tmp[p]))
type = 'F';
}
}
y = new State(name, dfa.states.size(), 0.0, 0.0, type);
dfa.states.add(y);
Transition t = new Transition(x, y, a);
dfa.transitions.add(t);
// }
}else{
y = dfa.getState(name);
Transition t = new Transition(x, y, a);
dfa.transitions.add(t);
}
}// for letra em alfabeto
size = dfa.states.size();
}// for State X : E'
return dfa;
}else{
System.out.println("Already a DFA");
return this;
}
}
/**
* Function to test if a given word belongs to this automata
* @param String word - input word
* @return Boolean
*/
public Boolean simulate(String word){
Boolean belongs = true;
Queue<Character> input = new LinkedList<Character>();
State s = this.states.get(0);
if(s.getType()!='I' && s.getType() != 'S'){
s = this.getState('I');
}
char[] carr = word.toCharArray();
for(char c : carr)
input.add(c);
while(input.isEmpty() == false && s != null){
s = this.getNextState(s, input.poll());
}
if(input.isEmpty() && s != null){
if(s.getType() == 'F' || s.getType() == 'S'){
System.out.println("Palavra pertence!");
belongs = true;
}else{
System.out.println("Palavra não pertence!");
belongs = false;
}
}else if(input.isEmpty() == false && s == null){
System.out.println("Palavra não pertence!");
belongs = false;
}else if (input.isEmpty() && s == null){
System.out.println("Palavra não pertence!");
belongs = false;
}
return belongs;
}
/* Print Area */
/**
* Method to print the Automata Settings
*/
public void printAutomata(){
System.out.println("Alphabet: " + this.alphabet );
System.out.println("States: ");
for(State s : states){
System.out.println(s);
}
System.out.println("Transitions: ");
for(Transition t : transitions){
System.out.println(t);
}
}
/**
* Method to print the Transition Table
*/
public void printTransTable(){
if(transitionTable == null){
this.getTransitionTable();
}
System.out.println("Tabela de transicao: ");
for( int i = 0; i < states.size(); i++){
for(int j = 0; j < alphabet.length(); j++){
System.out.print(transitionTable[i][j]+ " ");
}
System.out.println();
}
}
public String toXML(){
String answer = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><!--Created with JFLAP 6.4.--><structure>\n\t<type>fa</type>\n\t<automaton>\n\t\t";
for(State s : states){
answer += s.toXML();
}
for(Transition t : transitions){
answer += t.toXML();
}
answer += "\n\t</automaton>\n</structure>";
return answer;
}
}