-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainController.java
315 lines (268 loc) · 11.1 KB
/
MainController.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
package Project2;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextArea;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.util.*;
public class MainController implements Initializable {
boolean flag = false;
Grammar grammar;
Grammar PDAGrammar;
private File selectedFile = null;
File greibachFile = new File("src/Project2/log/Greibach.txt");
File chomskyFile = new File("src/Project2/log/Chomsky.txt");
File deleteTrashFile = new File("src/Project2/log/Delete Trash.txt");
Scanner reader;
FileWriter writer;
boolean isGreibachCalculated = false, isChomskyCalculated = false, isDeleteTrashCalculated = false;
private Stage myStage;
public void setStage(Stage stage) {
myStage = stage;
}
//region FXMLs
@FXML
JFXButton closeBtn;
@FXML
JFXButton deleteBtn;
@FXML
JFXButton listBtn;
@FXML
JFXButton submitBtn;
@FXML
JFXButton deleteTrashBtn;
@FXML
JFXButton cnfBtn;
@FXML
JFXButton openBtn;
@FXML
JFXButton gnfBtn;
@FXML
JFXButton calculateBtn;
@FXML
JFXButton generateBtn;
@FXML
JFXButton grammarValidationBtn;
@FXML
Pane sidebarPane;
@FXML
BorderPane borderPane;
@FXML
Pane contentAreaPane;
@FXML
JFXTextArea txtInput;
@FXML
JFXTextArea txtFixed;
@FXML
JFXTextArea txtExpression;
@FXML
Label lblNotif;
//endregion
//region Functions
@Override
public void initialize(URL location, ResourceBundle resources) {
borderPane.setLeft(null);
listBtn.setOnAction(actionEvent -> {
if (!flag) {
myStage.setWidth(1090);
borderPane.setLeft(sidebarPane);
myStage.centerOnScreen();
flag = true;
} else {
myStage.setWidth(790);
borderPane.setLeft(null);
myStage.centerOnScreen();
flag = false;
}
});
submitBtn.setOnAction(actionEvent -> {
try{
FileWriter fw = new FileWriter("src/Project2/log/input.txt");
fw.write(txtInput.getText());
fw.close();
File file = new File("src/Project2/log/input.txt");
Scanner input = new Scanner(file);
int productionNums = Integer.parseInt(input.nextLine());
String[] allProductionRules = new String[productionNums];
for (int line = 0; line < productionNums; line++) {
allProductionRules[line] = input.nextLine();
}
//region Example:
// <START> -> <SUBJECT><DISTANCE><VERB><DISTANCE><OBJECTS>
// <SUBJECT> -> i|we
// <DISTANCE> -> " "
// <VERB> -> eat|drink|go
// <OBJECTS> -> food|water|lambda
//endregion
ArrayList<String> variables = new ArrayList<>();
ArrayList<String> terminals = new ArrayList<>();
Map<String, Set<ArrayList<String>>> productions = new LinkedHashMap<>();
Main.GrammarParse(allProductionRules, variables, terminals, productions, Main.start);
grammar = new Grammar(variables, terminals, Main.start, productions);
txtFixed.setText(grammar.printGrammar());
lblNotif.setText("Successfully saved!");
lblNotif.setTextFill(Color.GREEN);
} catch (Exception e) {
lblNotif.setText("An error occurred.");
lblNotif.setTextFill(Color.RED);
}
});
calculateBtn.setOnAction(actionEvent -> {
try {
//region CreateGrammar
File file = new File("D:/Theory of Computation/src/Project2/PDA.txt");
Scanner reader = new Scanner(file);
int productionNums;
productionNums = Integer.parseInt(reader.nextLine());
String[] allProductionRules = new String[productionNums];
for (int line = 0; line < productionNums; line++)
allProductionRules[line] = reader.nextLine();
ArrayList<String> variables = new ArrayList<>();
ArrayList<String> terminals = new ArrayList<>();
Map<String, Set<ArrayList<String>>> productions = new LinkedHashMap<>();
String start = "EXPRESSION";
Main.GrammarParse(allProductionRules, variables, terminals, productions, start);
PDAGrammar = new Grammar(variables, terminals, start, productions);
//endregion
String expression = txtInput.getText().trim();
String isAccepted = PDAGrammar.IsGenerateByGrammar(expression) ? "Accepted" : "Rejected";
if (isAccepted.equals("Accepted")) {
lblNotif.setText(isAccepted + " -> " + PDA.Calculate(expression));
lblNotif.setTextFill(Color.GREEN);
} else {
lblNotif.setText(isAccepted);
lblNotif.setTextFill(Color.RED);
}
} catch (Exception e) {
lblNotif.setText("An error occurred.");
lblNotif.setTextFill(Color.RED);
}
});
deleteTrashBtn.setOnAction(actionEvent -> {
try {
if (!isDeleteTrashCalculated) {
grammar.DeleteTrash();
writer = new FileWriter("src/Project2/log/Delete Trash.txt");
writer.write(grammar.printGrammar());
writer.close();
txtFixed.setText(grammar.printGrammar());
isDeleteTrashCalculated = true;
} else {
txtFixed.setText("");
reader = new Scanner(deleteTrashFile);
while (reader.hasNextLine())
txtFixed.appendText(reader.nextLine() + "\n");
}
lblNotif.setText("Successfully Done!");
lblNotif.setTextFill(Color.GREEN);
} catch (Exception e) {
lblNotif.setText("An error occurred.");
lblNotif.setTextFill(Color.RED);
}
});
cnfBtn.setOnAction(actionEvent -> {
try {
if (!isChomskyCalculated) {
grammar.ChangeToChomskyForm();
writer = new FileWriter("src/Project2/log/Chomsky.txt");
writer.write(grammar.printGrammar());
writer.close();
txtFixed.setText(grammar.printGrammar());
isChomskyCalculated = true;
} else {
txtFixed.setText("");
reader = new Scanner(chomskyFile);
while (reader.hasNextLine())
txtFixed.appendText(reader.nextLine() + "\n");
}
lblNotif.setText("Successfully Done!");
lblNotif.setTextFill(Color.GREEN);
} catch (Exception e) {
lblNotif.setText("An error occurred.");
lblNotif.setTextFill(Color.RED);
}
});
gnfBtn.setOnAction(actionEvent -> {
try {
if (!isGreibachCalculated) {
grammar.ChangeToGreibachForm();
writer = new FileWriter("src/Project2/log/Greibach.txt");
writer.write(grammar.printGrammar());
writer.close();
txtFixed.setText(grammar.printGrammar());
isGreibachCalculated = true;
} else {
txtFixed.setText("");
reader = new Scanner(greibachFile);
while (reader.hasNextLine())
txtFixed.appendText(reader.nextLine() + "\n");
}
lblNotif.setText("Successfully Done!");
lblNotif.setTextFill(Color.GREEN);
} catch (Exception e) {
lblNotif.setText("An error occurred.");
lblNotif.setTextFill(Color.RED);
}
});
grammarValidationBtn.setOnAction(actionEvent -> {
try {
String isChomskeyForm = grammar.isChomskyForm() ? "Yes!" : "No!";
txtFixed.setText("'IsChomskyForm': " + isChomskeyForm + "\n");
String isGreibachForm = grammar.isGreibachNormalForm() ? "Yes!" : "No!";
txtFixed.appendText("'IsGreibachForm': " + isGreibachForm + "\n");
String isDeleteTrash = grammar.isDeleteTrash() ? "Yes!" : "No!";
txtFixed.appendText("'IsDeleteTrash': " + isDeleteTrash);
lblNotif.setText("Successfully Done!");
lblNotif.setTextFill(Color.GREEN);
} catch (Exception e) {
lblNotif.setText("An error occurred.");
lblNotif.setTextFill(Color.RED);
}
});
generateBtn.setOnAction(actionEvent -> {
try {
String expression = txtExpression.getText();
Main.hasSpace = txtInput.getText().contains("<DISTANCE>");
String isAccepted = grammar.IsGenerateByGrammar(expression) ? "Accepted!" : "Rejected!";
if (isAccepted.equals("Accepted!"))
lblNotif.setTextFill(Color.GREEN);
else
lblNotif.setTextFill(Color.RED);
lblNotif.setText(expression + ": " + isAccepted);
} catch (Exception e) {
lblNotif.setText("An error occurred.");
lblNotif.setTextFill(Color.RED);
}
});
openBtn.setOnAction(actionEvent -> {
try {
FileChooser fileChooser = new FileChooser();
Stage stage = (Stage) borderPane.getScene().getWindow();
fileChooser.setInitialDirectory(new File("D:/Theory of Computation"));
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
fileChooser.getExtensionFilters().add(extFilter);
selectedFile = fileChooser.showOpenDialog(stage);
txtInput.setText("");
reader = new Scanner(selectedFile);
while (reader.hasNextLine())
txtInput.appendText(reader.nextLine() + "\n");
lblNotif.setText("File has Successfully loaded!");
lblNotif.setTextFill(Color.GREEN);
} catch (Exception e) {
lblNotif.setText("An error occurred.");
lblNotif.setTextFill(Color.RED);
}
});
deleteBtn.setOnAction(actionEvent -> txtInput.clear());
closeBtn.setOnAction(actionEvent -> System.exit(0));
}
//endregion
}