From 14f15b2043206fad054e46a8844021d73da4cb5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=B6rl?= Date: Fri, 6 Dec 2024 00:15:16 +0100 Subject: [PATCH] fix(drt): names in attribute-based prebooking (#3629) * fix(drt): names in attribute-based prebooking * fix test and add setter --- .../run/RunPrebookingShiftDrtScenarioIT.java | 24 +++++++-------- .../logic/AttributeBasedPrebookingLogic.java | 30 +++++++++++++++---- .../prebooking/PrebookingTestEnvironment.java | 4 +-- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/contribs/drt-extensions/src/test/java/org/matsim/contrib/drt/extension/operations/shifts/run/RunPrebookingShiftDrtScenarioIT.java b/contribs/drt-extensions/src/test/java/org/matsim/contrib/drt/extension/operations/shifts/run/RunPrebookingShiftDrtScenarioIT.java index 61e3a64a97a..e4286bed31c 100644 --- a/contribs/drt-extensions/src/test/java/org/matsim/contrib/drt/extension/operations/shifts/run/RunPrebookingShiftDrtScenarioIT.java +++ b/contribs/drt-extensions/src/test/java/org/matsim/contrib/drt/extension/operations/shifts/run/RunPrebookingShiftDrtScenarioIT.java @@ -280,8 +280,8 @@ private void preparePopulation(Scenario scenario) { Plan plan = factory.createPlan(); Activity start = factory.createActivityFromLinkId("start", Id.createLinkId(1)); start.setEndTime(5000); - start.getAttributes().putAttribute("prebooking:submissionTime" + "drt", 1800.); - start.getAttributes().putAttribute("prebooking:plannedDepartureTime" + "drt", 5000.); + AttributeBasedPrebookingLogic.setSubmissionTime("drt", start, 1800.0); + AttributeBasedPrebookingLogic.setPlannedDepartureTime("drt", start, 5000.0); plan.addActivity(start); plan.addLeg(factory.createLeg("drt")); plan.addActivity(factory.createActivityFromLinkId("end", Id.createLinkId(2))); @@ -295,8 +295,8 @@ private void preparePopulation(Scenario scenario) { Plan plan = factory.createPlan(); Activity start = factory.createActivityFromLinkId("start", Id.createLinkId(1)); start.setEndTime(5000); - start.getAttributes().putAttribute("prebooking:submissionTime" + "drt", 900.); - start.getAttributes().putAttribute("prebooking:plannedDepartureTime" + "drt", 5005.); + AttributeBasedPrebookingLogic.setSubmissionTime("drt", start, 900.0); + AttributeBasedPrebookingLogic.setPlannedDepartureTime("drt", start, 5005.0); plan.addActivity(start); plan.addLeg(factory.createLeg("drt")); plan.addActivity(factory.createActivityFromLinkId("end", Id.createLinkId(2))); @@ -310,8 +310,8 @@ private void preparePopulation(Scenario scenario) { Plan plan = factory.createPlan(); Activity start = factory.createActivityFromLinkId("start", Id.createLinkId(1)); start.setEndTime(5000); - start.getAttributes().putAttribute("prebooking:submissionTime" + "drt", 4000.); - start.getAttributes().putAttribute("prebooking:plannedDepartureTime" + "drt", 5000.); + AttributeBasedPrebookingLogic.setSubmissionTime("drt", start, 4000.0); + AttributeBasedPrebookingLogic.setPlannedDepartureTime("drt", start, 5000.0); plan.addActivity(start); plan.addLeg(factory.createLeg("drt")); plan.addActivity(factory.createActivityFromLinkId("end", Id.createLinkId(2))); @@ -325,8 +325,8 @@ private void preparePopulation(Scenario scenario) { Plan plan = factory.createPlan(); Activity start = factory.createActivityFromLinkId("start", Id.createLinkId(1)); start.setEndTime(8000); - start.getAttributes().putAttribute("prebooking:submissionTime" + "drt", 4000.); - start.getAttributes().putAttribute("prebooking:plannedDepartureTime" + "drt", 11000.); + AttributeBasedPrebookingLogic.setSubmissionTime("drt", start, 4000.0); + AttributeBasedPrebookingLogic.setPlannedDepartureTime("drt", start, 11000.0); plan.addActivity(start); plan.addLeg(factory.createLeg("drt")); plan.addActivity(factory.createActivityFromLinkId("end", Id.createLinkId(2))); @@ -340,8 +340,8 @@ private void preparePopulation(Scenario scenario) { Plan plan = factory.createPlan(); Activity start = factory.createActivityFromLinkId("start", Id.createLinkId(1)); start.setEndTime(6000.); - start.getAttributes().putAttribute("prebooking:submissionTime" + "drt", 4000.); - start.getAttributes().putAttribute("prebooking:plannedDepartureTime" + "drt", 6000.); + AttributeBasedPrebookingLogic.setSubmissionTime("drt", start, 4000.0); + AttributeBasedPrebookingLogic.setPlannedDepartureTime("drt", start, 6000.0); plan.addActivity(start); plan.addLeg(factory.createLeg("drt")); plan.addActivity(factory.createActivityFromLinkId("end", Id.createLinkId(2))); @@ -355,8 +355,8 @@ private void preparePopulation(Scenario scenario) { Plan plan = factory.createPlan(); Activity start = factory.createActivityFromLinkId("start", Id.createLinkId(1)); start.setEndTime(6500.); - start.getAttributes().putAttribute("prebooking:submissionTime" + "drt", 4000.); - start.getAttributes().putAttribute("prebooking:plannedDepartureTime" + "drt", 6500.); + AttributeBasedPrebookingLogic.setSubmissionTime("drt", start, 4000.0); + AttributeBasedPrebookingLogic.setPlannedDepartureTime("drt", start, 6500.0); plan.addActivity(start); plan.addLeg(factory.createLeg("drt")); plan.addActivity(factory.createActivityFromLinkId("end", Id.createLinkId(2))); diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/prebooking/logic/AttributeBasedPrebookingLogic.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/prebooking/logic/AttributeBasedPrebookingLogic.java index a48cbe50e46..7908ca1378c 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/prebooking/logic/AttributeBasedPrebookingLogic.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/prebooking/logic/AttributeBasedPrebookingLogic.java @@ -2,6 +2,7 @@ import java.util.Optional; +import org.matsim.api.core.v01.population.Activity; import org.matsim.api.core.v01.population.Leg; import org.matsim.api.core.v01.population.PlanElement; import org.matsim.contrib.drt.prebooking.logic.helpers.PopulationIterator; @@ -35,23 +36,40 @@ * @author Sebastian Hörl (sebhoerl), IRT SystemX */ public class AttributeBasedPrebookingLogic implements PrebookingLogic, MobsimInitializedListener { - static private final String SUBMISSION_TIME_ATTRIBUTE_PREFIX = "prebooking:submissionTime"; - static private final String PLANNED_DEPARTURE_ATTRIBUTE_PREFIX = "prebooking:plannedDepartureTime"; + static private final String SUBMISSION_TIME_ATTRIBUTE_PREFIX = "prebooking:submissionTime:"; + static private final String PLANNED_DEPARTURE_ATTRIBUTE_PREFIX = "prebooking:plannedDepartureTime:"; - static public String getSubmissionAttribute(String mode) { + static public String getSubmissionTimeAttribute(String mode) { return SUBMISSION_TIME_ATTRIBUTE_PREFIX + mode; } - static public String getPlannedDepartureAttribute(String mode) { + static public String getPlannedDepartureTimeAttribute(String mode) { return PLANNED_DEPARTURE_ATTRIBUTE_PREFIX + mode; } static public Optional getSubmissionTime(String mode, Trip trip) { - return Optional.ofNullable((Double) trip.getTripAttributes().getAttribute(getSubmissionAttribute(mode))); + return Optional.ofNullable((Double) trip.getTripAttributes().getAttribute(getSubmissionTimeAttribute(mode))); } static public Optional getPlannedDepartureTime(String mode, Trip trip) { - return Optional.ofNullable((Double) trip.getTripAttributes().getAttribute(getPlannedDepartureAttribute(mode))); + return Optional + .ofNullable((Double) trip.getTripAttributes().getAttribute(getPlannedDepartureTimeAttribute(mode))); + } + + static public void setSubmissionTime(String mode, Trip trip, double submissionTime) { + trip.getTripAttributes().putAttribute(getSubmissionTimeAttribute(mode), submissionTime); + } + + static public void setPlannedDepartureTime(String mode, Trip trip, double plannedDepartureTime) { + trip.getTripAttributes().putAttribute(getPlannedDepartureTimeAttribute(mode), plannedDepartureTime); + } + + static public void setSubmissionTime(String mode, Activity originActivity, double submissionTime) { + originActivity.getAttributes().putAttribute(getSubmissionTimeAttribute(mode), submissionTime); + } + + static public void setPlannedDepartureTime(String mode, Activity originActivity, double plannedDepartureTime) { + originActivity.getAttributes().putAttribute(getPlannedDepartureTimeAttribute(mode), plannedDepartureTime); } private final PrebookingQueue prebookingQueue; diff --git a/contribs/drt/src/test/java/org/matsim/contrib/drt/prebooking/PrebookingTestEnvironment.java b/contribs/drt/src/test/java/org/matsim/contrib/drt/prebooking/PrebookingTestEnvironment.java index 0c527bbfb8d..fcdb7ab7d7a 100644 --- a/contribs/drt/src/test/java/org/matsim/contrib/drt/prebooking/PrebookingTestEnvironment.java +++ b/contribs/drt/src/test/java/org/matsim/contrib/drt/prebooking/PrebookingTestEnvironment.java @@ -283,13 +283,13 @@ private void buildPopulation(Scenario scenario) { plan.addLeg(firstLeg); if (!Double.isNaN(request.submissionTime)) { - firstActivity.getAttributes().putAttribute(AttributeBasedPrebookingLogic.getSubmissionAttribute("drt"), + firstActivity.getAttributes().putAttribute(AttributeBasedPrebookingLogic.getSubmissionTimeAttribute("drt"), request.submissionTime); } if (!Double.isNaN(request.plannedDepartureTime)) { firstActivity.getAttributes().putAttribute( - AttributeBasedPrebookingLogic.getPlannedDepartureAttribute("drt"), + AttributeBasedPrebookingLogic.getPlannedDepartureTimeAttribute("drt"), request.plannedDepartureTime); }