-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomato.js
262 lines (226 loc) · 7.95 KB
/
automato.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
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
/*
* Autor: Bárbara Boechat
* Data: 20/03/2021
* */
/* Construtor do Automato */
class Automato {
constructor(estados, transicoes, alfabeto) {
this.estados = estados; //array
this.transicoes = transicoes; //dict
this.alfabeto = alfabeto; //array
this.inicial = this.ini_final(">")
this.final = this.ini_final("*")
}
ini_final(marker) {
let finais = []
this.estados.forEach(estado => {
if (estado.includes(marker)) {
if (marker === ">") {
finais = estado.split(marker).join('')
} else {
finais.push(estado.split(marker).join(''))
}
}
})
return finais
}
le_transicao(estado, leitura) {
let prox_estado = this.transicoes[estado][leitura]
if (prox_estado === undefined) {
return 'Indefinido'
} else {
return prox_estado
}
}
estado_final(leitura) {
return this.final.includes(leitura)
}
exists_in_alf(leitura) {
return this.alfabeto.includes(leitura)
}
}
/* Controlador dos prints de leitura das
* transições do automato */
let output_count = 0
class HandleUI {
static out_transicao(comando, estado, leitura, prox_estado) {
let output = $('#sub-saida' + output_count);
let css_pill = 'afd-pill-' + comando
output.append(
`<div class="afd-item">
<span class="badge rounded-pill afd-pill-t afd-pill">${estado}, ${leitura}</span>
<img src="icons/arrow.png" alt="" style="width: 25px;display: inline-block;">
<span class="badge rounded-pill ${css_pill} afd-pill">${prox_estado}</span>
</div>`
)
}
static output_inicial(estado_inicial) {
let output = $('#saida-afd');
output.append(
`<div class="col-lg-3 afd-trans mt-2">
<div id="sub-saida${output_count}" class="output-area" style="margin-left: 0; margin-right: 0;">
<div class="afd-item">
<span class="badge rounded-pill afd-pill-t afd-pill">${estado_inicial}</span>
</div>
</div>
</div>`
)
}
static log_final(comando, entrada, mensagem, element) {
let icon = "";
let output = $(element);
(comando) ? icon = 'icon-aceita.png' : icon = 'icon-naceita.png'
output.append(
`<div class="output-item">
<p class="entrada-out">${entrada}</p>
<img class="output-icon" src="icons/${icon}" alt="">
<p class="description">${mensagem}</p>
</div>`
)
output_count += 1
}
}
/* Funções assincronas p/ animar o automato */
async function wait_for_me(time) {
await new Promise(done => setTimeout(() => done(), time));
}
async function paint_afd(historico) {
let curr_node, curr_edge, next_node
$('#qi').addClass('active');
$('#edgeqi').addClass('active');
for (let i = 0; i < historico.length - 1; i++) {
curr_node = '#' + historico[i]
curr_edge = '#' + historico[i] + '-' + historico[i + 1]
next_node = '#' + historico[i + 1]
$(curr_node).addClass('active');
// await new Promise(done => setTimeout(() => done(), 1000));
await wait_for_me(1000)
$(curr_edge).addClass('active');
// await new Promise(done => setTimeout(() => done(), 1000));
await wait_for_me(1000)
$(next_node).addClass('active');
}
}
async function clear_afd_output() {
await wait_for_me(500)
let nodes = $('.node'), edges = $('.edge'),
active_n, active_e
for (let i = 0; i < nodes.length; i++) {
active_n = $(nodes[i]).hasClass('active')
if (active_n)
$(nodes[i]).removeClass('active')
}
for (let i = 0; i < edges.length; i++) {
active_e = $(edges[i]).hasClass('active')
if (active_e)
$(edges[i]).removeClass('active')
}
}
function afd_action(afd, entrada) {
let fita = entrada.match(/[^,\s?]+/g),
estado_atual, estado_anterior, fim_fita = fita.length - 1,
i, mensagem = '', comando = false
estado_atual = afd.inicial
HandleUI.output_inicial(estado_atual)
let historico = [estado_atual]
for (i = 0; i < fita.length; i++) {
if (afd.exists_in_alf(fita[i])) {
estado_anterior = estado_atual
estado_atual = afd.le_transicao(estado_atual, fita[i])
historico.push(estado_atual)
if (estado_atual === 'Indefinido') {
//jquery indefine algo na ui
HandleUI.out_transicao('i', estado_anterior, fita[i], estado_atual)
mensagem = 'Simbolo Indf. no estado ' + estado_anterior
comando = false
break
} else if (!afd.estado_final(estado_atual) && i === fim_fita) {
//jquery nao-final warning na ui
HandleUI.out_transicao('nf', estado_anterior, fita[i], estado_atual)
mensagem = 'Fim da Fita em Estado Não Final'
comando = false
break
}
if (afd.estado_final(estado_atual) && i === fim_fita) {
//jquery palavra aceita na ui
HandleUI.out_transicao('f', estado_anterior, fita[i], estado_atual)
mensagem = 'Palavra Aceita!'
comando = true
break
}
HandleUI.out_transicao('next', estado_anterior, fita[i], estado_atual)
} else {
mensagem = 'Indf. - Simbolo ∉ Alfabeto.'
comando = false
break
}
}
return {historico: historico, comando: comando, mensagem: mensagem}
}
/* Leitura do conteudo do arquivo
* qd ele é carregado na interface */
let file_content = ""
$('#inputfile').change(function () {
let fr = new FileReader();
fr.onload = function () {
file_content = fr.result.split(/\r?\n/);
}
fr.readAsText(this.files[0]);
})
function inicia_afd(comando) {
const afd = new Automato(
['>q0', 'q1', 'q2', 'q1q2', 'qf*'],
{
'q0': {'7': 'q1'},
'q1': {'0b': 'q2', '9a': 'q2'},
'q2': {'0b': 'q1q2', '9a': 'q1q2', '7': 'qf'},
'q1q2': {'0b': 'q1q2', '9a': 'q1q2', '7': 'qf'},
'qf': {}
},
['7', '9a', '0b']
);
let data = {}, btn
switch (comando) {
case 'arquivo':
if (!file_content) {
console.log('insert valid file')
return 0
}
btn = $('#file-btn');
btn.addClass("disabled")
const forLoop = async _ => {
for (let i = 0; i < file_content.length; i++) {
data = afd_action(afd, file_content[i])
await paint_afd(data.historico)
await wait_for_me(1000)
HandleUI.log_final(data.comando, file_content[i],
data.mensagem, '#file-output')
await clear_afd_output()
}
btn.removeClass("disabled");
}
forLoop()
break
case 'input':
let user_input = $('#inputEntrada'),
fita = user_input.val()
if (!fita) {
console.log('insert valid input')
return 0
}
btn = $('#input-btn');
btn.addClass("disabled");
data = afd_action(afd, fita)
user_input.val('')
const paint_clear = async _ => {
await paint_afd(data.historico)
await wait_for_me(1000)
HandleUI.log_final(data.comando, fita,
data.mensagem, '#single-output')
await clear_afd_output()
btn.removeClass("disabled");
}
paint_clear()
break
}
}