From a405fa9dd6a3702dc1dbd7410d321a6b6421fc5f Mon Sep 17 00:00:00 2001 From: nkuehnel Date: Tue, 28 May 2024 15:08:05 +0200 Subject: [PATCH 1/2] add backwards compatibility for dvrp matrix and drt zone system config params --- .../analysis/zonal/DrtZoneSystemParams.java | 65 ++- .../drt/analysis/zonal/ReadOldConfigTest.java | 26 ++ .../skims/DvrpTravelTimeMatrixParams.java | 15 + .../scenarios/kelheim/config-with-drt_old.xml | 390 ++++++++++++++++++ 4 files changed, 495 insertions(+), 1 deletion(-) create mode 100644 contribs/drt/src/test/java/org/matsim/contrib/drt/analysis/zonal/ReadOldConfigTest.java create mode 100644 examples/scenarios/kelheim/config-with-drt_old.xml diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/DrtZoneSystemParams.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/DrtZoneSystemParams.java index 0f9ed49ea1f..5608a30b944 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/DrtZoneSystemParams.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/DrtZoneSystemParams.java @@ -22,10 +22,12 @@ import jakarta.validation.constraints.NotNull; import org.matsim.contrib.common.util.ReflectiveConfigGroupWithConfigurableParameterSets; +import org.matsim.contrib.common.zones.GridZoneSystem; import org.matsim.contrib.common.zones.ZoneSystemParams; import org.matsim.contrib.common.zones.systems.grid.GISFileZoneSystemParams; import org.matsim.contrib.common.zones.systems.grid.h3.H3GridZoneSystemParams; import org.matsim.contrib.common.zones.systems.grid.square.SquareGridZoneSystemParams; +import org.matsim.core.config.ConfigGroup; /** * @author Michal Maciejewski (michalm) @@ -65,8 +67,69 @@ private void initSingletonParameterSets() { params -> zoneSystemParams = (H3GridZoneSystemParams)params); } + @Override + public void handleAddUnknownParam(String paramName, String value) { + switch (paramName) { + case "zonesGeneration": { + if (getZoneSystemParams() == null) { + switch (value) { + case "ShapeFile": { + addParameterSet(createParameterSet(GISFileZoneSystemParams.SET_NAME)); + break; + } + case "GridFromNetwork": { + addParameterSet(createParameterSet(SquareGridZoneSystemParams.SET_NAME)); + break; + } + case "H3": { + addParameterSet(createParameterSet(H3GridZoneSystemParams.SET_NAME)); + break; + } + default: + super.handleAddUnknownParam(paramName, value); + } + } + break; + } + case "cellSize": { + SquareGridZoneSystemParams squareGridParams; + if(getZoneSystemParams() == null) { + squareGridParams = (SquareGridZoneSystemParams) createParameterSet(SquareGridZoneSystemParams.SET_NAME); + addParameterSet(squareGridParams); + } else { + squareGridParams = (SquareGridZoneSystemParams) getZoneSystemParams(); + } + squareGridParams.cellSize = Double.parseDouble(value); + break; + } + case "zonesShapeFile": { + GISFileZoneSystemParams gisFileParams; + if(getZoneSystemParams() == null) { + gisFileParams = (GISFileZoneSystemParams) createParameterSet(GISFileZoneSystemParams.SET_NAME); + addParameterSet(gisFileParams); + } else { + gisFileParams = (GISFileZoneSystemParams) getZoneSystemParams(); + } + gisFileParams.zonesShapeFile = value; + break; + } + case "h3Resolution": { + H3GridZoneSystemParams h3GridParams; + if(getZoneSystemParams() == null) { + h3GridParams = (H3GridZoneSystemParams) createParameterSet(GISFileZoneSystemParams.SET_NAME); + addParameterSet(h3GridParams); + } else { + h3GridParams = (H3GridZoneSystemParams) getZoneSystemParams(); + } + h3GridParams.h3Resolution = Integer.parseInt(value); + break; + } + default: + super.handleAddUnknownParam(paramName, value); + } + } + public ZoneSystemParams getZoneSystemParams() { return zoneSystemParams; } - } diff --git a/contribs/drt/src/test/java/org/matsim/contrib/drt/analysis/zonal/ReadOldConfigTest.java b/contribs/drt/src/test/java/org/matsim/contrib/drt/analysis/zonal/ReadOldConfigTest.java new file mode 100644 index 00000000000..36bcd1f936d --- /dev/null +++ b/contribs/drt/src/test/java/org/matsim/contrib/drt/analysis/zonal/ReadOldConfigTest.java @@ -0,0 +1,26 @@ +package org.matsim.contrib.drt.analysis.zonal; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup; +import org.matsim.contrib.dvrp.run.DvrpConfigGroup; +import org.matsim.core.config.Config; +import org.matsim.core.config.ConfigUtils; +import org.matsim.core.utils.io.IOUtils; +import org.matsim.examples.ExamplesUtils; + +import java.net.URL; + +/** + * @author nkuehnel / MOIA + */ +public class ReadOldConfigTest { + + @Test + public void test() { + URL context = ExamplesUtils.getTestScenarioURL("kelheim"); + Config config = ConfigUtils.loadConfig(IOUtils.extendUrl(context, "config-with-drt_old.xml")); + Assertions.assertThatNoException().isThrownBy(() -> ConfigUtils.addOrGetModule(config, MultiModeDrtConfigGroup.class)); + Assertions.assertThatNoException().isThrownBy(() -> ConfigUtils.addOrGetModule(config, DvrpConfigGroup.class)); + } +} diff --git a/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/DvrpTravelTimeMatrixParams.java b/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/DvrpTravelTimeMatrixParams.java index 1c95a88432f..9d2a0bdc074 100644 --- a/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/DvrpTravelTimeMatrixParams.java +++ b/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/DvrpTravelTimeMatrixParams.java @@ -79,6 +79,21 @@ private void initSingletonParameterSets() { params -> zoneSystemParams = (H3GridZoneSystemParams)params); } + @Override + public void handleAddUnknownParam(String paramName, String value) { + if ("cellSize".equals(paramName)) { + SquareGridZoneSystemParams squareGridParams; + if(getZoneSystemParams() == null) { + squareGridParams = (SquareGridZoneSystemParams) createParameterSet(SquareGridZoneSystemParams.SET_NAME); + addParameterSet(squareGridParams); + } else { + squareGridParams = (SquareGridZoneSystemParams) getZoneSystemParams(); + } + squareGridParams.cellSize = Double.parseDouble(value); + } + super.handleAddUnknownParam(paramName, value); + } + public ZoneSystemParams getZoneSystemParams() { return zoneSystemParams; } diff --git a/examples/scenarios/kelheim/config-with-drt_old.xml b/examples/scenarios/kelheim/config-with-drt_old.xml new file mode 100644 index 00000000000..76020e10903 --- /dev/null +++ b/examples/scenarios/kelheim/config-with-drt_old.xml @@ -0,0 +1,390 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 67aa3a26bb5512a5f392e6157d53b06093710758 Mon Sep 17 00:00:00 2001 From: nkuehnel Date: Tue, 28 May 2024 15:21:18 +0200 Subject: [PATCH 2/2] fix test issue --- .../matsim/contrib/zone/skims/DvrpTravelTimeMatrixParams.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/DvrpTravelTimeMatrixParams.java b/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/DvrpTravelTimeMatrixParams.java index 9d2a0bdc074..77e717cfea7 100644 --- a/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/DvrpTravelTimeMatrixParams.java +++ b/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/DvrpTravelTimeMatrixParams.java @@ -90,8 +90,9 @@ public void handleAddUnknownParam(String paramName, String value) { squareGridParams = (SquareGridZoneSystemParams) getZoneSystemParams(); } squareGridParams.cellSize = Double.parseDouble(value); + } else { + super.handleAddUnknownParam(paramName, value); } - super.handleAddUnknownParam(paramName, value); } public ZoneSystemParams getZoneSystemParams() {