Commit 5ae5481c authored by Jakob Schöttl's avatar Jakob Schöttl
Browse files

Merge branch 'activecallbackmodel' into develop

parents ea746e26 ea4c4583
package org.vadere.simulator.control;
/**
* This interface defines a callbacks for the simulation loop.
* It's implementations define the major part of the simulation model's logic.
* It is called "active" since it's implementations do change the state.
*
*
*/
public interface ActiveCallback {
void preLoop(final double simTimeInSec);
void postLoop(final double simTimeInSec);
void update(final double simTimeInSec);
}
...@@ -4,6 +4,7 @@ import org.apache.log4j.LogManager; ...@@ -4,6 +4,7 @@ import org.apache.log4j.LogManager;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.vadere.simulator.models.DynamicElementFactory; import org.vadere.simulator.models.DynamicElementFactory;
import org.vadere.simulator.models.MainModel; import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.Model;
import org.vadere.simulator.projects.ScenarioStore; import org.vadere.simulator.projects.ScenarioStore;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager; import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.state.attributes.AttributesSimulation; import org.vadere.state.attributes.AttributesSimulation;
...@@ -31,7 +32,7 @@ public class Simulation { ...@@ -31,7 +32,7 @@ public class Simulation {
private DynamicElementFactory dynamicElementFactory; private DynamicElementFactory dynamicElementFactory;
private final List<PassiveCallback> passiveCallbacks; private final List<PassiveCallback> passiveCallbacks;
private List<ActiveCallback> activeCallbacks; private List<Model> models;
private ProcessorManager processorManager; private ProcessorManager processorManager;
...@@ -72,7 +73,7 @@ public class Simulation { ...@@ -72,7 +73,7 @@ public class Simulation {
this.startTimeInSec = startTimeInSec; this.startTimeInSec = startTimeInSec;
this.simTimeInSec = startTimeInSec; this.simTimeInSec = startTimeInSec;
this.activeCallbacks = mainModel.getActiveCallbacks(); this.models = mainModel.getSubmodels();
// TODO [priority=normal] [task=bugfix] - the attributesCar are missing in initialize' parameters // TODO [priority=normal] [task=bugfix] - the attributesCar are missing in initialize' parameters
this.dynamicElementFactory = mainModel; this.dynamicElementFactory = mainModel;
...@@ -112,8 +113,8 @@ public class Simulation { ...@@ -112,8 +113,8 @@ public class Simulation {
runSimulation = true; runSimulation = true;
simTimeInSec = startTimeInSec; simTimeInSec = startTimeInSec;
for (ActiveCallback ac : activeCallbacks) { for (Model m : models) {
ac.preLoop(simTimeInSec); m.preLoop(simTimeInSec);
} }
for (PassiveCallback c : passiveCallbacks) { for (PassiveCallback c : passiveCallbacks) {
...@@ -126,8 +127,8 @@ public class Simulation { ...@@ -126,8 +127,8 @@ public class Simulation {
private void postLoop() { private void postLoop() {
simulationState = new SimulationState(name, topography, scenarioStore, simTimeInSec, step, mainModel); simulationState = new SimulationState(name, topography, scenarioStore, simTimeInSec, step, mainModel);
for (ActiveCallback ac : activeCallbacks) { for (Model m : models) {
ac.postLoop(simTimeInSec); m.postLoop(simTimeInSec);
} }
for (PassiveCallback c : passiveCallbacks) { for (PassiveCallback c : passiveCallbacks) {
...@@ -168,7 +169,7 @@ public class Simulation { ...@@ -168,7 +169,7 @@ public class Simulation {
c.preUpdate(simTimeInSec); c.preUpdate(simTimeInSec);
} }
updateActiveCallbacks(simTimeInSec); updateCallbacks(simTimeInSec);
updateWriters(simTimeInSec); updateWriters(simTimeInSec);
processorManager.update(this.simulationState); processorManager.update(this.simulationState);
...@@ -217,7 +218,7 @@ public class Simulation { ...@@ -217,7 +218,7 @@ public class Simulation {
this.simulationState = simulationState; this.simulationState = simulationState;
} }
private void updateActiveCallbacks(double simTimeInSec) { private void updateCallbacks(double simTimeInSec) {
this.targetControllers.clear(); this.targetControllers.clear();
for (Target target : this.topographyController.getTopography().getTargets()) { for (Target target : this.topographyController.getTopography().getTargets()) {
...@@ -235,8 +236,8 @@ public class Simulation { ...@@ -235,8 +236,8 @@ public class Simulation {
topographyController.update(simTimeInSec); topographyController.update(simTimeInSec);
step++; step++;
for (ActiveCallback ac : activeCallbacks) { for (Model m : models) {
ac.update(simTimeInSec); m.update(simTimeInSec);
} }
if (topographyController.getTopography().hasTeleporter()) { if (topographyController.getTopography().hasTeleporter()) {
......
...@@ -2,15 +2,12 @@ package org.vadere.simulator.models; ...@@ -2,15 +2,12 @@ package org.vadere.simulator.models;
import java.util.List; import java.util.List;
import org.vadere.simulator.control.ActiveCallback;
/** /**
* A main model of a simulation which can include submodels. * A main model of a simulation which can include submodels.
* *
*/ */
public interface MainModel public interface MainModel extends Model, DynamicElementFactory {
extends Model, ActiveCallback, DynamicElementFactory {
List<ActiveCallback> getActiveCallbacks(); List<Model> getSubmodels();
} }
...@@ -10,8 +10,10 @@ import org.vadere.state.scenario.Topography; ...@@ -10,8 +10,10 @@ import org.vadere.state.scenario.Topography;
import org.vadere.util.data.FindByClass; import org.vadere.util.data.FindByClass;
/** /**
* Interface for a simulation model. The <code>initialize</code> method must be called before usage! * Interface for a simulation model.
* * The <code>initialize</code> method must be called before usage!
* This interface defines a callbacks for the simulation loop.
* It's implementations define the major part of the simulation model's logic.
* *
*/ */
public interface Model { public interface Model {
...@@ -23,6 +25,12 @@ public interface Model { ...@@ -23,6 +25,12 @@ public interface Model {
void initialize(List<Attributes> attributesList, Topography topography, void initialize(List<Attributes> attributesList, Topography topography,
AttributesAgent attributesPedestrian, Random random); AttributesAgent attributesPedestrian, Random random);
void preLoop(final double simTimeInSec);
void postLoop(final double simTimeInSec);
void update(final double simTimeInSec);
public static <T extends Attributes> T findAttributes(List<Attributes> attributesList, final Class<T> type) { public static <T extends Attributes> T findAttributes(List<Attributes> attributesList, final Class<T> type) {
try { try {
final T a = FindByClass.findSingleObjectOfClass(attributesList, type); final T a = FindByClass.findSingleObjectOfClass(attributesList, type);
......
...@@ -4,7 +4,6 @@ import java.util.LinkedList; ...@@ -4,7 +4,6 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import org.vadere.simulator.control.ActiveCallback;
import org.vadere.state.attributes.Attributes; import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.scenario.AttributesAgent; import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.Topography; import org.vadere.state.scenario.Topography;
...@@ -12,7 +11,7 @@ import org.vadere.util.reflection.DynamicClassInstantiator; ...@@ -12,7 +11,7 @@ import org.vadere.util.reflection.DynamicClassInstantiator;
/** /**
* Helper class to build submodels of a main model and add them to a * Helper class to build submodels of a main model and add them to a
* ActiveCallback list. * list of models.
*/ */
public class SubModelBuilder { public class SubModelBuilder {
...@@ -40,18 +39,11 @@ public class SubModelBuilder { ...@@ -40,18 +39,11 @@ public class SubModelBuilder {
} }
} }
/** /** Add the builded submodels to the client's list of models. */
* Add submodels to a ActiveCallback list. public void addBuildedSubModelsToList(List<Model> modelList) {
* // Maybe in future getSubModels() instead? Currently, this is
* Maybe in future <code>getSubModels()</code> instead? Currently, this is // the best way to avoid the redundancy of adding them to the list.
* the best way to avoid redundancy of adding them to the list. modelList.addAll(subModels);
*/
public void addSubModelsToActiveCallbacks(List<ActiveCallback> activeCallbacks) {
for (Model model : subModels) {
if (model instanceof ActiveCallback) {
activeCallbacks.add((ActiveCallback) model);
}
}
} }
} }
package org.vadere.simulator.models.gnm; package org.vadere.simulator.models.gnm;
import org.vadere.simulator.control.ActiveCallback;
import org.vadere.simulator.models.Model; import org.vadere.simulator.models.Model;
import org.vadere.simulator.models.ode.IntegratorFactory; import org.vadere.simulator.models.ode.IntegratorFactory;
import org.vadere.simulator.models.ode.ODEModel; import org.vadere.simulator.models.ode.ODEModel;
...@@ -38,7 +37,7 @@ public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgen ...@@ -38,7 +37,7 @@ public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgen
private PotentialFieldObstacle potentialFieldObstacle; private PotentialFieldObstacle potentialFieldObstacle;
private PotentialFieldAgent potentialFieldPedestrian; private PotentialFieldAgent potentialFieldPedestrian;
private int pedestrianIdCounter; private int pedestrianIdCounter;
private List<ActiveCallback> activeCallbacks = new LinkedList<>(); private List<Model> models = new LinkedList<>();
@Deprecated @Deprecated
public GradientNavigationModel(final Topography scenario, public GradientNavigationModel(final Topography scenario,
...@@ -83,7 +82,7 @@ public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgen ...@@ -83,7 +82,7 @@ public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgen
modelAttributesList, topography, attributesPedestrian, attributes.getTargetPotentialModel()); modelAttributesList, topography, attributesPedestrian, attributes.getTargetPotentialModel());
this.potentialFieldTarget = iPotentialTargetGrid; this.potentialFieldTarget = iPotentialTargetGrid;
activeCallbacks.add(iPotentialTargetGrid); models.add(iPotentialTargetGrid);
this.potentialFieldObstacle = PotentialFieldObstacle.createPotentialField( this.potentialFieldObstacle = PotentialFieldObstacle.createPotentialField(
modelAttributesList, topography, random, attributes.getObstaclePotentialModel()); modelAttributesList, topography, random, attributes.getObstaclePotentialModel());
...@@ -91,7 +90,7 @@ public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgen ...@@ -91,7 +90,7 @@ public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgen
this.potentialFieldPedestrian = PotentialFieldAgent.createPotentialField( this.potentialFieldPedestrian = PotentialFieldAgent.createPotentialField(
modelAttributesList, topography, attributes.getPedestrianPotentialModel()); modelAttributesList, topography, attributes.getPedestrianPotentialModel());
activeCallbacks.add(this); models.add(this);
} }
public void rebuildFloorField(final double simTimeInSec) { public void rebuildFloorField(final double simTimeInSec) {
...@@ -165,8 +164,8 @@ public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgen ...@@ -165,8 +164,8 @@ public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgen
} }
@Override @Override
public List<ActiveCallback> getActiveCallbacks() { public List<Model> getSubmodels() {
return activeCallbacks; return models;
} }
@Override @Override
......
...@@ -23,8 +23,7 @@ import org.vadere.util.potential.gradients.GradientProvider; ...@@ -23,8 +23,7 @@ import org.vadere.util.potential.gradients.GradientProvider;
* calculating the distance to that point. * calculating the distance to that point.
* *
*/ */
public class PotentialFieldObstacleGNM implements GradientProvider, public class PotentialFieldObstacleGNM implements GradientProvider, PotentialFieldObstacle {
PotentialFieldObstacle {
private Collection<Obstacle> obstacles; private Collection<Obstacle> obstacles;
...@@ -125,4 +124,5 @@ public class PotentialFieldObstacleGNM implements GradientProvider, ...@@ -125,4 +124,5 @@ public class PotentialFieldObstacleGNM implements GradientProvider,
AttributesAgent attributesPedestrian, Random random) { AttributesAgent attributesPedestrian, Random random) {
// TODO should be used to initialize the Model // TODO should be used to initialize the Model
} }
} }
...@@ -112,4 +112,5 @@ public class PotentialFieldPedestrianGNM implements PotentialFieldAgent { ...@@ -112,4 +112,5 @@ public class PotentialFieldPedestrianGNM implements PotentialFieldAgent {
AttributesAgent attributesPedestrian, Random random) { AttributesAgent attributesPedestrian, Random random) {
// TODO should be used to initialize the Model // TODO should be used to initialize the Model
} }
} }
package org.vadere.simulator.models.groups; package org.vadere.simulator.models.groups;
import org.vadere.simulator.control.ActiveCallback; import java.util.List;
import java.util.Map;
import java.util.Random;
import org.vadere.simulator.models.Model; import org.vadere.simulator.models.Model;
import org.vadere.simulator.models.potential.fields.PotentialFieldTarget; import org.vadere.simulator.models.potential.fields.PotentialFieldTarget;
import org.vadere.state.attributes.Attributes; import org.vadere.state.attributes.Attributes;
...@@ -9,21 +12,8 @@ import org.vadere.state.attributes.scenario.AttributesAgent; ...@@ -9,21 +12,8 @@ import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.Pedestrian; import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.ScenarioElement; import org.vadere.state.scenario.ScenarioElement;
import org.vadere.state.scenario.Topography; import org.vadere.state.scenario.Topography;
import org.vadere.util.data.Table;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
public class CentroidGroupModel implements GroupModel, ActiveCallback {
/** public class CentroidGroupModel implements GroupModel {
* A Container for all the output this Callback generate. The output will be used
* by the processor.
*/
private Map<String, Table> outputTables;
private GroupSizeDeterminator groupSizeDeterminator; private GroupSizeDeterminator groupSizeDeterminator;
private Map<Integer, CentroidGroupFactory> groupFactories; private Map<Integer, CentroidGroupFactory> groupFactories;
......
...@@ -123,4 +123,5 @@ public class CentroidGroupPotential implements PotentialFieldAgent { ...@@ -123,4 +123,5 @@ public class CentroidGroupPotential implements PotentialFieldAgent {
AttributesAgent attributesPedestrian, Random random) { AttributesAgent attributesPedestrian, Random random) {
// TODO [priority=medium] [task=refactoring] should be used to initialize the Model // TODO [priority=medium] [task=refactoring] should be used to initialize the Model
} }
} }
package org.vadere.simulator.models.osm; package org.vadere.simulator.models.osm;
import org.vadere.simulator.control.ActiveCallback;
import org.vadere.simulator.models.MainModel; import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.Model; import org.vadere.simulator.models.Model;
import org.vadere.simulator.models.SpeedAdjuster; import org.vadere.simulator.models.SpeedAdjuster;
...@@ -77,7 +76,7 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel { ...@@ -77,7 +76,7 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
private PriorityQueue<PedestrianOSM> pedestrianEventsQueue; private PriorityQueue<PedestrianOSM> pedestrianEventsQueue;
private ExecutorService executorService; private ExecutorService executorService;
private List<ActiveCallback> activeCallbacks = new LinkedList<>(); private List<Model> models = new LinkedList<>();
@Deprecated @Deprecated
public OptimalStepsModel(final Topography topography, final AttributesOSM attributes, public OptimalStepsModel(final Topography topography, final AttributesOSM attributes,
...@@ -130,13 +129,13 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel { ...@@ -130,13 +129,13 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
final SubModelBuilder subModelBuilder = new SubModelBuilder(modelAttributesList, topography, final SubModelBuilder subModelBuilder = new SubModelBuilder(modelAttributesList, topography,
attributesPedestrian, random); attributesPedestrian, random);
subModelBuilder.buildSubModels(attributesOSM.getSubmodels()); subModelBuilder.buildSubModels(attributesOSM.getSubmodels());
subModelBuilder.addSubModelsToActiveCallbacks(activeCallbacks); subModelBuilder.addBuildedSubModelsToList(models);
IPotentialTargetGrid iPotentialTargetGrid = IPotentialTargetGrid.createPotentialField( IPotentialTargetGrid iPotentialTargetGrid = IPotentialTargetGrid.createPotentialField(
modelAttributesList, topography, attributesPedestrian, attributesOSM.getTargetPotentialModel()); modelAttributesList, topography, attributesPedestrian, attributesOSM.getTargetPotentialModel());
this.potentialFieldTarget = iPotentialTargetGrid; this.potentialFieldTarget = iPotentialTargetGrid;
activeCallbacks.add(iPotentialTargetGrid); models.add(iPotentialTargetGrid);
this.potentialFieldObstacle = PotentialFieldObstacle.createPotentialField( this.potentialFieldObstacle = PotentialFieldObstacle.createPotentialField(
modelAttributesList, topography, random, attributesOSM.getObstaclePotentialModel()); modelAttributesList, topography, random, attributesOSM.getObstaclePotentialModel());
...@@ -144,7 +143,7 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel { ...@@ -144,7 +143,7 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
this.potentialFieldPedestrian = PotentialFieldAgent.createPotentialField( this.potentialFieldPedestrian = PotentialFieldAgent.createPotentialField(
modelAttributesList, topography, attributesOSM.getPedestrianPotentialModel()); modelAttributesList, topography, attributesOSM.getPedestrianPotentialModel());
Optional<CentroidGroupModel> opCentroidGroupModel = activeCallbacks.stream(). Optional<CentroidGroupModel> opCentroidGroupModel = models.stream().
filter(ac -> ac instanceof CentroidGroupModel).map(ac -> (CentroidGroupModel)ac).findAny(); filter(ac -> ac instanceof CentroidGroupModel).map(ac -> (CentroidGroupModel)ac).findAny();
if (opCentroidGroupModel.isPresent()) { if (opCentroidGroupModel.isPresent()) {
...@@ -181,7 +180,7 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel { ...@@ -181,7 +180,7 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
this.executorService = null; this.executorService = null;
} }
activeCallbacks.add(this); models.add(this);
} }
private StepCircleOptimizer createStepCircleOptimizer( private StepCircleOptimizer createStepCircleOptimizer(
...@@ -327,8 +326,8 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel { ...@@ -327,8 +326,8 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
} }
@Override @Override
public List<ActiveCallback> getActiveCallbacks() { public List<Model> getSubmodels() {
return activeCallbacks; return models;
} }
@Override @Override
......
...@@ -9,7 +9,6 @@ import java.util.Collections; ...@@ -9,7 +9,6 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import org.vadere.simulator.control.ActiveCallback;
import org.vadere.simulator.models.Model; import org.vadere.simulator.models.Model;
import org.vadere.simulator.models.ode.IntegratorFactory; import org.vadere.simulator.models.ode.IntegratorFactory;
import org.vadere.simulator.models.ode.ODEModel; import org.vadere.simulator.models.ode.ODEModel;
...@@ -29,16 +28,11 @@ public class OptimalVelocityModel extends ODEModel<Car, AttributesCar> { ...@@ -29,16 +28,11 @@ public class OptimalVelocityModel extends ODEModel<Car, AttributesCar> {
private int carIdCounter = 10000000; // TODO [priority=low] [task=refactoring] hack, think about another way of separating car IDs and pedestrian IDs. private int carIdCounter = 10000000; // TODO [priority=low] [task=refactoring] hack, think about another way of separating car IDs and pedestrian IDs.
private AttributesOVM attributesOVM; private AttributesOVM attributesOVM;
private OVMEquations ovmEquations; private OVMEquations ovmEquations;
private List<ActiveCallback> activeCallbacks; private List<Model> models;
/** /**
* Constructor for OptimalVelocityModel used in the ModelCreator * Constructor for OptimalVelocityModel used in the ModelCreator
* *
* @param scenario
* @param ovmEquations
* @param attributesOVM
* @param elementAttributes
* @param random
*/ */
@Deprecated @Deprecated
public OptimalVelocityModel(Topography scenario, public OptimalVelocityModel(Topography scenario,
...@@ -83,7 +77,7 @@ public class OptimalVelocityModel extends ODEModel<Car, AttributesCar> { ...@@ -83,7 +77,7 @@ public class OptimalVelocityModel extends ODEModel<Car, AttributesCar> {
ovmEquations.setModelAttributes(attributesOVM); ovmEquations.setModelAttributes(attributesOVM);
ovmEquations.setGradients(null, null, null, topography); ovmEquations.setGradients(null, null, null, topography);
activeCallbacks = Collections.singletonList(this); models = Collections.singletonList(this);
} }
...@@ -137,8 +131,8 @@ public class OptimalVelocityModel extends ODEModel<Car, AttributesCar> { ...@@ -137,8 +131,8 @@ public class OptimalVelocityModel extends ODEModel<Car, AttributesCar> {
} }
@Override @Override
public List<ActiveCallback> getActiveCallbacks() { public List<Model> getSubmodels() {
return activeCallbacks; return models;
} }
} }
...@@ -30,6 +30,18 @@ public interface PotentialFieldAgent extends Model { ...@@ -30,6 +30,18 @@ public interface PotentialFieldAgent extends Model {
Logger logger = LogManager.getLogger(PotentialFieldAgent.class); Logger logger = LogManager.getLogger(PotentialFieldAgent.class);
@Override
default void preLoop(double simTimeInSec) {
}
@Override
default void postLoop(double simTimeInSec) {
}
@Override
default void update(double simTimeInSec) {
}
/** /**
* Computes the pedestrians possessing a potential that reaches into the * Computes the pedestrians possessing a potential that reaches into the
* given relevantArea.