From 16bab01622e67898a036ce814afdcbc08850e511 Mon Sep 17 00:00:00 2001 From: Zakrok09 Date: Fri, 29 Mar 2024 15:16:40 +0100 Subject: [PATCH] doc: basic readme and documentation for FA classes --- README.md | 57 +++++++++++++++++++++++++++++++++++++ package.json | 2 +- src/automata/regular/DFA.ts | 2 +- src/automata/regular/NFA.ts | 5 ++++ 4 files changed, 64 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e69de29..cc1ce0e 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,57 @@ +# TS Automata + +[![NPM Package](https://img.shields.io/npm/v/ts-automata.svg?style=flat)](https://npmjs.org/package/ts-automata "View this project on npm") + +A TypeScript/JavaScript library for managing automata. + +```shell +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 + +Automatons can be created by initialising 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. + +```typescript +/* Create an alphabet for the Finite Automaton */ +const alphabet = new Set(); +alphabet.add(toChar("a")); +alphabet.add(toChar("b")); + +/* Initialize the DFA */ +const dfa = new DFA(alphabet, "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", toChar('b'), "q0"); +dfa.addEdge("q0", toChar('a'), "q1"); +dfa.addEdge("q1", toChar('a'), "q2"); +dfa.addEdge("q1", toChar('b'), "q0"); +dfa.addEdge("q2", toChar('a'), "q2"); +dfa.addEdge("q2", toChar('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](https://i.imgur.com/pRuPlEv.jpeg "Image of the DFA described in the code abov") +_Figure 2: Visual representation of D._ + diff --git a/package.json b/package.json index b3ddae4..78877ac 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "test": "jest", "build": "tsup" }, - "keywords": ["automata", "typescript"], + "keywords": ["automata", "typescript", "math"], "author": "Kiril Panayotov", "license": "MIT", "devDependencies": { diff --git a/src/automata/regular/DFA.ts b/src/automata/regular/DFA.ts index b617571..6f02ee8 100644 --- a/src/automata/regular/DFA.ts +++ b/src/automata/regular/DFA.ts @@ -6,7 +6,7 @@ import {IllegalArgument, IllegalAutomatonState} from "../../exceptions/exception /** * Represents a deterministic finite automaton. * - * @class DFA + * @extends FiniteAutomaton with DFAState. */ export class DFA extends FiniteAutomaton { diff --git a/src/automata/regular/NFA.ts b/src/automata/regular/NFA.ts index ec16597..20edfb1 100644 --- a/src/automata/regular/NFA.ts +++ b/src/automata/regular/NFA.ts @@ -3,6 +3,11 @@ import {DFA, Symbol, toChar} from "../../index"; import {NFAState} from "../../states/RegularStates"; import {IllegalArgument} from "../../exceptions/exceptions"; +/** + * Class representation of a non-deterministic finite automaton. + * + * @extends FiniteAutomaton with NFAState. + */ export class NFA extends FiniteAutomaton { /**