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
|
// Enable/disable if input length is > 0.
function checkLength(el, bId) {
if (graphit && (el.id == 'word')) return;
if(el.value.length > 0) {
enable(bId);
} else {
disable(bId);
};
};
// Enable/disable a enabled/disabled form item.
function enable(id) { $(id).removeAttr("disabled"); };
function disable(id) { $(id).attr("disabled","disabled"); };
// UI messages.
function graph_inprogress() {
$('#checkMessage').html('...');
$('#checkMessage').removeClass('success');
$('#checkMessage').removeClass('failure');
$('#word').removeClass('success');
$('#word').removeClass('failure');
$('#word').addClass('inprogress');
window.g.mover.attr({fill: 'yellow'});//'#ffff88'});
};
function graph_success() {
$('#checkMessage').html('Word accepted');
$('#checkMessage').removeClass('failure');
$('#checkMessage').addClass('success');
$('#word').removeClass('failure');
$('#word').removeClass('inprogress');
$('#word').addClass('success');
$('#checkMessage').effect("highlight", {}, 1000);
window.g.mover.attr({fill: 'green'});//'#cdeb8b'});
};
function graph_failure() {
$('#checkMessage').html('Word not accepted');
$('#checkMessage').removeClass('success');
$('#checkMessage').addClass('failure');
$('#word').removeClass('success');
$('#word').removeClass('inprogress');
$('#word').addClass('failure');
$('#checkMessage').effect("highlight", {}, 1000);
window.g.mover.animate({fill: 'red'});//'#b02b2c'}, 250);
};
// Call of RegexParser.parse() from UI.
function uiParse() {
disable('#graphit');
var parser = new RegexParser();
nfa = parser.parse($('#regex').attr('value'));
$('#parseMessage').html('Parse: '+parser.getErrorMessage());
if (parser.getErrorMessage() != 'Ok') {
$('#parseMessage').removeClass('success');
$('#parseMessage').addClass('failure');
$('#regex').addClass('failure');
} else {
$('#parseMessage').removeClass('failure');
$('#regex').removeClass('failure');
$('#parseMessage').addClass('success');
$('#regex').addClass('success');
enable('#word');
var dfa = new Nfa2Dfa(nfa);
var ttable = dfa.do();
disable('#regex');
disable('#parseButton');
if(!graphit) return;
window.g = graph();
disable('#checkButton');
};
$('#parseMessage').effect("highlight", {}, 1000);
// preparing graph movements
window.gCurrentState = g.nodes[0];
window.gPrevStates = [];
gPrevStates.push(gCurrentState);
window.inSameState = [];
window.failedInputs = [];
window.gPrevState = gCurrentState;
g.mover = g.paper.circle(
gCurrentState.node[1][0].cx.baseVal.value,
gCurrentState.node[1][0].cy.baseVal.value, 30
).attr({fill:'#ffff88', stroke: '#ccc', 'stroke-width': 4, opacity: 0.5});
graph_inprogress();
};
// Call of NfaSimulator.simulate() from UI.
function uiSimulate() {
var simulator = new NfaSimulator(nfa);
var check = simulator.simulate($('#word').attr('value'));
if (!check) {
graph_failure();
} else {
graph_success();
};
};
// Filter input.
function getKey(e, set) {
var key = String.fromCharCode(e.which);
if (e.which == 13) return true;
if (set.indexOf(key) >= 0 || e.which == 8) {
return true;
};
return false;
};
// Moving inside the graph by input symbols.
// setTimeout ?
function graphMoveByInput(e) {
if (lock) {
if (failedInputs) {
lock = false;
} else {
//var w = $('#word').attr('value');
//$('#word').attr('value', w.substr(0, w.length-1));
lock = false;
return false;
}
} else {
lock = true;
}
// no graph
if(!graphit) return;
// we are at the beginning
if (!gCurrentState || !gPrevState) {
gPrevStates.push(g.nodes[0]);
gPrevState = g.nodes[0];
gCurrentState = g.nodes[0];
};
// backspace
if (e.which == 8) {
// step over failed inputs.
if (window.failedInputs.length > 0) {
if(window.failedInputs.length == 1) {
if (ttable[gCurrentState.name].isFinal) {
graph_success();
} else {
graph_inprogress();
};
};
window.failedInputs.pop();
return;
}
// no state change
if (window.inSameState.length > 0) {
var mx = g.mover.attr('cx');
var my = g.mover.attr('cy');
var ll = g.paper.path('M'+mx+','+my+' '+mx+','+(my-25)+'Z').attr({stroke: 0});
window.setTimeout(function() { g.mover.animateAlong(ll, 250, "bounce"); }, 250);
window.inSameState.pop();
if (ttable[window.gCurrentState.name].isFinal) {
graph_success();
} else {
graph_inprogress();
}
return;
}
// go back one state
window.gPrevState = gPrevStates.pop();
window.setTimeout(function() {
g.mover.animate({cx:gPrevState.node[1][0].cx.baseVal.value, cy:gPrevState.node[1][0].cy.baseVal.value}, 250);
lock = false;
}, 250);
window.gCurrentState = gPrevState;
if (ttable[window.gCurrentState.name].isFinal) {
graph_success();
} else {
graph_inprogress();
};
return;
};
// getting input
var key = String.fromCharCode(e.which);
var gNextState = g.graphNodeByName[window.ttable[window.gCurrentState.name][key]];
graph_inprogress();
// --> failure
if (!gNextState) {
window.failedInputs.push(true);
graph_failure();
return;
};
// no state change
if(gNextState.name == gCurrentState.name) {
window.setTimeout(function() {
g.mover.animateAlong(gCurrentState.node[4], 350);lock = false;
}, 125);
window.inSameState.push(true);
if (ttable[window.gCurrentState.name].isFinal) {
graph_success();
} else {
graph_inprogress();
};
return;
} else {
// state change
var theConn;
for (var c in g.connections) {
if ((window.gCurrentState.name == g.connections[c].name1) && (gNextState.name == g.connections[c].name2)) {
theConn = g.connections[c];
break;
};
};
var x = gCurrentState.node[1][0].cx.baseVal.value;
var y = gCurrentState.node[1][0].cy.baseVal.value;
line = g.paper.path(
'M'+x+gCurrentState.node[1][0].r.baseVal.value+','+y+' '+theConn.line.attr('path').toString().substring(1)
+'L'+(gNextState.node[1][0].cx.baseVal.value)+','+gNextState.node[1][0].cy.baseVal.value
).attr({stroke:'none'});
(function(g, line) {
setTimeout(function() {
g.mover.animateAlong(line, 250);
lock = false;
return line;
}, 250);
})(g, line);
};
window.gPrevStates.push(gCurrentState);
window.gPrevState = gCurrentState;
window.gCurrentState = gNextState;
if (ttable[window.gCurrentState.name].isFinal) {
graph_success();
};
};
|