final class TransformHelper(func: StateFunction) { def using(andThen: PartialFunction[State, State]): StateFunction = func andThen (andThen orElse { case x ? x }) } final def transform(func: StateFunction): TransformHelper = new TransformHelper(func)
when(Running) { transform { case Event(m, Target(Actor.noSender)) =>
goto(Uninitialised) using NoConfig case Event(m, Target(ref)) =>
ref ! m stay } using targetTransformer } def targetTransformer: PartialFunction[State, State] = { case s @ State(stateName, Target(ref), _, _, _) if ref.path.name.startsWith("testActor") => log.debug("Setting target to dead letters") s.using(Target(Actor.noSender)) }
when(Uninitialised) { case Event(Config(ref), _) =>
goto(Running) using Target(ref) case Event(_, _) => stash stay } when(Running) { case Event(m, Target(ref)) =>
ref ! m stay } onTransition { case Uninitialised -> Running => unstashAll() }
import akka.actor._ sealed trait Seasons //States
case object Spring extends Seasons case object Summer extends Seasons case object Fall extends Seasons case object Winter extends Seasons //sealed trait SeasonData //Data
case class SeasonInfo(talks: Int, month: Int) object FillSeasons { sealed trait Messages //功能消息
case object HowYouFeel extends Messages case object NextMonth extends Messages def props = Props(new FillSeasons) } class FillSeasons extends FSM[Seasons,SeasonInfo] with ActorLogging { import FillSeasons._ startWith(Spring,SeasonInfo(0,1)) //起始状态
when(Spring) { //状态在春季
case Event(HowYouFeel,seasonInfo) => val numtalks = seasonInfo.talks + 1 log.info(s"It's ${stateName.toString}, feel so gooood! You've asked me ${numtalks} times.") stay using seasonInfo.copy(talks = numtalks) } when(Summer) { //夏季状态
case Event(HowYouFeel,_) => val numtalks = stateData.talks + 1 log.info(s"It's ${stateName.toString}, it's so hot! You've asked me ${numtalks} times") stay().using(stateData.copy(talks = numtalks)) } when(Fall) { //秋季状态
case Event(HowYouFeel,SeasonInfo(tks,mnth)) => val numtalks = t