1
1
package org .eqasim .core .simulation ;
2
2
3
- import java .util .Arrays ;
4
- import java .util .LinkedList ;
5
- import java .util .List ;
3
+ import java .util .*;
4
+ import java .util .function .BiConsumer ;
6
5
7
6
import org .eqasim .core .components .EqasimComponentsModule ;
8
7
import org .eqasim .core .components .config .EqasimConfigGroup ;
15
14
import org .matsim .api .core .v01 .population .Person ;
16
15
import org .matsim .contribs .discrete_mode_choice .modules .DiscreteModeChoiceModule ;
17
16
import org .matsim .contribs .discrete_mode_choice .modules .config .DiscreteModeChoiceConfigGroup ;
17
+ import org .matsim .core .config .Config ;
18
18
import org .matsim .core .config .ConfigGroup ;
19
19
import org .matsim .core .controler .AbstractModule ;
20
20
import org .matsim .core .controler .Controler ;
21
21
import org .matsim .core .mobsim .qsim .AbstractQSimModule ;
22
+ import org .matsim .core .mobsim .qsim .components .QSimComponentsConfig ;
22
23
import org .matsim .households .Household ;
23
24
24
25
import ch .sbb .matsim .config .SwissRailRaptorConfigGroup ;
@@ -28,6 +29,10 @@ public class EqasimConfigurator {
28
29
protected final List <ConfigGroup > configGroups = new LinkedList <>();
29
30
protected final List <AbstractModule > modules = new LinkedList <>();
30
31
protected final List <AbstractQSimModule > qsimModules = new LinkedList <>();
32
+ private final Map <String , Collection <AbstractModule >> optionalModules = new HashMap <>();
33
+ private final Map <String , Collection <AbstractQSimModule >> optionalQSimModules = new HashMap <>();
34
+ private final Map <String , List <BiConsumer <Controler , QSimComponentsConfig >>> optionalQSimComponentConfigurationSteps = new HashMap <>();
35
+ private final Map <String , ConfigGroup > optionalConfigGroups = new HashMap <>();
31
36
32
37
public EqasimConfigurator () {
33
38
configGroups .addAll (Arrays .asList ( //
@@ -51,7 +56,7 @@ public EqasimConfigurator() {
51
56
}
52
57
53
58
public ConfigGroup [] getConfigGroups () {
54
- return configGroups .toArray (new ConfigGroup [configGroups . size ()] );
59
+ return configGroups .toArray (ConfigGroup []:: new );
55
60
}
56
61
57
62
public List <AbstractModule > getModules () {
@@ -63,19 +68,69 @@ public List<AbstractQSimModule> getQSimModules() {
63
68
}
64
69
65
70
public void configureController (Controler controller ) {
71
+
72
+ // The optional modules are added after the non-optional ones because we consider that their bindings have less priority
73
+ this .optionalModules .entrySet ().stream ()
74
+ .filter (e -> controller .getConfig ().getModules ().containsKey (e .getKey ()))
75
+ .map (Map .Entry ::getValue )
76
+ .flatMap (Collection ::stream )
77
+ .forEach (controller ::addOverridingModule );
78
+
66
79
for (AbstractModule module : getModules ()) {
67
80
controller .addOverridingModule (module );
68
81
}
69
82
83
+ this .optionalQSimModules .entrySet ().stream ()
84
+ .filter (e -> controller .getConfig ().getModules ().containsKey (e .getKey ()))
85
+ .map (Map .Entry ::getValue )
86
+ .flatMap (Collection ::stream )
87
+ .forEach (controller ::addOverridingQSimModule );
88
+
70
89
for (AbstractQSimModule module : getQSimModules ()) {
71
90
controller .addOverridingQSimModule (module );
72
91
}
73
92
74
- controller .configureQSimComponents (configurator -> {
75
- EqasimTransitQSimModule .configure (configurator , controller .getConfig ());
93
+ controller .configureQSimComponents (components -> {
94
+ optionalQSimComponentConfigurationSteps .entrySet ().stream ()
95
+ .filter (e -> controller .getConfig ().getModules ().containsKey (e .getKey ()))
96
+ .map (Map .Entry ::getValue )
97
+ .flatMap (Collection ::stream )
98
+ .forEach (step -> step .accept (controller , components ));
99
+ EqasimTransitQSimModule .configure (components , controller .getConfig ());
76
100
});
77
101
}
78
102
103
+ protected void registerOptionalConfigGroup (ConfigGroup configGroup ) {
104
+ registerOptionalConfigGroup (configGroup , new ArrayList <>());
105
+ }
106
+
107
+ protected void registerOptionalConfigGroup (ConfigGroup configGroup , Collection <AbstractModule > modules ) {
108
+ registerOptionalConfigGroup (configGroup , modules , new ArrayList <>());
109
+ }
110
+
111
+ protected void registerOptionalConfigGroup (ConfigGroup configGroup , Collection <AbstractModule > modules , Collection <AbstractQSimModule > qsimModules ) {
112
+ registerOptionalConfigGroup (configGroup , modules , qsimModules , new ArrayList <>());
113
+ }
114
+ protected void registerOptionalConfigGroup (ConfigGroup configGroup , Collection <AbstractModule > modules , Collection <AbstractQSimModule > qsimModules , List <BiConsumer <Controler , QSimComponentsConfig >> componentsConsumers ) {
115
+ this .optionalConfigGroups .put (configGroup .getName (), configGroup );
116
+ this .optionalModules .putIfAbsent (configGroup .getName (), new ArrayList <>());
117
+ this .optionalModules .get (configGroup .getName ()).addAll (modules );
118
+
119
+ this .optionalQSimModules .putIfAbsent (configGroup .getName (), new ArrayList <>());
120
+ this .optionalQSimModules .get (configGroup .getName ()).addAll (qsimModules );
121
+
122
+ this .optionalQSimComponentConfigurationSteps .putIfAbsent (configGroup .getName (), new ArrayList <>());
123
+ this .optionalQSimComponentConfigurationSteps .get (configGroup .getName ()).addAll (componentsConsumers );
124
+ }
125
+
126
+ public void addOptionalConfigGroups (Config config ) {
127
+ for (ConfigGroup configGroup : optionalConfigGroups .values ()) {
128
+ if (config .getModules ().get (configGroup .getName ()) != null ) {
129
+ config .addModule (configGroup );
130
+ }
131
+ }
132
+ }
133
+
79
134
public void configureScenario (Scenario scenario ) {
80
135
}
81
136
0 commit comments