States and symbols are represented by strings.
Transitions are objects of interface { state, symbol, resultState }.
Each state needs a transition for each input symbol. (completeness)
Tested with Node.js v16.13.1
Automaton that accepts binary strings that contain an even number of 0s.
const transitions = [
{ state: 'even', symbol: '0', resultState: 'odd' },
{ state: 'even', symbol: '1', resultState: 'even' },
{ state: 'odd', symbol: '0', resultState: 'even' },
{ state: 'odd', symbol: '1', resultState: 'odd' },
]
const automaton = new FiniteAutomaton({
transitions,
startState: 'even',
acceptStates: ['even']
})
console.log(automaton.isAccepting('00')) // true
console.log(automaton.isAccepting('10')) // false
console.log(automaton.states()) // ['even', 'odd']
console.log(automaton.alphabet()) // ['0', '1']
Consider automaton from example above.
automaton.isAccepting('010')
// true
automaton.process('010')
// { isAccepting: true, stateSequence: [ 'even', 'odd', 'odd', 'even' ] }
automaton.states()
// [ 'even', 'odd' ]
automaton.alphabet()
// [ '0', '1' ]
automaton.transitions()
// [
// { state: 'even', symbol: '0', resultState: 'odd' },
// { state: 'even', symbol: '1', resultState: 'even' },
// { state: 'odd', symbol: '0', resultState: 'even' },
// { state: 'odd', symbol: '1', resultState: 'odd' }
// ]
automaton.startState()
// 'even'
automaton.acceptStates()
// [ 'even' ]