diff --git a/README.md b/README.md
index 97c84d1..6576eec 100644
--- a/README.md
+++ b/README.md
@@ -13,4 +13,25 @@ using one elevator as an unit:
I don't expect to complete this at one go. Instead, I will build it phase-wise.
-The blog is here (to be inserted).
\ No newline at end of file
+The blog is [here](http://blogoloquy.blogspot.in/2016/11/finite-state-machine-using-akka.html).
+
+A few hints about various components (may help follow the implementation)
+
+- org.nirmalya.carriages.types.withExplicitMovingState.LiftCarriageWithMovingState
+
+It models the carriage itself; constructed with a _TimeConsumingHWSignalSimulator_
+
+- org.nirmalya.entities.TimeConsumingHWSignalSimulator
+
+It implements the _MovingStateSimulator_ trait. Specifically, it implements the simulation of
+spending time while moving to a particular floor (function: simulateMovementTo()).
+
+- org.nirmalya.entities.InlaidButtonPanel
+
+It models a button panel mounted inside a carriage. It is identified by an ID as well as the
+carriage it is mounted inside. The carriage is an actor; hence, it is constructed with an
+_ActorRef_.
+
+- org.nirmalya.entities.LiftController
+
+The Controller in the system. It is constructed with a collection of carriages.
\ No newline at end of file
diff --git a/src/main/scala/org/nirmalya/carriages/types/withExplicitMovingState/LiftCarriageWithMovingState.scala b/src/main/scala/org/nirmalya/carriages/types/withExplicitMovingState/LiftCarriageWithMovingState.scala
index 25ba6ba..021617f 100644
--- a/src/main/scala/org/nirmalya/carriages/types/withExplicitMovingState/LiftCarriageWithMovingState.scala
+++ b/src/main/scala/org/nirmalya/carriages/types/withExplicitMovingState/LiftCarriageWithMovingState.scala
@@ -22,7 +22,7 @@ class LiftCarriageWithMovingState (val movementHWIndicator: ActorRef) extends Ac
private val dontCare: StateFunction = {
case _ =>
- log.debug(context.self.toString() + s" in $this.stateName received message ( $this.Event ), nothing to do.")
+ log.debug(context.self.toString().+(StringContext.apply(" in ", ".stateName received message ( ", ".Event ), nothing to do.").s(this, this)))
stay
}
@@ -44,25 +44,25 @@ class LiftCarriageWithMovingState (val movementHWIndicator: ActorRef) extends Ac
private val moveToWaitingPassenger: StateFunction = {
case Event(PassengerIsWaitingAt(destFloorID), _) =>
- if (currentFloorID == destFloorID)
+ if (currentFloorID.==(destFloorID))
goto (Stopped)
else {
this.pendingPassengerRequests = accumulateWaitingRequest(destFloorID)
- movementHWIndicator ! InformMeOnReaching(
+ movementHWIndicator.!(InformMeOnReaching.apply(
this.currentFloorID,
- this.pendingPassengerRequests.head)
+ this.pendingPassengerRequests.head))
goto (Moving)
}
}
private val transportPassengerToDest: StateFunction = {
case Event(PassengerRequestsATransportTo(floorIDs),_) =>
- log.debug(s"Received message: PassengerWantsToGoTo($floorIDs)")
+ log.debug(StringContext.apply("Received message: PassengerWantsToGoTo(", ")").s(floorIDs))
this.pendingPassengerRequests = accumulateTransportRequest(floorIDs)
- movementHWIndicator ! InformMeOnReaching(
+ movementHWIndicator.!(InformMeOnReaching.apply(
this.currentFloorID,
- this.pendingPassengerRequests.head)
+ this.pendingPassengerRequests.head))
goto (Moving)
}
@@ -73,22 +73,20 @@ class LiftCarriageWithMovingState (val movementHWIndicator: ActorRef) extends Ac
goto (Ready)
}
else {
- log.debug(s"Stopped.timeout, moving to floor:( ${this.pendingPassengerRequests.head} )")
- movementHWIndicator ! InformMeOnReaching(
+ log.debug(StringContext.apply("Stopped.timeout, moving to floor:( ", " )").s(this.pendingPassengerRequests.head))
+ movementHWIndicator.!(InformMeOnReaching.apply(
this.currentFloorID,
- this.pendingPassengerRequests.head)
+ this.pendingPassengerRequests.head))
goto(Moving)
}
}
startWith(PoweredOff,InitialData)
- when (PoweredOff) (powerYourselfOn orElse
- dontCare)
+ when (PoweredOff) (powerYourselfOn.orElse(dontCare))
when (PoweredOn) (powerYourselfOff orElse
- beReady orElse
- dontCare)
+ beReady.orElse(dontCare))
when (Ready) (moveToWaitingPassenger orElse
transportPassengerToDest )
@@ -104,7 +102,7 @@ class LiftCarriageWithMovingState (val movementHWIndicator: ActorRef) extends Ac
// All pending requests to this floor, should be removed.
pendingPassengerRequests = pendingPassengerRequests.tail filter (n => n.floorID != this.currentFloorID)
- log.debug(s"Carriage(${self.path.name}): reached($currentFloorID), remaining ${prettifyForLogging(pendingPassengerRequests)}")
+ log.debug(StringContext.apply("Carriage(", "): reached(", "), remaining ", "").s(self.path.name, currentFloorID, prettifyForLogging(pendingPassengerRequests)))
goto (Stopped)
case Event(PassengerIsWaitingAt(floorID),_) =>
@@ -118,7 +116,7 @@ class LiftCarriageWithMovingState (val movementHWIndicator: ActorRef) extends Ac
whenUnhandled {
case Event(ReportCurrentFloor, _) =>
- sender ! StoppedAt(self.path.name,this.currentFloorID)
+ sender ! StoppedAt.apply(self.path.name,this.currentFloorID)
stay
}
@@ -131,11 +129,11 @@ class LiftCarriageWithMovingState (val movementHWIndicator: ActorRef) extends Ac
private def settleDownAtGroundFloor = this.currentFloorID = 0
private def accumulateWaitingRequest(toFloorID: Int): Vector[NextStop] =
- this.pendingPassengerRequests :+ NextStop(toFloorID,PurposeOfMovement.ToWelcomeInAnWaitingPassenger)
+ this.pendingPassengerRequests :+ NextStop.apply(toFloorID,PurposeOfMovement.ToWelcomeInAnWaitingPassenger)
private def accumulateTransportRequest(toFloorIDs: Vector[Int]): Vector[NextStop] =
this.pendingPassengerRequests ++
- (toFloorIDs.map(f => NextStop(f,PurposeOfMovement.ToAllowATransportedPassengerAlight)))
+ (toFloorIDs.map(f => NextStop.apply(f,PurposeOfMovement.ToAllowATransportedPassengerAlight)))
private def prettifyForLogging(floorsToVisit: Vector[NextStop]):String = {
@@ -150,7 +148,7 @@ class LiftCarriageWithMovingState (val movementHWIndicator: ActorRef) extends Ac
"toDrop"
else "toPick"
- buffer.append(s"($toVisitFloor-$reasonToVisitFloor)").append(" | ")
+ buffer.append(StringContext.apply("(", "-", ")").s(toVisitFloor, reasonToVisitFloor)).append(" | ")
}).toString
}
@@ -159,6 +157,6 @@ class LiftCarriageWithMovingState (val movementHWIndicator: ActorRef) extends Ac
}
object LiftCarriageWithMovingState {
- def props(hwSimulator: ActorRef) = Props(new LiftCarriageWithMovingState(hwSimulator))
+ def props(hwSimulator: ActorRef) = Props.apply(new LiftCarriageWithMovingState(hwSimulator))
}
diff --git a/src/main/scala/org/nirmalya/driver/HotelLiftSystem.scala b/src/main/scala/org/nirmalya/driver/HotelLiftSystem.scala
index d00a684..53ef860 100644
--- a/src/main/scala/org/nirmalya/driver/HotelLiftSystem.scala
+++ b/src/main/scala/org/nirmalya/driver/HotelLiftSystem.scala
@@ -19,55 +19,75 @@ object HotelLiftSystem extends App {
val hwSignalSimulatorProps = TimeConsumingHWSignalSimulator.props(i)
val hwSignalSimulator = system.actorOf(hwSignalSimulatorProps,s"hw_$i")
+ // Carriage needs a HW Simulator
val carriage = system.actorOf(LiftCarriageWithMovingState.props(hwSignalSimulator), s"Carriage_$i")
+
+ // Buttonpanel needs a Carriage to be attached to
val buttonPanel = system.actorOf(InlaidButtonPanel.props(i,carriage),s"ButtonPanel_$i")
+
Lift(carriage,buttonPanel,hwSignalSimulator)
}
val controller = system.actorOf(LiftController.props(lifts.map(l => l.carriage).toVector),"Controller")
+ // Hotel's Office staff powers the Controller on.
controller ! InstructedToPowerOn
// Thread.sleep(1000)
controller ! BeReady
+ // A passenger at the 2nd floor, presses the button at the lift-lobby.
controller ! PassengerIsWaitingAt(2)
+ // Another passenger at the 2nd floor, presses the button at the lift-lobby.
controller ! PassengerIsWaitingAt(4)
+ // More of a debugging aid: Controller confirms where the carriage is right now.
controller ! InquireWithCarriage(1)
+ // Let the threads in the dispatcher execute various Actors (all asynchronous).
Thread.sleep(3000)
+ // A passenger inside the Carriage(0), wants to go to 5th floor.
lifts(0).buttonPanel ! PassengerRequestsATransportTo(Vector(5))
+ // Two passengers inide the Carriage(1), want to go to 6th and 2nd floor.
lifts(1).buttonPanel ! PassengerRequestsATransportTo(Vector(6,2))
Thread.sleep(3000)
+ // More of a debugging aid: Controller confirms where the carriage is right now.
controller ! InquireWithCarriage(0)
+ // More of a debugging aid: Controller confirms where the carriage is right now.
controller ! InquireWithCarriage(1)
Thread.sleep(3000)
+ // More of a debugging aid: Controller confirms where the carriage is right now.
controller ! InquireWithCarriage(0)
+ // More of a debugging aid: Controller confirms where the carriage is right now.
controller ! InquireWithCarriage(1)
Thread.sleep(3000)
+ // More of a debugging aid: Controller confirms where the carriage is right now.
controller ! InquireWithCarriage(0)
+ // More of a debugging aid: Controller confirms where the carriage is right now.
controller ! InquireWithCarriage(1)
Thread.sleep(2000)
+ // More of a debugging aid: Controller confirms where the carriage is right now.
controller ! InquireWithCarriage(1)
+ // Give all Actors enough time to finish their work before ...
Thread.sleep(3000)
+ // .. shutting the system down.
system.terminate
}