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

add test to KnowledgeBase and TraCI SetInformationItem

parent ebe4305f
Pipeline #240432 canceled with stages
in 7 minutes and 14 seconds
......@@ -16,7 +16,7 @@ import org.vadere.manager.traci.commands.TraCISetCommand;
import org.vadere.manager.traci.compound.CompoundObject;
import org.vadere.manager.traci.response.TraCIGetResponse;
import org.vadere.simulator.control.simulation.SimulationState;
import org.vadere.state.psychology.perception.types.InformationStimulus;
import org.vadere.state.psychology.perception.types.KnowledgeItem;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.geometry.Vector3D;
......@@ -394,7 +394,7 @@ public class PersonCommandHandler extends CommandHandler<PersonVar> {
return cmd;
}
@PersonHandler(cmd = TraCICmd.SET_PERSON_STATE, var = PersonVar.STIMULUS, name = "setInformation")
@PersonHandler(cmd = TraCICmd.SET_PERSON_STATE, var = PersonVar.INFORMATION_ITEM, name = "setInformation")
public TraCICommand process_setStimulus(TraCISetCommand cmd, RemoteManager remoteManager) {
CompoundObject data = (CompoundObject) cmd.getVariableValue();
double start_t = (double)data.getData(0, TraCIDataType.DOUBLE);
......@@ -406,8 +406,8 @@ public class PersonCommandHandler extends CommandHandler<PersonVar> {
Pedestrian ped = state.getTopography().getPedestrianDynamicElements()
.getElement(Integer.parseInt(cmd.getElementId()));
if (checkIfPedestrianExists(ped, cmd)) {
InformationStimulus s = new InformationStimulus(start_t, obsolete_at, information);
ped.getKnowledgeBase().add_information(s);
KnowledgeItem s = new KnowledgeItem(start_t, obsolete_at, information);
ped.getKnowledgeBase().addInformation(s);
cmd.setOK();
}
});
......
......@@ -36,7 +36,7 @@ public enum PersonVar {
ADD(0x80, TraCIDataType.STRING),
REMOVE_STAGE(0xc5, TraCIDataType.INTEGER), // set
TARGET_LIST(0xfe, TraCIDataType.STRING_LIST), // get, set
STIMULUS(0xfd, TraCIDataType.COMPOUND_OBJECT)
INFORMATION_ITEM(0xfd, TraCIDataType.COMPOUND_OBJECT)
;
......
......@@ -4,7 +4,9 @@ import org.apache.commons.lang3.tuple.Pair;
import org.vadere.manager.TraCIException;
import org.vadere.manager.traci.TraCIDataType;
import java.util.Arrays;
import java.util.Iterator;
import java.util.function.Predicate;
/**
* CompoundObject implementation based on TraCI as described in https://sumo.dlr.de/docs/TraCI/Protocol.html#atomar_types
......@@ -94,16 +96,37 @@ public class CompoundObject {
return new Iter(this, typeAssertion);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CompoundObject that = (CompoundObject) o;
return Arrays.equals(type, that.type) &&
Arrays.equals(data, that.data);
}
@Override
public int hashCode() {
int result = Arrays.hashCode(type);
result = 31 * result + Arrays.hashCode(data);
return result;
}
private class Iter implements Iterator<Pair<TraCIDataType, Object>> {
private final CompoundObject compoundObject;
private final Predicate<TraCIDataType> typeAssertionTest;
private final TraCIDataType typeAssertion;
private int curr;
Iter(CompoundObject compoundObject, TraCIDataType typeAssertion) {
this.compoundObject = compoundObject;
this.typeAssertion = typeAssertion;
if (typeAssertion != null) {
this.typeAssertionTest = traCIDataType -> traCIDataType.equals(typeAssertion);
} else {
this.typeAssertionTest = traCIDataType -> true;
}
this.curr = 0;
}
......@@ -112,10 +135,12 @@ public class CompoundObject {
return curr < compoundObject.size();
}
@Override
public Pair<TraCIDataType, Object> next() {
Pair<TraCIDataType, Object> p = Pair.of(compoundObject.type[curr], compoundObject.data[curr]);
if (!p.getLeft().equals(this.typeAssertion)) {
if (! this.typeAssertionTest.test(p.getLeft())) {
throw new TraCIException("Type mismatch in CompoundObject. Expected '%s' but found '%s'",
this.typeAssertion.name(), p.getLeft().name());
}
......
package org.vadere.manager.traci.commandHandler;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.mockito.Mockito;
import org.vadere.manager.RemoteManager;
......@@ -12,36 +11,31 @@ import org.vadere.manager.traci.commandHandler.variables.PersonVar;
import org.vadere.manager.traci.commands.TraCICommand;
import org.vadere.manager.traci.commands.TraCIGetCommand;
import org.vadere.manager.traci.commands.TraCISetCommand;
import org.vadere.manager.traci.writer.TraCIPacket;
import org.vadere.simulator.entrypoints.ScenarioFactory;
import org.vadere.manager.traci.compound.CompoundObject;
import org.vadere.manager.traci.compound.CompoundObjectBuilder;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.MainModelBuilder;
import org.vadere.simulator.models.osm.OptimalStepsModel;
import org.vadere.simulator.projects.Scenario;
import org.vadere.state.scenario.DynamicElementContainer;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.psychology.perception.types.KnowledgeItem;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.types.ScenarioElementType;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.geometry.Vector3D;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.Vector2D;
import org.vadere.util.io.IOUtils;
import org.vadere.util.logging.Logger;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.Random;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doNothing;
public class PersonCommandHandlerTest extends CommandHandlerTest {
......@@ -673,6 +667,37 @@ public class PersonCommandHandlerTest extends CommandHandlerTest {
testSetValue(ret, varID, varType, elementID, data);
}
@Test
public void process_setInformationItem() {
PersonVar var = PersonVar.INFORMATION_ITEM;
int varID = var.id;
TraCIDataType varType = var.type;
String elementID = "10";
Pedestrian p = new Pedestrian(new AttributesAgent(10), new Random(1));
CompoundObject data = CompoundObjectBuilder.builder()
.add(TraCIDataType.DOUBLE)
.add(TraCIDataType.DOUBLE)
.add(TraCIDataType.STRING)
.build(12.2, 13.2, "reason001");
TraCISetCommand cmd = (TraCISetCommand) getFirstCommand(TraCISetCommand.build(
TraCICmd.SET_PERSON_STATE, elementID, varID, varType, data));
RemoteManager rm = new TestRemoteManager() {
@Override
protected void mockIt() {
when(simState.getTopography().getPedestrianDynamicElements().getElement(Integer.parseInt(elementID)))
.thenReturn(p);
}
};
KnowledgeItem s = new KnowledgeItem(12.2, 13.2, "reason001");
TraCICommand ret = persCmdHandler.process_setStimulus(cmd, rm);
assertThat(p.getKnowledgeBase().getKnowledge().get(0), equalTo(s));
checkSET_OK(ret);
testSetValue(ret, varID, varType, elementID, data);
}
@Test
public void process_setNextTargetListIndex2() {
PersonVar var = PersonVar.NEXT_TARGET_LIST_INDEX;
......
......@@ -26,9 +26,9 @@ public class MeshPedStimulusCountingProcessor extends MeshDensityCountingProcess
// setup filter
if (getAttributes().isRegexFilter()){
filter_pattern = Pattern.compile(getAttributes().getInformationFilter());
filter_by_stimuli = ped -> ped.getKnowledgeBase().knows_about(filter_pattern);
filter_by_stimuli = ped -> ped.getKnowledgeBase().knowsAbout(filter_pattern);
} else {
filter_by_stimuli = ped -> ped.getKnowledgeBase().knows_about(getAttributes().getInformationFilter());
filter_by_stimuli = ped -> ped.getKnowledgeBase().knowsAbout(getAttributes().getInformationFilter());
}
}
......@@ -41,7 +41,7 @@ public class MeshPedStimulusCountingProcessor extends MeshDensityCountingProcess
// compute count
for(Pedestrian ped : peds) {
// update knowledgeBase
ped.getKnowledgeBase().update_obsolete(state.getSimTimeInSec());
ped.getKnowledgeBase().updateObsolete(state.getSimTimeInSec());
// filter by knowledge of pedestrian
if (filter_by_stimuli.test(ped)) {
doUpdateOnPed(ped);
......
package org.vadere.state.psychology;
import org.vadere.state.psychology.perception.types.InformationStimulus;
import org.vadere.state.psychology.perception.types.Stimulus;
import org.vadere.state.psychology.perception.types.KnowledgeItem;
import java.util.ArrayList;
import java.util.regex.Pattern;
/**
* Simple Knowledge Store to manage {@link KnowledgeItem}s.
*/
public class KnowledgeBase {
private ArrayList<InformationStimulus> knowledge;
private ArrayList<KnowledgeItem> knowledge;
public KnowledgeBase() {
this.knowledge = new ArrayList<>();
......@@ -17,32 +19,33 @@ public class KnowledgeBase {
/**
* remove Information no longer usable at given time.
*/
public void update_obsolete(double current_time){
knowledge.removeIf(i-> i.getObsolete_at() > 0 && current_time > i.getObsolete_at());
public void updateObsolete(double current_time){
knowledge.removeIf(i-> i.getObsoleteAt() > 0 && current_time > i.getObsoleteAt());
}
/**
* True if KnowledgeBase contains this information.
*/
public boolean knows_about(String information){
return knowledge.stream().anyMatch(i->i.getInformation().equals(information));
}
public boolean knows_about(Pattern information){
return knowledge.stream().anyMatch(i->information.matcher(i.getInformation()).matches());
public boolean knowsAbout(String informationId){
return knowledge.stream().anyMatch(i->i.getInformationId().equals(informationId));
}
public boolean knows_about(String information, Class<? extends Stimulus> clz){
return knowledge.stream().anyMatch(i->i.getInformation().equals(information));
public boolean knowsAbout(Pattern informationIdPattern){
return knowledge.stream().anyMatch(i->informationIdPattern.matcher(i.getInformationId()).matches());
}
public void add_information(InformationStimulus info){
public void addInformation(KnowledgeItem info){
knowledge.add(info);
}
public void remove_information(String info){
knowledge.removeIf(i->i.getInformation().equals(info));
public void removeInformation(String informationId){
knowledge.removeIf(i->i.getInformationId().equals(informationId));
}
public void removeInformation(Pattern informationIdPattern){
knowledge.removeIf(i-> informationIdPattern.matcher(i.getInformationId()).matches());
}
public ArrayList<KnowledgeItem> getKnowledge() {
return knowledge;
}
}
package org.vadere.state.psychology.perception.types;
/**
* Class encodes some kind of information a pedestrian knows about.
* The information is active from the time gien in {@link #time} and will be
* forgotten at {@link #obsolete_at} (or never if {@link #obsolete_at} == -1
*/
public class InformationStimulus extends Stimulus {
private String information;
private double obsolete_at;
public InformationStimulus(String information) {
super(0.0);
this.information = information;
this.obsolete_at = -1; // never
}
public InformationStimulus(double time, double obsolete_at, String information) {
super(time);
this.information = information;
this.obsolete_at = obsolete_at;
}
public InformationStimulus(InformationStimulus other) {
super(other.time);
this.information = other.information;
this.obsolete_at = other.obsolete_at;
}
public String getInformation() {
return information;
}
public double getObsolete_at() {
return obsolete_at;
}
@Override
public Stimulus clone() {
return new InformationStimulus(this);
}
}
package org.vadere.state.psychology.perception.types;
import java.util.Objects;
/**
* Class encodes some kind of information a pedestrian knows about.
* The information is active from the time gien in {@link #time} and will be
* forgotten at {@link #obsoleteAt} (or never if {@link #obsoleteAt} == -1
*/
public class KnowledgeItem extends Stimulus {
private String informationId;
private double obsoleteAt;
public KnowledgeItem(String informationId) {
super(0.0);
this.informationId = informationId;
this.obsoleteAt = -1; // never
}
public KnowledgeItem(double time, double obsoleteAt, String informationId) {
super(time);
this.informationId = informationId;
this.obsoleteAt = obsoleteAt;
}
public KnowledgeItem(KnowledgeItem other) {
super(other.time);
this.informationId = other.informationId;
this.obsoleteAt = other.obsoleteAt;
}
public String getInformationId() {
return informationId;
}
public double getObsoleteAt() {
return obsoleteAt;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
KnowledgeItem that = (KnowledgeItem) o;
return Double.compare(that.obsoleteAt, obsoleteAt) == 0 &&
Objects.equals(informationId, that.informationId);
}
@Override
public int hashCode() {
return Objects.hash(informationId, obsoleteAt);
}
@Override
public Stimulus clone() {
return new KnowledgeItem(this);
}
}
......@@ -36,7 +36,6 @@ import java.util.List;
@Type(value = Wait.class, name = "Wait"),
@Type(value = WaitInArea.class, name = "WaitInArea"),
@Type(value = ChangeTarget.class, name = "ChangeTarget"),
@Type(value = InformationStimulus.class, name = "InformationAvailable")
})
// "time" is set when the stimulus is injected into the simulation run and must not be de-/serialized.
@JsonIgnoreProperties({ "time" })
......
package org.vadere.state.psychology;
import org.junit.Before;
import org.junit.Test;
import org.vadere.state.psychology.perception.types.KnowledgeItem;
import java.util.ArrayList;
import java.util.regex.Pattern;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;
public class KnowledgeBaseTest {
KnowledgeItem i1;
KnowledgeItem i2;
KnowledgeItem i3;
@Before
public void init(){
i1 = new KnowledgeItem(1.0, 2.0, "i001");
i2 = new KnowledgeItem(1.0, 3.0, "i002");
i3 = new KnowledgeItem(1.0, 4.0, "i003");
}
@Test
public void update_obsolete() {
KnowledgeBase base = new KnowledgeBase();
base.addInformation(i1);
base.addInformation(i2);
base.addInformation(i3);
base.updateObsolete(2.1);
assertThat(base.getKnowledge().size(), equalTo(2));
assertFalse(base.knowsAbout(i1.getInformationId()));
assertTrue(base.knowsAbout(i2.getInformationId()));
assertTrue(base.knowsAbout(i3.getInformationId()));
}
@Test
public void knowsAbout() {
KnowledgeBase base = new KnowledgeBase();
base.addInformation(i1);
assertTrue(base.knowsAbout(i1.getInformationId()));
assertFalse(base.knowsAbout("XXX"));
}
@Test
public void knowsAboutPattern() {
KnowledgeBase base = new KnowledgeBase();
base.addInformation(i1);
assertTrue(base.knowsAbout(Pattern.compile("i\\d\\d1")));
assertFalse(base.knowsAbout(Pattern.compile("i\\d\\d5")));
}
@Test
public void addInformation() {
KnowledgeBase base = new KnowledgeBase();
base.addInformation(i1);
assertEquals(base.getKnowledge().get(0), i1);
}
@Test
public void removeInformation() {
ArrayList<KnowledgeItem> items = new ArrayList<>();
items.add(i1);
items.add(i3);
KnowledgeBase base = new KnowledgeBase();
base.addInformation(i1);
base.addInformation(i2);
base.addInformation(i3);
base.removeInformation(i2.getInformationId());
assertEquals(items, base.getKnowledge());
}
@Test
public void removeInformation2() {
ArrayList<KnowledgeItem> items = new ArrayList<>();
items.add(i1);
items.add(i3);
KnowledgeBase base = new KnowledgeBase();
base.addInformation(i1);
base.addInformation(i2);
base.addInformation(i3);
base.removeInformation(Pattern.compile("i\\d\\d2"));
assertEquals(items, base.getKnowledge());
}
@Test
public void getKnowledge() {
ArrayList<KnowledgeItem> items = new ArrayList<>();
items.add(i1);
items.add(i2);
items.add(i3);
KnowledgeBase base = new KnowledgeBase();
base.addInformation(i1);
base.addInformation(i2);
base.addInformation(i3);
assertEquals(items, base.getKnowledge());
}
}
\ No newline at end of file
Supports Markdown
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