MATSIM
NewControler.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *********************************************************************** *
4  * *
5  * copyright : (C) 2007, 2008 by the members listed in the COPYING, *
6  * LICENSE and WARRANTY file. *
7  * email : info at matsim dot org *
8  * *
9  * *********************************************************************** *
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * See also COPYING, LICENSE and WARRANTY file *
16  * *
17  * *********************************************************************** */
18 
19 package org.matsim.core.controler;
20 
21 import com.google.inject.Provider;
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
25 import org.matsim.core.config.Config;
31 
32 import jakarta.inject.Inject;
33 import java.util.Set;
34 
35 class NewControler extends AbstractController implements ControlerI {
36 
37  private static final Logger log = LogManager.getLogger(NewControler.class);
38 
39  private final Config config;
40  private final PrepareForSim prepareForSim;
41  private final PrepareForMobsim prepareForMobsim;
42  private final EventsHandling eventsHandling;
43  private final PlansDumping plansDumping;
44  private final PlansReplanning plansReplanning;
45  private final Provider<Mobsim> mobsimProvider;
46  private final PlansScoring plansScoring;
47  private final TerminationCriterion terminationCriterion;
48  private final DumpDataAtEnd dumpDataAtEnd;
49  private final Set<ControlerListener> controlerListenersDeclaredByModules;
50  private final ControllerConfigGroup controllerConfigGroup;
51  private final OutputDirectoryHierarchy outputDirectoryHierarchy;
52 
53  @Inject
54  NewControler(Config config, ControlerListenerManagerImpl controlerListenerManager, MatsimServices matsimServices,
55  IterationStopWatch stopWatch, PrepareForSim prepareForSim, EventsHandling eventsHandling,
56  PlansDumping plansDumping, PlansReplanning plansReplanning, Provider<Mobsim> mobsimProvider,
57  PlansScoring plansScoring, TerminationCriterion terminationCriterion, DumpDataAtEnd dumpDataAtEnd,
58  Set<ControlerListener> controlerListenersDeclaredByModules, ControllerConfigGroup controllerConfigGroup,
59  OutputDirectoryHierarchy outputDirectoryHierarchy
60  , PrepareForMobsim prepareForMobsim
61  ) {
62  super(controlerListenerManager, stopWatch, matsimServices);
63  this.config = config;
64  this.prepareForMobsim = prepareForMobsim;
65  this.config.addConfigConsistencyChecker(new ConfigConsistencyCheckerImpl());
66  this.prepareForSim = prepareForSim;
67  this.eventsHandling = eventsHandling;
68  this.plansDumping = plansDumping;
69  this.plansReplanning = plansReplanning;
70  this.mobsimProvider = mobsimProvider;
71  this.plansScoring = plansScoring;
72  this.terminationCriterion = terminationCriterion;
73  this.dumpDataAtEnd = dumpDataAtEnd;
74  this.controlerListenersDeclaredByModules = controlerListenersDeclaredByModules;
75  this.controllerConfigGroup = controllerConfigGroup;
76  this.outputDirectoryHierarchy = outputDirectoryHierarchy;
77  }
78 
79  @Override
80  public final void run() {
81  super.setupOutputDirectory(outputDirectoryHierarchy);
82  super.run(this.config);
83  OutputDirectoryLogging.closeOutputDirLogging();
84  }
85 
86  @Override
87  protected final void loadCoreListeners() {
88  /*
89  * The order how the listeners are added is very important! As
90  * dependencies between different listeners exist or listeners may read
91  * and write to common variables, the order is important.
92  *
93  * IMPORTANT: The execution order is reverse to the order the listeners
94  * are added to the list.
95  */
96  if (controllerConfigGroup.getDumpDataAtEnd()) {
97  this.addCoreControlerListener(this.dumpDataAtEnd);
98  }
99 
100  this.addCoreControlerListener(this.plansScoring);
101  this.addCoreControlerListener(this.plansReplanning);
102  this.addCoreControlerListener(this.plansDumping);
103  this.addCoreControlerListener(this.eventsHandling);
104  // must be last being added (=first being executed)
105 
106  for (ControlerListener controlerListener : this.controlerListenersDeclaredByModules) {
107  this.addControlerListener(controlerListener);
108  }
109  }
110 
111  @Override
112  protected final void prepareForSim() {
113  this.prepareForSim.run();
114  }
115 
116  @Override
117  protected final void prepareForMobsim() {
118  this.prepareForMobsim.run() ;
119 // this.prepareForSim.run() ;
120  }
121 
122  @Override
123  protected final void runMobSim() {
124  this.mobsimProvider.get().run();
125  }
126 
127  @Override
128  protected final boolean mayTerminateAfterIteration(int iteration) {
129  return terminationCriterion.mayTerminateAfterIteration(iteration);
130  }
131 
132  @Override
133  protected final boolean shouldTerminate(int iteration) {
134  return terminationCriterion.doTerminate(iteration);
135  }
136 }
void addConfigConsistencyChecker(final ConfigConsistencyChecker checker)
Definition: Config.java:493