Help language development. Donate to The Perl Foundation

DSL::FiniteStateMachines zef:antononcube last updated on 2023-03-26

cce807fd3b5e6948320307717c85733530a904fb/

Raku DSL::FiniteStateMachines

License: Artistic-2.0

In brief

This repository is for a Raku package with class definitions and functions for creation of Finite State Machines (FSMs) and their execution.


Usage example (Address book)

Here we load the definition of the class AddressBookCaller (provided by this package) and related entities package, "DSL::Entity::AddressBook":

use DSL::FiniteStateMachines::AddressBookCaller;

use DSL::Entity::AddressBook;
use DSL::Entity::AddressBook::ResourceAccess;
# (Any)

Here we obtain a resource object to access a (particular) address book:

my $resourceObj = DSL::Entity::AddressBook::resource-access-object();
# DSL::Entity::AddressBook::ResourceAccess.new

Here we create the FSM and show its states:

my DSL::FiniteStateMachines::AddressBookCaller $abcFSM .= new;

$abcFSM.make-machine(($resourceObj,));

.say for $abcFSM.states;
# PrioritizedList => State object < id => PrioritizedList, action => -> $obj { #`(Block|6268295504336) ... } >
# ListOfItems => State object < id => ListOfItems, action => -> $obj { #`(Block|6268295504408) ... } >
# Exit => State object < id => Exit, action => -> $obj { #`(Block|6268295504480) ... } >
# WaitForRequest => State object < id => WaitForRequest, action => -> $obj { #`(Block|6268295504552) ... } >
# WaitForCallCommand => State object < id => WaitForCallCommand, action => -> $obj { #`(Block|6268295504624) ... } >
# ActOnItem => State object < id => ActOnItem, action => -> $obj { #`(Block|6268295504696) ... } >
# Help => State object < id => Help, action => -> $obj { #`(Block|6268295504768) ... } >
# AcquireItem => State object < id => AcquireItem, action => -> $obj { #`(Block|6268295504840) ... } >

(Each pair shows the name of the state object and the object itself.)

Here is the graph of FSM's state transitions:

```perl6, output.prompt=NONE, output-lang=mermaid $abcFSM.to-mermaid-js

```mermaid
graph TD
    PrioritizedList --> |priorityListGiven|WaitForCallCommand
    ListOfItems --> |manyItems|WaitForCallCommand
    ListOfItems --> |noItems|WaitForCallCommand
    ListOfItems --> |noChange|WaitForCallCommand
    ListOfItems --> |uniqueItemObtained|AcquireItem

    WaitForRequest --> |itemSpec|ListOfItems
    WaitForRequest --> |startOver|WaitForRequest
    WaitForRequest --> |prioritize|PrioritizedList
    WaitForRequest --> |help|Help
    WaitForRequest --> |quit|Exit
    WaitForCallCommand --> |translated|WaitForRequest
    WaitForCallCommand --> |unchanged|WaitForRequest
    ActOnItem --> |stay|ActOnItem
    ActOnItem --> |quit|Exit
    Help --> |helpGiven|WaitForCallCommand
    AcquireItem --> |acquired|ActOnItem

Here is how the dataset of the created FSM looks like:

.say for $abcFSM.dataset.pick(3);
# {Company => LOTR, DiscordHandle => sean.astin#1753, Email => [email protected], Name => Sean Astin, Phone => 365-119-3172, Position => actor}
# {Company => X-Men, DiscordHandle => richard.donner#1820, Email => [email protected], Name => Richard Donner, Phone => 849-923-9946, Position => producer}
# {Company => Caribbean Pirates, DiscordHandle => geoffrey.rush#5746, Email => [email protected], Name => Geoffrey Rush, Phone => 797-199-7144, Position => actor}

For an interactive execution of the FSM we use the command:

#$abcFSM.run('WaitForCallCommand');

Here we run the FSM with a sequence of commands:

$abcFSM.run('WaitForCallCommand', 
        ["call an actor from LOTR", "", 
         "take last three", "", 
         "take the second", "", "", 
         "2", "5", "", 
         "quit"]);
