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

fix CompoundObject handling.

parent f9875e3c
Pipeline #173226 passed with stages
in 129 minutes and 22 seconds
......@@ -372,7 +372,7 @@ public class TestClient extends org.vadere.manager.client.AbstractTestClient imp
String[] targets = Arrays.copyOfRange(args,4,args.length);
CompoundObject compoundObj = CompoundObjectBuilder.createPerson(x, y, targets);
CompoundObject compoundObj = CompoundObjectBuilder.createPerson(elementIdentifier, x, y, targets);
TraCIResponse res = personapi.createNew(elementIdentifier, compoundObj);
System.out.println(res.toString());
}
......
......@@ -10,22 +10,20 @@ 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.compoundobjects.CompoundObject;
import org.vadere.manager.traci.compoundobjects.PersonCreateData;
import org.vadere.manager.traci.respons.TraCIGetResponse;
import org.vadere.simulator.control.factory.SourceControllerFactory;
import org.vadere.simulator.models.AgentFactory;
import org.vadere.simulator.models.DynamicElementFactory;
import org.vadere.simulator.models.osm.PedestrianOSM;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.AttributesBuilder;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.Agent;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.util.geometry.Vector3D;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.logging.Logger;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
......@@ -339,9 +337,11 @@ public class PersonCommandHandler extends CommandHandler<PersonVar>{
return cmd;
}
@PersonHandler(cmd = TraCICmd.SET_PERSON_STATE, var = PersonVar.ADD, name = "createNew", dataTypeStr = "CompoundObject")
@PersonHandler(cmd = TraCICmd.SET_PERSON_STATE, var = PersonVar.ADD, ignoreElementId = true, name = "createNew", dataTypeStr = "CompoundObject")
public TraCICommand process_addPerson(TraCISetCommand cmd, RemoteManager remoteManager) {
VPoint tmp = (VPoint) cmd.getVariableValue();
PersonCreateData data = new PersonCreateData((CompoundObject) cmd.getVariableValue());
VPoint pos = data.getPos();
LinkedList<Integer> targets = data.getTargetsAsInt();
String id = cmd.getElementId();
remoteManager.accessState((manager, state) -> {
......@@ -354,9 +354,9 @@ public class PersonCommandHandler extends CommandHandler<PersonVar>{
// call it a failure
cmd.setErr("id is not free");
} else {
Pedestrian oldPed = state.getTopography().getPedestrianDynamicElements().getElement(Integer.parseInt(idList.get(0)));
Pedestrian newDynamicElement = (Pedestrian) state.getMainModel().get().createElement(tmp, Integer.parseInt(id), oldPed.getClass());
Pedestrian newDynamicElement = (Pedestrian) state.getMainModel().get().createElement(pos, Integer.parseInt(id), oldPed.getClass());
newDynamicElement.setTargets(targets);
state.getTopography().getPedestrianDynamicElements().addElement(newDynamicElement);
cmd.setOK();
......
......@@ -6,6 +6,22 @@ import org.vadere.manager.traci.TraCIDataType;
import java.util.Iterator;
/**
* CompoundObject implementation based on TraCI as described in https://sumo.dlr.de/docs/TraCI/Protocol.html#atomar_types
*
* This implementation consist of two equally long arrays {@link #type} and {@link #data}. The
* {@link #type} array saves Type of the objects at the same index in {@link #data}.
*
* At time of creation it must be stated how many objects will be tide together. See @{@link CompoundObjectBuilder}
* for usage of Constructor and the {@link #add(int, Object)} method.
*
* See @{@link GenericCompoundObject} and its subclasses on the use of {@link #getData(int, TraCIDataType)}
* to create objects type specific child classes @{@link GenericCompoundObject} to correctly parse the
* objects in {@link #data} into useful objects.
*
* See @{{@link PersonCreateData}} on how to use @{@link CompoundObject}.
*
*/
public class CompoundObject {
private TraCIDataType[] type;
......
......@@ -4,8 +4,15 @@ import org.vadere.manager.TraCIException;
import org.vadere.manager.traci.TraCIDataType;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
/**
* Builder class to create any combination atomar data types combined in a @{@link CompoundObject}.
* See static methods on how the builder is used. Ensure that the number of {@link #add(TraCIDataType)}
* calls is equal to the number of arguments to the {@link #build(Object...)}.
*/
public class CompoundObjectBuilder {
private LinkedList<TraCIDataType> types;
......@@ -45,15 +52,17 @@ public class CompoundObjectBuilder {
return new CompoundObjectBuilder();
}
static public CompoundObject createPerson(String x, String y, String... targets){
static public CompoundObject createPerson(String id, String x, String y, String... targets){
VPoint p = new VPoint(Double.parseDouble(x), Double.parseDouble(y));
ArrayList<String> targetList = new ArrayList<>(Arrays.asList(targets));
return CompoundObjectBuilder.builder()
.rest()
.add(TraCIDataType.STRING)
.add(TraCIDataType.POS_2D)
.add(TraCIDataType.STRING_LIST)
.build(p, targets);
.build(id, p, targetList);
}
......
......@@ -11,8 +11,8 @@ public abstract class GenericCompoundObject {
}
protected void assertElementCount(final CompoundObject o, int size){
if (o.size() == size)
throw new TraCIException("Cannot create %s from CompoundObject containing %s", getClass().getName(), o.types());
if (o.size() != size)
throw new TraCIException("Element mismatch: Cannot create %s from CompoundObject containing %s", getClass().getName(), o.types());
}
abstract protected void init(CompoundObject o);
......
......@@ -4,6 +4,8 @@ import org.vadere.manager.traci.TraCIDataType;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.stream.Collectors;
public class PersonCreateData extends GenericCompoundObject{
......@@ -11,7 +13,7 @@ public class PersonCreateData extends GenericCompoundObject{
private VPoint pos;
private ArrayList<String> targets;
PersonCreateData(CompoundObject o){
public PersonCreateData(CompoundObject o){
super(o, 3);
}
......@@ -33,4 +35,9 @@ public class PersonCreateData extends GenericCompoundObject{
public ArrayList<String> getTargets() {
return targets;
}
public LinkedList<Integer> getTargetsAsInt(){
return targets.stream().mapToInt(Integer::parseInt).boxed().collect(Collectors.toCollection(LinkedList::new));
}
}
......@@ -233,7 +233,7 @@ public class TraCIByteBuffer implements TraCIReader {
CompoundObject compoundObject = new CompoundObject(noElements);
for(int i = 0; i<= noElements; i++){
for(int i = 0; i < noElements; i++){
TraCIDataType type = TraCIDataType.fromId(readUnsignedByte());
if (type.equals(TraCIDataType.COMPOUND_OBJECT))
throw new TraCIException("Recursive CompoundObject are not allowed.");
......
......@@ -99,6 +99,7 @@ public class ByteArrayOutputStreamTraCIWriter implements TraCIWriter {
break;
case COMPOUND_OBJECT:
writeCompoundObject((CompoundObject) data);
break;
default:
logger.errorf("cannot write %s", dataType.toString());
......@@ -302,7 +303,7 @@ public class ByteArrayOutputStreamTraCIWriter implements TraCIWriter {
@Override
public TraCIWriter writeCompoundObject(CompoundObject compoundObject) {
writeUnsignedByteWithId(TraCIDataType.COMPOUND_OBJECT.id);
writeUnsignedByte(TraCIDataType.COMPOUND_OBJECT.id);
writeInt(compoundObject.size());
Iterator<Pair<TraCIDataType, Object>> iter = compoundObject.itemIterator();
while (iter.hasNext()){
......
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