20 package org.matsim.core.config;
22 import org.apache.logging.log4j.LogManager;
32 import java.io.BufferedWriter;
33 import java.io.IOException;
34 import java.io.UncheckedIOException;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.HashSet;
39 import java.util.Map.Entry;
52 class ConfigWriterHandlerImplV2
extends ConfigWriterHandler {
57 private String newline =
"\n";
64 private final Verbosity verbosity;
66 private final Set<String> commentsAlreadyWritten =
new HashSet<>() ;
68 ConfigWriterHandlerImplV2( Verbosity verbosity ) {
69 this.verbosity = verbosity ;
72 private void writeModule(
73 final BufferedWriter writer,
75 final String moduleTag,
76 final String moduleNameAtt,
77 final String moduleName,
78 final ConfigGroup module,
79 final ConfigGroup comparisonModule ) {
80 Map<String, String> params = module.getParams();
81 Map<String, String> comments = module.getComments();
86 boolean headerHasBeenWritten = writeRegularEntries(writer, indent, moduleTag, moduleNameAtt, moduleName, comparisonModule, params, comments);
89 if ( moduleName.equals(
"thisAintNoFlat") ) {
90 LogManager.getLogger(this.getClass()).warn(
"here") ;
94 headerHasBeenWritten = processParameterSets(writer, indent, moduleTag, moduleNameAtt, moduleName, module, comparisonModule, headerHasBeenWritten);
96 if ( headerHasBeenWritten ) {
98 writer.write(
"\t</" + moduleTag +
">");
99 writer.write(this.newline);
102 }
catch (IOException e) {
103 throw new UncheckedIOException(e);
107 private Boolean processParameterSets(BufferedWriter writer, String indent, String moduleTag, String moduleNameAtt, String moduleName,
108 ConfigGroup module, ConfigGroup comparisonModule, Boolean headerHasBeenWritten)
throws IOException {
109 for ( Entry<String, ? extends Collection<? extends ConfigGroup>> entry : module.getParameterSets().entrySet() ) {
110 Collection<? extends ConfigGroup> comparisonSets =
new ArrayList<>() ;
111 if ( comparisonModule != null ) {
112 comparisonSets = comparisonModule.getParameterSets(entry.getKey());
114 for ( ConfigGroup pSet : entry.getValue() ) {
115 ConfigGroup comparisonPSet = null ;
116 for ( ConfigGroup cg : comparisonSets ) {
117 if ( sameType( pSet, cg ) ) {
118 comparisonPSet = cg ;
126 if ( verbosity== Verbosity.minimal && comparisonPSet==null ) {
127 if ( pSet instanceof ScoringParameterSet) {
128 comparisonPSet = ((ScoringConfigGroup) comparisonModule).getOrCreateScoringParameters(((ScoringParameterSet) pSet).getSubpopulation());
129 }
else if ( pSet instanceof ModeParams ) {
130 comparisonPSet = ((ScoringParameterSet) comparisonModule).getOrCreateModeParams(((ModeParams) pSet).getMode());
131 }
else if ( pSet instanceof ActivityParams ) {
132 comparisonPSet = ((ScoringParameterSet) comparisonModule).getOrCreateActivityParams(((ActivityParams) pSet).getActivityType());
133 }
else if ( pSet instanceof RoutingConfigGroup.TeleportedModeParams ) {
134 comparisonPSet = ((RoutingConfigGroup) comparisonModule).getOrCreateModeRoutingParams(((RoutingConfigGroup.TeleportedModeParams) pSet).getMode() ) ;
137 comparisonPSet = pSet.getClass().newInstance();
138 }
catch (InstantiationException | IllegalAccessException e) {
142 comparisonPSet =
new ConfigGroup(pSet.getName()) ;
149 if ( !headerHasBeenWritten ) {
150 headerHasBeenWritten = true ;
151 writeHeader(writer, indent, moduleTag, moduleNameAtt, moduleName, newline);
153 writeModule(writer, indent+
"\t", PARAMETER_SET, TYPE, entry.getKey(), pSet, comparisonPSet );
156 return headerHasBeenWritten;
159 private Boolean writeRegularEntries(BufferedWriter writer, String indent, String moduleTag, String moduleNameAtt,
160 String moduleName, ConfigGroup comparisonModule, Map<String, String> params,
161 Map<String, String> comments)
throws IOException {
162 boolean headerHasBeenWritten = false ;
163 for (Entry<String, String> entry : params.entrySet()) {
165 final String actual = entry.getValue();
166 if ( verbosity== Verbosity.minimal ) {
167 if ( comparisonModule!=null ) {
168 String defaultValue = comparisonModule.getParams().get( entry.getKey() ) ;
171 switch( entry.getKey() ) {
172 case ActivityParams.TYPICAL_DURATION:
173 defaultValue = null ;
175 case ModeParams.MODE:
177 defaultValue = null ;
179 case ActivityParams.ACTIVITY_TYPE:
180 defaultValue = null ;
183 if (actual.equals(defaultValue)) {
192 if ( !headerHasBeenWritten ) {
193 headerHasBeenWritten = true ;
194 writeHeader(writer, indent, moduleTag, moduleNameAtt, moduleName, newline);
197 String key = entry.getKey() +
"." + moduleName ;
198 if (comments.get( entry.getKey() ) != null && !commentsAlreadyWritten.contains( key )) {
199 commentsAlreadyWritten.add( key ) ;
202 writer.write( indent );
203 writer.write(
"\t\t<!-- " + comments.get(entry.getKey()) +
" -->");
204 writer.write( this.newline );
212 writer.write( indent );
213 writer.write(
"\t\t<"+PARAMETER+
" name=\"" + entry.getKey() +
"\" value=\"" + actual +
"\" />");
214 writer.write( this.newline );
216 return headerHasBeenWritten;
219 private static void writeHeader(BufferedWriter writer, String indent, String moduleTag, String moduleNameAtt, String moduleName, String newline)
throws IOException {
221 writer.write( indent );
222 writer.write(
"\t<"+moduleTag);
223 writer.write(
" "+moduleNameAtt+
"=\"" + moduleName +
"\" >");
224 writer.write( newline );
227 private static boolean sameType(ConfigGroup pSet, ConfigGroup cg) {
228 if ( ! ( pSet.getName().equals( cg.getName() ) ) ) {
231 if ( pSet instanceof RoutingConfigGroup.TeleportedModeParams ) {
233 if ( ((RoutingConfigGroup.TeleportedModeParams)pSet).getMode().equals( ((RoutingConfigGroup.TeleportedModeParams)cg).getMode() ) ) {
237 if ( pSet instanceof ScoringParameterSet ) {
240 if ( pSet instanceof ModeParams ) {
241 if ( ((ModeParams)pSet).getMode().equals( ((ModeParams)cg).getMode() ) ) {
245 if ( pSet instanceof ActivityParams ) {
246 if ( ((ActivityParams)pSet).getActivityType().equals( ((ActivityParams)cg).getActivityType() ) ) {
250 if ( pSet instanceof StrategySettings ) {
260 final BufferedWriter out) {
262 out.write(
"<"+CONFIG+
">");
263 out.write( this.newline );
264 }
catch (IOException e) {
265 throw new UncheckedIOException(e);
272 final BufferedWriter out) {
274 out.write( this.newline );
275 out.write(
"</"+CONFIG+
">");
276 out.write( this.newline );
277 }
catch (IOException e) {
278 throw new UncheckedIOException(e);
284 final ConfigGroup module,
285 final BufferedWriter out) {
286 if ( ! (module instanceof ChangeLegModeConfigGroup) ) {
290 ConfigGroup comparisonConfig = null ;
291 if ( verbosity==Verbosity.minimal) {
292 comparisonConfig = ConfigUtils.createConfig().getModules().get(module.getName());
309 void writeSeparator(
final BufferedWriter out) {
324 String setNewline(
final String newline) {
325 String former = this.newline;
326 this.newline = newline;