This library can be used as an alternative to akka.actor.FSM,
for creating state machines in simple Scala control flow.
The following state machine contains two states and two transitions between them.
It can be created as a simple while loop with the help of keywords.akka.actor.ReceiveMessagePartial:
import akka.actor.typed._
import com.yang_bo.dsl.keywords.akka.actor.ReceiveMessagePartial
sealedtrait State
caseobject Opened extends State
caseobject Closed extends State
sealedtrait Transition
caseclass Open(response: ActorRef[State]) extends Transition
caseclass Close(response: ActorRef[State]) extends Transition
def doorActor: Behavior[Transition] = {
while (true) {
val open = !ReceiveMessagePartial[Open]
open.response ! Opened
val close = !ReceiveMessagePartial[Close]
close.response ! Closed
}
thrownew Exception("Unreachable code!")
}
The door should reply an Opened state after performing an Open transition,
import akka.actor.testkit.typed.scaladsl._
val door = BehaviorTestKit(doorActor)
val state = TestInbox[State]()
door.run(Open(state.ref))
state.expectMessage(Opened)
and the state of the door can be switched between Opend and Closed according to Open and Close transition.
Contains the com.thoughtworks.dsl.Dsl instances in the akka.actor.typed.Behavior domain.
Installation
== Imports == {{{ import com.yang_bo.dsl.domains.akka.actor.typed._ }}}
Author:
杨博 (Yang Bo)
This library can be used as an alternative to akka.actor.FSM, for creating state machines in simple Scala control flow. The following state machine contains two states and two transitions between them. It can be created as a simple
while
loop with the help of keywords.akka.actor.ReceiveMessagePartial:The door should reply an
Opened
state after performing anOpen
transition,and the state of the door can be switched between
Opend
andClosed
according toOpen
andClose
transition.