• Stars
    star
    192
  • Rank 196,006 (Top 4 %)
  • Language
    Clojure
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Minimalistic finite state machine (FSM) in Clojure

tilakone

Minimalistic finite state machine (FSM) in Clojure.

Tilakone (eng. State machine)

Noun

state machine (plural state machines)

 (computing theory) A formalism for describing computation, consisting of a set of 
 states and a transition function describing when to move from one state to another.

source: wiktionary.org

Clojars Project

Usage

Tested with Clojure 1.10.0

All bundled:

[metosin/tilakone "0.0.4"]

Optionally, the modules can be required separately:

[metosin/tilakone.core "0.0.4"]
[metosin/tilakone.schema "0.0.4"]

Intro

The excellent cdorrat/reduce-fsm library has a nice FSM example:

; from https://github.com/cdorrat/reduce-fsm#basic-fsm

(defn inc-val [val & _] (inc val))

(fsm/defsm count-ab
  [[:start
    \a -> :found-a]
   [:found-a
    \a ->  :found-a
    \b -> {:action inc-val} :start
    _ -> :start]])

;; We can use the generated fsm like any function
(map (partial count-ab 0) ["abaaabc" "aaacb" "bbbcab"])
;; returns => (2 0 1)

This is very nice and works for many cases. The reduce-fsm library uses macros to define the FSM and actions are functions. This makes the reduce-fsm less suitable if you need to serialize your FSMs.

Tilakone is a similar FSM library, but it uses pure data to define the FSM states (no macros needed) and the action functions can be defined separately.

Here's the same example with tilakone:

(ns example.count-ab-example
  (:require [tilakone.core :as tk :refer [_]]))

; State definitions, pure data here:

(def count-ab-states
  [{::tk/name        :start
    ::tk/transitions [{::tk/on \a, ::tk/to :found-a}
                      {::tk/on _}]}
   {::tk/name        :found-a
    ::tk/transitions [{::tk/on \a}
                      {::tk/on \b, ::tk/to :start, ::tk/actions [:inc-val]}
                      {::tk/on _, ::tk/to :start}]}])

; FSM has states, a function to execute actions, and current state and value:

(def count-ab
  {::tk/states  count-ab-states
   ::tk/action! (fn [{::tk/keys [action] :as fsm}]
                  (case action
                    :inc-val (update fsm :count inc)))
   ::tk/state   :start
   :count       0})

; Lets apply same inputs to our FSM:

(->> ["abaaabc" "aaacb" "bbbcab"]
     (map (partial reduce tk/apply-signal count-ab))
     (map :count))
;=> (2 0 1)

Note that the state definitions in count-ab-states are pure data.

Also, there is one extra state transfer defined in tilakone example from state :start. In reduce_fsm the default behaviour for signal (that is, if no transition is found for signal) is to stay at the current state, but tilakone treats all undeclared state transfers as errors. For this purpose the above example declares an explicit rule to allow any unmatched signal (_) in state :start to be handled as transition to state :start.

The count-ab is the actual FSM. It contains the state declarations, optional actions function (more of actions later), current state and current value. Note that the state and value can be any clojure value.

The tilakone.core/apply-signal function accepts an FSM and a signal. It returns the FSM with possibly updated state and value.

Documentation

  • TODO

Comparing reduce-fsm and Tilakone

  • reduce-fsm is older project with far more users
  • reduce-fsm is faster (at least at the moment)
  • reduce-fsm is more feature complete
  • tilakone FSMs are pure dataâ„¢
  • tilakone code is quite a bit simpler with no macros and less code (reduce-fsm 592 lines, tilakone 145 lines)

TODO

  • proper documentation
  • examples on :enter/:leave actions, state guards, etc
  • add tilakone visualization
  • add perf tests

License

Copyright © 2019 Metosin Oy

Distributed under the Eclipse Public License, the same as Clojure.

More Repositories

1

malli

High-performance data-driven data specification library for Clojure/Script.
Clojure
1,419
star
2

