MATSIM
RunPluggableTripRouterExample.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 tutorial.programming.example12PluggableTripRouter;
21 
22 import org.matsim.core.config.Config;
27 
29 
30  public static void main(final String[] args) {
31 
32  Config config;
33  if ( args.length==0 ) {
34  config = ConfigUtils.loadConfig("examples/equil/config.xml") ;
35  } else {
36  config = ConfigUtils.loadConfig(args[0]);
37  }
38  config.controler().setOverwriteFileSetting(OverwriteFileSetting.deleteDirectoryIfExists);
39  final Controler controler = new Controler(config);
40 
41  final MySimulationObserver observer = new MySimulationObserver();
42  // My observer is an EventHandler. I can ask it what it thinks the world currently looks like,
43  // based on the last observed iteration, and pass that into my routing module to make decisions.
44  //
45  // Do not plug a routing module itself into the EventsManager! Trip routers are short lived,
46  // they are recreated as needed (per iteration, per thread...), and factory methods
47  // such as this should normally not have visible side effects (such as plugging something
48  // into the EventsManager).
49 
50  controler.addOverridingModule(new AbstractModule() {
51  @Override
52  public void install() {
53  addEventHandlerBinding().toInstance(observer);
54  bind(MySimulationObserver.class).toInstance(observer);
55  addRoutingModuleBinding("car").to(MyRoutingModule.class);
56  }
57  });
58 
59  controler.run();
60 
61  }
62 
63 }
final void addOverridingModule(AbstractModule abstractModule)
Definition: Controler.java:410
ControlerConfigGroup controler
Definition: Config.java:87
static Config loadConfig(final String filename, ConfigGroup...customModules)