This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
PassengerRole
Matthew Pohlmann edited this page Jan 1, 2014
·
1 revision
###Data
Structure destination;
Structure currentLocation;
List<Vehicle> boardingRequest;
Vehicle currentVehicle;
Car car;
Queue<Move> waypoints;
class Move {Structure s, MoveType m};
enum MoveType {Walk,Bus,Car};
Person p;
enum PassengerState {None,Waiting,Boarding,InTransit,Arrived};
PassengerState s;
Semaphore waitingForVehicle;
###Messages
public void msgGoTo (Structure structure)
{
destination = structure;
s = PassengerState.None;
waypoints.clear();
}
public void msgPleaseBoard (Vehicle vehicle)
{
boardingRequest.add(vehicle);
waitingForVehicle.release ();
}
public void msgReachedDestination(Structure location)
{
currentLocation = location;
s = Arrived;
}
###Scheduler
@Override
protected boolean pickAndExecuteAnAction()
{
if currentLocation == destination && s == PassengerState.None && waypoints.isEmpty()
finishMoving ();
if s == PassengerState.None && waypoints.isEmpty()
populateWaypoints ();
if !boardingRequest.isEmpty()
checkBoardingRequest (boardingRequest.remove(0));
if s == Arrived
processArrival ();
if s != PassengerState.InTransit;
goToLocation(waypoints.front());
}
###Actions
private void checkBoardingRequest(Vehicle vehicle)
{
if vehicle instanceof Bus
if bus has next waypoint on its route
vehicle.msgDoneBoarding(this)
currentVehicle = vehicle;
boardingRequest.clear();
s = PassengerState.InTransit
else
vehicle.msgNotBoarding(this);
waitingForVehicle.acquire ();
if vehicle instanceof Car && car == vehicle
vehicle.msgDoneBoarding (this)
currentVehicle = vehicle;
boardingRequest.clear();
s = PassengerState.InTransit;
}
private void finishMoving ()
{
p.msgDoneMoving (currentLocation);
active = false;
currentVehicle = null;
}
private void populateWaypoints ()
{
if car != null
waypoints.add (new Move (destination,MoveType.Car));
waypoints.add (new Move (destination,MoveType.Walk));
List<BusStop> stops = //find the two bus stops I need to go to
if found route
waypoints.add (new Move (stops.get(0),MoveType.Walk));
waypoints.add (new Move (stops.get(1),MoveType.Bus));
waypoints.add (new Move (stops.get(1),MoveType.Walk));
waypoints.add (new Move (destination, MoveType.Walk));
else
waypoints.add (new Move (destination,MoveType.Walk));
}
private void goToLocation (Move move)
{
switch(move.m)
{
case MoveType.Walk : doGoToStructure (move.s); s = Arrived;
case MoveType.Bus : (currentLocation as BusStop).add(this); waitingForVehicle.acquire ();
case MoveType.Car : car.msgCallCar(currentLocation,destination); waitingForVehicle.acquire ();
}
}
private void processArrival ()
{
if currentLocation == waypoints.front().s
Structure s = waypoints.pop ();
if currentVehicle != null
currentVehicle.leaving ();
currentVehicle = null;
s = PassengerState.None;
else
currentVehicle.staying ();
}