reitit

A fast data-driven routing library for Clojure/Script
Clojure
1,313
star
3

spec-tools

Clojure(Script) tools for clojure.spec
Clojure
586
star
4

muuntaja

Clojure library for fast http api format negotiation, encoding and decoding.
Clojure
410
star
5

jsonista

Clojure library for fast JSON encoding and decoding.
Clojure
384
star
6

ring-swagger

Swagger Spec for Clojure Web Apps
Clojure
360
star
7

kekkonen

A remote (CQRS) API library for Clojure.
Clojure
220
star
8

sieppari

Small, fast, and complete interceptor library for Clojure/Script
Clojure
189
star
9

pohjavirta

Fast & Non-blocking Clojure wrapper for Undertow
Clojure
162
star
10

ring-http-response

Handling HTTP Statuses with Clojure(Script)
Clojure
139
star
11

schema-tools

Clojure(Script) tools for Plumatic Schema
Clojure
106
star
12

porsas

Experimental stuff for going fast with Clojure + JDBC & Async SQL
Clojure
95
star
13

talvi

Opinionated and Performant Web Application Stack for Clojure/Script
79
star
14

schema-viz

Plumatic Schema visualization using Graphviz.
Clojure
74
star
15

potpuri

Common clojure stuff.
Clojure
69
star
16

komponentit

Collection of bespoke Reagent components
Clojure
61
star
17

bat-test

Fast Clojure.test runner for Boot and Leiningen
Clojure
60
star
18

ring-swagger-ui

Swagger UI packaged for Ring Apps
Clojure
48
star
19

scjsv

Simple Clojure JSON-Schema Validator
Clojure
47
star
20

testit

Midje like assertions for Clojure.test
Clojure
43
star
21

compojure-api-examples

Compojure API example
Clojure
32
star
22

maailma

Opinionated environment variables library
Clojure
31
star
23

vega-tools

Utilities for working with Vega visualization grammar in ClojureScript.
Clojure
27
star
24

fnhouse-swagger

Swagger integration for fnhouse
Clojure
22
star
25

spec-swagger

Master Swagger2 & OpenAPI3 specs with Clojure(Script) & clojure.spec
Clojure
21
star
26

virhe

Beautiful Error Message for Clojure/Script
Clojure
20
star
27

reagent-dev-tools

Development tool panel for Reagent
Clojure
19
star
28

mallitaulut

Extract Malli schemas from SQL table schemas.
Clojure
17
star
29

eines

Simple Clojure and ClojureScript library for WebSocket communication
Clojure
16
star
30

sauna-todo

Simple full-stack TODO app example for demonstrating Clojure(script)
Clojure
16
star
31

packaging-clojure-examples

Packaging a full-stack Clojure web app for production
Clojure
16
star
32

loiste

Excellent Excel library
Clojure
16
star
33

clojure-bootcamp

Clojure
14
star
34

metosin-common

Random collection of various namespaces used in multiple Metosin projects
Clojure
13
star
35

viesti

Data-Driven Message Dispatcher for Clojure/Script
12
star
36

sormilla

Playing with Leap Motion and Parrot AR.Drone
Clojure
11
star
37

malli.io

Malli playground, https://malli.io
Clojure
11
star
38

lomakkeet

Proof of concept: Form library for Reagent
Clojure
11
star
39

c2

Demo about compojure-api2 stuff
Clojure
8
star
40

cloud-busting

Basis for using Terraform to manage application runtime in AWS
HCL
7
star
41

palikka

Opinionated component library
Clojure
7
star
42

tyylikas

Clojure linter and fixer
Clojure
6
star
43

oksa

Generate GraphQL queries using Clojure data structures.
Clojure
6
star
44

kekkonen-sample

Sample project With Kekkonen
Clojure
5
star
45

clojure-bootcamp-setup

Setup instructions for Metosin Clojure Bootcamp training
Clojure
5
star
46

web-schemas