# 🔊 PLEASE enter call request.
# ⚙️Input: ⚙️"call an actor from LOTR"
# ⚙️Translated input: ⚙️filter by Position is "actor" and Company is "LOTR"
# 🔊 LISTING items.
# ⚙️ListOfItems: Obtained the records:
# ⚙️+----------------------+--------------+----------+--------------------------------+-----------------+---------+
# ⚙️|    DiscordHandle     |    Phone     | Position |             Email              |       Name      | Company |
# ⚙️+----------------------+--------------+----------+--------------------------------+-----------------+---------+
# ⚙️|   andy.serkis#8484   | 408-573-4472 |  actor   |   [email protected]    |   Andy Serkis   |   LOTR  |
# ⚙️|   elijah.wood#7282   | 321-985-9291 |  actor   |     [email protected]     |   Elijah Wood   |   LOTR  |
# ⚙️|  ian.mckellen#9077   | 298-517-5842 |  actor   |    [email protected]     |   Ian McKellen  |   LOTR  |
# ⚙️|    liv.tyler#8284    | 608-925-5727 |  actor   |    [email protected]     |    Liv Tyler    |   LOTR  |
# ⚙️|  orlando.bloom#6219  | 570-406-4260 |  actor   |  [email protected]   |  Orlando Bloom  |   LOTR  |
# ⚙️|   sean.astin#1753    | 365-119-3172 |  actor   |   [email protected]    |    Sean Astin   |   LOTR  |
# ⚙️| viggo.mortensen#7157 | 287-691-8138 |  actor   | [email protected] | Viggo Mortensen |   LOTR  |
# ⚙️+----------------------+--------------+----------+--------------------------------+-----------------+---------+
# 🔊 PLEASE enter call request.
# ⚙️Input: ⚙️"take last three"
# 🔊 LISTING items.
# ⚙️ListOfItems: Obtained the records:
# ⚙️+--------------------------------+----------+---------+--------------+----------------------+-----------------+
# ⚙️|             Email              | Position | Company |    Phone     |    DiscordHandle     |       Name      |
# ⚙️+--------------------------------+----------+---------+--------------+----------------------+-----------------+
# ⚙️|  [email protected]   |  actor   |   LOTR  | 570-406-4260 |  orlando.bloom#6219  |  Orlando Bloom  |
# ⚙️|   [email protected]    |  actor   |   LOTR  | 365-119-3172 |   sean.astin#1753    |    Sean Astin   |
# ⚙️| [email protected] |  actor   |   LOTR  | 287-691-8138 | viggo.mortensen#7157 | Viggo Mortensen |
# ⚙️+--------------------------------+----------+---------+--------------+----------------------+-----------------+
# 🔊 PLEASE enter call request.
# ⚙️Input: ⚙️"take the second"
# 🔊 LISTING items.
# ⚙️ListOfItems: Obtained the records:
# ⚙️+---------+-----------------+----------+------------+--------------+---------------------------+
# ⚙️| Company |  DiscordHandle  | Position |    Name    |    Phone     |           Email           |
# ⚙️+---------+-----------------+----------+------------+--------------+---------------------------+
# ⚙️|   LOTR  | sean.astin#1753 |  actor   | Sean Astin | 365-119-3172 | [email protected] |
# ⚙️+---------+-----------------+----------+------------+--------------+---------------------------+
# 🔊 ACQUIRE item: {Company => LOTR, DiscordHandle => sean.astin#1753, Email => [email protected], Name => Sean Astin, Phone => 365-119-3172, Position => actor}
# ⚙️Acquiring contact info for : ⚙️Sean Astin
# 🔊 ACT ON item: {Company => LOTR, DiscordHandle => sean.astin#1753, Email => [email protected], Name => Sean Astin, Phone => 365-119-3172, Position => actor}
# ⚙️[1] email, [2] phone message, [3] phone call, [4] discord message, or [5] nothing
# ⚙️(choose one...)
# ⚙️Input: ⚙️"2"
# ⚙️message by phone 365-119-3172
# 🔊 ACT ON item: {Company => LOTR, DiscordHandle => sean.astin#1753, Email => [email protected], Name => Sean Astin, Phone => 365-119-3172, Position => actor}
# ⚙️[1] email, [2] phone message, [3] phone call, [4] discord message, or [5] nothing
# ⚙️(choose one...)
# ⚙️Input: ⚙️"5"
# ⚙️do nothing
# 🔊 SHUTTING down...

Object Oriented Design

Here is the Unified Modeling Language (UML) diagram corresponding to the classes in this package:

