Commit 6b03e2f5 authored by Hengwen Zhang's avatar Hengwen Zhang
Browse files

Remove VehicleProperty, use classes from vehicle.physicalvalues instead.

parent bc46e9e4
......@@ -24,7 +24,8 @@ public abstract class Goal {
/**
* Update goal status depending on the LTL operator and the new status
* Update goal status regarding the LTL operator and the current value of the boolean
* expression it contains.
*
* @param status new status
*/
......
......@@ -4,6 +4,8 @@ import de.rwth.montisim.commons.utils.LTLOperator;
import de.rwth.montisim.commons.utils.json.Json;
import de.rwth.montisim.commons.utils.json.SerializationException;
import java.util.HashMap;
public abstract class GoalBuilder<T extends GoalBuilder<T>> {
public Goal fromJson(String json) throws SerializationException {
......
......@@ -5,11 +5,20 @@ import de.rwth.montisim.commons.utils.Comparator;
import de.rwth.montisim.commons.utils.LTLOperator;
import de.rwth.montisim.commons.utils.json.Typed;
import de.rwth.montisim.simulation.vehicle.Vehicle;
import de.rwth.montisim.simulation.vehicle.physicalvalues.TrueVelocity;
/**
* This class represents a simple LTL formula that can be evaluated/updated
* at any time given an instance of the Vehicle class.
*
* The LTL formula of this class concentrate on vehicle's property such as
* orientation, velocity, speed, battery, etc.
*
*/
@Typed
public class MetricGoal extends Goal {
private VehicleProperty property;
private String property;
private Comparator comparator;
private double value;
private String unit;
......@@ -17,7 +26,7 @@ public class MetricGoal extends Goal {
public MetricGoal() {
}
public MetricGoal(LTLOperator ltlOperator, VehicleProperty property, Comparator comparator, double value, String unit) {
public MetricGoal(LTLOperator ltlOperator, String property, Comparator comparator, double value, String unit) {
this.ltlOperator = ltlOperator;
this.property = property;
this.comparator = comparator;
......@@ -25,13 +34,21 @@ public class MetricGoal extends Goal {
this.unit = unit;
}
/**
* Update the goal's status by evaluating the LTL formula.
*
* @param v an instance of Vehicle class
*/
public void update(Vehicle v) {
double currValue = 0;
if (property.equals(VehicleProperty.SPEED)) {
currValue = v.physicalObject.velocity.magnitude();
boolean result;
if (property.equals(TrueVelocity.VALUE_NAME)) {
double vel = (double) v.getPhysicalValues().getPhysicalValue(property).get();
result = evaluate(vel);
} else {
System.out.printf("Vehicle is missing property %s.\n", this.property);
return;
}
boolean result = compare(currValue);
if (result) {
updateStatus(TaskStatus.SUCCEEDED);
} else {
......@@ -39,17 +56,23 @@ public class MetricGoal extends Goal {
}
}
private boolean compare(double currValue) {
/**
* Evaluate the boolean expression of this goal
*
* @param value
* @return result of vehicle.[property] comparator value[unit]
*/
private boolean evaluate(double value) {
// TODO take unit into consideration
if (comparator.equals(Comparator.EQUAL) && currValue == value) {
if (comparator.equals(Comparator.EQUAL) && value == this.value) {
return true;
} else if (comparator.equals(Comparator.GREATER) && currValue > value) {
} else if (comparator.equals(Comparator.GREATER) && value > this.value) {
return true;
} else if (comparator.equals(Comparator.GREATER_EQUAL) && currValue >= value) {
} else if (comparator.equals(Comparator.GREATER_EQUAL) && value >= this.value) {
return true;
} else if (comparator.equals(Comparator.LESS) && currValue < value) {
} else if (comparator.equals(Comparator.LESS) && value < this.value) {
return true;
} else if (comparator.equals(Comparator.LESS_EQUAL) && currValue <= value) {
} else if (comparator.equals(Comparator.LESS_EQUAL) && value <= this.value) {
return true;
}
return false;
......@@ -79,7 +102,7 @@ public class MetricGoal extends Goal {
return new MetricGoalBuilder();
}
public VehicleProperty getProperty() {
public String getProperty() {
return property;
}
......
......@@ -2,14 +2,23 @@ package de.rwth.montisim.simulation.vehicle.task.goal;
import de.rwth.montisim.commons.utils.Comparator;
import java.util.HashMap;
public class MetricGoalBuilder extends GoalBuilder<MetricGoalBuilder> {
VehicleProperty property;
private final HashMap<String, String> metricNameAdapter = new HashMap<String, String>(){{
put("speed", "true_velocity"); // simulation.vehicle.physicalvalues
}};
String property;
Comparator comparator;
double targetValue;
String targetUnit;
public MetricGoalBuilder setProperty(VehicleProperty property) {
this.property = property;
public MetricGoalBuilder setProperty(String property) {
if (metricNameAdapter.containsKey(property))
this.property = metricNameAdapter.get(property);
else
this.property = property;
return getThis();
}
......
......@@ -4,6 +4,7 @@ import de.rwth.montisim.commons.utils.Comparator;
import de.rwth.montisim.commons.utils.LTLOperator;
import de.rwth.montisim.commons.utils.json.Json;
import de.rwth.montisim.commons.utils.json.SerializationException;
import de.rwth.montisim.simulation.vehicle.physicalvalues.TrueVelocity;
import de.rwth.montisim.simulation.vehicle.task.goal.*;
import org.junit.Test;
......@@ -17,7 +18,7 @@ public class TaskTest {
origTask.addGoal(
MetricGoal.newBuilder()
.setProperty(VehicleProperty.SPEED)
.setProperty("speed")
.never()
.greater(100, "m/s")
.build()
......@@ -55,12 +56,8 @@ public class TaskTest {
assertEquals(LTLOperator.UNTIL, goal.getLtlOperator());
// test vehicle properties
goal = MetricGoal.newBuilder().setProperty(VehicleProperty.ACCELERATION).build();
assertEquals(VehicleProperty.ACCELERATION, ((MetricGoal) goal).getProperty());
goal = MetricGoal.newBuilder().setProperty(VehicleProperty.SPEED).build();
assertEquals(VehicleProperty.SPEED, ((MetricGoal) goal).getProperty());
goal = MetricGoal.newBuilder().setProperty(VehicleProperty.MIN_BATTERY).build();
assertEquals(VehicleProperty.MIN_BATTERY, ((MetricGoal) goal).getProperty());
goal = MetricGoal.newBuilder().setProperty("speed").build();
assertEquals(TrueVelocity.VALUE_NAME, ((MetricGoal) goal).getProperty());
// test comparators, target values and target units
goal = MetricGoal.newBuilder().less(1, "m/s").build();
......
package de.rwth.montisim.simulation.vehicle.task.goal;
import de.rwth.montisim.commons.physicalvalue.PhysicalValueRegistry;
import de.rwth.montisim.commons.simulation.DynamicObject;
import de.rwth.montisim.commons.simulation.TaskStatus;
import de.rwth.montisim.commons.utils.Vec3;
import de.rwth.montisim.simulation.vehicle.Vehicle;
import de.rwth.montisim.simulation.vehicle.VehicleBuilder;
import de.rwth.montisim.simulation.vehicle.physicalvalues.TrueVelocity;
import junit.framework.TestCase;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MetricGoalTest extends TestCase {
public void testNever() {
Goal goal = MetricGoal.newBuilder()
.setProperty(VehicleProperty.SPEED)
.setProperty(TrueVelocity.VALUE_NAME)
.never()
.greater(100, "m/s")
.build();
Vehicle v = mock(Vehicle.class);
v.physicalObject = mock(DynamicObject.class);
Vehicle mockVehicle = mock(Vehicle.class);
PhysicalValueRegistry mockPValue = mock(PhysicalValueRegistry.class);
TrueVelocity mockVelocity = mock(TrueVelocity.class);
when(mockVehicle.getPhysicalValues()).thenReturn(mockPValue);
when(mockPValue.getPhysicalValue(TrueVelocity.VALUE_NAME)).thenReturn(mockVelocity);
// speed ok
v.physicalObject.velocity = new Vec3(0, 0, 0);
goal.update(v);
when(mockVelocity.get()).thenReturn(0d);
goal.update(mockVehicle);
assertEquals(TaskStatus.SUCCEEDED, goal.getStatus());
// speed limit exceeded
v.physicalObject.velocity = new Vec3(200, 0, 0);
goal.update(v);
when(mockVelocity.get()).thenReturn(200d);
goal.update(mockVehicle);
assertEquals(TaskStatus.FAILED, goal.getStatus());
// should always be FAILED even if vehicle slowed down
v.physicalObject.velocity = new Vec3(0, 0, 0);
goal.update(v);
when(mockVelocity.get()).thenReturn(0d);
goal.update(mockVehicle);
assertEquals(TaskStatus.FAILED, goal.getStatus());
}
public void testAlways() {
Goal goal = MetricGoal.newBuilder()
.setProperty(VehicleProperty.SPEED)
.setProperty(TrueVelocity.VALUE_NAME)
.always()
.less(100, "m/s")
.build();
Vehicle v = mock(Vehicle.class);
v.physicalObject = mock(DynamicObject.class);
Vehicle mockVehicle = mock(Vehicle.class);
PhysicalValueRegistry mockPValue = mock(PhysicalValueRegistry.class);
TrueVelocity mockVelocity = mock(TrueVelocity.class);
when(mockVehicle.getPhysicalValues()).thenReturn(mockPValue);
when(mockPValue.getPhysicalValue(TrueVelocity.VALUE_NAME)).thenReturn(mockVelocity);
// speed ok
v.physicalObject.velocity = new Vec3(0, 0, 0);
goal.update(v);
when(mockVelocity.get()).thenReturn(0d);
goal.update(mockVehicle);
assertEquals(TaskStatus.SUCCEEDED, goal.getStatus());
// speed limit exceeded
v.physicalObject.velocity = new Vec3(200, 0, 0);
goal.update(v);
when(mockVelocity.get()).thenReturn(200d);
goal.update(mockVehicle);
assertEquals(TaskStatus.FAILED, goal.getStatus());
// should always be FAILED even if vehicle slowed down
v.physicalObject.velocity = new Vec3(0, 0, 0);
goal.update(v);
when(mockVelocity.get()).thenReturn(0d);
goal.update(mockVehicle);
assertEquals(TaskStatus.FAILED, goal.getStatus());
}
......
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