Prismatic Schema extensions for the Web.
Clojure
5
star
47

compojure-intro

compojure-intro
Clojure
4
star
48

clj-suomi

A Clojure library designed to access Finnish code sets.
Clojure
4
star
49

lokit

Single dependency for logging on the JVM
Clojure
4
star
50

tom

Tom, a graph-based component library
Clojure
4
star
51

om-dev-tools

Clojure
4
star
52

bootcamp-2019-04-08

Bootcamp 2019-04-08
Clojure
4
star
53

boot-deps-size

Boot task to check size of dependencies
Clojure
4
star
54

compojure-api-template

Compojure Api Template
Clojure
4
star
55

bootcamp-2021-feb

Lessons and exercises for bootcamp in February 2021
Clojure
4
star
56

kekkonen-building-permit-example

a complex simulated real-life case example showcase project
Clojure
4
star
57

terraform-study-group

3
star
58

training-2023-05-32

Advanced Clojure training
Clojure
3
star
59

open-source

Home page for Metosin's open source development work
JavaScript
3
star
60

lein-simulflow

ABANDONED: Combine several lein auto tasks for leaner workflow.
Clojure
3
star
61

bootcamp-2018-03-15-sample-app

Sample app for bootcamp 2018-03-15
Clojure
3
star
62

linkit

Om.next + Kekkonen test
Clojure
3
star
63

clj-ai-meetup

Case Studies in AI for Clojure Tampere meetup
Clojure
3
star
64

bootcamp-2

Bootcamp 2
Clojure
2
star
65

bootcamp-2018-05-04

Sample app for bootcamp at 2018-05-04
Clojure
2
star
66

bootcamp3

Refactored bootcamp
Clojure
2
star
67

clojure-koulutus-2023-01-24-esitehtavat

Clojure ja ClojureScript koulutus 2023-01-24 esitehtävät
Clojure
2
star
68

clojure-bootcamp-20150130

Clojure Bootcamp 2015-01-30 for Affecto
Clojure
2
star
69

clojure-finland-2018-05-30-cljs-ws-demo

ClojureScript and WebSocket demo for Clojure Finland 2018-05-20 meetup
Clojure
2
star
70

juustometsae

vain käyttötarkoitus puuttuu
2
star
71

2016-09-09-clojure-training

Material for 2016-09-09 intermediate Clojure training topics about Tooling and Workflow; and Full-stack apps
Clojure
2
star
72

training-day-1

Example materials for first part of training
Clojure
2
star
73

postgres-tools

WIP Postgresql utilities
Clojure
2
star
74

boot-alt-http

Simple boot http server task to serve files from classpath.
Clojure
2
star
75

clojure-koulutus-2023-01-24

Koulutusmateriaali 2023-01-24 koulutukseen
Clojure
1
star
76

clojurebridge-intro

Intro project for ClojureBridge, in Finnish. Olkaa hyvä.
Clojure
1
star
77

clojure-bootcamp-intro

Bootcamp intro project
Clojure
1
star
78

reitit-example

Sample layout for reitit
Clojure
1
star
79

bootbook

BootBook :- Clojure Bootcamp book-store example
Clojure
1
star
80

docker-circle-convox

Dockerfile
1
star
81

docker-circle-lein

1
star
82

ks-example

Simple example project with clj and cljs
Clojure
1
star
83

rabbitmq-agent

Clojure
1
star
84

bootcamp-luminus-2021-feb

Luminus application for 2021 February bootcamp
Clojure
1
star
85

compojure-api-sample

Compojure-api sample project with Component
Clojure
1
star
86

2016-09-09-clojure-training-2

Material for 2016-09-09 intermediate Clojure training: perf, polymorfic, data, diy weblib
Clojure
1
star
87

bootcamp-20170314

Bootcamp example for 2017/03/14-16 bootcamp
Clojure
1
star
88

clojurebridge-helsinki

Homepage for ClojureBridge Finland
CSS
1
star