https://github.com/danpersa/edge-state-machine-scala
https://github.com/danpersa/edge-state-machine-scala
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/danpersa/edge-state-machine-scala
- Owner: danpersa
- Created: 2013-10-24T12:35:02.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-11-01T09:25:44.000Z (over 11 years ago)
- Last Synced: 2025-01-22T04:31:21.776Z (4 months ago)
- Language: Scala
- Size: 117 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rdoc
Awesome Lists containing this project
README
= Edge State Machine
== State Machine Examples
=== User State Machine
==== Define
class User(var state: State = State("pending")) extends AbstractStateMachine {
state("pending")
state("active")
state("blocked")event("activate") {
transition(_, from = Set("pending", "blocked"), to = "active", doActivate)
}event("block") {
transition(_, from = Set("active"), to = "blocked", doBlock)
}def doBlock = println("do_block")
def doActivate = println("do_activate")
}
==== Useval user = new User()
// user.state must beEqualTo(State("pending"))
user activate()
// user.state must beEqualTo(State("active"))
user block()
// user.state must beEqualTo(State("blocked"))
user activate()
// user.state must beEqualTo(State("active"))=== Dice State Machine
==== Define
object DiceStates {
val states: List[String] = List("one", "two", "three", "four", "five", "six")
val statesSet: Set[String] = states.to[Set]
}class Dice(var state: State = State("one")) extends AbstractStateMachine {
state("one")
state("two")
state("three")
state("four")
state("five")
state("six")event("roll") {
transition(_, from = DiceStates.statesSet,
to = DiceStates.statesSet,
guard = rollResult,
onTransition = displayDiceRollingAnimation)
}def rollResult = { State(DiceStates.states(Random.nextInt(6))) }
def displayDiceRollingAnimation = println(print(s"--------------- DICE: throw: $state ------------"))
}
==== Useval dice = new Dice()
dice.roll()
// dice.state.name must(beOneOf("one", "two", "three", "four", "five", "six"))