Skip to content

Commit

Permalink
feat: optional modules, qSim modules and qSim components configuratio…
Browse files Browse the repository at this point in the history
…n steps

Allows subclasses to register optional config groups to avoid writing many Run scripts.
  • Loading branch information
Tarek Chouaki committed Dec 15, 2023
1 parent b1331d7 commit f795045
Showing 1 changed file with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.eqasim.core.simulation;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.function.BiConsumer;

import org.eqasim.core.components.EqasimComponentsModule;
import org.eqasim.core.components.config.EqasimConfigGroup;
Expand All @@ -15,10 +14,12 @@
import org.matsim.api.core.v01.population.Person;
import org.matsim.contribs.discrete_mode_choice.modules.DiscreteModeChoiceModule;
import org.matsim.contribs.discrete_mode_choice.modules.config.DiscreteModeChoiceConfigGroup;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.mobsim.qsim.AbstractQSimModule;
import org.matsim.core.mobsim.qsim.components.QSimComponentsConfig;
import org.matsim.households.Household;

import ch.sbb.matsim.config.SwissRailRaptorConfigGroup;
Expand All @@ -28,6 +29,10 @@ public class EqasimConfigurator {
protected final List<ConfigGroup> configGroups = new LinkedList<>();
protected final List<AbstractModule> modules = new LinkedList<>();
protected final List<AbstractQSimModule> qsimModules = new LinkedList<>();
private final Map<String, Collection<AbstractModule>> optionalModules = new HashMap<>();
private final Map<String, Collection<AbstractQSimModule>> optionalQSimModules = new HashMap<>();
private final Map<String, List<BiConsumer<Controler, QSimComponentsConfig>>> optionalQSimComponentConfigurationSteps = new HashMap<>();
private final Map<String, ConfigGroup> optionalConfigGroups = new HashMap<>();

public EqasimConfigurator() {
configGroups.addAll(Arrays.asList( //
Expand All @@ -51,7 +56,7 @@ public EqasimConfigurator() {
}

public ConfigGroup[] getConfigGroups() {
return configGroups.toArray(new ConfigGroup[configGroups.size()]);
return configGroups.toArray(ConfigGroup[]::new);
}

public List<AbstractModule> getModules() {
Expand All @@ -63,19 +68,69 @@ public List<AbstractQSimModule> getQSimModules() {
}

public void configureController(Controler controller) {

// The optional modules are added after the non-optional ones because we consider that their bindings have less priority
this.optionalModules.entrySet().stream()
.filter(e -> controller.getConfig().getModules().containsKey(e.getKey()))
.map(Map.Entry::getValue)
.flatMap(Collection::stream)
.forEach(controller::addOverridingModule);

for (AbstractModule module : getModules()) {
controller.addOverridingModule(module);
}

this.optionalQSimModules.entrySet().stream()
.filter(e -> controller.getConfig().getModules().containsKey(e.getKey()))
.map(Map.Entry::getValue)
.flatMap(Collection::stream)
.forEach(controller::addOverridingQSimModule);

for (AbstractQSimModule module : getQSimModules()) {
controller.addOverridingQSimModule(module);
}

controller.configureQSimComponents(configurator -> {
EqasimTransitQSimModule.configure(configurator, controller.getConfig());
controller.configureQSimComponents(components -> {
optionalQSimComponentConfigurationSteps.entrySet().stream()
.filter(e -> controller.getConfig().getModules().containsKey(e.getKey()))
.map(Map.Entry::getValue)
.flatMap(Collection::stream)
.forEach(step -> step.accept(controller, components));
EqasimTransitQSimModule.configure(components, controller.getConfig());
});
}

protected void registerOptionalConfigGroup(ConfigGroup configGroup) {
registerOptionalConfigGroup(configGroup, new ArrayList<>());
}

protected void registerOptionalConfigGroup(ConfigGroup configGroup, Collection<AbstractModule> modules) {
registerOptionalConfigGroup(configGroup, modules, new ArrayList<>());
}

protected void registerOptionalConfigGroup(ConfigGroup configGroup, Collection<AbstractModule> modules, Collection<AbstractQSimModule> qsimModules) {
registerOptionalConfigGroup(configGroup, modules, qsimModules, new ArrayList<>());
}
protected void registerOptionalConfigGroup(ConfigGroup configGroup, Collection<AbstractModule> modules, Collection<AbstractQSimModule> qsimModules, List<BiConsumer<Controler, QSimComponentsConfig>> componentsConsumers) {
this.optionalConfigGroups.put(configGroup.getName(), configGroup);
this.optionalModules.putIfAbsent(configGroup.getName(), new ArrayList<>());
this.optionalModules.get(configGroup.getName()).addAll(modules);

this.optionalQSimModules.putIfAbsent(configGroup.getName(), new ArrayList<>());
this.optionalQSimModules.get(configGroup.getName()).addAll(qsimModules);

this.optionalQSimComponentConfigurationSteps.putIfAbsent(configGroup.getName(), new ArrayList<>());
this.optionalQSimComponentConfigurationSteps.get(configGroup.getName()).addAll(componentsConsumers);
}

public void addOptionalConfigGroups(Config config) {
for(ConfigGroup configGroup: optionalConfigGroups.values()) {
if(config.getModules().get(configGroup.getName()) != null) {
config.addModule(configGroup);
}
}
}

public void configureScenario(Scenario scenario) {
}

Expand Down

0 comments on commit f795045

Please sign in to comment.