The name of the initial branch for new projects is now "main" instead of "master". Existing projects remain unchanged. More information: https://doku.lrz.de/display/PUBLIC/GitLab

Commit ad66e039 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

Add TargetFloorFieldGridProcessorTest, Remove unused processor

parent 3aca3e0d
......@@ -20,7 +20,7 @@ import java.util.List;
import java.util.Optional;
public class TargetFloorFieldGridProcessor extends DataProcessor<TimestepRowKey, FloorFieldGridRow> {
private static Logger logger = LogManager.getLogger(TargetFloorFieldProcessor.class);
private static Logger logger = LogManager.getLogger(TargetFloorFieldGridProcessor.class);
private AttributesFloorFieldProcessor att;
private List<Integer> targetIds;
private boolean hasOnceProcessed = false;
......@@ -34,24 +34,24 @@ public class TargetFloorFieldGridProcessor extends DataProcessor<TimestepRowKey,
protected void doUpdate(final SimulationState state) {
Optional<MainModel> optMainModel = state.getMainModel();
if(optMainModel.isPresent() && optMainModel.get() instanceof PotentialFieldModel) {
if (optMainModel.isPresent() && optMainModel.get() instanceof PotentialFieldModel) {
IPotentialFieldTarget pft = ((PotentialFieldModel) optMainModel.get()).getPotentialFieldTarget();
Rectangle.Double bound = state.getTopography().getBounds();
/**
/*
* If the floor field is static we do not have to process it twice.
*/
if(!hasOnceProcessed || pft.needsUpdate()) {
/**
if (!hasOnceProcessed || pft.needsUpdate()) {
/*
* We assume that all pedestrian navigate to a specific target using the same floor field. This is not always true.
* For example in the cooperative and competitive queueing model, pedestrians use different floor fields.
*/
Optional<Pedestrian> optPed = state.getTopography().getPedestrianDynamicElements().getElements().stream().findAny();
if(optPed.isPresent()) {
if (optPed.isPresent()) {
int row = 0;
for (double y = bound.y; y < bound.y + bound.height; y += att.getResolution()) {
FloorFieldGridRow floorFieldGridRow = new FloorFieldGridRow((int)Math.floor(bound.width / att.getResolution()));
FloorFieldGridRow floorFieldGridRow = new FloorFieldGridRow((int) Math.floor(bound.width / att.getResolution()));
int col = 0;
for (double x = bound.x; x < bound.x + bound.width; x += att.getResolution()) {
floorFieldGridRow.setValue(col++, pft.getPotential(new VPoint(x, y), optPed.get()));
......@@ -62,8 +62,7 @@ public class TargetFloorFieldGridProcessor extends DataProcessor<TimestepRowKey,
}
}
}
else {
} else {
logger.warn("could not process, main model is missing or is not the instance of " + PotentialFieldModel.class.getName());
}
}
......@@ -77,17 +76,17 @@ public class TargetFloorFieldGridProcessor extends DataProcessor<TimestepRowKey,
this.hasOnceProcessed = false;
}
@Override
public String[] toStrings(TimestepRowKey key) {
return this.getValue(key).toStrings();
}
@Override
public String[] toStrings(TimestepRowKey key) {
return this.getValue(key).toStrings();
}
@Override
public AttributesProcessor getAttributes() {
if(super.getAttributes() == null) {
setAttributes(new AttributesFloorFieldProcessor());
}
@Override
public AttributesProcessor getAttributes() {
if (super.getAttributes() == null) {
setAttributes(new AttributesFloorFieldProcessor());
}
return super.getAttributes();
}
return super.getAttributes();
}
}
package org.vadere.simulator.projects.dataprocessing.processor;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPositionKey;
import org.vadere.state.attributes.processor.AttributesFloorFieldProcessor;
import org.vadere.state.attributes.processor.AttributesProcessor;
/**
* @author Mario Teixeira Parente
*/
public class TargetFloorFieldProcessor extends DataProcessor<TimestepPositionKey, Double> {
private static Logger logger = LogManager.getLogger(TargetFloorFieldProcessor.class);
private AttributesFloorFieldProcessor att;
private int targetId;
public TargetFloorFieldProcessor() {
super("potential");
setAttributes(new AttributesFloorFieldProcessor());
}
@Override
protected void doUpdate(final SimulationState state) {
throw new UnsupportedOperationException("not jet implemented.");
/*Optional<MainModel> optMainModel = state.getMainModel();
if(optMainModel.isPresent() && optMainModel.get() instanceof PotentialFieldModel) {
PotentialFieldTarget pft = ((PotentialFieldModel) optMainModel.get()).getPotentialFieldTarget();
Rectangle.Double bound = state.getTopography().getBounds();
// First try, TODO: Implementation
for (double x = bound.x; x < bound.x + bound.width; x += att.getResolution()) {
for (double y = bound.y; y < bound.y + bound.height; y += att.getResolution()) {
this.setValue(new TimestepPositionKey(state.getStep(), new VPoint(x, y)), 0.0);
}
}
}
else {
logger.warn("could not process, main model is missing or is not the instance of " + PotentialFieldModel.class.getName());
}*/
}
@Override
public void init(final ProcessorManager manager) {
super.init(manager);
this.att = (AttributesFloorFieldProcessor) this.getAttributes();
this.targetId = att.getTargetId();
}
@Override
public AttributesProcessor getAttributes() {
if(super.getAttributes() == null) {
setAttributes(new AttributesFloorFieldProcessor());
}
return super.getAttributes();
}
}
package org.vadere.simulator.projects.dataprocessing.processor;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.sfm.SocialForceModel;
import java.util.Optional;
public class TargetFloorFieldGridProcessorTest extends ProcessorTest {
@Before
public void setup() {
processorTestEnv = new TargetFloorFieldGridProcessorTestEnv();
super.setup();
}
/**
* If Model is not present do nothing
*/
@Test
public void doUpdateNoMainModel() throws Exception {
processorTestEnv.clearStates();
processorTestEnv.addSimState(new SimulationStateMock(1) {
@Override
public void mockIt() {
Mockito.when(state.getMainModel()).thenReturn(Optional.empty());
processorTestEnv.expectedOutput.clear();
}
});
super.doUpdate();
}
@Test
public void doUpdateWrongMainModel() throws Exception {
processorTestEnv.clearStates();
processorTestEnv.addSimState(new SimulationStateMock(1) {
@Override
public void mockIt() {
MainModel wrongModel = Mockito.mock(SocialForceModel.class);
Mockito.when(state.getMainModel()).thenReturn(Optional.of(wrongModel));
processorTestEnv.expectedOutput.clear();
}
});
super.doUpdate();
}
@Test
public void doUpdate() throws Exception {
super.doUpdate();
}
}
\ No newline at end of file
package org.vadere.simulator.projects.dataprocessing.processor;
import org.mockito.Mockito;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.osm.OptimalStepsModel;
import org.vadere.simulator.models.potential.PotentialFieldModel;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTarget;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepRowKey;
import org.vadere.simulator.projects.dataprocessing.writer.VadereWriterFactory;
import org.vadere.state.attributes.processor.AttributesFloorFieldProcessor;
import org.vadere.state.scenario.Agent;
import org.vadere.util.data.FloorFieldGridRow;
import org.vadere.util.geometry.shapes.VPoint;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.StringJoiner;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class TargetFloorFieldGridProcessorTestEnv extends ProcessorTestEnv<TimestepRowKey, FloorFieldGridRow> {
PedestrianListBuilder b = new PedestrianListBuilder();
TargetFloorFieldGridProcessorTestEnv() {
testedProcessor = processorFactory.createDataProcessor(TargetFloorFieldGridProcessor.class);
testedProcessor.setId(nextProcessorId());
AttributesFloorFieldProcessor attr =
(AttributesFloorFieldProcessor) testedProcessor.getAttributes();
attr.setResolution(1.0);
attr.setTargetId(1);
outputFile = outputFileFactory.createDefaultOutputfileByDataKey(
TimestepRowKey.class,
testedProcessor.getId()
);
outputFile.setVadereWriterFactory(VadereWriterFactory.getStringWriterFactory());
}
@Override
public void loadDefaultSimulationStateMocks() {
MainModel model = mock(OptimalStepsModel.class, Mockito.RETURNS_DEEP_STUBS);
IPotentialFieldTarget pft = mock(IPotentialFieldTarget.class);
when(pft.getPotential(any(VPoint.class), any(Agent.class)))
.thenReturn(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
addSimState(new SimulationStateMock(1) {
@Override
public void mockIt() {
when(state.getMainModel()).thenReturn(Optional.of(model));
when(((PotentialFieldModel) model).getPotentialFieldTarget()).thenReturn(pft);
when(state.getTopography().getBounds()).thenReturn(new Rectangle2D.Double(0.0, 0.0, 3.0, 2.0));
when(pft.needsUpdate()).thenReturn(false);
b.clear().add(1);
when(state.getTopography().getPedestrianDynamicElements().getElements()).thenReturn(b.getList());
int step = state.getStep();
addToExpectedOutput(new TimestepRowKey(step, 0), createGrid(1.0, 2.0, 3.0));
addToExpectedOutput(new TimestepRowKey(step, 1), createGrid(4.0, 5.0, 6.0));
}
});
}
private FloorFieldGridRow createGrid(Double... val) {
FloorFieldGridRow out = new FloorFieldGridRow(val.length);
for (int i = 0; i < val.length; i++) {
out.setValue(i, val[i]);
}
return out;
}
@Override
List<String> getExpectedOutputAsList() {
List<String> outputList = new ArrayList<>();
expectedOutput.entrySet()
.stream()
.sorted(Comparator.comparing(Map.Entry::getKey))
.forEach(e -> {
StringJoiner sj = new StringJoiner(getDelimiter());
sj.add(Integer.toString(e.getKey().getTimeStep()))
.add(Integer.toString(e.getKey().getRow()));
String[] value = e.getValue().toStrings();
for (String aValue : value) {
sj.add(aValue);
}
outputList.add(sj.toString());
});
return outputList;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment