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 4aa98b41 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

Add MeanPedestrianEvacuationTimeProcessor

parent acf108eb
......@@ -11,49 +11,49 @@ import java.util.stream.Collectors;
/**
* @author Mario Teixeira Parente
*
*/
public class MeanPedestrianEvacuationTimeProcessor extends DataProcessor<NoDataKey, Double> {
private PedestrianEvacuationTimeProcessor pedEvacTimeProc;
public MeanPedestrianEvacuationTimeProcessor() {
super("meanEvacuationTime");
setAttributes(new AttributesMeanPedestrianEvacuationTimeProcessor());
}
@Override
protected void doUpdate(final SimulationState state) {
// No implementation needed, look at 'postLoop(SimulationState)'
}
@Override
public void init(final ProcessorManager manager) {
super.init(manager);
AttributesMeanPedestrianEvacuationTimeProcessor att = (AttributesMeanPedestrianEvacuationTimeProcessor) this.getAttributes();
this.pedEvacTimeProc = (PedestrianEvacuationTimeProcessor) manager.getProcessor(att.getPedestrianEvacuationTimeProcessorId());
}
@Override
public void postLoop(final SimulationState state) {
this.pedEvacTimeProc.postLoop(state);
List<Double> nonNans = this.pedEvacTimeProc.getValues().stream()
.filter(val -> val != Double.NaN)
.collect(Collectors.toList());
int count = nonNans.size();
this.putValue(NoDataKey.key(), count > 0
? nonNans.stream().reduce(0.0, (val1, val2) -> val1 + val2) / count
: Double.NaN);
}
@Override
public AttributesProcessor getAttributes() {
if(super.getAttributes() == null) {
setAttributes(new AttributesMeanPedestrianEvacuationTimeProcessor());
}
return super.getAttributes();
}
private PedestrianEvacuationTimeProcessor pedEvacTimeProc;
public MeanPedestrianEvacuationTimeProcessor() {
super("meanEvacuationTime");
setAttributes(new AttributesMeanPedestrianEvacuationTimeProcessor());
}
@Override
protected void doUpdate(final SimulationState state) {
//ensure that all required DataProcessors are updated.
this.pedEvacTimeProc.doUpdate(state);
}
@Override
public void init(final ProcessorManager manager) {
super.init(manager);
AttributesMeanPedestrianEvacuationTimeProcessor att = (AttributesMeanPedestrianEvacuationTimeProcessor) this.getAttributes();
this.pedEvacTimeProc = (PedestrianEvacuationTimeProcessor) manager.getProcessor(att.getPedestrianEvacuationTimeProcessorId());
}
@Override
public void postLoop(final SimulationState state) {
this.pedEvacTimeProc.postLoop(state);
List<Double> nonNans = this.pedEvacTimeProc.getValues().stream()
.filter(val -> !val.isNaN())
.collect(Collectors.toList());
int count = nonNans.size();
this.putValue(NoDataKey.key(), count > 0
? nonNans.parallelStream().reduce(0.0, (val1, val2) -> val1 + val2) / count
: Double.NaN);
}
@Override
public AttributesProcessor getAttributes() {
if (super.getAttributes() == null) {
setAttributes(new AttributesMeanPedestrianEvacuationTimeProcessor());
}
return super.getAttributes();
}
}
......@@ -18,10 +18,15 @@ public class EvacuationTimeProcessorTestEnv extends ProcessorTestEnv<NoDataKey,
private PedestrianListBuilder b = new PedestrianListBuilder();
@SuppressWarnings("unchecked")
EvacuationTimeProcessorTestEnv() {
this(1);
}
@SuppressWarnings("unchecked")
private EvacuationTimeProcessorTestEnv(int nextProcessorId) {
testedProcessor = processorFactory.createDataProcessor(EvacuationTimeProcessor.class);
testedProcessor.setId(nextProcessorId());
testedProcessor.setId(nextProcessorId);
this.nextProcessorId = nextProcessorId + 1;
DataProcessor pedEvacTimeProc;
PedestrianEvacuationTimeProcessorTestEnv pedEvacTimeProcEnv;
......
package org.vadere.simulator.projects.dataprocessing.processor;
import org.junit.Before;
import org.junit.Test;
public class MeanPedestrianEvacuationTimeProcessorTest extends ProcessorTest {
@Before
public void setup() {
processorTestEnv = new MeanPedestrianEvacuationTimeProcessorTestEnv();
super.setup();
}
@Test
public void doUpdate() throws Exception {
super.doUpdate();
}
@Test
public void init() throws Exception {
super.init();
}
}
\ No newline at end of file
package org.vadere.simulator.projects.dataprocessing.processor;
import org.mockito.Mockito;
import org.vadere.simulator.projects.dataprocessing.datakey.NoDataKey;
import org.vadere.simulator.projects.dataprocessing.writer.VadereWriterFactory;
import org.vadere.state.attributes.processor.AttributesMeanPedestrianEvacuationTimeProcessor;
import org.vadere.state.scenario.Pedestrian;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
public class MeanPedestrianEvacuationTimeProcessorTestEnv extends ProcessorTestEnv<NoDataKey, Double> {
private PedestrianListBuilder b = new PedestrianListBuilder();
@SuppressWarnings("unchecked")
MeanPedestrianEvacuationTimeProcessorTestEnv() {
testedProcessor = processorFactory.createDataProcessor(MeanPedestrianEvacuationTimeProcessor.class);
testedProcessor.setId(nextProcessorId());
DataProcessor pedEvacTimeProc;
PedestrianEvacuationTimeProcessorTestEnv pedEvacTimeProcEnv;
int pedEvacTimeProcId = nextProcessorId();
//add ProcessorId of required Processors to current Processor under test
AttributesMeanPedestrianEvacuationTimeProcessor attr = (AttributesMeanPedestrianEvacuationTimeProcessor) testedProcessor.getAttributes();
attr.setPedestrianEvacuationTimeProcessorId(pedEvacTimeProcId);
//create required Processor enviroment and add it to current Processor under test
pedEvacTimeProcEnv = new PedestrianEvacuationTimeProcessorTestEnv(pedEvacTimeProcId);
pedEvacTimeProc = pedEvacTimeProcEnv.getTestedProcessor();
Mockito.when(manager.getProcessor(pedEvacTimeProcId)).thenReturn(pedEvacTimeProc);
addRequiredProcessors(pedEvacTimeProcEnv);
//setup output file with different VadereWriter impl for test
outputFile = outputFileFactory.createDefaultOutputfileByDataKey(
NoDataKey.class,
testedProcessor.getId()
);
outputFile.setVadereWriterFactory(VadereWriterFactory.getStringWriterFactory());
}
@Override
public void loadDefaultSimulationStateMocks() {
addSimState(new SimulationStateMock(1) {
@Override
public void mockIt() {
b.clear().add(1, 2, 3);
Mockito.when(state.getTopography().getElements(Pedestrian.class)).thenReturn(b.getList());
Mockito.when(state.getSimTimeInSec()).thenReturn(0.0);
}
});
addSimState(new SimulationStateMock(2) {
@Override
public void mockIt() {
b.clear().add(1, 2, 3, 4, 5);
Mockito.when(state.getTopography().getElements(Pedestrian.class)).thenReturn(b.getList());
Mockito.when(state.getSimTimeInSec()).thenReturn(5.0);
}
});
addSimState(new SimulationStateMock(3) {
@Override
public void mockIt() {
b.clear().add(1, 2, 3, 4, 5, 6);
Mockito.when(state.getTopography().getElements(Pedestrian.class)).thenReturn(b.getList());
Mockito.when(state.getSimTimeInSec()).thenReturn(10.0);
}
});
addSimState(new SimulationStateMock(4) {
@Override
public void mockIt() {
b.clear().add(6);
Mockito.when(state.getTopography().getElements(Pedestrian.class)).thenReturn(b.getList());
Mockito.when(state.getSimTimeInSec()).thenReturn(15.0);
double meanEvacTime = (3 * (10.0 - 0.0) + 2 * (10.0 - 5.0)) / 5;
addToExpectedOutput(NoDataKey.key(), meanEvacTime);
}
});
}
@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(Double.toString(e.getValue()));
outputList.add(sj.toString());
});
return outputList;
}
}
......@@ -2,13 +2,17 @@ package org.vadere.state.attributes.processor;
/**
* @author Mario Teixeira Parente
*
*/
public class AttributesMeanPedestrianEvacuationTimeProcessor extends AttributesProcessor {
private int pedestrianEvacuationTimeProcessorId;
private int pedestrianEvacuationTimeProcessorId;
public int getPedestrianEvacuationTimeProcessorId() {
return this.pedestrianEvacuationTimeProcessorId;
}
public int getPedestrianEvacuationTimeProcessorId() {
return this.pedestrianEvacuationTimeProcessorId;
}
public void setPedestrianEvacuationTimeProcessorId(int id) {
checkSealed();
this.pedestrianEvacuationTimeProcessorId = id;
}
}
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