classDiagram
class DSL_FiniteStateMachines_DataObtainer {
  +$!FSMGrammar
  +$!acquiredData
  +$!currentStateID
  +$!dataset
  +$!datasetColumnNames
  +$!initDataset
  +$!itemSpec
  +$!itemSpecCommand
  +%!states
  +&!ECHOLOGGING
  +&!choose-transition
  +&!re-say
  +&!re-warn
  +@!grammar-args
  +BUILDALL()
  +ECHOLOGGING()
  +FSMGrammar()
  +acquiredData()
  +add-state()
  +add-transition()
  +apply-query-retrieve-act-pattern()
  +choose-transition()
  +currentStateID()
  +dataset()
  +datasetColumnNames()
  +grammar-args()
  +init-dataset()
  +initDataset()
  +is-metadata-dataset()
  +is-metadata-row()
  +itemSpec()
  +itemSpecCommand()
  +make-machine()
  +re-say()
  +re-warn()
  +run()
  +states()
  +to-wl()
  +transition-target()
}
DSL_FiniteStateMachines_DataObtainer --|> DSL_FiniteStateMachines_FSMish
DSL_FiniteStateMachines_DataObtainer --|> DSL_FiniteStateMachines_QueryRetrieveActFSMRole


class DSL_FiniteStateMachines_State {
  +$!id
  +$!implicitNext
  +&!action
  +@!explicitNext
  +BUILDALL()
  +Str()
  +action()
  +explicitNext()
  +gist()
  +id()
  +implicitNext()
  +to-wl()
}

class DSL_FiniteStateMachines_Transition {
  +$!id
  +$!to
  +BUILDALL()
  +Str()
  +gist()
  +id()
  +to()
}

class DSL_FiniteStateMachines_QueryRetrieveActFSMRole {
  <<role>>
  +$!FSMGrammar
  +$!acquiredData
  +$!dataset
  +$!datasetColumnNames
  +$!initDataset
  +$!itemSpec
  +$!itemSpecCommand
  +@!grammar-args
  +apply-query-retrieve-act-pattern()
  +init-dataset()
  +is-metadata-dataset()
  +is-metadata-row()
}
DSL_FiniteStateMachines_QueryRetrieveActFSMRole --|> DSL_FiniteStateMachines_FSMish


class DSL_FiniteStateMachines_AddressBookCaller {
  +$!FSMGrammar
  +$!acquiredData
  +$!currentStateID
  +$!dataset
  +$!datasetColumnNames
  +$!initDataset
  +$!itemSpec
  +$!itemSpecCommand
  +%!states
  +&!ECHOLOGGING
  +&!choose-transition
  +&!re-say
  +&!re-warn
  +@!grammar-args
  +BUILDALL()
  +ECHOLOGGING()
  +FSMGrammar()
  +acquiredData()
  +add-state()
  +add-transition()
  +apply-query-retrieve-act-pattern()
  +choose-transition()
  +currentStateID()
  +dataset()
  +datasetColumnNames()
  +grammar-args()
  +init-dataset()
  +initDataset()
  +is-metadata-dataset()
  +is-metadata-row()
  +itemSpec()
  +itemSpecCommand()
  +make-machine()
  +re-say()
  +re-warn()
  +run()
  +states()
  +to-wl()
  +transition-target()
}
DSL_FiniteStateMachines_AddressBookCaller --|> DSL_FiniteStateMachines_FSMish
DSL_FiniteStateMachines_AddressBookCaller --|> DSL_FiniteStateMachines_QueryRetrieveActFSMRole


class DSL_FiniteStateMachines_FSMish {
  <<role>>
  +$!currentStateID
  +%!states
  +&!ECHOLOGGING
  +&!choose-transition
  +&!re-say
  +&!re-warn
  +add-state()
  +run()
  +to-wl()
}

(The UML spec and the Mermaid spec above were automatically generated with "UML::Translators", [AAp5].)

Here is the MermaidJS spec generation shell command:

to-uml-spec --format=MermaidJS DSL::FiniteStateMachines 

References

Packages

[AAp1] Anton Antonov, DSL::Shared Raku package, (2020), GitHub/antononcube.

[AAp2] Anton Antonov, DSL::Entity::Metadata Raku package, (2021), GitHub/antononcube.

[AAp3] Anton Antonov, DSL::English::DataAcquisitionWorkflows Raku package, (2021), GitHub/antononcube.

[AAp4] Anton Antonov, DSL::Entity::AddressBook Raku package, (2023), GitHub/antononcube.

[AAp5] Anton Antonov, UML::Translators Raku package, (2021), GitHub/antononcube.

Videos

[AAv1] Anton Antonov, "Multi-language Data Wrangling and Acquisition Conversational Agents (in Raku)", (2021), YouTube.com.