From 1c2cf78dc91e8d1d92098d938b74dad6746ad386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nico=20K=C3=BChnel?= Date: Tue, 28 May 2024 16:14:53 +0200 Subject: [PATCH] add backwards compatibility for dvrp matrix and drt zone system configs (#3294) * add backwards compatibility for dvrp matrix and drt zone system config params * fix test issue * addresses https://github.com/matsim-org/matsim-libs/pull/3173#issuecomment-2134745460 --- .../analysis/zonal/DrtZoneSystemParams.java | 65 ++- .../drt/analysis/zonal/ReadOldConfigTest.java | 26 ++ .../skims/DvrpTravelTimeMatrixParams.java | 16 + .../scenarios/kelheim/config-with-drt_old.xml | 390 ++++++++++++++++++ 4 files changed, 496 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..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 @@ -79,6 +79,22 @@ 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); + } else { + 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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +