Introduce the FSM editor
This commit is contained in:
@@ -89,6 +89,7 @@ export class GraphEditor extends HTMLElement {
|
|||||||
}
|
}
|
||||||
const [x, y] = d3.pointer(event, this.canvas!.node());
|
const [x, y] = d3.pointer(event, this.canvas!.node());
|
||||||
this.createNode(x, y);
|
this.createNode(x, y);
|
||||||
|
this.fireOnChange();
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
initMarkers(this.canvas, this.config);
|
initMarkers(this.canvas, this.config);
|
||||||
@@ -171,6 +172,7 @@ export class GraphEditor extends HTMLElement {
|
|||||||
}
|
}
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if (this.graph.createLink(source.id, target.id) !== null) {
|
if (this.graph.createLink(source.id, target.id) !== null) {
|
||||||
|
this.fireOnChange();
|
||||||
this.restart();
|
this.restart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -193,12 +195,14 @@ export class GraphEditor extends HTMLElement {
|
|||||||
const transition = prompt('Transition');
|
const transition = prompt('Transition');
|
||||||
if (transition !== null) {
|
if (transition !== null) {
|
||||||
d.transition = transition;
|
d.transition = transition;
|
||||||
|
this.fireOnChange();
|
||||||
this.restart();
|
this.restart();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on('contextmenu', (event: MouseEvent, d: Link) => {
|
.on('contextmenu', (event: MouseEvent, d: Link) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
this.graph.removeLink(d);
|
this.graph.removeLink(d);
|
||||||
|
this.fireOnChange();
|
||||||
this.restart();
|
this.restart();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -230,14 +234,17 @@ export class GraphEditor extends HTMLElement {
|
|||||||
this.moveMenu(event, [
|
this.moveMenu(event, [
|
||||||
new MenuAction('Start', () => d.start, () => {
|
new MenuAction('Start', () => d.start, () => {
|
||||||
d.start = !d.start;
|
d.start = !d.start;
|
||||||
|
this.fireOnChange();
|
||||||
this.restart();
|
this.restart();
|
||||||
}),
|
}),
|
||||||
new MenuAction('Accepting', () => d.accepting, () => {
|
new MenuAction('Accepting', () => d.accepting, () => {
|
||||||
d.accepting = !d.accepting;
|
d.accepting = !d.accepting;
|
||||||
|
this.fireOnChange();
|
||||||
this.restart();
|
this.restart();
|
||||||
}),
|
}),
|
||||||
new MenuAction('Delete', () => false, () => {
|
new MenuAction('Delete', () => false, () => {
|
||||||
this.graph.removeNode(d);
|
this.graph.removeNode(d);
|
||||||
|
this.fireOnChange();
|
||||||
this.resetDraggableLink();
|
this.resetDraggableLink();
|
||||||
this.restart();
|
this.restart();
|
||||||
}),
|
}),
|
||||||
@@ -439,6 +446,10 @@ export class GraphEditor extends HTMLElement {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
this.menu.setAttribute('hidden', 'hidden');
|
this.menu.setAttribute('hidden', 'hidden');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fireOnChange() {
|
||||||
|
this.dispatchEvent(new Event('change'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeSelection = d3.Selection<
|
type NodeSelection = d3.Selection<
|
||||||
|
50
src/editor/mapper.ts
Normal file
50
src/editor/mapper.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { Graph } from './d3/graph.ts';
|
||||||
|
|
||||||
|
type State = {
|
||||||
|
transitions: Record<string, number>;
|
||||||
|
start?: boolean;
|
||||||
|
accepting?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function createGraph(states: State[]): Graph {
|
||||||
|
const graph = new Graph();
|
||||||
|
for (let i = 0; i < states.length; i++) {
|
||||||
|
const node = graph.createNode(i);
|
||||||
|
if (states[i].accepting) {
|
||||||
|
node.accepting = true;
|
||||||
|
}
|
||||||
|
if (i === 0) {
|
||||||
|
node.start = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let i = 0; i < states.length; i++) {
|
||||||
|
const state = states[i];
|
||||||
|
for (const [letter, target] of Object.entries(state.transitions)) {
|
||||||
|
graph.createLink(i, target, letter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return graph;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createStateList(graph: Graph): State[] {
|
||||||
|
const states: State[] = [];
|
||||||
|
// Map node ids to state ids, as graph nodes may contain gaps.
|
||||||
|
const reduced: { [id: number]: number } = {};
|
||||||
|
for (const node of graph.nodes) {
|
||||||
|
const state: State = {
|
||||||
|
transitions: {},
|
||||||
|
start: node.start,
|
||||||
|
accepting: node.accepting,
|
||||||
|
};
|
||||||
|
reduced[node.id] = states.length;
|
||||||
|
states.push(state);
|
||||||
|
}
|
||||||
|
for (const link of graph.links) {
|
||||||
|
const source = reduced[link.source.id];
|
||||||
|
const target = reduced[link.target.id];
|
||||||
|
for (const label of link.transition.split('')) {
|
||||||
|
states[source].transitions[label] = target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return states;
|
||||||
|
}
|
@@ -1,6 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* @typedef State
|
* @typedef State
|
||||||
* @property {Object.<string, number>} transitions
|
* @property {Object.<string, number>} transitions
|
||||||
|
* @property {boolean} [start]
|
||||||
* @property {boolean} [accepting]
|
* @property {boolean} [accepting]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
69
src/fsm.js
69
src/fsm.js
@@ -1,5 +1,5 @@
|
|||||||
import { Graph } from './editor/d3/graph.ts';
|
|
||||||
import { GraphEditor } from './editor/GraphEditor.ts';
|
import { GraphEditor } from './editor/GraphEditor.ts';
|
||||||
|
import { createGraph, createStateList } from './editor/mapper.ts';
|
||||||
|
|
||||||
const IS_VALID = 'is-valid';
|
const IS_VALID = 'is-valid';
|
||||||
const IS_INVALID = 'is-invalid';
|
const IS_INVALID = 'is-invalid';
|
||||||
@@ -11,30 +11,23 @@ const light = /** @type {HTMLDivElement} */ (document.getElementById('light'));
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {import('./examples.js').State[]} states
|
* @param {import('./examples.js').State[]} states
|
||||||
|
* @param {boolean} [editable]
|
||||||
*/
|
*/
|
||||||
export async function selectAutomaton(states) {
|
export function openAutomaton(states, editable = false) {
|
||||||
let state = 0;
|
let state = findStart(states);
|
||||||
let builder = '';
|
let builder = '';
|
||||||
|
|
||||||
const viewer = new GraphEditor();
|
const viewer = new GraphEditor();
|
||||||
viewer.readonly = true;
|
viewer.readonly = !editable;
|
||||||
const graph = new Graph();
|
const graph = createGraph(states);
|
||||||
for (let i = 0; i < states.length; i++) {
|
viewer.addEventListener('change', () => {
|
||||||
const node = graph.createNode(i);
|
try {
|
||||||
if (states[i].accepting) {
|
states = createStateList(graph);
|
||||||
node.accepting = true;
|
type();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
}
|
}
|
||||||
if (i === 0) {
|
});
|
||||||
node.start = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (let i = 0; i < states.length; i++) {
|
|
||||||
const state = states[i];
|
|
||||||
for (const [letter, target] of Object.entries(state.transitions)) {
|
|
||||||
graph.createLink(i, target, letter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const container = /** @type {HTMLDivElement} */ (document.getElementById('state-graph'));
|
const container = /** @type {HTMLDivElement} */ (document.getElementById('state-graph'));
|
||||||
container.appendChild(viewer);
|
container.appendChild(viewer);
|
||||||
viewer.graph = graph;
|
viewer.graph = graph;
|
||||||
@@ -44,7 +37,7 @@ export async function selectAutomaton(states) {
|
|||||||
*/
|
*/
|
||||||
function updateUIState() {
|
function updateUIState() {
|
||||||
wordInput.value = builder;
|
wordInput.value = builder;
|
||||||
if (state === -1 || !states[state].accepting) {
|
if (state === -1 || states.length === 0 || !states[state].accepting) {
|
||||||
light.classList.remove(IS_VALID);
|
light.classList.remove(IS_VALID);
|
||||||
light.classList.add(IS_INVALID);
|
light.classList.add(IS_INVALID);
|
||||||
} else {
|
} else {
|
||||||
@@ -58,6 +51,18 @@ export async function selectAutomaton(states) {
|
|||||||
viewer.restart();
|
viewer.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function type() {
|
||||||
|
const value = wordInput.value;
|
||||||
|
builder = '';
|
||||||
|
state = findStart(states);
|
||||||
|
for (const letter of value) {
|
||||||
|
step(letter);
|
||||||
|
}
|
||||||
|
if (!value.length) {
|
||||||
|
updateUIState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Steps the FSM with the given letter.
|
* Steps the FSM with the given letter.
|
||||||
*
|
*
|
||||||
@@ -88,24 +93,22 @@ export async function selectAutomaton(states) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Reacts to input in the text box
|
// Reacts to input in the text box
|
||||||
wordInput.addEventListener('input', () => {
|
wordInput.addEventListener('input', () => type());
|
||||||
const value = wordInput.value;
|
|
||||||
builder = '';
|
|
||||||
state = 0;
|
|
||||||
for (const letter of value) {
|
|
||||||
step(letter);
|
|
||||||
}
|
|
||||||
if (!value.length) {
|
|
||||||
updateUIState();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
clearButton.addEventListener('click', () => {
|
clearButton.addEventListener('click', () => {
|
||||||
wordInput.value = '';
|
wordInput.value = '';
|
||||||
builder = '';
|
builder = '';
|
||||||
state = 0;
|
state = findStart(states);
|
||||||
updateUIState();
|
updateUIState();
|
||||||
});
|
});
|
||||||
|
|
||||||
updateUIState();
|
updateUIState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {import('./examples.js').State[]} states
|
||||||
|
*/
|
||||||
|
function findStart(states) {
|
||||||
|
let state = states.findIndex(state => state.start);
|
||||||
|
return Math.max(state, 0);
|
||||||
|
}
|
||||||
|
24
src/main.js
24
src/main.js
@@ -1,5 +1,5 @@
|
|||||||
import { AUTOMATONS } from './examples.js';
|
import { AUTOMATONS } from './examples.js';
|
||||||
import { selectAutomaton } from './fsm.js';
|
import { openAutomaton } from './fsm.js';
|
||||||
|
|
||||||
const automatonSelector = /** @type {HTMLDivElement} */ (document.getElementById('automaton-selector'));
|
const automatonSelector = /** @type {HTMLDivElement} */ (document.getElementById('automaton-selector'));
|
||||||
const automatonCollection = /** @type {HTMLDivElement} */ (document.getElementById('automaton-collection'));
|
const automatonCollection = /** @type {HTMLDivElement} */ (document.getElementById('automaton-collection'));
|
||||||
@@ -16,16 +16,30 @@ for (const [displayName, automaton] of Object.entries(AUTOMATONS)) {
|
|||||||
<p class="card-text">${automaton.length} states</p>
|
<p class="card-text">${automaton.length} states</p>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
const handleEvent = async () => {
|
const handleEvent = () => {
|
||||||
automatonSelector.setAttribute('hidden', 'hidden');
|
automatonSelector.setAttribute('hidden', 'hidden');
|
||||||
app.removeAttribute('hidden');
|
app.removeAttribute('hidden');
|
||||||
await selectAutomaton(automaton);
|
openAutomaton(automaton);
|
||||||
};
|
};
|
||||||
card.addEventListener('click', handleEvent);
|
card.addEventListener('click', handleEvent);
|
||||||
card.addEventListener('keydown', async (event) => {
|
card.addEventListener('keydown', (event) => {
|
||||||
if (event.key === 'Enter' || event.key === ' ') {
|
if (event.key === 'Enter' || event.key === ' ') {
|
||||||
await handleEvent();
|
handleEvent();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
automatonCollection.appendChild(card);
|
automatonCollection.appendChild(card);
|
||||||
}
|
}
|
||||||
|
const create = document.createElement('div');
|
||||||
|
create.classList.add('card');
|
||||||
|
create.innerHTML = `
|
||||||
|
<div class="card-body">
|
||||||
|
<h5 class="card-title">Create New Automaton</h5>
|
||||||
|
<p class="card-text">Create a new automaton from scratch.</p>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
automatonCollection.appendChild(create);
|
||||||
|
create.addEventListener('click', () => {
|
||||||
|
automatonSelector.setAttribute('hidden', 'hidden');
|
||||||
|
app.removeAttribute('hidden');
|
||||||
|
openAutomaton([], true);
|
||||||
|
});
|
||||||
|
@@ -103,19 +103,6 @@ button:focus-visible {
|
|||||||
background-color: #2a2a2a;
|
background-color: #2a2a2a;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (prefers-color-scheme: light) {
|
|
||||||
:root {
|
|
||||||
color: #213547;
|
|
||||||
background-color: #ffffff;
|
|
||||||
}
|
|
||||||
button {
|
|
||||||
background-color: #f9f9f9;
|
|
||||||
}
|
|
||||||
.accepting-node {
|
|
||||||
outline-color: #9370DB;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
graph-editor {
|
graph-editor {
|
||||||
display: block;
|
display: block;
|
||||||
height: 600px;
|
height: 600px;
|
||||||
@@ -169,8 +156,8 @@ graph-editor {
|
|||||||
|
|
||||||
.context-menu {
|
.context-menu {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
background: white;
|
background: #313131;
|
||||||
border: 1px solid #ccc;
|
border: 1px solid #222;
|
||||||
box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
|
box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
|
||||||
min-width: 150px;
|
min-width: 150px;
|
||||||
}
|
}
|
||||||
@@ -187,13 +174,33 @@ graph-editor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.context-menu li:hover, .context-menu li:focus {
|
.context-menu li:hover, .context-menu li:focus {
|
||||||
background-color: #f0f0f0;
|
background-color: #2a2a2a;
|
||||||
}
|
}
|
||||||
|
|
||||||
.context-menu li:active {
|
.context-menu li:active {
|
||||||
background-color: #e0e0e0;
|
background-color: #1a1a1a;
|
||||||
}
|
}
|
||||||
.context-menu .checked:after {
|
.context-menu .checked:after {
|
||||||
content: "✓";
|
content: "✓";
|
||||||
float: right;
|
float: right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: light) {
|
||||||
|
:root {
|
||||||
|
color: #213547;
|
||||||
|
background-color: #ffffff;
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
}
|
||||||
|
.context-menu {
|
||||||
|
background: white;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
|
||||||
|
}
|
||||||
|
.context-menu li:hover, .context-menu li:focus {
|
||||||
|
background-color: #f0f0f0;
|
||||||
|
}
|
||||||
|
.context-menu li:active {
|
||||||
|
background-color: #e0e0e0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user