-- the route of road the bus is moving along
route : seqof Road; -- what roads are left in the current pass of the route
curRoute : seqof Road; -- where are we heading
nextWP : Waypoint; -- what road are we currently on
currentRoad : Road; --waypoints passed by the bus
wps : seqof Waypoint;
operations public Bus : nat * seqof Road * seqof Waypoint ==> Bus
Bus(linenumber, busroute, waypoints)==
(
line := linenumber;
route := busroute;
curRoute := busroute;
nextWP := hd waypoints;
wps := waypoints;
seats := {};
) prelen waypoints > 1; --ensure bus has somewhere to go
public GotOff : setof Passenger ==> ()
GotOff(p) ==
(
seats := seats \ p;
World`graphics.busPassengerCountChanged(line, card seats);
) pre p inter seats <> {};
pure public GetWaypoints : () ==> seqof Waypoint
GetWaypoints()== return wps;
pure public GetStops : () ==> seqof Busstop
GetStops()== return [wp | wp inseq wps & wp.IsStop() ];
private NextWaypoint : () ==> Waypoint
NextWaypoint()==
( --start route over if(len curRoute = 0) then
curRoute := route;
--next road let nextRoad = hd curRoute in
( --move along route
curRoute := tl curRoute; --what road are we on
currentRoad := nextRoad; -- update waypoints let currentWp = nextWP in
(
nextWP := currentRoad.OppositeEnd(currentWp); return currentWp;
)
);
);
--functionality to ensure thread start public WaitForThreadStart : () ==> ()
WaitForThreadStart() == skip;
--loop which moves bus along route, and lets passengers on and off at stops whiletruedo
(
Printer`OutWithTS("%Bus " ^ Printer`natToString(line) ^ ": running on " ^
VDMUtil`val2seq_of_char[Road`RoadNumber](currentRoad.GetRoadNumber()) ^ " with length " ^ Printer`natToString(currentRoad.GetLength()) ^ " and speedlimit " ^
Printer`natToString(currentRoad.GetSpeedLimit()) ^ " Next: " ^ VDMUtil`val2seq_of_char[Waypoint`WaypointsEnum](nextWP.GetId()) ^ " Time: " ^ Printer`natToString(currentRoad.GetTimePenalty())
);
--add to penalty for moving along road
World`timerRef.WaitRelative(currentRoad.GetTimePenalty());
World`timerRef.NotifyAll();
World`timerRef.Awake();
--next on bus route let next = NextWaypoint() in
(
Printer`OutWithTS("%Bus " ^ Printer`natToString(line) ^ " arrived at " ^
VDMUtil`val2seq_of_char[Waypoint`WaypointsEnum](next.GetId()));
let nextId = next.GetId() in
( --let passengers in at central station if --their destination is on the bus' route if nextId = <Central> then
( --bus returned with passengers; in the current model this should not be possible --if(seats <> {}) then exit "Bus returned with passengers";
let central : Busstop = next in
( --find passengers for bus let potentialPassengers : setof Passenger = central.GetWaitingOn(wps) in
( if(card potentialPassengers > 0) then
( --count available sets in bus if((Config`BusCapacity - card seats) < card potentialPassengers) then --not room for all, select some
passGettingOn := SelectSubset(potentialPassengers, Config`BusCapacity - card seats) else
passGettingOn := potentialPassengers;
-- leave busstop and enter bus seats
central.PassengerLeft(passGettingOn);
GetOn(passGettingOn); forall p inset passGettingOn do p.GotOnBus();
--add to time penalty for stopping
World`graphics.busStopping(line);
World`timerRef.WaitRelative(3);
World`timerRef.NotifyAll();
World`timerRef.Awake();
)
);
);
);
-- let passengers off at their destination let gettingOff = { p | p inset seats & p.GetDestination() = next} in
( if(card gettingOff > 0) then
(
GotOff(gettingOff);
Printer`OutWithTS("%Bus " ^ Printer`natToString(line) ^ ": " ^
Printer`natToString(card gettingOff) ^ " got off");
World`env.TransportedPassengers(card gettingOff);
--add to time penalty for stopping
World`graphics.busStopping(line);
World`timerRef.WaitRelative(3);
World`timerRef.NotifyAll();
World`timerRef.Awake();
);
);
);
);
);
);
operations
pure private SelectSubset : setof Passenger * nat ==> setof Passenger
SelectSubset(ps, limit)==
( --base case if limit = 0 then return {};
--smaller than limit, return ifcard ps <= limit then return ps;
--recusive let sub inset ps in return {sub} union SelectSubset(ps \ {sub}, limit -1);
)
end Bus
¤ Dauer der Verarbeitung: 0.16 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.