ademenev

ademenev

Using states that are more than a single atom

AshStateMachine is all about transitions between states, and the state is represented by an atom. But transitions are only one part of FSMs, in general we want them to react on inputs, and the state is more than a single constant value.

Let’s consider a simple coffee vending machine that can make several types of drinks, each having its own price. In its initial state, the machine will only accept one input - “coin inserted”, and that input will transition to “drink selection” state. The input has an additional property – the coin’s monetary value, and the “drink selection” state should also store that monetary value. In “drink selection” state, two inputs can be accepted: “coin inserted”, which will keep the machine in “drink selection” state and increase the stored monetary value, and “drink selected” input which will transition to “drink preparation” state, but only if enough coins were inserted to cover the cost of the selected drink.

Reading the documentation, I do not see a declarative way to define that state machine. Am I missing something?

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

The main thing that makes ash_state_machine different in design from other state machine tools is that the actions are still the source of truth. The state transitions defined in the state machine block describe what transitions are allowed, and then in a given action you actually perform the state transition.

attributes do
  attribute :unspent_cents, :integer, allow_nil?: false, default: 0 
end

state_machine do
  transitions do
    initial_states [:waiting_for_coin]

    transition :coin_inserted, from: :waiting_for_coin, to: :drink_selection
    transition :drink_selected, from: :drink_selected, to: :drink_preparation
  end
end

actions do
  update :coin_inserted do
    argument :coin_amount, :integer, allow_nil?: false
    change atomic_update(:unspent_cents, expr(unsepent_cents + ^arg(:coin_amount))
    change transition_state(:drink_selection)
  end

  update :drink_selected do
    argument :selection, :atom, constraints: [one_of: [:coke, :diet_coke]]
    change set_attribute(:drink_selection, arg(:selection))
    change transition_state(:drink_preparation), where: [CanAffordDrinkSelection]
  end
end

This is just an example, certainly not 100% correct, but thrown together as an example of what it looks like in practice.

Where Next?

Popular in Questions Top

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement