MATSIM
TravelTimeCalculatorModule.java
Go to the documentation of this file.
1 /*
2  * *********************************************************************** *
3  * * project: org.matsim.*
4  * * TravelTimeCalculatorModule.java
5  * * *
6  * * *********************************************************************** *
7  * * *
8  * * copyright : (C) 2014 by the members listed in the COPYING, *
9  * * LICENSE and WARRANTY file. *
10  * * email : info at matsim dot org *
11  * * *
12  * * *********************************************************************** *
13  * * *
14  * * This program is free software; you can redistribute it and/or modify *
15  * * it under the terms of the GNU General Public License as published by *
16  * * the Free Software Foundation; either version 2 of the License, or *
17  * * (at your option) any later version. *
18  * * See also COPYING, LICENSE and WARRANTY file *
19  * * *
20  * * ***********************************************************************
21  */
22 
23 package org.matsim.core.trafficmonitoring;
24 
25 import com.google.inject.Injector;
26 import com.google.inject.Key;
27 import com.google.inject.Singleton;
28 import com.google.inject.name.Names;
29 import jakarta.inject.Inject;
30 import jakarta.inject.Provider;
38 
39 import java.util.Set;
40 
41 
50 
51  @Override
52  public void install() {
53  if (getConfig().travelTimeCalculator().getSeparateModes()) {
54  // (this is the default)
55 
56  if (getConfig().travelTimeCalculator().isCalculateLinkToLinkTravelTimes()) {
57  throw new RuntimeException("separate modes together with link2link routing currently not implemented. doesn't look difficult, "
58  + "but I cannot say if it would be picked up correctly by downstream modules. kai, nov'16");
59  }
60 
61  Set<String> analyzedModes = getConfig().travelTimeCalculator().getAnalyzedModes();
62 
63  // go through all modes:
64  // for (final String mode : CollectionUtils.stringToSet(getConfig().travelTimeCalculator().getAnalyzedModesAsString() )) {
65  for (final String mode : getConfig().routing().getNetworkModes()) {
66 
67  if (analyzedModes.contains(mode)) {
68  // generate and bind the observer:
69  bind(TravelTimeCalculator.class).annotatedWith(Names.named(mode)).toProvider(new SingleModeTravelTimeCalculatorProvider(mode)).in(Singleton.class);
70 
71  // bind the observer to travel time provider (for router):
72  addTravelTimeBinding(mode).toProvider(new Provider<TravelTime>() {
73  @Inject
74  Injector injector;
75 
76  @Override
77  public TravelTime get() {
78  return injector.getInstance(Key.get(TravelTimeCalculator.class, Names.named(mode))).getLinkTravelTimes();
79  }
80 
81  // the following is not there yet (leads to NPE). Presumably, the collection into the underlying multi-binder is
82  // done later, and until then it is only available per annotation (as above)? kai, nov'19
83 // @Inject Map<String,TravelTime> travelTimes ;
84 // @Override public TravelTime get() { return travelTimes.get( mode ) ; }
85  }).in(Singleton.class);
86  // (This used to be without "Singleton". I think that with Singleton it makes more sense, but don't know ramifications. kai, nov'19)
87  } else {
88 
89  // For modes that are not analyzed, no travel time calculator is bound
90  // however, travel time is still provided
91  addTravelTimeBinding(mode).to(FreeSpeedTravelTime.class).in(Singleton.class);
92 
93  }
94 
95  }
96  } else {
97  // (all analyzed modes are measured together, and the same result is returned to each mode)
98 
99  // bind the TravelTimeCalculator, which is the observer and aggregator:
100  bind(TravelTimeCalculator.class).in(Singleton.class);
101 
102  // bind the TravelTime objects. In this case, this just passes on the same information from TravelTimeCalculator to each individual mode:
103  if (getConfig().travelTimeCalculator().isCalculateLinkTravelTimes()) {
104 // for (String mode : CollectionUtils.stringToSet(getConfig().travelTimeCalculator().getAnalyzedModesAsString() )) {
105  for (String mode : getConfig().routing().getNetworkModes()) {
106  addTravelTimeBinding(mode).toProvider(ObservedLinkTravelTimes.class);
107  }
108  }
109  if (getConfig().travelTimeCalculator().isCalculateLinkToLinkTravelTimes()) {
110  bind(LinkToLinkTravelTime.class).toProvider(ObservedLinkToLinkTravelTimes.class);
111  }
112  }
113 
114  }
115 
116  private static class SingleModeTravelTimeCalculatorProvider implements Provider<TravelTimeCalculator> {
117 
118  @Inject
120  @Inject
121  EventsManager eventsManager;
122  @Inject
123  Network network;
124 
125  private String mode;
126 
128  this.mode = mode;
129  }
130 
131  @Override
132  public TravelTimeCalculator get() {
133 // TravelTimeCalculator calculator = new TravelTimeCalculator(network, config.getTraveltimeBinSize(), config.getMaxTime(),
134 // config.isCalculateLinkTravelTimes(), config.isCalculateLinkToLinkTravelTimes(), true, CollectionUtils.stringToSet(mode));
135 // eventsManager.addHandler(calculator);
136 // return TravelTimeCalculator.configure(calculator, config, network);
138  builder.setTimeslice(config.getTraveltimeBinSize());
139  builder.setMaxTime(config.getMaxTime());
140  builder.setCalculateLinkTravelTimes(config.isCalculateLinkTravelTimes());
141  builder.setCalculateLinkToLinkTravelTimes(config.isCalculateLinkToLinkTravelTimes());
142  builder.setFilterModes(true); // no point asking the config since we are in "separateModes" anyways.
143  builder.setAnalyzedModes(CollectionUtils.stringToSet(mode));
144  builder.configure(config);
145  TravelTimeCalculator calculator = builder.build();
146  eventsManager.addHandler(calculator);
147  return calculator;
148  }
149  }
150 
151  private static class ObservedLinkTravelTimes implements Provider<TravelTime> {
152 
153  @Inject
154  TravelTimeCalculator travelTimeCalculator;
155 
156  @Override
157  public TravelTime get() {
158  return travelTimeCalculator.getLinkTravelTimes();
159  }
160 
161  }
162 
163  private static class ObservedLinkToLinkTravelTimes implements Provider<LinkToLinkTravelTime> {
164 
165  @Inject
166  TravelTimeCalculator travelTimeCalculator;
167 
168  @Override
169  public LinkToLinkTravelTime get() {
170  return travelTimeCalculator.getLinkToLinkTravelTimes();
171  }
172 
173  }
174 }
static Set< String > stringToSet(final String values)
TravelTimeCalculatorConfigGroup travelTimeCalculator()
Definition: Config.java:431
final com.google.inject.binder.LinkedBindingBuilder< TravelTime > addTravelTimeBinding(String mode)