DEV Community

Cover image for OneMaton: The 1LoC Automata Pattern That Changes Everything
owly
owly

Posted on

OneMaton: The 1LoC Automata Pattern That Changes Everything

OneMaton Software Design Pattern

the OneMaton software design pattern enables the construction of Automata (state machines) by adding each state (Automaton) with 1 line of code.


Merits

  1. capsulation of states

  2. clean code

  3. modular reusable states

  4. Lego like algorithm construction

  5. the skill is only attentive to the triggers of the active automaton (current state)

  6. in the python version automatons are methods or functions.


How This Differs Than Using The AlgPart Objects To Construct An Algorithm?

1. activation triggers do not affect the active automata whereas with algparts you end up calling an additional algorithm while the older one runs unless you run an extra step to manage that.

2. the algorithmic queue is free because only short immediate actions are sent, run and done. whereas running an automata via a algparts keeps an algorithm actively listening for triggers while its in the algorithm que.


Example Use

class AutomataTest(Automata):
    def __init__(self):
        super().__init__()
        self.automatas[0] = AtmtStrTrg("test1",self).action # declares automata starts and move to next state
        self.automatas[1] = AtmtDeclare(self, Responder("waiting", "waitin", "wait maxing")).action
        # move to next state after "ok"
        whilst = Responder("process in progress", "process underway mkay")
        cancel = Responder("cancelling")
        finished = Responder("process completed")
        process = AtmtProcessV1(self, whilst,cancel,finished)
        self.automatas[2] = process.action # move to next state after timeout(or cancel to reset to state 0)
Enter fullscreen mode Exit fullscreen mode

see the OneMaton code at: https://github.com/yotamarker/LivinGrimoire/blob/main/livingrimoire%20start%20here/LivinGrimoire%20python%20easy%20start%20packet/DLC/OneMaton.py

Top comments (0)