-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
353 lines (275 loc) · 8.24 KB
/
Copy pathscript.js
File metadata and controls
353 lines (275 loc) · 8.24 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
let canvasParent;
let boxCounter = 0;
let SuperFA = new FA();
//get button elements from html
const resetbutton = document.getElementById("reset");
const getTypeBtn = document.getElementById("testfa");
const minimizeBtn = document.getElementById("minimize");
const convertBtn = document.getElementById("convertNFA");
const testStringInp = document.getElementById("textInput");
const testStringBtn = document.getElementById("teststr");
const alphabetInp = document.getElementById('alphabet-input');
const alphabetResult = document.getElementById("alphabet-result");
const loadBtn = document.getElementById("load-btn");
const bulkTestBtn = document.getElementById("bulk");
// Empty boxlist array if user clicks reset
resetbutton.addEventListener("click", () => {resetFAcanvas()});
// load FA JSON
loadBtn.addEventListener('click', () => {loadFAfromJson();})
// Handle Alphabet Setup
alphabetInp.addEventListener("change", setAlphabetFA);
minimizeBtn.onclick = () => {
let minDFA = SuperFA.getMinimizedDFA();
popupCanvas(minDFA)
}
convertBtn.onclick = () => {
let DFA = SuperFA.getNFAtoDFA();
popupCanvas(DFA)
}
bulkTestBtn.addEventListener("click", bulkTestStr)
minimizeBtn.disabled = true;
convertBtn.disabled = true;
// Set up canvas using p5js
function setup() {
// Get the parent container element
canvasParent = document.getElementById("canvasParent");
// Set initial canvas size based on div parent container
const mycanvas = createCanvas(
canvasParent.offsetWidth,
canvasParent.offsetHeight
);
mycanvas.parent("canvasParent");
windowResized();
// renderFA(f1)
renderFA(f3); //nfa
}
// Adding resize function for responsiveness
function windowResized() {
const width = canvasParent.offsetWidth;
const height = canvasParent.offsetHeight;
resizeCanvas(width, height);
console.log(width);
console.log(height);
}
window.addEventListener("resize", windowResized);
document.getElementById("save").addEventListener("click", function () {
saveFAToJSON(SuperFA);
});
let boxList = [];
let lineList = [];
let currentLine = null;
function draw() {
background(255);
for (let i = 0; i < boxList.length; i++) {
boxList[i].update();
boxList[i].over();
boxList[i].show();
}
for (let line of lineList) {
line.show();
}
if (currentLine) {
currentLine.update(mouseX, mouseY);
currentLine.show();
}
push();
fill(165,247,0)
stroke(0);
strokeWeight(3);
if(boxList.length > 0) {
text("start", boxList[0].x, boxList[0].y)
}
pop();
}
testStringBtn.addEventListener('click', function(){
console.log(testStringInp.value);
SuperFA.checkStr(testStringInp.value);
if(SuperFA.output == 0){
document.getElementById("str-result").innerText = "Reject";
}else{
document.getElementById("str-result").innerText = "Accept";
}
document.getElementById("str-result").style.display = "inline";
})
getTypeBtn.addEventListener('click', function(){
if(SuperFA.states.length == 0) {
alert("oh may gah")
} else {
SuperFA.determineType();
showFAType();
}
})
// Handle double-clicked event
function doubleClicked() {
if (
mouseX >= 0 &&
mouseX <= canvasParent.offsetWidth &&
mouseY >= 0 &&
mouseY <= canvasParent.offsetHeight
) {
// Check if the mouse position is within the canvas width and height
let box = new Draggable(mouseX, mouseY);
boxList.push(box);
box.pressed(); // Allow dragging immediately after creation
box.createState();
console.log("created");
console.log(mouseX);
console.log(mouseY);
} else {
console.log("not allowed");
}
}
function showFAType() {
let NFAEle = document.querySelectorAll('.fa span')[0];
let DFAEle = document.querySelectorAll('.fa span')[1];
// After Reset, set to default color
if(boxCounter == 0) {
DFAEle.style.backgroundColor = "#9faec1";
NFAEle.style.backgroundColor = "#9faec1";
return;
}
if(SuperFA.type == TypeFA.NFA) {
DFAEle.style.backgroundColor = "#9faec1";
NFAEle.style.backgroundColor = "Green";
convertBtn.disabled = false;
convertBtn.style.backgroundColor = "orange"
minimizeBtn.disabled = true;
minimizeBtn.style.backgroundColor = "#9faec1";
}
if(SuperFA.type == TypeFA.DFA){
DFAEle.style.backgroundColor = "Green";
NFAEle.style.backgroundColor = "#9faec1";
minimizeBtn.disabled = false;
minimizeBtn.style.background = "orange";
convertBtn.disabled = true;
convertBtn.style.backgroundColor = "#9faec1";
}
}
function setAlphabetFA() {
let chars = alphabetInp.value;
chars = chars.split(',');
for(let i = 0; i < chars.length; i++) chars[i] = chars[i].trim();
chars = new Set(chars); // Remove duplicates
let alphabetSet = Array.from(chars);
alphabetSet = alphabetSet.filter(char => char.length == 1);
SuperFA.alphabet = alphabetSet;
alphabetResult.innerText = `[${SuperFA.alphabet}]`;
alphabetResult.style.display = "inline";
console.log(fa.alphabet);
}
function popupCanvas(faObject) {
let win = window.open('view/popup/canvas.html', null, 'popup, width=800, height=600');
win.onload = () => {
win.postMessage({"FA" : faObject})
}
console.log("Sending")
console.log(faObject);
}
function loadFAfromJson(){
const input = document.createElement("input");
input.type = "file";
input.accept = "application/json";
input.onchange = e =>{
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function(event){
resetFAcanvas();
const faData = JSON.parse(event.target.result);
let FAobject = setFAdata(faData);
renderFA(FAobject);
};
reader.readAsText(file);
};
input.click();
}
function setFAdata(FAjson) {
let FAobject = new FA();
FAobject.alphabet = FAjson.alphabet;
let states = FAjson.states;
let finalStateIndex = FAjson.finalStateIndex;
states.forEach(state => {
FAobject.createState();
})
for(let i = 0; i < states.length; i++) {
console.log("state: ", i)
let transitions = states[i].allTransitionsIndex;
for(let [char, destIndexes] of Object.entries(transitions)) {
destIndexes.forEach(index => {
FAobject.createTransition(i, index, char);
})
}
}
finalStateIndex.forEach(index => {
FAobject.makeFinalState(FAobject.states[index]);
})
return FAobject;
}
function resetFAcanvas() {
console.log("clicked");
boxList.forEach(box => {
box.removeCheckbox();
});
boxList = [];
boxCounter = 0;
lineList = [];
showFAType(); // reset color of type labels
SuperFA = new FA();
minimizeBtn.disabled = true;
minimizeBtn.style.backgroundColor = "grey";
convertBtn.disabled = true;
convertBtn.style.backgroundColor = "grey";
alphabetResult.style.display = 'none';
document.getElementById("str-result").style.display = "none";
}
function saveFAToJSON(fa) {
const faData = fa.getFAData();
const jsonString = JSON.stringify(faData, null, 2);
downloadJSON(jsonString, "fa_data.json");
}
function downloadJSON(content, filename) {
const a = document.createElement("a");
const file = new Blob([content], {type: "application/json"});
a.href = URL.createObjectURL(file);
a.download = filename;
a.click();
}
function bulkTestStr() {
const getAccept = document.getElementById('acc_input').value;
const acceptValue = getAccept.split('\n');
const getReject = document.getElementById('rej_input').value;
const rejectValue = getReject.split('\n');
const resultEle = document.querySelector(".result");
for(let i = resultEle.childNodes.length - 1; i > 2 ; i--) {
let child = resultEle.childNodes[i]
resultEle.removeChild(child);
}
console.log(acceptValue);
console.log(rejectValue);
let testResult = [];
acceptValue.forEach(str => {
SuperFA.checkStr(str)
let res;
if(SuperFA.output == 0) {
res = str + " Failed"
} else{
res = str + " Passed"
}
testResult.push(res);
})
rejectValue.forEach(str => {
SuperFA.checkStr(str)
let res;
if(SuperFA.output == 0) {
res = str + " Passed"
} else{
res = str + " Failed"
}
testResult.push(res);
})
testResult.forEach(result => {
let spanText = document.createElement("span");
spanText.innerText = result;
resultEle.appendChild(spanText);
console.log("appending: ", result)
})
}