ts-automata
TypeScript icon, indicating that this package has built-in type declarations

0.5.0 • Public • Published

TS-Automata

A TypeScript/JavaScript library for managing automata.

NPM Package

npm i ts-automata

Overview

Currently supported are the Deterministic and Non-deterministic finite state automata. Make use of the provided TSDoc and JSDoc for each method to see

Usage

Automata can be created by choosing an alphabet to feed to the Automaton, defining the states of the automaton and finally the transition function. The code below is an example of creating and running input on a DFA.

/* Initialize the DFA */
const dfa = new DFA("ab", "q0", false)

/* Add states to the DFA */
dfa.addState("q1", false);
dfa.addState("q2", true)

/* Define the transition function for each state */
dfa.addEdge("q0", 'b', "q0");
dfa.addEdge("q0", 'a', "q1");
dfa.addEdge("q1", 'a', "q2");
dfa.addEdge("q1", 'b', "q0");
dfa.addEdge("q2", 'a', "q2");
dfa.addEdge("q2", 'b', "q2");

/* Check for validity */
console.log(dfa.isValid());

/* Run strings on the DFA */
dfa.runString("ababababaa") // true
dfa.runString("aa") // true
dfa.runString("abb") // false

Figure 1: The code for the deterministic finite automaton D.

There are three stares: q0, q1 and q2 q0 is the starting state q2 is the only accepting state Figure 2: Visual representation of D.

This can also be done using the helpful DFABuilder:

const dfa = new DFABuilder("ab")
    /* Add states to the DFA */
    .withNotFinalStates("q0", "q1")
    .withFinalStates("q2")

    /* Define the transition function for each state */
    .withEdges.from("q0").toSelf().over("b")
    .withEdges.from("q0").to("q1").over("a")
    .withEdges.from("q1").to("q2").over("a")
    .withEdges.from("q1").to("q0").over("b")
    .withEdges.from("q2").toSelf().over("ab")
    .getResult()

Figure 3: Creating a DFA using a DFABuilder.

Dependencies (0)

    Dev Dependencies (7)

    Package Sidebar

    Install

    npm i ts-automata

    Weekly Downloads

    3

    Version

    0.5.0

    License

    MIT

    Unpacked Size

    75 kB

    Total Files

    7

    Last publish

    Collaborators

    • zakrok09