Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional modules, qSim modules and qSim components configurations #182

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading