MATSIM
StrategyManagerModule.java
Go to the documentation of this file.
1 /*
2  * *********************************************************************** *
3  * * project: org.matsim.*
4  * * StrategyManagerModule.java
5  * * *
6  * * *********************************************************************** *
7  * * *
8  * * copyright : (C) 2015 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.replanning;
24 
25 import com.google.inject.Key;
26 import com.google.inject.Singleton;
27 import com.google.inject.TypeLiteral;
28 import com.google.inject.multibindings.MapBinder;
29 import com.google.inject.name.Names;
30 import jakarta.inject.Provider;
39 
40 public class StrategyManagerModule extends AbstractModule {
41  @Override
42  public void install() {
44  // (does commands of type "bind(PlanStrategy.class).annotatedWith(Names.named(strategyName))", i.e.
45  // plan strategies can be looked up under their names (*))
46 
47  bind(StrategyManager.class).in(Singleton.class);
48  bind(new TypeLiteral<StrategyChooser<Plan, Person>>() {}).to(new TypeLiteral<WeightedStrategyChooser<Plan, Person>>() {}).asEagerSingleton();
49  bind(ReplanningContext.class).to(ReplanningContextImpl.class).asEagerSingleton();
50 
51  MapBinder<ReplanningConfigGroup.StrategySettings, PlanStrategy> planStrategyMapBinder = MapBinder.newMapBinder(binder(), ReplanningConfigGroup.StrategySettings.class, PlanStrategy.class);
52  // (this will bind a Map that has StrategySettings as key, and PlanStrategy as value. Not sure why StrategySettings as key, and not just the name, but possibly this is mean to allow adding
53  // the same strategy multiple times, with possibly different settings.)
54 
55  for (ReplanningConfigGroup.StrategySettings settings : getConfig().replanning().getStrategySettings()) {
56  String name = settings.getStrategyName() ;
57  if (name.contains(".")) {
58  // plan strategy is in Java, but it is found via the class loader:
59  if (name.startsWith("org.matsim.core")
60  // && !name.startsWith("org.matsim.contrib.")
61  ) {
62  // org.matsim.core strategies are not to be loaded via the class loader:
63  throw new RuntimeException("Strategies in the org.matsim.core package must not be loaded by name!");
64  } else {
65  try {
66  Class klass = Class.forName(name);
67  if (PlanStrategy.class.isAssignableFrom(klass)) {
68  planStrategyMapBinder.addBinding(settings).to(klass);
69  } else if (Provider.class.isAssignableFrom(klass)) {
70  planStrategyMapBinder.addBinding(settings).toProvider(klass);
71  } else {
72  throw new RuntimeException("You specified a class name as a strategy, but it is neither a PlanStrategy nor a Provider.");
73  }
74  } catch (ClassNotFoundException e) {
75  throw new RuntimeException("You specified something which looks like a class name as a strategy, but the class could not be found.", e);
76  }
77  }
78  } else {
79  // this is the normal case: plan strategy comes from within matsim
80  planStrategyMapBinder.addBinding(settings).to(Key.get(PlanStrategy.class, Names.named(settings.getStrategyName())));
81  // (settings is the key ... ok. The Key.get(...) returns the PlanStrategy that was registered under its name at (*) above.)
82  }
83  }
84 
85  install(new ConflictModule());
86  }
87 }