DSL::FiniteStateMachines

Introduction

This blog post introduces and exemplifies the Raku package “DSL::FiniteStateMachines” that has class definitions and functions for creation of Finite State Machines (FSMs) and their execution.

This video excerpt, [AAv1], demonstrates the usage workflow of a particular FSM made with that package:


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;

# WaitForCallCommand => State object < id => WaitForCallCommand, action => -> $obj { #`(Block|4570571503456) ... } >
# ListOfItems => State object < id => ListOfItems, action => -> $obj { #`(Block|4570571503528) ... } >
# Help => State object < id => Help, action => -> $obj { #`(Block|4570571503600) ... } >
# Exit => State object < id => Exit, action => -> $obj { #`(Block|4570571503672) ... } >
# PrioritizedList => State object < id => PrioritizedList, action => -> $obj { #`(Block|4570571503744) ... } >
# AcquireItem => State object < id => AcquireItem, action => -> $obj { #`(Block|4570571503816) ... } >
# ActOnItem => State object < id => ActOnItem, action => -> $obj { #`(Block|4570571503888) ... } >
# WaitForRequest => State object < id => WaitForRequest, action => -> $obj { #`(Block|4570571503960) ... } >

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

Here is the graph of FSM’s state transitions:

$abcFSM.to-mermaid-js

Remark: In order to obtain Mathematica — or Wolfram Language (WL) — representation of the state transitions graph the method to-wl can be used.

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

.say for $abcFSM.dataset.pick(3);

# {Company => X-Men, DiscordHandle => hugh.jackman#1391, Email => hugh.jackman.523@aol.com, Name => Hugh Jackman, Phone => 940-463-2296, Position => actor}
# {Company => Caribbean Pirates, DiscordHandle => jack.davenport#1324, Email => jack.davenport.152@icloud.net, Name => Jack Davenport, Phone => 627-500-7919, Position => actor}
# {Company => LOTR, DiscordHandle => robert.shaye#6399, Email => robert.shaye.768@gmail.com, Name => Robert Shaye, Phone => 292-252-6866, Position => producer}

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.
# filter by Position is "actor" and Company is "LOTR"
# 🔊 LISTING items.
# ⚙️ListOfItems: Obtained the records:
# ⚙️+--------------+-----------------+--------------------------------+----------+---------+----------------------+
# ⚙️|    Phone     |       Name      |             Email              | Position | Company |    DiscordHandle     |
# ⚙️+--------------+-----------------+--------------------------------+----------+---------+----------------------+
# ⚙️| 408-573-4472 |   Andy Serkis   |   andy.serkis.981@gmail.com    |  actor   |   LOTR  |   andy.serkis#8484   |
# ⚙️| 321-985-9291 |   Elijah Wood   |     elijah.wood.53@aol.com     |  actor   |   LOTR  |   elijah.wood#7282   |
# ⚙️| 298-517-5842 |   Ian McKellen  |    ian.mckellen581@aol.com     |  actor   |   LOTR  |  ian.mckellen#9077   |
# ⚙️| 608-925-5727 |    Liv Tyler    |    liv.tyler1177@gmail.com     |  actor   |   LOTR  |    liv.tyler#8284    |
# ⚙️| 570-406-4260 |  Orlando Bloom  |  orlando.bloom.914@gmail.net   |  actor   |   LOTR  |  orlando.bloom#6219  |
# ⚙️| 365-119-3172 |    Sean Astin   |   sean.astin.1852@gmail.net    |  actor   |   LOTR  |   sean.astin#1753    |
# ⚙️| 287-691-8138 | Viggo Mortensen | viggo.mortensen1293@icloud.com |  actor   |   LOTR  | viggo.mortensen#7157 |
# ⚙️+--------------+-----------------+--------------------------------+----------+---------+----------------------+
# 🔊 PLEASE enter call request.
# 🔊 LISTING items.
# ⚙️ListOfItems: Obtained the records:
# ⚙️+--------------+----------------------+----------+-----------------+---------+--------------------------------+
# ⚙️|    Phone     |    DiscordHandle     | Position |       Name      | Company |             Email              |
# ⚙️+--------------+----------------------+----------+-----------------+---------+--------------------------------+
# ⚙️| 570-406-4260 |  orlando.bloom#6219  |  actor   |  Orlando Bloom  |   LOTR  |  orlando.bloom.914@gmail.net   |
# ⚙️| 365-119-3172 |   sean.astin#1753    |  actor   |    Sean Astin   |   LOTR  |   sean.astin.1852@gmail.net    |
# ⚙️| 287-691-8138 | viggo.mortensen#7157 |  actor   | Viggo Mortensen |   LOTR  | viggo.mortensen1293@icloud.com |
# ⚙️+--------------+----------------------+----------+-----------------+---------+--------------------------------+
# 🔊 PLEASE enter call request.
# 🔊 LISTING items.
# ⚙️ListOfItems: Obtained the records:
# ⚙️+---------+-----------------+------------+--------------+----------+---------------------------+
# ⚙️| Company |  DiscordHandle  |    Name    |    Phone     | Position |           Email           |
# ⚙️+---------+-----------------+------------+--------------+----------+---------------------------+
# ⚙️|   LOTR  | sean.astin#1753 | Sean Astin | 365-119-3172 |  actor   | sean.astin.1852@gmail.net |
# ⚙️+---------+-----------------+------------+--------------+----------+---------------------------+
# 🔊 ACQUIRE item: {Company => LOTR, DiscordHandle => sean.astin#1753, Email => sean.astin.1852@gmail.net, 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 => sean.astin.1852@gmail.net, 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...)
# ⚙️message by phone 365-119-3172
# 🔊 ACT ON item: {Company => LOTR, DiscordHandle => sean.astin#1753, Email => sean.astin.1852@gmail.net, 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...)
# ⚙️do nothing
# 🔊 SHUTTING down...


Object Oriented Design

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

(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.

Advertisement

One thought on “DSL::FiniteStateMachines

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s