-
Notifications
You must be signed in to change notification settings - Fork 0
/
turingTable.js
211 lines (178 loc) · 6.99 KB
/
turingTable.js
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
$(document).ready(function () {
$('#btnAddCol').click(function () {
addColumn(document.getElementById("tabela_turing"));
});
$('#btnAddLin').click(function () {
addRow(document.getElementById("tabela_turing"));
});
$("#tabela_turing").on("click", "#btnRemoveCol", function () {
removeColumn(document.getElementById("tabela_turing"), $(this)[0].parentElement.cellIndex);
});
$("#tabela_turing").on("click", "#btnRemoveLin", function () {
removeLine(document.getElementById("tabela_turing"), $(this)[0].parentElement.parentElement.rowIndex);
});
$("#btn_json").click(function () {
document.getElementById("div_resposta").innerText = "";
if (lastEvents.length > 0) {
document.getElementById("alerta").innerText = "";
saveJson(document.getElementById("tabela_turing"));
} else {
document.getElementById("alerta").innerText = "Defina pelo menos um estado final";
}
});
$("#tabela_turing").on("click", "td", function () {
if ($(this)[0].cellIndex == 0 && $(this)[0].parentElement.rowIndex != 1){
toggleLastEvent(document.getElementById("tabela_turing"), $(this)[0].parentElement.rowIndex);
}
});
});
var lastEvents = [];
function saveJson(tbl) {
class JsonObj{
constructor(machineName, word, transitions, states){
this.machineName = machineName;
this.word = word;
this.transitions = transitions;
this.states = states;
}
};
class State{
constructor(isFinal, name){
this.name = name;
this.transitions = [];
this.isFinal = isFinal;
}
};
class Transition{
constructor(name, targetState, transitionSymbol, writeSymbol, action){
this.name = name;
this.targetState = targetState;
this.transitionSymbol = transitionSymbol;
this.writeSymbol = writeSymbol;
this.action = action;
}
};
var transitions = [];
var states = [];
for (var y = 1; y < tbl.rows.length; y++) {
var state = new State(($.inArray((tbl.rows[y].cells[0].innerText), lastEvents) == -1 ? false : true), tbl.rows[y].cells[0].innerText);
for (var x = 1; x < tbl.rows[y].cells.length; x++) {
var partsOfStr = tbl.rows[y].cells[x].innerHTML.split(',');
var transition = new Transition("transition" + x + y, partsOfStr[0], tbl.rows[0].cells[x].innerText, partsOfStr[1], partsOfStr[2]);
transitions.push(transition);
state.transitions.push(transition.name);
}
states.push(state);
};
var obj = new JsonObj("Minha máquina",document.getElementById("palavra").value, transitions, states);
console.log(JSON.stringify(obj, null, 2));
$.ajax({
type: 'post',
url: 'https://teoria.nicolas.eti.br:8080/send',
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
traditional: true,
success: function (data) {
console.log(data);
if(data.FinalState){
document.getElementById("div_resposta").style.backgroundColor = "#e0ffe0";
document.getElementById("div_resposta").innerText = "A palavra foi aceita pela máquina";
}else{
document.getElementById("div_resposta").style.backgroundColor = "#ffe0e0";
document.getElementById("div_resposta").innerText = "A palavra não foi aceita pela máquina";
}
}
});
}
function toggleLastEvent(tbl, rowIndex) {
if (rowIndex > 0 && rowIndex < tbl.rows.length) {
if($.inArray(tbl.rows[rowIndex].cells[0].innerText, lastEvents) == -1){
lastEvents.push(tbl.rows[rowIndex].cells[0].innerText);
console.log(lastEvents);
for (var y = 2; y < tbl.rows.length; y++) {
tbl.rows[y].cells[0].style.backgroundColor = "#eeeeee"
}
for(var indexes = 0; indexes < lastEvents.length; indexes++)
for (var y = 1; y < tbl.rows.length; y++)
if(tbl.rows[y].cells[0].innerText == lastEvents[indexes])
tbl.rows[y].cells[0].style.backgroundColor = "#ffcccc";
}else{
lastEvents.splice(lastEvents.indexOf(tbl.rows[rowIndex].cells[0].innerText), 1);
console.log(lastEvents);
for (var y = 2; y < tbl.rows.length; y++) {
tbl.rows[y].cells[0].style.backgroundColor = "#eeeeee"
}
for(var indexes = 0; indexes < lastEvents.length; indexes++)
for (var y = 1; y < tbl.rows.length; y++)
if(tbl.rows[y].cells[0].innerText == lastEvents[indexes])
tbl.rows[y].cells[0].style.backgroundColor = "#ffcccc";
}
}
}
function updateStateNumbers(tbl) {
var button = document.createElement("input");
button.value = "-";
button.type = "button";
button.id = "btnRemoveLin";
button.innerHTML = "-";
for (var y = 2; y < tbl.rows.length; y++) {
if (y != 0) {
var temp = tbl.rows[y].cells[0].innerHTML;
tbl.rows[y].cells[0].innerHTML ="";
tbl.rows[y].cells[0].append(button);
tbl.rows[y].cells[0].innerHTML += "<b>q"+(y-1)+"</b>";
}
}
}
function removeLine(tbl, lineNum) {
tbl.rows[lineNum].remove();
updateStateNumbers(document.getElementById("tabela_turing"));
lastEvents = [];
for (var y = 2; y < tbl.rows.length; y++) {
if(tbl.rows[y].cells[0].style.backgroundColor == "rgb(255, 204, 204)"){
lastEvents.push(tbl.rows[y].cells[0].innerText);
}
}
console.log(lastEvents);
}
function removeColumn(tbl, colNum) {
for (var y = 0; y < tbl.rows.length; y++) {
tbl.rows[y].cells[colNum].remove();
}
}
function addColumn(tbl) {
var button = document.createElement("input");
button.value = "-";
button.type = "button";
button.id = "btnRemoveCol";
button.innerHTML = "-";
//button.children[0].class = "unselectable";
for (var y = 0; y < tbl.rows.length; y++) {
var cell = tbl.rows[y].insertCell(tbl.rows[y].cells.length);
cell.contentEditable = true;
if (y == 0) {
cell.innerHTML = "<b>letra " + cell.cellIndex + "</b>";
cell.append(button);
} else {
cell.innerHTML = "";
}
}
}
function addRow(tbl) {
var button = document.createElement("input");
button.value = "-";
button.type = "button";
button.id = "btnRemoveLin";
button.innerHTML = "-";
var row = tbl.insertRow(tbl.rows.length);
for (var x = 0; x < tbl.rows[0].cells.length; x++) {
var cell = row.insertCell();
if (x == 0) {
cell.append(button);
cell.innerHTML += "<b>q" + (row.rowIndex-1) + "</b>";
} else {
cell.innerHTML = "";
cell.contentEditable = true;
}
}
}