-
Notifications
You must be signed in to change notification settings - Fork 0
/
Accepter.java
365 lines (342 loc) · 13.8 KB
/
Accepter.java
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
* @author Amirhossein Alibakhshi (id: 9731096)
*/
public class Accepter {
//fields:
private ArrayList<State> accepterStates;
private ArrayList<String> accepterAlphabet;
private ArrayList<Path> accepterPaths;
//constructor:
/**
* user input constructor
* - alphabets
* - states
* - start state
* - final state(s)
* - paths
*/
public Accepter(){
Scanner sc = new Scanner(System.in);
//alphabets
System.out.println("ENTER THE ALPHABETS:");
accepterAlphabet = new ArrayList<>(separateStringBySpace(sc.nextLine()));
//states
System.out.println("ENTER THE STATES:");
ArrayList<String> stateTitles = new ArrayList<>(separateStringBySpace(sc.nextLine()));
accepterStates = new ArrayList<>();
for (String title : stateTitles){
accepterStates.add(new State(title));
}
//start state
System.out.println("ENTER THE START STATE:" );
String startStateTitle = sc.nextLine().trim();
for (State state : accepterStates){
if(state.getTitle().equals(startStateTitle)){
state.setStarsState(true);
}
}
//final states
System.out.println("ENTER THE FINAL STATE(S):" );
ArrayList<String> finalStatesTitle = new ArrayList<>(separateStringBySpace(sc.nextLine().trim()));
for (State state : accepterStates){
if(finalStatesTitle.contains(state.getTitle())){
state.setFinalState(true);
}
}
//paths
System.out.println("ENTER THE PATHS:\n(USE THIS FORMAT: START_STATE ALPHABET END_STATE)");
accepterPaths = new ArrayList<>();
while (true){
ArrayList<String> pathInputString = new ArrayList<>(separateStringBySpace(sc.nextLine()));
if (pathInputString.size() == 3){
Path path = new Path(pathInputString.get(0), pathInputString.get(1), pathInputString.get(2));
accepterPaths.add(path);
}else{
break;
}
}
}
/**
* default constructor
* a b
* Q0 Q1 Q2
* Q0
* Q1
* Q0 a Q1
* Q0 b Q1
* Q1 a Q2
* Q1 b Q2
* Q2 a Q2
* Q2 b Q2
*/
public Accepter(boolean bool){
//alphabets
accepterAlphabet = new ArrayList<>();
accepterAlphabet.add("a");
accepterAlphabet.add("b");
//states
accepterStates = new ArrayList<>();
accepterStates.add(new State("Q0"));
accepterStates.add(new State("Q1"));
accepterStates.add(new State("Q2"));
//start state
for (State state : accepterStates){
if(state.getTitle().equals("Q0")){
state.setStarsState(true);
}
}
//final states
ArrayList<String> finalStatesTitle = new ArrayList<>();
finalStatesTitle.add("Q1");
for (State state : accepterStates){
if(finalStatesTitle.contains(state.getTitle())){
state.setFinalState(true);
}
}
//paths
accepterPaths = new ArrayList<>();
accepterPaths.add(new Path("Q0", "a" , "Q1"));
accepterPaths.add(new Path("Q0", "b" , "Q1"));
accepterPaths.add(new Path("Q1", "a" , "Q2"));
accepterPaths.add(new Path("Q1", "b" , "Q2"));
accepterPaths.add(new Path("Q2", "a" , "Q2"));
accepterPaths.add(new Path("Q2", "b" , "Q2"));
}
//constructor:
/**
*txt constructor
*/
public Accepter(String filePath) throws FileNotFoundException {
File file = new File(filePath);
Scanner sc = new Scanner(file);
accepterPaths = new ArrayList<>();
System.out.println("TEXT IN "+filePath+".txt :");
while (sc.hasNextLine())
System.out.println(sc.nextLine());
System.out.println("");
sc = new Scanner(file);
//a counter to detect which line is scanning:
int lineNumber = 1;
while (sc.hasNextLine()) {
switch (lineNumber){
case 1:
// reading alphabets
System.out.println("READING ALPHABETS:");
accepterAlphabet = new ArrayList<>(separateStringBySpace(sc.nextLine()));
// System.out.println(accepterAlphabet);
for (String s : accepterAlphabet){
System.out.print(s + " ");
}
System.out.println("");
break;
case 2:
// reading states
System.out.println("READING STATES:");
ArrayList<String> stateTitles = new ArrayList<>(separateStringBySpace(sc.nextLine()));
accepterStates = new ArrayList<>();
for (String title : stateTitles){
accepterStates.add(new State(title));
}
for (State s : accepterStates){
System.out.print(s.getTitle() + " ");
}
System.out.println("");
// System.out.println(accepterStates);
break;
case 3:
// start state
System.out.println("READING THE START STATE:" );
String startStateTitle = sc.nextLine().trim();
for (State state : accepterStates){
if(state.getTitle().equals(startStateTitle)){
state.setStarsState(true);
}
}
System.out.println(getStartState().getTitle());
break;
case 4:
// final states
System.out.println("READING THE FINAL STATE(S):" );
ArrayList<String> finalStatesTitle = new ArrayList<>(separateStringBySpace(sc.nextLine().trim()));
for (State state : accepterStates){
if(finalStatesTitle.contains(state.getTitle())){
state.setFinalState(true);
}
}
for (State s : getFinalStates()){
System.out.print(s.getTitle() + " ");
}
System.out.println("");
// System.out.println(getFinalStates());
break;
default:
// paths:
System.out.println("READING A PATH");
ArrayList<String> pathInputString = new ArrayList<>(separateStringBySpace(sc.nextLine()));
Path path = new Path(pathInputString.get(0), pathInputString.get(1), pathInputString.get(2));
accepterPaths.add(path);
System.out.println(path.getStartState() + " " + path.getInputAlphabet() + " " + path.getEndState() + " ");
// System.out.println(pathInputString);
break;
}
// System.out.println(sc.nextLine());
lineNumber++;
}
}
//finding the start state:
public State getStartState(){
for (State state : this.accepterStates){
if (state.isStarsState()){
return state;
}
}
System.out.println("NO START STATE HAS BEEN DEFINED!");
return null;
}
//finding final state(s):
public ArrayList<State> getFinalStates(){
ArrayList<State> finalStates = new ArrayList<>();
for (State state : this.accepterStates){
if (state.isFinalState()){
finalStates.add(state);
}
}
return finalStates;
}
//change a input string to a string array list (separated by state)
public List<String> separateStringBySpace (String input){
String inputString = input;
String[] substrings = inputString.split(" ");
List<String> result = Arrays.asList(substrings);
return result;
}
//get accepter's status - alphabets, states (normal, start and final) and paths:
public void getAcceptorsStatus(){
System.out.println("---------------------------------------");
System.out.println(">>> ALPHABETS:");
for (String alphabet : accepterAlphabet){
System.out.println(" >>> " + alphabet);
}
System.out.println("---------------------------------------");
System.out.println(">>> ALL OF THE STATES:");
for (State state : accepterStates){
System.out.println(" >>> " + state.getTitle());
}
System.out.println("---------------------------------------");
System.out.println(">>> START STATES: ");
System.out.println(" >>> " + getStartState().getTitle());
System.out.println("---------------------------------------");
System.out.println(">>> FINAL STATES:");
for (State state : getFinalStates()){
System.out.println(" >>> " + state.getTitle());
}
System.out.println("---------------------------------------");
System.out.println(">>> PATHS:");
for (Path path : accepterPaths){
System.out.println(" >>> " + path);
}
}
//finding a path with its start state and input alphabet:
public Path findPathByInitialStateAndInputAlphabet (String initialState , String input){
for (Path p : accepterPaths){
if ( p.getStartState().equals(initialState) && p.getInputAlphabet().equals(input)){
return p;
}
}
return null;
}
//getting a state with it's title:
public State getStateByTitle (String stateTitle) {
for ( State s : accepterStates){
if (s.getTitle().equals(stateTitle)){
return s;
}
}
return null;
}
//finding out if the user input is valid for this accepter
public void isTheInputStringValid(String inputString){
System.out.println("---------------------------------------");
System.out.println("INPUT STRING: \" " + inputString + " \"");
System.out.println("( 1 = SUCCESSFUL STEP , 0 = FAILED)");
boolean[] inputStatus = new boolean[inputString.length()];
String currentState = getStartState().getTitle();
String[] inputArray = inputString.split("");
String currentAlphabet = inputArray[0];
Path currentPath;
int index = 0 ;
for (int i = 0 ; i < inputString.length() ; i++){
try {
index = i;
currentAlphabet = inputArray[i];
currentPath = findPathByInitialStateAndInputAlphabet(currentState, currentAlphabet);
if (currentPath.toString()!=null){
System.out.println(currentPath);
inputStatus[index] = true;
}
// System.out.println((getStateByTitle(currentPath.getEndState()).isFinalState()));
currentState = currentPath.getEndState();
printTheInputStringStatus(inputString, inputStatus , index);
} catch (Exception e){
inputStatus[index] = false;
System.out.println("ERROR FOUND WHILE READING INPUT \"" + currentAlphabet + "\"!");
printTheInputStringStatus(inputString, inputStatus, index);
System.out.println("THIS STRING IS NOT VALID!!!!");
return;
}
}
System.out.println("THE STRING WAS DETECTED SUCCESSFULLY!");
System.out.println("CURRENT STATE: ( " + currentState + " )");
if (getStateByTitle(currentState).isFinalState()){
System.out.println("( "+ currentState +" ) IS A FINAL STATE, SO THE\nSTRING \"" + inputString +"\" IS VALID.");
}else{
System.out.println("( "+ currentState +" ) IS NOT A FINAL STATE, SO THE\nSTRING \"" + inputString +"\" ISN'T VALID.");
}
}
//showing the user's input string in each step of checking:
public void printTheInputStringStatus(String input , boolean[] status , int index){
String[] inputArray = input.split("");
System.out.println("");
for ( int i = 0 ; i < input.length() ; i++ ){
System.out.print("| " + inputArray[i] + " ");
}
System.out.println("|");
for ( int i = 0 ; i < input.length() ; i++ ){
if (i <= index){
if (status[i]){
System.out.print("| 1 ");
}else{
System.out.print("| 0 ");
}
}else{
System.out.print("| - ");
}
}
System.out.println("|\n");
}
//setters and getters:
public ArrayList<State> getAccepterStates() {
return accepterStates;
}
public void setAccepterStates(ArrayList<State> accepterStates) {
this.accepterStates = accepterStates;
}
public ArrayList<String> getAccepterAlphabet() {
return accepterAlphabet;
}
public void setAccepterAlphabet(ArrayList<String> accepterAlphabet) {
this.accepterAlphabet = accepterAlphabet;
}
public ArrayList<Path> getAccepterPaths() {
return accepterPaths;
}
public void setAccepterPaths(ArrayList<Path> accepterPaths) {
this.accepterPaths = accepterPaths;
}
}