Commit 61ccbd3f authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier
Browse files

Replaced "UnsupportedStimulusException" by "UnsupportedSelfCategoryException"

Implemented also unit test "UnsupportedSelfCategoryExceptionTest"
parent 3badcfaf
......@@ -8,7 +8,7 @@ import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.models.AttributesBHM;
import org.vadere.state.attributes.models.AttributesBMM;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.psychology.perception.exceptions.UnsupportedStimulusException;
import org.vadere.state.psychology.cognition.UnsupportedSelfCategoryException;
import org.vadere.state.scenario.DynamicElement;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Topography;
......@@ -93,7 +93,7 @@ public class BiomechanicsModel implements MainModel {
List<VPoint> positions = pedestriansBMM.stream().map(ped -> ped.getPosition()).collect(Collectors.toList());
UnsupportedStimulusException.throwIfNotElapsedTimeEvent(pedestriansBMM, this.getClass());
UnsupportedSelfCategoryException.throwIfPedestriansNotTargetOrientied(pedestriansBMM, this.getClass());
for (PedestrianBMM agent : pedestriansBMM) {
agent.update(simTimeInSec, deltaTime);
......
......@@ -7,14 +7,15 @@ import org.vadere.simulator.models.ode.IntegratorFactory;
import org.vadere.simulator.models.ode.ODEModel;
import org.vadere.simulator.models.potential.FloorGradientProviderFactory;
import org.vadere.simulator.models.potential.PotentialFieldModel;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTarget;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTargetGrid;
import org.vadere.simulator.models.potential.fields.PotentialFieldAgent;
import org.vadere.simulator.models.potential.fields.PotentialFieldObstacle;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTarget;
import org.vadere.simulator.models.potential.solver.gradients.GradientProvider;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.models.AttributesGNM;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.psychology.perception.exceptions.UnsupportedStimulusException;
import org.vadere.state.psychology.cognition.UnsupportedSelfCategoryException;
import org.vadere.state.scenario.DynamicElement;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Target;
......@@ -23,15 +24,8 @@ import org.vadere.state.types.GradientProviderType;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape;
import org.vadere.util.parallel.ParallelWorkerUtil;
import org.vadere.simulator.models.potential.solver.gradients.GradientProvider;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.*;
@ModelClass(isMainModel = true)
public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgent> implements PotentialFieldModel {
......@@ -145,7 +139,7 @@ public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgen
Collection<Pedestrian> pedestrians = topography.getElements(Pedestrian.class);
UnsupportedStimulusException.throwIfNotElapsedTimeEvent(pedestrians, this.getClass());
UnsupportedSelfCategoryException.throwIfPedestriansNotTargetOrientied(pedestrians, this.getClass());
// set gradient provider and pedestrians
equations.setElements(pedestrians);
......
......@@ -4,16 +4,11 @@ import org.jetbrains.annotations.NotNull;
import org.vadere.annotation.factories.models.ModelClass;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.Model;
import org.vadere.simulator.models.reynolds.behaviour.CollisionAvoidance;
import org.vadere.simulator.models.reynolds.behaviour.Containment;
import org.vadere.simulator.models.reynolds.behaviour.Seek;
import org.vadere.simulator.models.reynolds.behaviour.Separation;
import org.vadere.simulator.models.reynolds.behaviour.WallAvoidance;
import org.vadere.simulator.models.reynolds.behaviour.Wander;
import org.vadere.simulator.models.reynolds.behaviour.*;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.models.AttributesReynolds;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.psychology.perception.exceptions.UnsupportedStimulusException;
import org.vadere.state.psychology.cognition.UnsupportedSelfCategoryException;
import org.vadere.state.scenario.DynamicElement;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Topography;
......@@ -21,11 +16,7 @@ import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape;
import org.vadere.util.geometry.shapes.Vector2D;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.*;
@ModelClass(isMainModel = true)
public class ReynoldsSteeringModel implements MainModel {
......@@ -75,7 +66,7 @@ public class ReynoldsSteeringModel implements MainModel {
public void update(final double simTimeInSec) {
Collection<Pedestrian> pedestrians = topography.getElements(Pedestrian.class);
UnsupportedStimulusException.throwIfNotElapsedTimeEvent(pedestrians, this.getClass());
UnsupportedSelfCategoryException.throwIfPedestriansNotTargetOrientied(pedestrians, this.getClass());
Iterator<Pedestrian> it = pedestrians.iterator();
double maxSpeed = 3;
......
......@@ -9,10 +9,11 @@ import org.vadere.simulator.models.potential.FloorGradientProviderFactory;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTargetGrid;
import org.vadere.simulator.models.potential.fields.PotentialFieldAgent;
import org.vadere.simulator.models.potential.fields.PotentialFieldObstacle;
import org.vadere.simulator.models.potential.solver.gradients.GradientProvider;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.models.AttributesSFM;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.psychology.perception.exceptions.UnsupportedStimulusException;
import org.vadere.state.psychology.cognition.UnsupportedSelfCategoryException;
import org.vadere.state.scenario.DynamicElement;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Target;
......@@ -20,15 +21,8 @@ import org.vadere.state.scenario.Topography;
import org.vadere.state.types.GradientProviderType;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape;
import org.vadere.simulator.models.potential.solver.gradients.GradientProvider;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.*;
@ModelClass(isMainModel = true)
public class SocialForceModel extends ODEModel<Pedestrian, AttributesAgent> {
......@@ -139,7 +133,7 @@ public class SocialForceModel extends ODEModel<Pedestrian, AttributesAgent> {
Collection<Pedestrian> pedestrians = topography.getElements(Pedestrian.class);
UnsupportedStimulusException.throwIfNotElapsedTimeEvent(pedestrians, this.getClass());
UnsupportedSelfCategoryException.throwIfPedestriansNotTargetOrientied(pedestrians, this.getClass());
// set gradient provider and pedestrians
equations.setElements(pedestrians);
......
package org.vadere.state.psychology.cognition;
import org.jetbrains.annotations.NotNull;
import org.vadere.state.scenario.Pedestrian;
import java.util.Collection;
/**
* Throw this exception if a (locomotion) class does not handle a specific {@link SelfCategory}.
*/
public class UnsupportedSelfCategoryException extends RuntimeException {
public UnsupportedSelfCategoryException(@NotNull SelfCategory unsupportedSelfCategory, @NotNull Class implementingClass) {
super(String.format("SelfCategory \"%s\" is not supported by class \"%s\"!",
unsupportedSelfCategory,
implementingClass.getSimpleName())
);
}
public static void throwIfPedestriansNotTargetOrientied(Collection<? extends Pedestrian> pedestrians, Class caller) {
for (Pedestrian pedestrian : pedestrians) {
SelfCategory selfCategory = pedestrian.getSelfCategory();
if (selfCategory != SelfCategory.TARGET_ORIENTED) {
throw new UnsupportedSelfCategoryException(selfCategory, caller);
}
}
}
}
package org.vadere.state.psychology.perception.exceptions;
import org.jetbrains.annotations.NotNull;
import org.vadere.state.psychology.perception.types.ElapsedTime;
import org.vadere.state.psychology.perception.types.Stimulus;
import org.vadere.state.scenario.Pedestrian;
import java.util.Collection;
/**
* Use this exception if a stimuli-handling class does not support a specific stimulus.
*/
public class UnsupportedStimulusException extends RuntimeException {
public UnsupportedStimulusException(@NotNull Stimulus unsupportedStimulus, @NotNull Class implementingClass) {
super(String.format("Stimulus \"%s\" not supported by class \"%s\"!",
unsupportedStimulus.getClass().getSimpleName(),
implementingClass.getSimpleName())
);
}
public static void throwIfNotElapsedTimeEvent(Collection<? extends Pedestrian> pedestrians, Class caller) {
for (Pedestrian pedestrian : pedestrians) {
Stimulus currentStimulus = pedestrian.getMostImportantStimulus();
if ((currentStimulus instanceof ElapsedTime) == false) {
throw new UnsupportedStimulusException(currentStimulus, caller);
}
}
}
}
package org.vadere.state.psychology.cognition;
import org.junit.Test;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static org.junit.Assert.*;
public class UnsupportedSelfCategoryExceptionTest {
private List<Pedestrian> createPedestrians(int totalPedestrians) {
List<Pedestrian> pedestrians = new ArrayList<>();
long seed = 0;
Random random = new Random(seed);
for (int i = 0; i < totalPedestrians; i++) {
AttributesAgent attributesAgent = new AttributesAgent(i);
Pedestrian pedestrian = new Pedestrian(attributesAgent, random);
pedestrian.setPosition(new VPoint(i, i));
pedestrians.add(pedestrian);
}
return pedestrians;
}
@Test
public void throwIfPedestriansNotTargetOrientiedThrowsNoExceptionIfTargetOriented() {
SelfCategory expectedSelfCategory = SelfCategory.TARGET_ORIENTED;
List<Pedestrian> pedestrians = createPedestrians(2);
pedestrians.stream().forEach(pedestrian -> pedestrian.setSelfCategory(expectedSelfCategory));
UnsupportedSelfCategoryException.throwIfPedestriansNotTargetOrientied(pedestrians, this.getClass());
pedestrians.stream().forEach(pedestrian -> assertEquals(expectedSelfCategory, pedestrian.getSelfCategory()));
}
@Test
public void throwIfPedestriansNotTargetOrientiedThrowsExceptionIfNotTargetOriented() {
SelfCategory[] selfCategories = SelfCategory.values();
int actualTotalExceptions = 0;
int expectedTotalExceptions = selfCategories.length - 1;
for (int i = 0; i < selfCategories.length; i++) {
SelfCategory currentCategory = selfCategories[i];
List<Pedestrian> pedestrians = createPedestrians(2);
pedestrians.stream().forEach(pedestrian -> pedestrian.setSelfCategory(currentCategory));
try {
UnsupportedSelfCategoryException.throwIfPedestriansNotTargetOrientied(pedestrians, this.getClass());
} catch (UnsupportedSelfCategoryException exception) {
actualTotalExceptions++;
}
}
assertEquals(expectedTotalExceptions, actualTotalExceptions);
}
@Test(expected = UnsupportedSelfCategoryException.class)
public void throwIfPedestriansNotTargetOrientiedThrowsExceptionIfAtLeastOneNotTargetOriented() {
List<Pedestrian> pedestrians = createPedestrians(2);
pedestrians.get(0).setSelfCategory(SelfCategory.TARGET_ORIENTED);
pedestrians.get(1).setSelfCategory(SelfCategory.COOPERATIVE);
UnsupportedSelfCategoryException.throwIfPedestriansNotTargetOrientied(pedestrians, this.getClass());
}
}
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