From dc1fe991bb12fd7366f89d06faf3daa183792dd7 Mon Sep 17 00:00:00 2001 From: momania Date: Tue, 4 Jan 2011 09:32:13 +0100 Subject: [PATCH] small adjustment to the example, showing the correct use of the startWith and initialize --- .../src/main/scala/DiningHakkersOnFsm.scala | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/akka-samples/akka-sample-fsm/src/main/scala/DiningHakkersOnFsm.scala b/akka-samples/akka-sample-fsm/src/main/scala/DiningHakkersOnFsm.scala index 1147e6d0bd..7a4641c35e 100644 --- a/akka-samples/akka-sample-fsm/src/main/scala/DiningHakkersOnFsm.scala +++ b/akka-samples/akka-sample-fsm/src/main/scala/DiningHakkersOnFsm.scala @@ -33,6 +33,9 @@ case class TakenBy(hakker: Option[ActorRef]) class Chopstick(name: String) extends Actor with FSM[ChopstickState, TakenBy] { self.id = name + // A chopstick begins its existence as available and taken by no one + startWith(Available, TakenBy(None)) + // When a chopstick is available, it can be taken by a some hakker when(Available) { case Event(Take, _) => @@ -49,8 +52,8 @@ class Chopstick(name: String) extends Actor with FSM[ChopstickState, TakenBy] { goto(Available) using TakenBy(None) } - // A chopstick begins its existence as available and taken by no one - startWith(Available, TakenBy(None)) + // Initialze the chopstick + initialize } /** @@ -81,6 +84,9 @@ case class TakenChopsticks(left: Option[ActorRef], right: Option[ActorRef]) class FSMHakker(name: String, left: ActorRef, right: ActorRef) extends Actor with FSM[FSMHakkerState, TakenChopsticks] { self.id = name + //All hakkers start waiting + startWith(Waiting, TakenChopsticks(None, None)) + when(Waiting) { case Event(Think, _) => log.info("%s starts to think", name) @@ -147,12 +153,12 @@ class FSMHakker(name: String, left: ActorRef, right: ActorRef) extends Actor wit startThinking(5 seconds) } + // Initialize the hakker + initialize + private def startThinking(duration: Duration): State = { goto(Thinking) using TakenChopsticks(None, None) forMax duration } - - //All hakkers start waiting - startWith(Waiting, TakenChopsticks(None, None)) } /*