-
Notifications
You must be signed in to change notification settings - Fork 0
patterns.state.wren
Utility classes for creating simple State Machines.
-
References:
-
Since:
1.0.0
-
Example:
import "domepunk/patterns/state" for StateMachine, STB
// Create a new state machine
var water = StateMachine.new({
"data": {
"mydata":true
},
// Optional
"ignoreErrors": false,
"onError": Fn.new {|message, name, transition, machine|
System.print("Error Handler")
System.print(name)
System.print(message)
},
"init": "solid",
"transitions": [
STB.new("melt").from("solid").to("liquid"),
STB.new("freeze").from("liquid").to("solid"),
STB.new("vaporize").from("liquid").to("gas"),
STB.new("condense").from("gas").to("liquid"),
STB.new("magnetize").from("solid").to("magnet").when {|transition|
var canBeMagnetized = false
System.print(transition)
System.print("Can be magnetized? " + canBeMagnetized.toString)
return canBeMagnetized
}
],
"methods": {
"before": {
"melt": Fn.new {|transition|
System.print("Before Melting " + transition.toString)
},
"freeze": Fn.new{|transition|
System.print("Before Freezing " + transition.toString)
}
},
"on": {
"melt": Fn.new{|transition|
System.print("Is Melting " + transition.toString)
}
},
"after": {
"melt": Fn.new{|transition|
System.print("After Melting " + transition.toString)
}
}
}
})
// Start moving through states
System.print(water.state)
water.do("melt")
System.print(water.state)
water.do("vaporize")
System.print(water.state)
water.do("condense")
System.print(water.state)
// Go back to solid
water.do("freeze")
System.print(water.state)
// Condense is only possible from gas to liquid
// Current state should be solid
water.do("condense")
// State should remain solid
System.print(water.state)
// now should be liquid
water.do("melt")
System.print(water.state)
// And back to solid
water.reset()
System.print(water.state)
// This state is not possible
// since when condition is not true
water.do("magnetize")
System.print(water.state)
// This should be ignored
water.do("invalid")
import "domepunk/patterns/state" for StateMachine
// alias
import "domepunk/patterns/state" for SM
Get or set the state machine data
By default state errors triggers a fiber.abort()
.
use "ignoreErrors":true
on the constructor's Map to ignore all errors.
By default state errors triggers a fiber.abort()
.
use "onError": Fn.new {|message, name, transition, machine|}
to catch errors.
Error message if available
- Signature:
error:String?
Get the current state
Returns true if the provided name is equal to the current state name.
- Signature:
is(name:String) -> Bool
Map of possible states
Map of possible transitions
Tries to execute a transition by name.
- Signature:
do(name:String) -> this
- Example:
ice.do("melt")
- Throws:
Fiber.abort()
ifignoreErrors
andonError
properties are not set.
Resets the state to the init
value.
Creates a new state machine.
- Signature:
construct new(machine:Map) -> StateMachine
. - Throws:
Fiber.abort()
ifmachine
is not a Map.
import "domepunk/patterns/state" for StateTransitionBuilder
// alias
import "domepunk/patterns/state" for STB
Get or sets the from
state
Fluent interface
- Signature:
from(value:<String|List>) -> this
Fluent interface to set from = "*"
- Signature:
any() -> this
Get or sets the to
state
Fluent interface
- Signature:
to(value:String) -> this
Get or sets the name
for the transition
Fluent interface
- Signature:
name(value:String) -> this
Get or set the when
function to determine if the transition is possible
Fluent interface
- Signature:
when(value:Fn) -> this
Creates a new transition.
- Signature:
construct new(name:String?, from:String?, to:String?, when:Fn?) -> StateTransitionBuilder
- Example:
STB.new("melt").from("solid").to("liquid")
The basic State
The state name
The machine where this state belongs
Is the initial state?
States to transition to
- Signature:
children : [State]
States from which this originates.
- Signature:
parents : [State]
A state transition.
name of the transition
origin state
- Signature:
from:<String|List>
final state
state machine reference
Get or sets the before
callback
Executes the before
callback
Get or sets the on
callback
Executes the on
callback
Get or sets the before
callback
Executes the after
callback
Get or sets the when
callback.
Return false if this transition should not be executed.
Executes the when
callback
Triggers the on
callback, then
return the to
property
Creates a new StateTransition