Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow addModule method that will not allow a 2nd override of the same interface #628

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions matsim/src/main/java/org/matsim/core/controler/Controler.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public IterationStopWatch getStopwatch() {

private List<AbstractQSimModule> overridingQSimModules = new LinkedList<>();

private List<AbstractModule> modulesToAdd = new LinkedList<>() ;

public static void main(final String[] args) {
if ((args == null) || (args.length == 0)) {
System.out.println("No argument given!");
Expand Down Expand Up @@ -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<AbstractModule> combinedStandardModule = Collections.singleton(
AbstractModule.override( standardModules , modulesThatShouldNotOverrideEachOther ) );;

this.injector = Injector.createInjector( config, AbstractModule.override( combinedStandardModule, overrides ) );
ControlerI controler = injector.getInstance(ControlerI.class);
controler.run();
}
Expand Down Expand Up @@ -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.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 ;
}
} );
}
}
}