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 { ...@@ -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 * @param status new status
*/ */
......
...@@ -4,6 +4,8 @@ import de.rwth.montisim.commons.utils.LTLOperator; ...@@ -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.Json;
import de.rwth.montisim.commons.utils.json.SerializationException; import de.rwth.montisim.commons.utils.json.SerializationException;
import java.util.HashMap;
public abstract class GoalBuilder<T extends GoalBuilder<T>> { public abstract class GoalBuilder<T extends GoalBuilder<T>> {
public Goal fromJson(String json) throws SerializationException { public Goal fromJson(String json) throws SerializationException {
......
...@@ -5,11 +5,20 @@ import de.rwth.montisim.commons.utils.Comparator; ...@@ -5,11 +5,20 @@ import de.rwth.montisim.commons.utils.Comparator;
import de.rwth.montisim.commons.utils.LTLOperator; import de.rwth.montisim.commons.utils.LTLOperator;
import de.rwth.montisim.commons.utils.json.Typed; import de.rwth.montisim.commons.utils.json.Typed;
import de.rwth.montisim.simulation.vehicle.Vehicle; 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 @Typed
public class MetricGoal extends Goal { public class MetricGoal extends Goal {
private VehicleProperty property; private String property;
private Comparator comparator; private Comparator comparator;
private double value; private double value;
private String unit; private String unit;
...@@ -17,7 +26,7 @@ public class MetricGoal extends Goal { ...@@ -17,7 +26,7 @@ public class MetricGoal extends Goal {
public MetricGoal() { 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.ltlOperator = ltlOperator;
this.property = property; this.property = property;
this.comparator = comparator; this.comparator = comparator;
...@@ -25,13 +34,21 @@ public class MetricGoal extends Goal { ...@@ -25,13 +34,21 @@ public class MetricGoal extends Goal {
this.unit = unit; 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) { public void update(Vehicle v) {
double currValue = 0; boolean result;
if (property.equals(VehicleProperty.SPEED)) { if (property.equals(TrueVelocity.VALUE_NAME)) {
currValue = v.physicalObject.velocity.magnitude(); 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) { if (result) {
updateStatus(TaskStatus.SUCCEEDED); updateStatus(TaskStatus.SUCCEEDED);
} else { } else {
...@@ -39,17 +56,23 @@ public class MetricGoal extends Goal { ...@@ -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 // TODO take unit into consideration
if (comparator.equals(Comparator.EQUAL) && currValue == value) { if (comparator.equals(Comparator.EQUAL) && value == this.value) {
return true; return true;
} else if (comparator.equals(Comparator.GREATER) && currValue > value) { } else if (comparator.equals(Comparator.GREATER) && value > this.value) {
return true; return true;
} else if (comparator.equals(Comparator.GREATER_EQUAL) && currValue >= value) { } else if (comparator.equals(Comparator.GREATER_EQUAL) && value >= this.value) {
return true; return true;
} else if (comparator.equals(Comparator.LESS) && currValue < value) { } else if (comparator.equals(Comparator.LESS) && value < this.value) {
return true; return true;
} else if (comparator.equals(Comparator.LESS_EQUAL) && currValue <= value) { } else if (comparator.equals(Comparator.LESS_EQUAL) && value <= this.value) {
return true; return true;
} }
return false; return false;
...@@ -79,7 +102,7 @@ public class MetricGoal extends Goal { ...@@ -79,7 +102,7 @@ public class MetricGoal extends Goal {
return new MetricGoalBuilder(); return new MetricGoalBuilder();
} }
public VehicleProperty getProperty() { public String getProperty() {
return property; return property;
} }
......
...@@ -2,14 +2,23 @@ package de.rwth.montisim.simulation.vehicle.task.goal; ...@@ -2,14 +2,23 @@ package de.rwth.montisim.simulation.vehicle.task.goal;
import de.rwth.montisim.commons.utils.Comparator; import de.rwth.montisim.commons.utils.Comparator;
import java.util.HashMap;
public class MetricGoalBuilder extends GoalBuilder<MetricGoalBuilder> { 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; Comparator comparator;
double targetValue; double targetValue;
String targetUnit; String targetUnit;
public MetricGoalBuilder setProperty(VehicleProperty property) { public MetricGoalBuilder setProperty(String property) {
this.property = property; if (metricNameAdapter.containsKey(property))
this.property = metricNameAdapter.get(property);
else
this.property = property;
return getThis(); return getThis();
} }
......
...@@ -4,6 +4,7 @@ import de.rwth.montisim.commons.utils.Comparator; ...@@ -4,6 +4,7 @@ import de.rwth.montisim.commons.utils.Comparator;
import de.rwth.montisim.commons.utils.LTLOperator; import de.rwth.montisim.commons.utils.LTLOperator;
import de.rwth.montisim.commons.utils.json.Json; import de.rwth.montisim.commons.utils.json.Json;
import de.rwth.montisim.commons.utils.json.SerializationException; 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 de.rwth.montisim.simulation.vehicle.task.goal.*;
import org.junit.Test; import org.junit.Test;
...@@ -17,7 +18,7 @@ public class TaskTest { ...@@ -17,7 +18,7 @@ public class TaskTest {
origTask.addGoal( origTask.addGoal(
MetricGoal.newBuilder() MetricGoal.newBuilder()
.setProperty(VehicleProperty.SPEED) .setProperty("speed")
.never() .never()
.greater(100, "m/s") .greater(100, "m/s")
.build() .build()
...@@ -55,12 +56,8 @@ public class TaskTest { ...@@ -55,12 +56,8 @@ public class TaskTest {
assertEquals(LTLOperator.UNTIL, goal.getLtlOperator()); assertEquals(LTLOperator.UNTIL, goal.getLtlOperator());
// test vehicle properties // test vehicle properties
goal = MetricGoal.newBuilder().setProperty(VehicleProperty.ACCELERATION).build(); goal = MetricGoal.newBuilder().setProperty("speed").build();
assertEquals(VehicleProperty.ACCELERATION, ((MetricGoal) goal).getProperty()); assertEquals(TrueVelocity.VALUE_NAME, ((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());
// test comparators, target values and target units // test comparators, target values and target units
goal = MetricGoal.newBuilder().less(1, "m/s").build(); goal = MetricGoal.newBuilder().less(1, "m/s").build();
......
package de.rwth.montisim.simulation.vehicle.task.goal; 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.DynamicObject;
import de.rwth.montisim.commons.simulation.TaskStatus; import de.rwth.montisim.commons.simulation.TaskStatus;
import de.rwth.montisim.commons.utils.Vec3; import de.rwth.montisim.commons.utils.Vec3;
import de.rwth.montisim.simulation.vehicle.Vehicle; 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 junit.framework.TestCase;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MetricGoalTest extends TestCase { public class MetricGoalTest extends TestCase {
public void testNever() { public void testNever() {
Goal goal = MetricGoal.newBuilder() Goal goal = MetricGoal.newBuilder()
.setProperty(VehicleProperty.SPEED) .setProperty(TrueVelocity.VALUE_NAME)
.never() .never()
.greater(100, "m/s") .greater(100, "m/s")
.build(); .build();
Vehicle v = mock(Vehicle.class); Vehicle mockVehicle = mock(Vehicle.class);
v.physicalObject = mock(DynamicObject.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 // speed ok
v.physicalObject.velocity = new Vec3(0, 0, 0); when(mockVelocity.get()).thenReturn(0d);
goal.update(v); goal.update(mockVehicle);
assertEquals(TaskStatus.SUCCEEDED, goal.getStatus()); assertEquals(TaskStatus.SUCCEEDED, goal.getStatus());
// speed limit exceeded // speed limit exceeded
v.physicalObject.velocity = new Vec3(200, 0, 0); when(mockVelocity.get()).thenReturn(200d);
goal.update(v); goal.update(mockVehicle);
assertEquals(TaskStatus.FAILED, goal.getStatus()); assertEquals(TaskStatus.FAILED, goal.getStatus());
// should always be FAILED even if vehicle slowed down // should always be FAILED even if vehicle slowed down
v.physicalObject.velocity = new Vec3(0, 0, 0); when(mockVelocity.get()).thenReturn(0d);
goal.update(v); goal.update(mockVehicle);
assertEquals(TaskStatus.FAILED, goal.getStatus()); assertEquals(TaskStatus.FAILED, goal.getStatus());
} }
public void testAlways() { public void testAlways() {
Goal goal = MetricGoal.newBuilder() Goal goal = MetricGoal.newBuilder()
.setProperty(VehicleProperty.SPEED) .setProperty(TrueVelocity.VALUE_NAME)
.always() .always()
.less(100, "m/s") .less(100, "m/s")
.build(); .build();
Vehicle v = mock(Vehicle.class); Vehicle mockVehicle = mock(Vehicle.class);
v.physicalObject = mock(DynamicObject.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 // speed ok
v.physicalObject.velocity = new Vec3(0, 0, 0); when(mockVelocity.get()).thenReturn(0d);
goal.update(v); goal.update(mockVehicle);
assertEquals(TaskStatus.SUCCEEDED, goal.getStatus()); assertEquals(TaskStatus.SUCCEEDED, goal.getStatus());
// speed limit exceeded // speed limit exceeded
v.physicalObject.velocity = new Vec3(200, 0, 0); when(mockVelocity.get()).thenReturn(200d);
goal.update(v); goal.update(mockVehicle);
assertEquals(TaskStatus.FAILED, goal.getStatus()); assertEquals(TaskStatus.FAILED, goal.getStatus());
// should always be FAILED even if vehicle slowed down // should always be FAILED even if vehicle slowed down
v.physicalObject.velocity = new Vec3(0, 0, 0); when(mockVelocity.get()).thenReturn(0d);
goal.update(v); goal.update(mockVehicle);
assertEquals(TaskStatus.FAILED, goal.getStatus()); 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