diff --git a/contribs/application/src/main/java/org/matsim/application/ApplicationUtils.java b/contribs/application/src/main/java/org/matsim/application/ApplicationUtils.java index 24b3d712bb8..083a315ecb5 100644 --- a/contribs/application/src/main/java/org/matsim/application/ApplicationUtils.java +++ b/contribs/application/src/main/java/org/matsim/application/ApplicationUtils.java @@ -119,7 +119,7 @@ else if (property.equalsIgnoreCase("false") || property.equals("0")) public static void applyConfigUpdate(Config config, Path yaml) { if (!Files.exists(yaml)) { - throw new IllegalArgumentException("Desired run config does not exist:" + yaml); + throw new IllegalArgumentException("Given config yaml does not exist: " + yaml); } ObjectMapper mapper = new ObjectMapper(new YAMLFactory() @@ -139,8 +139,8 @@ public static void applyConfigUpdate(Config config, Path yaml) { String configGroupName = aliases.resolveAlias(field.getKey(), emptyStack); ConfigGroup group = config.getModules().get(configGroupName); if (group == null) { - log.warn("Config group not found: {}", configGroupName); - continue; + group = new ConfigGroup(configGroupName); + config.addModule(group); } applyNodeToConfigGroup(field.getValue(), group); @@ -168,7 +168,17 @@ private static void applyNodeToConfigGroup(JsonNode node, ConfigGroup group) { List params = new ArrayList<>(group.getParameterSets(field.getKey())); for (JsonNode item : field.getValue()) { - applyNodeAsParameterSet(field.getKey(), item, group, params); + + + // Special case of parameter sets that have only one entry + if (field.getValue().size() == 1 && params.size() == 1 && field.getValue().get(0).isObject()) { + + applyNodeToConfigGroup(field.getValue().get(0), params.get(0)); + + } else { + + applyNodeAsParameterSet(field.getKey(), item, group, params); + } } } else { diff --git a/contribs/application/src/main/java/org/matsim/application/MATSimApplication.java b/contribs/application/src/main/java/org/matsim/application/MATSimApplication.java index ecda5e0200a..7d63518e323 100644 --- a/contribs/application/src/main/java/org/matsim/application/MATSimApplication.java +++ b/contribs/application/src/main/java/org/matsim/application/MATSimApplication.java @@ -404,7 +404,7 @@ public static void runWithDefaults(Class clazz, Str } else { // Automatically add run command String[] runArgs = ApplicationUtils.mergeArgs(new String[]{"run"}, defaultArgs); - args = ApplicationUtils.mergeArgs(defaultArgs, runArgs); + args = ApplicationUtils.mergeArgs(runArgs, args); } } else diff --git a/contribs/application/src/test/java/org/matsim/application/ConfigYamlUpdateTest.java b/contribs/application/src/test/java/org/matsim/application/ConfigYamlUpdateTest.java index 0c90c69c854..8c6c4768b22 100644 --- a/contribs/application/src/test/java/org/matsim/application/ConfigYamlUpdateTest.java +++ b/contribs/application/src/test/java/org/matsim/application/ConfigYamlUpdateTest.java @@ -3,10 +3,12 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import org.matsim.api.core.v01.TransportMode; import org.matsim.core.config.Config; import org.matsim.core.config.ConfigGroup; import org.matsim.core.config.ConfigUtils; import org.matsim.core.config.ReflectiveConfigGroup; +import org.matsim.core.config.groups.ScoringConfigGroup; import org.matsim.testcases.MatsimTestUtils; import java.nio.file.Path; @@ -22,6 +24,32 @@ public class ConfigYamlUpdateTest { @RegisterExtension private MatsimTestUtils utils = new MatsimTestUtils(); + @Test + void params() { + + Path input = Path.of(utils.getClassInputDirectory()); + + Config config = ConfigUtils.loadConfig(input.resolve("config.xml").toString()); + + ApplicationUtils.applyConfigUpdate( + config, input.resolve("params.yml") + ); + + ScoringConfigGroup scoring = ConfigUtils.addOrGetModule(config, ScoringConfigGroup.class); + + assertThat(scoring.getModes()) + .hasSize(7); + + assertThat(scoring.getPerforming_utils_hr()) + .isEqualTo(6.88); + + ScoringConfigGroup.ModeParams car = scoring.getModes().get(TransportMode.car); + + assertThat(car.getConstant()).isEqualTo(-0.62); + assertThat(car.getMarginalUtilityOfTraveling()).isEqualTo(0); + + } + @Test void standard() { @@ -74,6 +102,22 @@ void createParamSet() { assertThat(next.getParams().get("extra")).isEqualTo("extra"); } + @Test + void createGroup() { + Config config = ConfigUtils.createConfig(); + Path input = Path.of(utils.getClassInputDirectory()); + + ApplicationUtils.applyConfigUpdate( + config, input.resolve("multiLevel.yml") + ); + + + TestConfigGroup test = ConfigUtils.addOrGetModule(config, TestConfigGroup.class); + + assertThat(test.values).containsExactly(1, 2, 3); + + } + @Test void multiLevel() { diff --git a/contribs/application/test/input/org/matsim/application/ConfigYamlUpdateTest/config.xml b/contribs/application/test/input/org/matsim/application/ConfigYamlUpdateTest/config.xml new file mode 100644 index 00000000000..c67cc24de58 --- /dev/null +++ b/contribs/application/test/input/org/matsim/application/ConfigYamlUpdateTest/config.xml @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/contribs/application/test/input/org/matsim/application/ConfigYamlUpdateTest/params.yml b/contribs/application/test/input/org/matsim/application/ConfigYamlUpdateTest/params.yml new file mode 100644 index 00000000000..31d0f926bd2 --- /dev/null +++ b/contribs/application/test/input/org/matsim/application/ConfigYamlUpdateTest/params.yml @@ -0,0 +1,13 @@ +scoring: + scoringParameters: + - modeParams: + - mode: walk + constant: 0.0 + - mode: car + constant: -0.62 + - mode: pt + constant: -0.25 + - mode: bike + constant: -2.23 + - mode: ride + constant: -1.37 \ No newline at end of file