20 package org.matsim.core.controler.corelisteners;
22 import com.google.inject.Inject;
23 import com.google.inject.Singleton;
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
61 import java.util.Collections;
65 final class DumpDataAtEndImpl
implements DumpDataAtEnd, ShutdownListener {
66 private static final Logger log = LogManager.getLogger( DumpDataAtEndImpl.class );
69 private Config config;
72 private ControllerConfigGroup controllerConfigGroup;
75 private VspExperimentalConfigGroup vspConfig;
78 private Network network;
81 private Population population;
84 private ActivityFacilities activityFacilities;
87 private Vehicles vehicles;
89 @Inject(optional =
true)
90 private TransitSchedule transitSchedule = null;
92 @Inject(optional = true)
94 private Vehicles transitVehicles = null;
96 @Inject(optional = true)
97 private Counts<Link> counts = null;
100 private Households households;
106 private Scenario scenario;
109 private OutputDirectoryHierarchy controlerIO;
112 private Map<Class<?>,AttributeConverter<?>> attributeConverters = Collections.emptyMap();
116 public
void notifyShutdown(ShutdownEvent event) {
117 if ( event.isUnexpected() ) {
124 dumpNetworkChangeEvents();
126 dumpTransitSchedule();
127 dumpTransitVehicles();
133 if (!event.isUnexpected() && this.vspConfig.isWritingOutputEvents() && (this.controllerConfigGroup.getWriteEventsInterval()!=0)) {
134 dumpOutputEvents(event.getIteration());
136 dumpOutputTrips(event.getIteration());
137 dumpOutputLegs(event.getIteration());
138 dumpOutputActivities(event.getIteration());
139 dumpExperiencedPlans(event.getIteration());
141 if (controllerConfigGroup.getCleanItersAtEnd() == ControllerConfigGroup.CleanIterations.delete) {
142 this.controlerIO.deleteIterationDirectory();
146 private void dumpOutputEvents(
int iteration) {
147 for (ControllerConfigGroup.EventsFileFormat format :
this.controllerConfigGroup.getEventsFileFormats()) {
149 Controler.DefaultFiles file;
152 file = Controler.DefaultFiles.events;
155 file = Controler.DefaultFiles.eventsPb;
158 file = Controler.DefaultFiles.eventsJson;
164 IOUtils.copyFile(this.controlerIO.getIterationFilename(iteration, file),
165 this.controlerIO.getOutputFilename(file));
167 LogManager.getLogger(this.getClass()).error(
"writing output events did not work; probably parameters were such that no events were " 168 +
"generated in the final iteration");
173 private void dumpOutputTrips(
int iteration) {
175 IOUtils.copyFile(this.controlerIO.getIterationFilename(iteration, Controler.DefaultFiles.tripscsv),
176 this.controlerIO.getOutputFilename(Controler.DefaultFiles.tripscsv));
178 LogManager.getLogger(this.getClass()).error(
"writing output trips did not work; probably parameters were such that no trips CSV were " 179 +
"generated in the final iteration");
183 private void dumpOutputActivities(
int iteration) {
185 IOUtils.copyFile(this.controlerIO.getIterationFilename(iteration, Controler.DefaultFiles.activitiescsv),
186 this.controlerIO.getOutputFilename(Controler.DefaultFiles.activitiescsv));
188 LogManager.getLogger(this.getClass()).error(
"writing output activities did not work; probably parameters were such that no activities CSV were " 189 +
"generated in the final iteration");
193 private void dumpOutputLegs(
int iteration) {
195 IOUtils.copyFile(this.controlerIO.getIterationFilename(iteration, Controler.DefaultFiles.legscsv),
196 this.controlerIO.getOutputFilename(Controler.DefaultFiles.legscsv));
198 LogManager.getLogger(this.getClass()).error(
"writing output legs did not work; probably parameters were such that no legs CSV were " 199 +
"generated in the final iteration");
203 private void dumpExperiencedPlans(
int iteration) {
204 if (this.config.scoring().isWriteExperiencedPlans() ) {
206 IOUtils.copyFile(this.controlerIO.getIterationFilename(iteration, Controler.DefaultFiles.experiencedPlans),
207 this.controlerIO.getOutputFilename(Controler.DefaultFiles.experiencedPlans));
209 LogManager.getLogger(this.getClass()).error(
"writing output experienced plans did not work; probably parameters were such that they " 210 +
"were not generated in the final iteration", ee);
215 private void dumpCounts() {
217 if (this.counts != null ) {
222 new CountsWriter(this.counts).write(this.controlerIO.getOutputFilename(Controler.DefaultFiles.counts));
240 log.error(
"Exception writing counts.", ee);
244 private void dumpLanes() {
246 if ( this.lanes!=null && !this.lanes.getLanesToLinkAssignments().isEmpty() ){
247 new LanesWriter( this.lanes ).write( this.controlerIO.getOutputFilename( Controler.DefaultFiles.lanes ) );
250 log.error(
"Exception writing lanes.", ee);
254 private void dumpHouseholds() {
256 HouseholdsWriterV10 writer =
new HouseholdsWriterV10(this.households);
257 writer.putAttributeConverters(this.attributeConverters);
258 writer.writeFile(this.controlerIO.getOutputFilename(Controler.DefaultFiles.households));
260 log.error(
"Exception writing households.", ee);
264 private void dumpVehicles() {
266 new MatsimVehicleWriter(this.vehicles).writeFile(this.controlerIO.getOutputFilename(Controler.DefaultFiles.vehicles));
267 Vehicles allVehicles = VehicleUtils.getOrCreateAllvehicles( scenario );
268 if ( allVehicles!=null && !allVehicles.getVehicleTypes().isEmpty() ){
269 new MatsimVehicleWriter( allVehicles ).writeFile( this.controlerIO.getOutputFilename( Controler.DefaultFiles.allVehicles ) );
272 log.error(
"Exception writing vehicles.", ee);
276 private void dumpTransitVehicles() {
278 if (this.transitVehicles != null ) {
279 new MatsimVehicleWriter(this.transitVehicles).writeFile(this.controlerIO.getOutputFilename(Controler.DefaultFiles.transitVehicles));
282 log.error(
"Exception writing transit vehicles.", ee);
286 private void dumpTransitSchedule() {
288 if (this.transitSchedule != null ) {
289 new TransitScheduleWriter(this.transitSchedule).writeFile(this.controlerIO.getOutputFilename(Controler.DefaultFiles.transitSchedule));
292 log.error(
"Exception writing transit schedule.", ee);
296 private void dumpNetworkChangeEvents() {
297 if (this.config.network().isTimeVariantNetwork()) {
298 new NetworkChangeEventsWriter().write(this.controlerIO.getOutputFilename(Controler.DefaultFiles.changeEvents),
299 NetworkUtils.getNetworkChangeEvents(this.network));
303 private void dumpFacilities() {
306 new FacilitiesWriter(this.activityFacilities).write(this.controlerIO.getOutputFilename(Controler.DefaultFiles.facilities));
308 log.error(
"Exception writing facilities.", ee);
312 private void dumpConfig() {
314 new ConfigWriter(this.config).write(this.controlerIO.getOutputFilename(Controler.DefaultFiles.config, ControllerConfigGroup.CompressionType.none));
315 new ConfigWriter(this.config, ConfigWriter.Verbosity.minimal).write(this.controlerIO.getOutputFilename(Controler.DefaultFiles.configReduced, ControllerConfigGroup.CompressionType.none));
318 private void dumpNetwork() {
320 new NetworkWriter(this.network).write(this.controlerIO.getOutputFilename(Controler.DefaultFiles.network));
323 private void dumpPlans() {
326 final PopulationWriter writer =
new PopulationWriter(this.population, this.network);
327 writer.putAttributeConverters(this.attributeConverters);
328 writer.write(this.controlerIO.getOutputFilename(Controler.DefaultFiles.population));