MATSIM
ConfigUtils.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2010 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.core.config;
21 
22 import org.matsim.api.core.v01.Id;
28 import org.matsim.core.gbl.Gbl;
29 import org.matsim.core.utils.io.IOUtils;
30 
31 import java.io.File;
32 import java.io.UncheckedIOException;
33 import java.net.URL;
34 import java.util.Arrays;
35 
41 public class ConfigUtils implements MatsimExtensionPoint {
42  private ConfigUtils() {} // do not instantiate
43 
44  public static Config createConfig(final String context) {
45  URL url = IOUtils.resolveFileOrResource(context) ;
46  return createConfig( url ) ;
47  }
48 
49  public static Config createConfig(URL context) {
50  Config config = createConfig();
51  config.setContext(context);
52  return config;
53  }
54 
55  public static Config createConfig() {
56  Config config = new Config();
57  config.addCoreModules();
58  return config;
59  }
60 
61  public static Config createConfig(ConfigGroup... customModules) {
62  Config config = createConfig();
63  for (ConfigGroup customModule : customModules) {
64  config.addModule(customModule);
65  }
66  return config;
67  }
68 
69  public static Config loadConfig(final String filename, ConfigGroup... customModules) throws UncheckedIOException {
70  return loadConfig(IOUtils.resolveFileOrResource(filename), customModules);
71  }
72 
82  public static Config loadConfig( String [] args, ConfigGroup... customModules ) {
83  String[] typedArgs = Arrays.copyOfRange( args, 1, args.length );
84  return loadConfig( IOUtils.resolveFileOrResource( args[0] ), typedArgs, customModules );
85  }
86 
87  public static Config loadConfig( Config config, String [] args, ConfigGroup... customModules ) {
88  String[] typedArgs = Arrays.copyOfRange( args, 1, args.length );
89  return loadConfig( config, IOUtils.resolveFileOrResource( args[0] ), typedArgs, customModules );
90  }
91 
104  public static CommandLine getCommandLine( String[] args ){
105  String[] typedArgs = Arrays.copyOfRange( args, 1, args.length );
106  try{
107  return new CommandLine.Builder( typedArgs )
108  .allowPositionalArguments( false )
109  .allowAnyOption( true )
110  .build() ;
111  } catch( CommandLine.ConfigurationException e ){
112  throw new RuntimeException( e ) ;
113  }
114  }
115 
116  public static Config loadConfig( final URL url, String [] typedArgs, ConfigGroup... customModules ) {
117  Config config = loadConfig( url, customModules ) ;
118  return applyCommandline( config, typedArgs );
119  }
120 
121  public static Config loadConfig( Config config, final URL url, String [] typedArgs, ConfigGroup... customModules ) {
122  loadConfig( config, url, customModules ) ;
123  return applyCommandline( config, typedArgs );
124  }
125 
126  public static Config applyCommandline( Config config, String[] typedArgs ){
127  try{
128  CommandLine.Builder bld = new CommandLine.Builder( typedArgs ) ;
129  bld.allowAnyOption( true );
130  bld.allowPositionalArguments( false ) ;
131  CommandLine cmd = bld.build();
132  cmd.applyConfiguration( config );
133  } catch( CommandLine.ConfigurationException e ){
134  e.printStackTrace();
135  throw new RuntimeException( e ) ;
136  }
137  return config ;
138  }
139 
140  public static Config loadConfig(final URL url, ConfigGroup... customModules) throws UncheckedIOException {
141  Gbl.assertNotNull(url);
142 
143  Config config = new Config();
144  config.addCoreModules();
145 
146  for (ConfigGroup customModule : customModules) {
147  config.addModule(customModule);
148  }
149 
150  new ConfigReader(config).parse(url);
151  config.setContext(url);
152  return config;
153  }
154 
155 
166  public static void loadConfig(final Config config, final String filename) throws UncheckedIOException {
167  if (config.global() == null) {
168  // if the config does not exist yet, we interpret the file name as is:
169  config.addCoreModules();
170  new ConfigReader(config).readFile(filename);
171  } else {
172  // if the config does already exist, this can be used to read a second config file to override
173  // settings from before, e.g. using a base config and then several case study configs.
174  // In this case, using the above syntax generates inconsistent behavior between
175  // gui and command line: command line takes the config file from the java root,
176  // while the gui takes it from the config file root. The following syntax should
177  // now also take it from the config file root when it is called from the command line
178  // (same as in other places: we are making the command line behavior
179  // and GUI behavior consistent).
180  // kai, jan'18
181 // URL url = ConfigGroup.getInputFileURL(config.getContext(), filename);;
182 // new ConfigReader(config).parse(url) ;
183  // yyyyyy the above probably works, but has ramifications across many test
184  // cases. Need to discuss first (and then find some time again).
185  // See MATSIM-776 and MATSIM-777. kai, feb'18
186 
187  new ConfigReader(config).readFile(filename);
188  }
189  }
190 
191  public static void loadConfig(final Config config, final URL url, ConfigGroup... customModules ) throws UncheckedIOException {
192  if (config.global() == null) {
193  config.addCoreModules();
194  }
195  for (ConfigGroup customModule : customModules) {
196  config.addModule(customModule);
197  }
198  new ConfigReader(config).parse(url);
199  }
200 
201 
202  public static Config loadConfig(URL url) {
203  Config config = new Config();
204  config.addCoreModules();
205  new ConfigReader(config).parse(url);
206  config.setContext(url);
207  return config;
208 
209  }
210 
232  public static void modifyFilePaths(final Config config, final String pathPrefix) {
233  String prefix = pathPrefix;
234  if (!prefix.endsWith("/") && !prefix.endsWith(File.separator)) {
235  prefix = prefix + File.separator;
236  }
238  config.network().setInputFile(getAbsolutePath(prefix, config.network().getInputFile()));
239  config.plans().setInputFile(getAbsolutePath(prefix, config.plans().getInputFile()));
240  config.facilities().setInputFile(getAbsolutePath(prefix, config.facilities().getInputFile()));
241  config.counts().setInputFile(getAbsolutePath(prefix, config.counts().getCountsFileName()));
242  config.households().setInputFile(getAbsolutePath(prefix, config.households().getInputFile()));
243  }
244 
245  private static String getAbsolutePath(final String prefix, final String path) {
246  if (path == null) {
247  return null;
248  }
249  File file = new File(path);
250  if (file.exists() && path.equals(file.getAbsolutePath())) {
251  return path;
252  }
253  /* even if the file exists at the given location, its path
254  * seems not to be specified as absolute path, thus we have to
255  * interpret it as a relative path and add the prefix to it.
256  */
257  String absolutePath = prefix + path;
258  return absolutePath;
259  }
260  @Deprecated // using this vs not using this can change results, presumably because strategies may be used in a different sequence from the registry.
261  // (Had the problem in RandomizingTransitRotuerIT.) kai, dec'19
263  long maxStrategyId = 0;
264  for( StrategySettings strategySettings : config.replanning().getStrategySettings() ){
265  maxStrategyId = Math.max( maxStrategyId , Long.parseLong( strategySettings.getId().toString() ) );
266  }
267  return Id.create(maxStrategyId + 1, StrategySettings.class);
268  }
269 
270 
281  public static <T extends ConfigGroup> T addOrGetModule( Config config, Class<T> moduleClass ) {
282  String groupName;
283  try {
284  groupName = moduleClass.newInstance().getName();
285  } catch (InstantiationException | IllegalAccessException e) {
286  throw new RuntimeException(e) ;
287  }
288  return addOrGetModule( config, groupName, moduleClass ) ;
289  }
290 
302  public static <T extends ConfigGroup> T addOrGetModule(Config config, String groupName, Class<T> moduleClass) {
303  ConfigGroup module = config.getModule(groupName);
304  if (module == null || module.getClass() == ConfigGroup.class) {
305  try {
306  module = moduleClass.newInstance();
307  config.addModule(module);
308  } catch (InstantiationException | IllegalAccessException e) {
309  throw new RuntimeException(e);
310  }
311  }
312  return moduleClass.cast(module);
313  }
314 
318  public static boolean hasModule(Config config, Class<? extends ConfigGroup> moduleClass) {
319  String groupName;
320  try {
321  groupName = moduleClass.getDeclaredConstructor().newInstance().getName();
322  return config.getModules().containsKey(groupName);
323  } catch (ReflectiveOperationException e) {
324  throw new RuntimeException(e) ;
325  }
326  }
327 
328  public static void setVspDefaults(final Config config) {
329  config.timeAllocationMutator().setMutationRange(7200.);
334  }
335 
336  public static void writeConfig( final Config config, String filename ) {
337  new ConfigWriter(config).write(filename);
338  }
339  public static void writeMinimalConfig( final Config config, String filename ) {
340  new ConfigWriter(config,Verbosity.minimal).write(filename);
341  }
342 }
static< T extends ConfigGroup > T addOrGetModule(Config config, String groupName, Class< T > moduleClass)
void readFile(final String filename, final String dtdFilename)
static Config createConfig(URL context)
static String getAbsolutePath(final String prefix, final String path)
void applyConfiguration(Config config)
static< T extends ConfigGroup > T addOrGetModule(Config config, Class< T > moduleClass)
static Config applyCommandline(Config config, String[] typedArgs)
final NetworkConfigGroup network()
Definition: Config.java:411
static Config loadConfig(final String filename, ConfigGroup... customModules)
final void write(final String filename)
void setRemovingUnneccessaryPlanAttributes(final boolean removingUnneccessaryPlanAttributes)
static Config loadConfig(final URL url, ConfigGroup... customModules)
static void setVspDefaults(final Config config)
static Config loadConfig(final URL url, String [] typedArgs, ConfigGroup... customModules)
static void writeMinimalConfig(final Config config, String filename)
final CountsConfigGroup counts()
Definition: Config.java:403
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
static Config loadConfig(Config config, final URL url, String [] typedArgs, ConfigGroup... customModules)
static void loadConfig(final Config config, final String filename)
final FacilitiesConfigGroup facilities()
Definition: Config.java:423
void setInputFile(final String countsFileName)
Builder allowPositionalArguments(boolean allow)
VspExperimentalConfigGroup vspExperimental()
Definition: Config.java:443
static Config loadConfig(String [] args, ConfigGroup... customModules)
final void addModule(final ConfigGroup specializedConfigModule)
Definition: Config.java:229
void setActivityDurationInterpretation(final PlansConfigGroup.ActivityDurationInterpretation actDurInterpret)
TimeAllocationMutatorConfigGroup timeAllocationMutator()
Definition: Config.java:463
static Config loadConfig(Config config, String [] args, ConfigGroup... customModules)
static URL resolveFileOrResource(String filename)
Definition: IOUtils.java:207
final PlansConfigGroup plans()
Definition: Config.java:415
static void assertNotNull(Object obj)
Definition: Gbl.java:212
void setVspDefaultsCheckingLevel(VspDefaultsCheckingLevel vspDefaultsCheckingLevel)
static Id< StrategySettings > createAvailableStrategyId(Config config)
void setContext(URL context)
Definition: Config.java:541
static void writeConfig(final Config config, String filename)
final TreeMap< String, ConfigGroup > getModules()
Definition: Config.java:288
final ReplanningConfigGroup replanning()
Definition: Config.java:427
final ControllerConfigGroup controller()
Definition: Config.java:399
static void modifyFilePaths(final Config config, final String pathPrefix)
static Config createConfig(ConfigGroup... customModules)
final ConfigGroup getModule(final String moduleName)
Definition: Config.java:301
final HouseholdsConfigGroup households()
Definition: Config.java:419
static Config loadConfig(URL url)
static boolean hasModule(Config config, Class<? extends ConfigGroup > moduleClass)
static CommandLine getCommandLine(String[] args)
StrategySettings getStrategySettings(final Id< StrategySettings > index, final boolean createIfMissing)
static void loadConfig(final Config config, final URL url, ConfigGroup... customModules)
static Config createConfig(final String context)