-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.js
More file actions
198 lines (164 loc) · 7.81 KB
/
Copy pathsimulation.js
File metadata and controls
198 lines (164 loc) · 7.81 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
import { MACHINE, simState } from './state.js';
import { sleep } from './utils.js';
import { computeEpsilonClosure } from './automata.js';
/**
* Runs a simulation on a given machine and input string.
* Can operate in UI mode (updating the DOM) or silent mode (returning a result).
* @param {string} inputStr The string to test.
* @param {object} [machineToTest=MACHINE] The automaton to run against. Defaults to the global MACHINE.
* @returns {Promise<{isAccepted: boolean}> | void} If a machineToTest is provided, returns a result object. Otherwise, updates the UI.
*/
export async function runSimulation(inputStr, machineToTest = null) {
const isSilentMode = machineToTest !== null;
const machine = machineToTest || MACHINE;
if (!isSilentMode) {
simState.steps.length = 0;
simState.index = 0;
clearTimeout(simState.timer);
document.getElementById('stepLog').innerHTML = '';
const testOutput = document.getElementById('testOutput');
testOutput.textContent = 'Simulating...';
}
const startStates = machine.states.filter(s => s.initial).map(s => s.id);
if (startStates.length === 0) {
if (!isSilentMode) {
const testOutput = document.getElementById('testOutput');
testOutput.textContent = 'Error: No initial state defined.';
testOutput.style.color = '#e53e3e';
}
return { isAccepted: false };
}
let currentStates = (machine.type === 'ENFA') ? computeEpsilonClosure(startStates, machine.transitions) : [...startStates];
if (machine.type === 'DFA' && currentStates.length > 1) {
currentStates = [currentStates[0]];
}
if (!isSilentMode) simState.steps.push({ start: true, active: [...currentStates] });
let halted = false;
for (const symbol of inputStr) {
if (halted) break;
const frame = { before: [...currentStates], symbol: symbol, steps: [], after: [] };
const nextStates = new Set();
if (machine.type === 'DFA') {
if (currentStates.length > 0) {
const transition = machine.transitions.find(t => t.from === currentStates[0] && t.symbol === symbol);
if (transition) {
frame.steps.push({ from: transition.from, to: transition.to, symbol: transition.symbol });
nextStates.add(transition.to);
}
}
} else {
for (const stateId of currentStates) {
machine.transitions
.filter(t => t.from === stateId && t.symbol === symbol)
.forEach(t => {
frame.steps.push({ from: t.from, to: t.to, symbol: t.symbol });
nextStates.add(t.to);
});
}
}
const afterStates = (machine.type === 'ENFA') ? computeEpsilonClosure([...nextStates], machine.transitions) : [...nextStates];
frame.after = afterStates;
if (!isSilentMode) simState.steps.push(frame);
currentStates = afterStates;
if (currentStates.length === 0) {
halted = true;
}
}
const isAccepted = currentStates.some(sid => machine.states.find(s => s.id === sid && s.accepting));
if (isSilentMode) {
return { isAccepted };
}
simState.steps.push({ end: true, active: [...currentStates] });
document.getElementById('manualButtons').style.display = 'none';
playAuto();
}
/**
* Animates a single step of the FA simulation (DFA/NFA/ENFA).
* Updated to synchronize with the Machine Intelligence Logic Explorer.
*/
export async function showStep(idx) {
if (idx < 0 || idx >= simState.steps.length) {
simState.index = Math.max(0, Math.min(idx, simState.steps.length - 1));
return;
}
simState.index = idx;
const step = simState.steps[idx];
const log = document.getElementById('stepLog');
const speed = parseInt(document.getElementById('testSpeed').value || '500');
// 1. Reset all visual animations on canvas and modal
document.querySelectorAll('.state-animating, .transition-animating').forEach(el =>
el.classList.remove('state-animating', 'transition-animating')
);
if (idx === 0) log.innerHTML = '';
// --- HANDLE SIMULATION END ---
if (step.end) {
const isAccepted = (step.active || []).some(sid =>
MACHINE.states.find(s => s.id === sid && s.accepting)
);
const testOutput = document.getElementById('testOutput');
testOutput.textContent = isAccepted ? 'Accepted' : 'Rejected';
testOutput.style.color = isAccepted ? '#38a169' : '#e53e3e';
step.active.forEach(sid =>
document.querySelector(`.state-circle[data-id="${sid}"]`)?.classList.add('state-animating')
);
const finalLog = `<div><strong>Final active states: {${(step.active || []).join(', ') || '∅'}}</strong></div>`;
const resultLog = `<div><strong style="color:${isAccepted ? '#4ade80' : '#f87171'}">${isAccepted ? '✔ Accepted' : '✘ Rejected'}</strong></div>`;
log.innerHTML = resultLog + finalLog + log.innerHTML;
return;
}
// --- HANDLE SIMULATION START ---
if (step.start) {
log.innerHTML = `<div><strong>Initial active states: {${(step.active || []).join(', ')}}</strong></div>` + log.innerHTML;
step.active.forEach(sid =>
document.querySelector(`.state-circle[data-id="${sid}"]`)?.classList.add('state-animating')
);
return;
}
// --- STANDARD PROCESSING STEP ---
document.getElementById('testOutput').textContent = `Processing '${step.symbol}'... Active: {${(step.after || []).join(', ') || '∅'}}`;
// Highlight states existing BEFORE the transition
step.before.forEach(sid =>
document.querySelector(`.state-circle[data-id="${sid}"]`)?.classList.add('state-animating')
);
// Short pause before showing the transition paths
await sleep(speed / 2);
if (step.steps.length > 0) {
step.steps.forEach(s => {
// Update log message
log.innerHTML = `<div>Read '<b>${s.symbol}</b>': δ(${s.from}, ${s.symbol}) → ${s.to}</div>` + log.innerHTML;
// 2. Animate transition path on SVG canvas
document.querySelector(`.transition-path[data-from="${s.from}"][data-to="${s.to}"]`)?.classList.add('transition-animating');
// --- 3. MACHINE INTELLIGENCE SYNC: Logic Row Highlight ---
// Find the index of this transition in the global MACHINE object
const transIdx = MACHINE.transitions.findIndex(t =>
t.from === s.from &&
t.to === s.to &&
(t.symbol || '') === (s.symbol === 'ε' ? '' : s.symbol)
);
if (transIdx !== -1) {
// Trigger the Royal Blue glow in the modal table
import('./fa_logic_table.js').then(m => m.highlightFaTableRow(transIdx));
}
});
} else {
log.innerHTML = `<div>Read '<b>${step.symbol}</b>': No transitions from {${step.before.join(', ')}}. Halting.</div>` + log.innerHTML;
}
// Pause to allow user to see the transition highlight
await sleep(speed / 2);
// 4. Update highlights to show states AFTER the transition
document.querySelectorAll('.state-animating, .transition-animating').forEach(el =>
el.classList.remove('state-animating', 'transition-animating')
);
step.after.forEach(sid =>
document.querySelector(`.state-circle[data-id="${sid}"]`)?.classList.add('state-animating')
);
}
async function playAuto() {
for (let i = 0; i < simState.steps.length; i++) {
await showStep(i);
const speed = parseInt(document.getElementById('testSpeed').value || '800');
if (i < simState.steps.length - 1) {
await sleep(speed);
}
}
}