UNPKG

fauton

Version:

A library to test any finite automaton with arbitrary alphabets

59 lines (58 loc) 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateGraphFromString = void 0; function generateGraphFromString(automaton, inputString) { let currentParents = [ { name: `${automaton.start_state}`, state: automaton.start_state, string: '', depth: 0, symbol: null, children: [], }, ]; let automatonTestResult = false; const finalStates = new Set(automaton.final_states); const graph = currentParents; for (let index = 0; index < inputString.length; index += 1) { const newParents = []; const symbol = inputString[index]; currentParents.forEach((currentParent) => { const transitionStateRecord = automaton.transitions[currentParent.state]; if (transitionStateRecord) { const transitionTargetStates = transitionStateRecord[symbol]; if (Array.isArray(transitionTargetStates)) { transitionTargetStates.forEach((transitionTargetState) => { const parentGraphNode = { name: `${transitionTargetState}(${symbol})`, state: transitionTargetState, string: inputString.slice(0, index + 1), depth: index + 1, symbol, children: [], }; currentParent.children.push(parentGraphNode); newParents.push(parentGraphNode); }); } } }); if (index === inputString.length - 1) { for (let newParentsIndex = 0; newParentsIndex < newParents.length; newParentsIndex += 1) { const newChild = newParents[newParentsIndex]; if (finalStates.has(newChild.state)) { automatonTestResult = true; break; } } } currentParents = newParents; } return { automatonTestResult, finalNodes: currentParents, graph: graph[0], }; } exports.generateGraphFromString = generateGraphFromString;