-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathSimWrapperConfigGroup.java
162 lines (133 loc) · 4.12 KB
/
SimWrapperConfigGroup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package org.matsim.simwrapper;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.config.ReflectiveConfigGroup;
import java.util.*;
/**
* Config group for the {@link SimWrapperModule}.
*/
public class SimWrapperConfigGroup extends ReflectiveConfigGroup {
private static final String NAME = "simwrapper";
/**
* Stores context configs.
*/
private final Map<String, ContextParams> params = new HashMap<>();
@Parameter
@Comment("Whether default dashboards are loaded via SPI.")
public Mode defaultDashboards = Mode.enabled;
@Parameter
@Comment("Set of packages to scan for dashboard provider classes.")
public Set<String> packages = new HashSet<>();
@Parameter
@Comment("Set of simple class names or fully qualified class names of dashboards to exclude")
public Set<String> exclude = new HashSet<>();
@Parameter
@Comment("Set of simple class names or fully qualified class names of dashboards to include. Any none included dashboard will be excluded.")
public Set<String> include = new HashSet<>();
@Parameter
@Comment("Sample size of the run, which may be required by certain analysis functions.")
public Double sampleSize = 1.0d;
public SimWrapperConfigGroup() {
super(NAME);
get("");
}
/**
* Get the default parameters that are always present in the config.
*/
public ContextParams defaultParams() {
return get("");
}
/**
* Get an existing or add a new parameter set for specific context.
*/
public ContextParams get(String context) {
if (!params.containsKey(context)) {
ContextParams p = new ContextParams();
p.context = context;
if (!context.isEmpty()) {
// Copy default params from the global config
p.shp = defaultParams().shp;
p.mapCenter = defaultParams().mapCenter;
p.mapZoomLevel = defaultParams().mapZoomLevel;
}
addParameterSet(p);
return p;
}
return params.get(context);
}
@Override
public ContextParams createParameterSet(String type) {
if (type.equals(ContextParams.GROUP_NAME)) {
return new ContextParams();
} else {
throw new IllegalArgumentException("Unsupported parameter set type: " + type);
}
}
@Override
public void addParameterSet(ConfigGroup set) {
if (set instanceof ContextParams ctx) {
super.addParameterSet(set);
params.put(ctx.context, ctx);
} else {
throw new IllegalArgumentException("Unsupported parameter set class: " + set);
}
}
@Override
protected void checkConsistency(Config config) {
super.checkConsistency(config);
if (!include.isEmpty() && !exclude.isEmpty()) {
throw new IllegalStateException("Include and exclude option can't be set both.");
}
}
/**
* Mode how default dashboards are loaded.
*/
public enum Mode {
enabled,
disabled
}
/**
* Stores context specific parameters.
*/
public static final class ContextParams extends ReflectiveConfigGroup {
private static final String GROUP_NAME = "params";
@Parameter
@Comment("Name of the context, empty string means default context.")
public String context = "";
@Parameter
@Comment("Shp file that may be used by analysis functions that support shp file input.")
public String shp = null;
@Parameter
@Comment("Tuple of two coordinate separated with ',' that may be used to define the center of map views.")
public String mapCenter = null;
@Parameter
@Comment("Default zoom level used for map view.")
public Double mapZoomLevel = null;
public ContextParams() {
super(GROUP_NAME, true);
}
/**
* Return center coordinates, or null if not set.
*/
public double[] getCenter() {
if (mapCenter == null || !mapCenter.contains(","))
return null;
return Arrays.stream(mapCenter.split(",")).mapToDouble(Double::parseDouble).toArray();
}
/**
* Return an arbitrary config parameter that has been stored, or the default value if it is not present.
*/
public String getOrDefault(String name, String def) {
return getParams().getOrDefault(name, def);
}
/**
* Sets an arbitrary config value.
*
* @return same instance.
*/
public ContextParams set(String key, String value) {
super.getParams().put(key, value);
return this;
}
}
}