Machines¶
A machine keeps something in exactly one of its states and moves it between them when events come. See machines.
machine¶
Makes a state machine from a block and starts it in its initial state.
machine spec
spec(block!)/from saved(block!): Resumes it where a snapshot of it left it, instead; saved: What snapshot gave
The block says the states and the ways between them:
Word |
What it says |
|---|---|
|
the machine’s own fields, as in an object; code inside reads them as |
|
the state the machine, or the state this is in, starts in |
|
a state; the block holds its hooks, and states of its own with their |
|
a state that is in all the states of its block at once; an event goes to each of them |
|
a state that ends the machine; the outcome word and the block may be left out |
|
code run when the machine comes into the state and when it leaves it |
|
code run when an event comes that has no way out of the state |
|
coming back, the state starts from its |
|
a way out of |
|
after |
|
after the rest: the fields of the data changed when the way is taken |
|
with no |
|
the block has |
|
taken when code run on the way into a state fails; the event is the error |
|
the block runs, and the branch |
|
in a state: when the machine has been in it that long, it sends itself |
|
at the top: when no event has been sent for that long, the machine sends itself |
|
in a state: the block runs as a task while the machine is in it; its end sends |
|
in a state: the machine of the block |
|
one machine of |
|
at the top: code run after each event, |
|
at the top: every tab of the site that makes a machine with this name has the same one; see below |
|
in a state: the words of the fragment |
|
an event with no way out waits for a state that has one; |
A state with timeout needs a way out on timeout from it or from a state around it, and on timeout needs
a timeout in its state or inside it. on expired needs ttl and goes to a final state; with no on expired
the machine ends where it is, with the outcome expired. on done needs a run or an invoke in its state or
inside it, and a state has one of them at most; leaving it stops the task or the machine invoked, which takes no
events from outside. error, timeout, expired and done are the machine’s own events: send does not take
them, nor defer.
Code in a machine reads data/field and event/field. A condition that fails while it is worked out does not
hold. A target is a name, or a path from the state the way is in, outer/inner; it is looked for among the
states next to the way, then in each level further out.
Each event is finished before the next one starts: one sent by the machine’s own code waits its turn.
machine gives the machine in its initial states at once, but their on-enter, timeouts, run, invoke and
ways with no event happen once the expression that called machine is done, so the word it is set to already has
it: m: machine [... on-enter [send m 'go] ...] works. An event sent in that same expression waits behind them, and
a failure among them is a failure of that expression. restart and machine/from start at once.
A parallel has no initial and no ways between its states: all of them start when it is entered, and it ends
when all of them have. A way out of it with no event sees in event/name how its state name ended (the outcome
of its final state, or the name of that state; none while it goes on); a to is taken once all have ended, a
choose is looked at each time one ends. Its states take an event first; its own ways out only when none does.
The machine is an object with state (a word, or a path when it is in a state inside another; a block of them
when it is in a parallel), data, done,
outcome, error, the last error taken by on error, owner, false on the copy of a shared machine, and
warnings, a block of where and what for each state no way leads to.
A mistake in the block is an error when the machine is made. A state no way leads to is a warning instead: the
machine is made and runs, the warning is in warnings and shows once in the console and the dev tools, with
where the state is written (only the outermost one, not the states inside it), and trebol check lists it with
warning: in front without failing.
A shared machine is run by one tab, the first that made it, which holds a lock named after it: only there do
hooks, timeouts, run, invoke and on-save happen. The other tabs and windows of the same browser and site get
a copy whose state and data follow it after each event (owner is false on it); send and restart on a
copy go to the tab that runs it and wait until they are done there. When that tab closes, another one goes on
from the last snapshot it was told. Data and event fields travel as mold/all gives them. With no browser to
share it with, it runs on its own, as any machine.
machine/from spec saved makes the machine of spec where the snapshot saved left it,
instead of starting it: no on-enter runs again, each timeout and the ttl go off when they were going to (at once
if that time has passed), a run starts over and a machine invoked goes on from its own snapshot. A state or a
field of the data the snapshot has and spec does not is an error.
fragment¶
Makes a piece of machine with blanks, which use puts in a state of a machine with the blanks filled.
fragment spec body
spec(block!): Its blanks: words, each with an optional block of the types it takes and a stringbody(block!): The words of a machine for one level: states, ways, hooks, initial
A piece of machine written once and put in states of machines by use, each time with its blanks filled:
retry: fragment [work [block!] check [block!]] [
initial attempt
state attempt [on-enter work]
state waiting []
state done []
from attempt select check on result [ok to done fail to waiting]
from waiting to attempt on again
]
order: machine [
data [verdict: 'fail]
initial charging
state charging [use retry with [work: [print "charging"] check: [data/verdict]]]
final paid
from charging to paid
]
The spec names the blanks, each with a block of the types it takes, any value when it has none, and a string if
wanted. A blank is a word of the body: wherever that word stands, in a block or deeper, its value takes its place,
as a block of code, a state, an event, a number. Set-words, lit-words and paths are left as they are. Each use
is a copy of its own. A blank not given, one the fragment has not, or a value of a type it does not take is a
mistake when the machine is made. A fragment may use another and pass on its own blanks; one that uses itself is
a mistake. Unlike invoke, the states of a fragment are the machine’s own: they share its data and take its
events.
fragment?¶
Whether a value is a fragment of machine.
fragment? value
value(any-value!)
For example fragment? retry.
send¶
Sends an event to a machine and gives the state it is in then, or sends a value on a socket.
send m event
m(object!handle!): A machine, or a socketevent(any-value!): The event: a word, or a block with the word and its fields; for a socket, the value
For example send repair 'start or send repair [start bike: "Road"]. An event that no way or defer of the
machine names is an error, with the one it may have meant, unless the machine has ttl or a state with
on-activity. On a socket a string goes as text, a
binary as it is and any other value as JSON; see the network.
in?¶
Whether a machine is in a state now: its name, or a path of names from the top.
in? m name
m(object!)name(word!path!)
graph-of¶
The states of a machine and the ways between them, as plain data; of a block, the machine it would make, run not.
graph-of machine
machine(object!block!): A machine, or the block of one
For example graph-of repair, or graph-of [initial a state a []] to read a block without making it run. The
block is [state ... data [fields] ttl ... shared ... done ... states [name [kind ... initial ... on ...] ...] ways [[from a to b on [events] where [...] set [...]] ...]]; see the machine as data.
can?¶
Whether a machine has a way out on an event from where it is now; what where would say is not asked.
can? m event
m(object!)event(word!)
For example can? signup 'next. A way whose where is false still counts: the block of where reads the
event, which is not there yet. A form in steps led by a machine shows Next while this is true.
restart¶
Starts a machine again from its initial state, with its data as it was made.
restart m
m(object!)
snapshot¶
The state of a machine as a block of plain data, which machine/from resumes.
snapshot m
m(object!)
The block is plain data: mold/all and load keep it, so it can be saved with save-local or sent to a server
and given to machine/from later. It holds the states the machine is in, its data, history, the events held by
defer, when each timer goes off and the snapshots of the machines it invoked.