Commit b850ca79 authored by Christina's avatar Christina
Browse files

add unit tests to StimulusController

parent a106c3ee
Pipeline #745830 passed with stages
in 131 minutes and 13 seconds
......@@ -26,8 +26,10 @@ public class StimulusController {
private ScenarioStore scenarioStore;
private List<StimulusInfo> oneTimeStimuli;
private List<StimulusInfo> recurringStimuli;
private HashMap<Integer, Double> reactionProbabilities;
private HashMap<Integer, Double> reactionProbabilities;
private HashMap<Pedestrian, List<StimulusInfo>> pedSpecificStimuli;
......@@ -42,17 +44,12 @@ public class StimulusController {
oneTimeStimuli.stream().forEach(stimulusInfo -> throwExceptionIfTimeframeIsInvalid(stimulusInfo.getTimeframe(), false));
recurringStimuli.stream().forEach(stimulusInfo -> throwExceptionIfTimeframeIsInvalid(stimulusInfo.getTimeframe(), true));
reactionProbabilities = getReactionProbabilites(scenarioStore.getStimulusInfoStore().getReactionProbabilities());
setReactionProbabilites(scenarioStore.getStimulusInfoStore().getReactionProbabilities());
pedSpecificStimuli = new HashMap<>();
}
private HashMap<Integer, Double> getReactionProbabilites(List<ReactionProbability> reactionProbabilities) {
reactionProbabilities.forEach(reactionProbability -> throwExceptionIfReactionProbabilityIsInvalid(reactionProbability));
return (HashMap<Integer, Double>) reactionProbabilities
.stream()
.collect(Collectors.toMap(ReactionProbability::getStimulusId,ReactionProbability::getReactionProbability));
}
// Getters
public ScenarioStore getScenarioStore() {
......@@ -60,6 +57,8 @@ public class StimulusController {
}
public List<StimulusInfo> getOneTimeStimuli() { return oneTimeStimuli; }
public List<StimulusInfo> getRecurringStimuli() { return recurringStimuli; }
public HashMap<Integer, Double> getReactionProbabilities() { return reactionProbabilities; }
// Setters
public void setScenarioStore(ScenarioStore scenarioStore) {
......@@ -67,6 +66,10 @@ public class StimulusController {
}
public void setOneTimeStimuli(List<StimulusInfo> oneTimeStimuli) { this.oneTimeStimuli = oneTimeStimuli; }
public void setRecurringStimuli(List<StimulusInfo> recurringStimuli) { this.recurringStimuli = recurringStimuli; }
protected void setReactionProbabilites(List<ReactionProbability> reactionProbabilities) {
reactionProbabilities.forEach(reactionProbability -> throwExceptionIfReactionProbabilityIsInvalid(reactionProbability));
this.reactionProbabilities = (HashMap<Integer, Double>) reactionProbabilities.stream().collect(Collectors.toMap(ReactionProbability::getStimulusId,ReactionProbability::getReactionProbability));
}
// Methods
public List<Stimulus> getStimuliForTime(double simulationTime) {
......
......@@ -443,7 +443,7 @@ public class StimulusControllerTest {
List<Stimulus> stimuli1, pedSpecificStimuli;
StimulusInfo stimulusInfo2 = getStimulusInfo(new Timeframe(0, 4.0, false,0), changeTargetOriginal);
StimulusInfo stimulusInfo2 = getStimulusInfo(new Timeframe(0, 5.0, false,0), changeTargetOriginal);
stimulusController.setPedSpecificDynamicStimulus(ped, stimulusInfo2);
stimuli1 = stimulusController.getStimuliForTime(4.0);
......@@ -452,8 +452,80 @@ public class StimulusControllerTest {
pedSpecificStimuli = stimulusController.getStimuliForTime(4.0, ped);
assertEquals(3, pedSpecificStimuli.size());
pedSpecificStimuli = stimulusController.getStimuliForTime(5.0, ped);
assertEquals(3, pedSpecificStimuli.size());
pedSpecificStimuli = stimulusController.getStimuliForTime(5.2, ped);
assertEquals(2, pedSpecificStimuli.size());
}
@Test
public void checkProbabilityMappingGeneralStimuliOnly(){
int commandIdWait = -33;
double probWait = 0.11;
// define stimuli and store them in stimulusInfo object
Stimulus wait = new Wait();
wait.setId(commandIdWait);
Timeframe timeframe = new Timeframe(0, 1, false,0);
StimulusInfo waitInfo = new StimulusInfo(timeframe, Collections.singletonList(wait));
// define probababilites for stimuli
LinkedList<ReactionProbability> reactionProbabilities = new LinkedList<>();
reactionProbabilities.add(new ReactionProbability(commandIdWait, probWait));
// setup stimuluscontroller
StimulusController stimulusController = new StimulusController(getScenarioStore(getStimulusInfoStore(Collections.singletonList(waitInfo))));
stimulusController.setReactionProbabilites(reactionProbabilities);
List<Stimulus> stimuli = stimulusController.getStimuliForTime(0.4);
double isProbWait = stimuli.stream().filter(stimulus -> stimulus instanceof Wait).findFirst().orElseThrow().getPerceptionProbability();
assert isProbWait == probWait;
}
@Test
public void checkProbabilityMappingMixedSetting(){
int commandIdWait = -33;
int commandIdChangeTarget = 55;
double probWait = 0.5;
double probChangeTarget = 0.75;
Pedestrian ped = new Pedestrian(new AttributesAgent(), new Random());
// define stimuli and store them in stimulusInfo object
Stimulus wait = new Wait();
wait.setId(commandIdWait);
Stimulus changeTargetOriginal = new ChangeTarget();
changeTargetOriginal.setId(commandIdChangeTarget);
Timeframe timeframe = new Timeframe(0, 1, false,0);
StimulusInfo waitInfo = new StimulusInfo(timeframe, Collections.singletonList(wait));
// define probababilites for stimuli
LinkedList<ReactionProbability> reactionProbabilities = new LinkedList<>();
reactionProbabilities.add(new ReactionProbability(commandIdWait, probWait));
reactionProbabilities.add(new ReactionProbability(commandIdChangeTarget, probChangeTarget));
// setup stimuluscontroller
StimulusController stimulusController = new StimulusController(getScenarioStore(getStimulusInfoStore(Collections.singletonList(waitInfo))));
stimulusController.setReactionProbabilites(reactionProbabilities);
stimulusController.setPedSpecificDynamicStimulusEnduring(ped, changeTargetOriginal);
// check if probability is set in mixed setting (general + pedestrian specific stimuli)
List<Stimulus> stimuli = stimulusController.getStimuliForTime(0.0, ped);
double isProbWait = stimuli.stream().filter(stimulus -> stimulus instanceof Wait).findFirst().orElseThrow().getPerceptionProbability();
double isProbChangeTarget = stimuli.stream().filter(stimulus -> stimulus instanceof ChangeTarget).findFirst().orElseThrow().getPerceptionProbability();
assert isProbWait == probWait;
assert isProbChangeTarget == probChangeTarget;
}
@Test
public void getDynamicStimuli(){
......
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