|
|
@ -1,5 +1,4 @@
|
|
|
|
import mermaid from 'mermaid';
|
|
|
|
import mermaid from 'mermaid';
|
|
|
|
import { ENDS_WITH_TWO_B } from './examples.js';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const IS_VALID = 'is-valid';
|
|
|
|
const IS_VALID = 'is-valid';
|
|
|
|
const IS_INVALID = 'is-invalid';
|
|
|
|
const IS_INVALID = 'is-invalid';
|
|
|
@ -14,40 +13,37 @@ mermaid.initialize({
|
|
|
|
startOnLoad: false,
|
|
|
|
startOnLoad: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const states = ENDS_WITH_TWO_B;
|
|
|
|
/**
|
|
|
|
let state = 0;
|
|
|
|
* @param {import('./examples.js').State[]} states
|
|
|
|
let builder = '';
|
|
|
|
*/
|
|
|
|
|
|
|
|
export async function selectAutomaton(states) {
|
|
|
|
|
|
|
|
let state = 0;
|
|
|
|
|
|
|
|
let builder = '';
|
|
|
|
|
|
|
|
|
|
|
|
// Build the mermaid graph definition
|
|
|
|
// Build the mermaid graph definition
|
|
|
|
let graphDefinition = 'stateDiagram-v2';
|
|
|
|
let graphDefinition = 'stateDiagram-v2\n classDef acceptingnode font-weight:bold,stroke-width:2px,stroke:yellow';
|
|
|
|
for (let i = 0; i < states.length; ++i) {
|
|
|
|
for (let i = 0; i < states.length; ++i) {
|
|
|
|
graphDefinition += `\n s${i} : ${i}`;
|
|
|
|
graphDefinition += `\n s${i} : ${i}`;
|
|
|
|
|
|
|
|
if (i === 0) {
|
|
|
|
|
|
|
|
graphDefinition += '\n [*] --> s0';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (states[i].accepting) {
|
|
|
|
|
|
|
|
graphDefinition += `\n s${i} --> [*]`;
|
|
|
|
|
|
|
|
graphDefinition += `\n class s${i} acceptingnode`;
|
|
|
|
|
|
|
|
}
|
|
|
|
for (const [transition, destination] of Object.entries(states[i].transitions)) {
|
|
|
|
for (const [transition, destination] of Object.entries(states[i].transitions)) {
|
|
|
|
graphDefinition += `\n s${i} --> s${destination}: ${transition}`;
|
|
|
|
graphDefinition += `\n s${i} --> s${destination}: ${transition}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const graph = /** @type {HTMLDivElement} */ (document.getElementById('pen'));
|
|
|
|
|
|
|
|
const { svg } = await mermaid.render('state-graph', graphDefinition);
|
|
|
|
|
|
|
|
graph.innerHTML = svg;
|
|
|
|
|
|
|
|
const nodes = graph.querySelectorAll('.label-container');
|
|
|
|
|
|
|
|
for (let i = 0; i < nodes.length; ++i) {
|
|
|
|
|
|
|
|
if (states[i].accepting) {
|
|
|
|
|
|
|
|
nodes[i].classList.add('accepting-node');
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const graph = /** @type {HTMLDivElement} */ (document.getElementById('pen'));
|
|
|
|
// Create a triangular arrow pointing to the initial state
|
|
|
|
const { svg } = await mermaid.render('state-graph', graphDefinition);
|
|
|
|
const arrow = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
|
|
graph.innerHTML = svg;
|
|
|
|
const x = -(getIntAttribute(nodes[0], 'x') - 20);
|
|
|
|
const nodes = graph.querySelectorAll('.label-container');
|
|
|
|
const y = -getIntAttribute(nodes[0], 'y');
|
|
|
|
|
|
|
|
arrow.setAttribute('d', 'M 0 0 L 10 5 L 0 10 z');
|
|
|
|
|
|
|
|
arrow.setAttribute('transform', `translate(${x}, ${y})`);
|
|
|
|
|
|
|
|
arrow.setAttribute('fill', 'currentColor');
|
|
|
|
|
|
|
|
(/** @type {Element} */ (graph.querySelector('.edgePaths'))).appendChild(arrow);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Updates the UI to reflect the current state.
|
|
|
|
* Updates the UI to reflect the current state.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
function updateUIState() {
|
|
|
|
function updateUIState() {
|
|
|
|
wordInput.value = builder;
|
|
|
|
wordInput.value = builder;
|
|
|
|
if (state === -1 || !states[state].accepting) {
|
|
|
|
if (state === -1 || !states[state].accepting) {
|
|
|
|
light.classList.remove(IS_VALID);
|
|
|
|
light.classList.remove(IS_VALID);
|
|
|
@ -60,50 +56,39 @@ function updateUIState() {
|
|
|
|
if (state in nodes) {
|
|
|
|
if (state in nodes) {
|
|
|
|
nodes[state].classList.add('current-node');
|
|
|
|
nodes[state].classList.add('current-node');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Steps the FSM with the given letter.
|
|
|
|
* Steps the FSM with the given letter.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {string} letter
|
|
|
|
* @param {string} letter
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
function step(letter) {
|
|
|
|
function step(letter) {
|
|
|
|
if (state === -1) {
|
|
|
|
if (state === -1) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
builder += letter;
|
|
|
|
builder += letter;
|
|
|
|
state = states[state].transitions[letter] ?? -1;
|
|
|
|
state = states[state].transitions[letter] ?? -1;
|
|
|
|
updateUIState();
|
|
|
|
updateUIState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Gets the integer value of the given attribute on the given element.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param {Element} element
|
|
|
|
|
|
|
|
* @param {string} attribute
|
|
|
|
|
|
|
|
* @returns {number}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function getIntAttribute(element, attribute) {
|
|
|
|
|
|
|
|
return parseInt(/** @type {string} */ (element.getAttribute(attribute)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Dynamically create buttons for each letter in the alphabet
|
|
|
|
// Dynamically create buttons for each letter in the alphabet
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @type {string[]}
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
const alphabet = Array.from(states.reduce((acc, current) => {
|
|
|
|
const alphabet = Array.from(states.reduce((acc, current) => {
|
|
|
|
Object.keys(current.transitions).forEach(current => acc.add(current));
|
|
|
|
Object.keys(current.transitions).forEach(current => acc.add(current));
|
|
|
|
return acc;
|
|
|
|
return acc;
|
|
|
|
}, new Set())).sort();
|
|
|
|
}, new Set())).sort();
|
|
|
|
for (const letter of alphabet) {
|
|
|
|
for (const letter of alphabet) {
|
|
|
|
const button = document.createElement('button');
|
|
|
|
const button = document.createElement('button');
|
|
|
|
button.innerText = letter;
|
|
|
|
button.innerText = letter;
|
|
|
|
button.addEventListener('click', () => step(letter));
|
|
|
|
button.addEventListener('click', () => step(letter));
|
|
|
|
buttons.appendChild(button);
|
|
|
|
buttons.appendChild(button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reacts to input in the text box
|
|
|
|
// Reacts to input in the text box
|
|
|
|
wordInput.addEventListener('input', () => {
|
|
|
|
wordInput.addEventListener('input', () => {
|
|
|
|
const value = wordInput.value;
|
|
|
|
const value = wordInput.value;
|
|
|
|
builder = '';
|
|
|
|
builder = '';
|
|
|
|
state = 0;
|
|
|
|
state = 0;
|
|
|
@ -113,13 +98,14 @@ wordInput.addEventListener('input', () => {
|
|
|
|
if (!value.length) {
|
|
|
|
if (!value.length) {
|
|
|
|
updateUIState();
|
|
|
|
updateUIState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
clearButton.addEventListener('click', () => {
|
|
|
|
clearButton.addEventListener('click', () => {
|
|
|
|
wordInput.value = '';
|
|
|
|
wordInput.value = '';
|
|
|
|
builder = '';
|
|
|
|
builder = '';
|
|
|
|
state = 0;
|
|
|
|
state = 0;
|
|
|
|
updateUIState();
|
|
|
|
updateUIState();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
updateUIState();
|
|
|
|
updateUIState();
|
|
|
|
|
|
|
|
}
|
|
|
|