Skip to content

Commit

Permalink
Merge pull request #3511 from matsim-org/ControlerUtils
Browse files Browse the repository at this point in the history
allow for Controller with double l
  • Loading branch information
kainagel authored Oct 13, 2024
2 parents 5f085ef + 3709f00 commit 7c2bb7a
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
* @author kturner
*
*/
public class CarrierControlerUtilsIT {
public class CarrierControllerUtilsIT{

private final Id<Carrier> CARRIER_SERVICES_ID = Id.create("CarrierWServices", Carrier.class);
private final Id<Carrier> CARRIER_SHIPMENTS_ID = Id.create("CarrierWShipments", Carrier.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@
import java.net.URL;
import java.util.Collection;

public class CarrierControlerUtilsTest {
public class CarrierControllerUtilsTest{

@RegisterExtension
private MatsimTestUtils utils = new MatsimTestUtils();

private static final Logger log = LogManager.getLogger(CarrierControlerUtilsTest.class);
private static final Logger log = LogManager.getLogger( CarrierControllerUtilsTest.class );

private final Id<Carrier> CARRIER_SERVICES_ID = Id.create("CarrierWServices", Carrier.class);
private final Id<Carrier> CARRIER_SHIPMENTS_ID = Id.create("CarrierWShipments", Carrier.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.ControlerUtils;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.scenario.ScenarioUtils;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected final void run(final Config config) {
public void run() throws MatsimRuntimeModifications.UnexpectedShutdownException {
loadCoreListeners();
controlerListenerManagerImpl.fireControlerStartupEvent();
ControlerUtils.checkConfigConsistencyAndWriteToLog(config, "config dump before iterations start");
ControllerUtils.checkConfigConsistencyAndWriteToLog(config, "config dump before iterations start" );
prepareForSim();
doIterations(config);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
*
* @author mrieser
*/
public final class Controler implements ControlerI, MatsimServices, AllowsConfiguration{
public final class Controler implements Controller, ControlerI, MatsimServices, AllowsConfiguration{
// yyyy Design thoughts:
// * Seems to me that we should try to get everything here final. Flexibility is provided by the ability to set or add factories. If this is
// not sufficient, people should use AbstractController. kai, jan'13
Expand Down
101 changes: 14 additions & 87 deletions matsim/src/main/java/org/matsim/core/controler/ControlerUtils.java
Original file line number Diff line number Diff line change
@@ -1,103 +1,30 @@
/* *********************************************************************** *
* project: org.matsim.* *
* *
* *********************************************************************** *
* *
* copyright : (C) 2008 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** */
package org.matsim.core.controler;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.google.inject.Injector;
import org.matsim.api.core.v01.Scenario;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigWriter;
import org.matsim.core.controler.corelisteners.ControlerDefaultCoreListenersModule;
import org.matsim.core.scenario.ScenarioByInstanceModule;

/**
* @author nagel
*
* @deprecated -- use class and methods with double l.
*/
public final class ControlerUtils {
private static final Logger log = LogManager.getLogger( ControlerUtils.class ) ;
@Deprecated
public class ControlerUtils {
private ControlerUtils(){} // namespace only; do not instantiate

/**
* This is meant for creating the matsim injector if one does not need/want {@link Controler}. Technical reason is that {@link Controler} creates
* the injector in the run method, and then it is too late to extract material in a direct way.
*
* @param config
* @param scenario
* @return
* @deprecated -- use controller methods with double l
*/
public static com.google.inject.Injector createAdhocInjector( Config config, Scenario scenario ){
return Injector.createInjector( config, new AbstractModule(){
@Override public void install(){
install( new NewControlerModule() );
install( new ControlerDefaultCoreListenersModule() );
install( new ControlerDefaultsModule() );
install( new ScenarioByInstanceModule( scenario ) );
}
} );
}

private ControlerUtils() {} // namespace for static methods only should not be instantiated

/**
* Design decisions:
* <ul>
* <li>I extracted this method since it is now called <i>twice</i>: once
* directly after reading, and once before the iterations start. The second
* call seems more important, but I wanted to leave the first one there in
* case the program fails before that config dump. Might be put into the
* "unexpected shutdown hook" instead. kai, dec'10
*
* Removed the first call for now, because I am now also checking for
* consistency with loaded controler modules. If still desired, we can
* put it in the shutdown hook.. michaz aug'14
*
* </ul>
*
* @param config TODO
* @param message the message that is written just before the config dump
*/
public static final void checkConfigConsistencyAndWriteToLog(Config config,
final String message) {
log.info(message);
String newline = System.lineSeparator();// use native line endings for logfile
StringWriter writer = new StringWriter();
new ConfigWriter(config).writeStream(new PrintWriter(writer), newline);
log.info(newline + newline + writer.getBuffer().toString());
log.info("Complete config dump done.");
log.info("Checking consistency of config...");
config.checkConsistency();
log.info("Checking consistency of config done.");
}

@Deprecated // use OutputDirectoryLogging.catchLogEntries() directly. kai, mar'18
public static final void initializeOutputLogging() {
OutputDirectoryLogging.catchLogEntries();
@Deprecated
public Controler createControler( Scenario scenario ) {
return (Controler) ControllerUtils.createController( scenario );
}

public static void catchLogEntries() {
OutputDirectoryLogging.catchLogEntries();
public Injector createAdhocInjector( Config config, Scenario scenario ) {
return ControllerUtils.createAdhocInjector( config, scenario );
}

public Controler createControler( Scenario scenario ) {
return new Controler( scenario );
public Injector createAdhocInjector( Scenario scenario ) {
return ControllerUtils.createAdhocInjector( scenario );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.matsim.core.controler;

/**
* This is an attempt to allow a syntax that spells controller with double l.
*/
public interface Controller extends MatsimServices, AllowsConfiguration, Runnable {
}
121 changes: 121 additions & 0 deletions matsim/src/main/java/org/matsim/core/controler/ControllerUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* *********************************************************************** *
* project: org.matsim.* *
* *
* *********************************************************************** *
* *
* copyright : (C) 2008 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** */
package org.matsim.core.controler;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Scenario;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigWriter;
import org.matsim.core.controler.corelisteners.ControlerDefaultCoreListenersModule;
import org.matsim.core.scenario.ScenarioByInstanceModule;

/**
* @author nagel
*
*/
public final class ControllerUtils{
private static final Logger log = LogManager.getLogger( ControllerUtils.class ) ;
/**
* This is meant for creating the matsim injector if one does not need/want {@link Controler}. Technical reason is that {@link Controler} creates
* the injector in the run method, and then it is too late to extract material in a direct way.
*
* @param config
* @param scenario
* @return
*/
public static com.google.inject.Injector createAdhocInjector( Config config, Scenario scenario ){
return Injector.createInjector( config, new AbstractModule(){
@Override public void install(){
install( new NewControlerModule() );
install( new ControlerDefaultCoreListenersModule() );
install( new ControlerDefaultsModule() );
install( new ScenarioByInstanceModule( scenario ) );
}
} );
}
/**
* This is meant for creating the matsim injector if one does not need/want {@link Controler}.
* Technical reason is that {@link Controler} creates
* the injector in the run method, and then it is too late to extract material in a direct way.
*
* @param scenario
* @return
*/
public static com.google.inject.Injector createAdhocInjector( Scenario scenario ){
return Injector.createInjector( scenario.getConfig(), new AbstractModule(){
@Override public void install(){
install( new NewControlerModule() );
install( new ControlerDefaultCoreListenersModule() );
install( new ControlerDefaultsModule() );
install( new ScenarioByInstanceModule( scenario ) );
}
} );
}

private ControllerUtils() {} // namespace for static methods only should not be instantiated

/**
* Design decisions:
* <ul>
* <li>I extracted this method since it is now called <i>twice</i>: once
* directly after reading, and once before the iterations start. The second
* call seems more important, but I wanted to leave the first one there in
* case the program fails before that config dump. Might be put into the
* "unexpected shutdown hook" instead. kai, dec'10
*
* Removed the first call for now, because I am now also checking for
* consistency with loaded controler modules. If still desired, we can
* put it in the shutdown hook.. michaz aug'14
*
* </ul>
*
* @param config TODO
* @param message the message that is written just before the config dump
*/
public static final void checkConfigConsistencyAndWriteToLog(Config config,
final String message) {
log.info(message);
String newline = System.lineSeparator();// use native line endings for logfile
StringWriter writer = new StringWriter();
new ConfigWriter(config).writeStream(new PrintWriter(writer), newline);
log.info(newline + newline + writer.getBuffer().toString());
log.info("Complete config dump done.");
log.info("Checking consistency of config...");
config.checkConsistency();
log.info("Checking consistency of config done.");
}

@Deprecated // use OutputDirectoryLogging.catchLogEntries() directly. kai, mar'18
public static final void initializeOutputLogging() {
OutputDirectoryLogging.catchLogEntries();
}

public static void catchLogEntries() {
OutputDirectoryLogging.catchLogEntries();
}

public static Controller createController( Scenario scenario ) {
return new Controler( scenario );
}

}

0 comments on commit 7c2bb7a

Please sign in to comment.