From 43eb9aa1d7127a4324c04f590b4cb09df0a5e83f Mon Sep 17 00:00:00 2001 From: Duc Pham Date: Wed, 11 Aug 2021 15:13:59 +0700 Subject: [PATCH] Make `unregister` an action in driving skill --- .../gaml/extensions/traffic/DrivingSkill.java | 32 +++++++++++++++++-- .../gaml/extensions/traffic/RoadSkill.java | 28 +++------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/simtools.gaml.extensions.traffic/src/simtools/gaml/extensions/traffic/DrivingSkill.java b/simtools.gaml.extensions.traffic/src/simtools/gaml/extensions/traffic/DrivingSkill.java index 3517d99989..2a74fa85db 100644 --- a/simtools.gaml.extensions.traffic/src/simtools/gaml/extensions/traffic/DrivingSkill.java +++ b/simtools.gaml.extensions.traffic/src/simtools/gaml/extensions/traffic/DrivingSkill.java @@ -1394,7 +1394,7 @@ public void primDriveRandom(final IScope scope) throws GamaRuntimeException { int newLane = laneAndAccPair.getKey(); setCurrentTarget(vehicle, newTarget); - RoadSkill.unregister(scope, vehicle); + unregister(scope, vehicle); RoadSkill.register(scope, vehicle, newRoad, newLane); // Choose the next road in advance IAgent nextRoad = chooseNextRoadRandomly(scope, graph, newTarget, roadProba); @@ -1551,7 +1551,7 @@ public void primDrive(final IScope scope) throws GamaRuntimeException { setCurrentIndex(vehicle, newEdgeIdx); setCurrentTarget(vehicle, newTarget); - RoadSkill.unregister(scope, vehicle); + unregister(scope, vehicle); RoadSkill.register(scope, vehicle, newRoad, newLane); setViolatingOneway(vehicle, violatingOneway); @@ -1835,6 +1835,32 @@ private void clearDrivingStates(final IScope scope) { setCurrentPath(vehicle, null); } + @action( + name = "unregister", + doc = @doc( + value = "remove the vehicle from its current roads", + examples = { @example("do unregister") } + ) + ) + public void primUnregister(final IScope scope) throws GamaRuntimeException { + IAgent vehicle = getCurrentAgent(scope); + unregister(scope, vehicle); + } + + public static void unregister(final IScope scope, final IAgent driver) + throws GamaRuntimeException { + IAgent currentRoad = getCurrentRoad(driver); + if (currentRoad == null) return; + + Integer lowestLane = (Integer) driver.getAttribute(LOWEST_LANE); + int numLanesOccupied = (int) driver.getAttribute(NUM_LANES_OCCUPIED); + for (int i = 0; i < numLanesOccupied; i += 1) { + int lane = lowestLane + i; + RoadSkill.getVehiclesOnLaneSegment(scope, currentRoad, lane).remove(driver); + } + setCurrentRoad(driver, null); + } + // TODO: this action is not overridden @action( name = "die", @@ -1846,7 +1872,7 @@ private void clearDrivingStates(final IScope scope) { public void primDieWrapper(final IScope scope) throws GamaRuntimeException { AbstractAgent vehicle = (AbstractAgent) getCurrentAgent(scope); if (!vehicle.dead() && getCurrentRoad(vehicle) != null) { - RoadSkill.unregister(scope, vehicle); + unregister(scope, vehicle); } vehicle.primDie(scope); } diff --git a/simtools.gaml.extensions.traffic/src/simtools/gaml/extensions/traffic/RoadSkill.java b/simtools.gaml.extensions.traffic/src/simtools/gaml/extensions/traffic/RoadSkill.java index dde2b8fed2..6935555cc4 100644 --- a/simtools.gaml.extensions.traffic/src/simtools/gaml/extensions/traffic/RoadSkill.java +++ b/simtools.gaml.extensions.traffic/src/simtools/gaml/extensions/traffic/RoadSkill.java @@ -391,33 +391,13 @@ public static void register(IScope scope, IAgent vehicle, IAgent road, int lowes }, doc = @doc( value = "unregister the agent on the road", - examples = { @example ("do unregister agent: the_driver") } + examples = { @example ("do unregister agent: the_driver") }, + deprecated = "use the `unregister` action in advanced_driving skill instead" ) ) + @Deprecated public void primUnregister(final IScope scope) throws GamaRuntimeException { IAgent driver = (IAgent) scope.getArg("agent", IType.AGENT); - unregister(scope, driver); - } - - /** - * Unregisters the driver from all the roads that it's currently on. - * - * @param scope - * @param driver the agent that we want to unregister. - * - * @throws GamaRuntimeException - */ - public static void unregister(IScope scope, final IAgent driver) - throws GamaRuntimeException { - IAgent currentRoad = (IAgent) driver.getAttribute(DrivingSkill.CURRENT_ROAD); - if (currentRoad == null) return; - - Integer lowestLane = (Integer) driver.getAttribute(DrivingSkill.LOWEST_LANE); - int numLanesOccupied = (int) driver.getAttribute(DrivingSkill.NUM_LANES_OCCUPIED); - for (int i = 0; i < numLanesOccupied; i += 1) { - int lane = lowestLane + i; - getVehiclesOnLaneSegment(scope, currentRoad, lane).remove(driver); - } - getAgents(currentRoad).remove(driver); + DrivingSkill.unregister(scope, driver); } }