Skip to content

Commit

Permalink
Merge branch 'feature/runnablesimulation' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Macbeth committed Jul 12, 2011
2 parents f432546 + bedabf6 commit 220f643
Show file tree
Hide file tree
Showing 8 changed files with 732 additions and 116 deletions.
31 changes: 18 additions & 13 deletions core/src/main/java/uk/ac/imperial/presage2/core/IntegerTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@

package uk.ac.imperial.presage2.core;


/**
* @author Sam Macbeth
*
*
*/
public class IntegerTime implements Time {

private int time = 0;

public IntegerTime() {

}

/**
* @param time
*/
Expand All @@ -44,18 +44,18 @@ public IntegerTime(int time) {
*/
@Override
public boolean equals(Time t) {
if(t instanceof IntegerTime) {
if (t instanceof IntegerTime) {
IntegerTime it = (IntegerTime) t;
return it.time == this.time;
}
return false;
}

@Override
public boolean equals(Object o) {
if(o instanceof Time) {
if (o instanceof Time) {
return this.equals((Time) o);
} else if(o instanceof Integer) {
} else if (o instanceof Integer) {
return ((Integer) o).intValue() == this.time;
} else
return false;
Expand All @@ -71,28 +71,33 @@ public void increment() {

@Override
public void setTime(Time t) {
if(t instanceof IntegerTime) {
if (t instanceof IntegerTime) {
IntegerTime it = (IntegerTime) t;
this.time = it.time;
}
}

@Override
public String toString() {
return new Integer(this.time).toString();
}

@Override
public Time clone() {
return new IntegerTime(this.time);
}

@Override
public boolean greaterThan(Time t) {
if(t != null)
if (t != null)
return this.time > ((IntegerTime) t).time;
else
return false;
}

@Override
public int intValue() {
return this.time;
}

}
45 changes: 29 additions & 16 deletions core/src/main/java/uk/ac/imperial/presage2/core/Time.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,71 @@

package uk.ac.imperial.presage2.core;


import com.google.inject.AbstractModule;

/**
* This is a generic representation of a time within the simulation. Using
* this representation allows more complex time structures to be used rather
* than just discrete integer time.
* This is a generic representation of a time within the simulation. Using this
* representation allows more complex time structures to be used rather than
* just discrete integer time.
*
* @author Sam Macbeth
*
*
*/
public interface Time {

public String toString();

/**
* Check if two times are equal.
*
* @param t
* @return true if both Times represent the same discrete simulation time.
*/
public boolean equals(Time t);

/**
* Increment this time to the next discrete time value.
*/
public void increment();

public void setTime(Time t);

/**
* Clone this time
*
* @return clone of this time.
*/
public Time clone();

/**
*
* @param t time to compare to.
* @param t
* time to compare to.
* @return true if this > t, false otherwise
*/
public boolean greaterThan(Time t);

/**
* Provides various {@link AbstractModule}s to bind different
* types of {@link Time}.
* Get this time as an integer value. Used to determine quantities of time
* ticks.
*
* @return value of this time as an integer.
*/
public int intValue();

/**
* Provides various {@link AbstractModule}s to bind different types of
* {@link Time}.
*
* @author Sam Macbeth
*
*
*/
class Bind {

/**
* Bind {@link Time} to {@link IntegerTime} and set the simulation
* finish time to finshTime.
*
* @param finishTime
* @return {@link AbstractModule}
*/
Expand All @@ -80,7 +92,8 @@ public static AbstractModule integerTime(final int finishTime) {
@Override
protected void configure() {
bind(Time.class).to(IntegerTime.class);
bind(Time.class).annotatedWith(FinishTime.class).toInstance(new IntegerTime(finishTime));
bind(Time.class).annotatedWith(FinishTime.class)
.toInstance(new IntegerTime(finishTime));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Copyright (C) 2011 Sam Macbeth <sm1106 [at] imperial [dot] ac [dot] uk>
*
* This file is part of Presage2.
*
* Presage2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Presage2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with Presage2. If not, see <http://www.gnu.org/licenses/>.
*/
package uk.ac.imperial.presage2.core.simulator;

import java.util.HashSet;
import java.util.Set;


import com.google.inject.AbstractModule;
import com.google.inject.Injector;

/**
* A {@link RunnableSimulation} for simulations which we define with a set of
* {@link AbstractModule}s.
*
* @author Sam Macbeth
*
*/
public abstract class InjectedSimulation extends RunnableSimulation {

private Injector injector;

private final Set<AbstractModule> modules = new HashSet<AbstractModule>();

/**
* Create the simulation wrapper with some addition modules to use.
*
* @param modules
*/
public InjectedSimulation(Set<AbstractModule> modules) {
super();
this.modules.addAll(modules);
}

public InjectedSimulation() {
super();
}

/**
* Add an {@link AbstractModule} to the set we will use to create this
* simulation's {@link Scenario} and {@link Simulator}.
*
* @param module
*/
final protected void addModule(AbstractModule module) {
this.modules.add(module);
}

/**
* Add a set of {@link AbstractModule}s to the set we will use to create
* this simulation's {@link Scenario} and {@link Simulator}.
*
* @param module
*/
final protected void addModules(Set<AbstractModule> modules) {
this.modules.addAll(modules);
}

protected abstract Set<AbstractModule> getModules();

final public void load() {
this.addModules(getModules());
// create scenario, simulator & injector
AbstractModule[] marray = new AbstractModule[modules.size()];
int i = 0;
for (AbstractModule mod : modules) {
marray[i] = mod;
i++;
}
scenario = Scenario.Builder.createFromModules(marray);
injector = Scenario.Builder.injector;
simulator = injector.getInstance(Simulator.class);
this.addToScenario(scenario);
this.state = SimulationState.READY;
}

protected abstract void addToScenario(Scenario s);

protected final Injector getInjector() {
return injector;
}

}
Loading

0 comments on commit 220f643

Please sign in to comment.