diff --git a/matsim/src/main/java/org/matsim/core/controler/Controler.java b/matsim/src/main/java/org/matsim/core/controler/Controler.java index 9605d0cdd63..c876db024de 100755 --- a/matsim/src/main/java/org/matsim/core/controler/Controler.java +++ b/matsim/src/main/java/org/matsim/core/controler/Controler.java @@ -132,6 +132,8 @@ public IterationStopWatch getStopwatch() { private List overridingQSimModules = new LinkedList<>(); + private List modulesToAdd = new LinkedList<>() ; + public static void main(final String[] args) { if ((args == null) || (args.length == 0)) { System.out.println("No argument given!"); @@ -243,9 +245,20 @@ public void install(){ // should not be necessary: created in the controler //install(new ScenarioByInstanceModule(scenario)); } - } - ); - this.injector = Injector.createInjector( config, AbstractModule.override( standardModules, overrides ) ); + }) ; + + AbstractModule modulesThatShouldNotOverrideEachOther = new AbstractModule(){ + @Override public void install(){ + for( AbstractModule module : modulesToAdd ){ + install( module ); + } + } + };; + + Set combinedStandardModule = Collections.singleton( + AbstractModule.override( standardModules , modulesThatShouldNotOverrideEachOther ) );; + + this.injector = Injector.createInjector( config, AbstractModule.override( combinedStandardModule, overrides ) ); ControlerI controler = injector.getInstance(ControlerI.class); controler.run(); } @@ -454,6 +467,14 @@ public void install() { @Override public final Controler addOverridingModule( AbstractModule abstractModule ) { + if (this.injectorCreated) { + throw new RuntimeException("Too late for configuring the Controler. This can only be done before calling run."); + } + this.modulesToAdd.add( abstractModule ) ; + return this ; + } + + public final Controler addFullyOverridingModule( AbstractModule abstractModule ) { if (this.injectorCreated) { throw new RuntimeException("Too late for configuring the Controler. This can only be done before calling run."); } diff --git a/matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBindingTest.java b/matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBindingTest.java new file mode 100644 index 00000000000..f18643d472e --- /dev/null +++ b/matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBindingTest.java @@ -0,0 +1,95 @@ +package org.matsim.core.controler; + +import com.google.inject.CreationException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.matsim.api.core.v01.Scenario; +import org.matsim.core.config.Config; +import org.matsim.core.config.ConfigUtils; +import org.matsim.core.mobsim.framework.Mobsim; +import org.matsim.core.scenario.ScenarioUtils; +import org.matsim.testcases.MatsimTestUtils; + +public class MultipleOverridesOfSameBindingTest { + private static final Logger log = LogManager.getLogger( MultipleOverridesOfSameBindingTest.class ) ; + + @Rule public MatsimTestUtils utils = new MatsimTestUtils() ; + + private static int mobsimVersion = 0 ; + + @Test + public void testSecondOverridesFirst() { + Controler controler = getControler(); + + controler.addFullyOverridingModule( new Mobsim1Module() ) ; + controler.addFullyOverridingModule( new Mobsim2Module() ) ; + + controler.run() ; + + Assert.assertEquals( 2, mobsimVersion ); + + } + @Test + public void testInstallDoesNotOverride() { + Controler controler = getControler(); + + try{ + controler.addFullyOverridingModule( new AbstractModule(){ + @Override public void install(){ + install( new Mobsim1Module() ); + install( new Mobsim2Module() ); + } + } ); + } catch ( Exception ee ) { + Assert.assertTrue( ee instanceof CreationException ); + } + + } + @Test + public void testNewAddModueMethod() { + Controler controler = getControler(); + + try{ + controler.addOverridingModule( new Mobsim1Module() ); + controler.addOverridingModule( new Mobsim2Module() ); + } catch ( Exception ee ) { + Assert.assertTrue( ee instanceof CreationException ); + } + } + + private Controler getControler(){ + mobsimVersion = 0 ; + + Config config = ConfigUtils.createConfig() ; + config.controler().setOutputDirectory( utils.getOutputDirectory() ); + config.controler().setLastIteration( 0 ); + + Scenario scenario = ScenarioUtils.loadScenario( config ) ; + + return new Controler( scenario ); + } + + private static class Mobsim1Module extends AbstractModule{ + @Override public void install(){ + this.bindMobsim().toInstance( new Mobsim(){ + @Override public void run(){ + log.warn("running mobsim1") ; + mobsimVersion = 1 ; + } + } ); + } + } + private static class Mobsim2Module extends AbstractModule{ + @Override public void install(){ + this.bindMobsim().toInstance( new Mobsim(){ + @Override public void run(){ + log.warn("running mobsim2") ; + mobsimVersion = 2 ; + } + } ); + } + } +}