From d093e090ef02b1929462c857b609522353aa102f Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Sun, 29 Jun 2014 19:40:07 +0200 Subject: [PATCH] added skills to vehicle --- .../jsprit/core/problem/vehicle/Vehicle.java | 6 ++ .../core/problem/vehicle/VehicleImpl.java | 84 +++++++++++++++++-- .../core/problem/vehicle/VehicleImplTest.java | 28 +++++++ 3 files changed, 111 insertions(+), 7 deletions(-) diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java index bb6b55a7..37c7a755 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java @@ -16,6 +16,8 @@ ******************************************************************************/ package jsprit.core.problem.vehicle; +import java.util.Set; + import jsprit.core.util.Coordinate; /** @@ -82,4 +84,8 @@ public interface Vehicle { * Returns the end-locationCoord of this vehicle. */ public abstract Coordinate getEndLocationCoordinate(); + + public abstract Set getSkills(); + + public abstract boolean hasSkill(String string); } diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java index e1fe8aba..f2fff84f 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java @@ -16,6 +16,10 @@ ******************************************************************************/ package jsprit.core.problem.vehicle; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + import jsprit.core.util.Coordinate; import org.apache.log4j.Logger; @@ -74,6 +78,8 @@ public class VehicleImpl implements Vehicle { private VehicleType type = VehicleTypeImpl.Builder.newInstance("default").build(); + private Set skills = new HashSet(); + /** * Constructs the builder with the vehicleId. * @@ -227,6 +233,17 @@ public class VehicleImpl implements Vehicle { * @return vehicle builder */ public static Builder newInstance(String vehicleId){ return new Builder(vehicleId); } + + /** + * Adds skill and returns build. + * + * @param skill + * @return + */ + public Builder addSkill(String skill) { + this.skills.add(skill.toLowerCase()); + return this; + } } @@ -241,14 +258,18 @@ public class VehicleImpl implements Vehicle { return new NoVehicle(); } + public static VehicleImpl copyAndCreateVehicle(Vehicle vehicleToCopy){ + return new VehicleImpl(vehicleToCopy); + } +// + public static VehicleImpl copyAndCreateVehicleWithNewType(Vehicle vehicleToCopy, VehicleType newType){ + return new VehicleImpl(vehicleToCopy, newType); + } +// private final String id; private final VehicleType type; - private final String locationId; - - private final Coordinate coord; - private final double earliestDeparture; private final double latestArrival; @@ -262,12 +283,12 @@ public class VehicleImpl implements Vehicle { private final Coordinate startLocationCoord; private final String startLocationId; + + private final Set skills; private VehicleImpl(Builder builder){ id = builder.id; type = builder.type; - coord = builder.locationCoord; - locationId = builder.locationId; earliestDeparture = builder.earliestStart; latestArrival = builder.latestArrival; returnToDepot = builder.returnToDepot; @@ -275,6 +296,43 @@ public class VehicleImpl implements Vehicle { startLocationCoord = builder.startLocationCoord; endLocationId = builder.endLocationId; endLocationCoord = builder.endLocationCoord; + skills = builder.skills; + } + + /** + * Copy constructor. + * + * @param vehicle + */ + private VehicleImpl(Vehicle vehicle){ + id = vehicle.getId(); + type = vehicle.getType(); + startLocationId = vehicle.getStartLocationId(); + startLocationCoord = vehicle.getStartLocationCoordinate(); + endLocationId = vehicle.getEndLocationId(); + endLocationCoord = vehicle.getEndLocationCoordinate(); + earliestDeparture = vehicle.getEarliestDeparture(); + latestArrival = vehicle.getLatestArrival(); + returnToDepot = vehicle.isReturnToDepot(); + skills = vehicle.getSkills(); + } + + /** + * Copy constructor. + * + * @param vehicle + */ + private VehicleImpl(Vehicle vehicle, VehicleType newType){ + id = vehicle.getId(); + this.type = newType; + startLocationId = vehicle.getStartLocationId(); + startLocationCoord = vehicle.getStartLocationCoordinate(); + endLocationId = vehicle.getEndLocationId(); + endLocationCoord = vehicle.getEndLocationCoordinate(); + earliestDeparture = vehicle.getEarliestDeparture(); + latestArrival = vehicle.getLatestArrival(); + returnToDepot = vehicle.isReturnToDepot(); + skills = vehicle.getSkills(); } /** @@ -284,7 +342,9 @@ public class VehicleImpl implements Vehicle { */ @Override public String toString() { - return "[id="+id+"][type="+type+"][locationId="+locationId+"][coord=" + coord + "][isReturnToDepot=" + isReturnToDepot() + "]"; + return "[id="+id+"][type="+type+"][startLocationId="+startLocationId+"][startLocationCoord=" + startLocationCoord + "]" + + "[endLocationId="+endLocationId+"][endLocationCoord=" + endLocationCoord + "]" + + "[isReturnToDepot=" + isReturnToDepot() + "]"; } @Override @@ -368,6 +428,16 @@ public class VehicleImpl implements Vehicle { return true; } + @Override + public Set getSkills() { + return Collections.unmodifiableSet(skills); + } + + @Override + public boolean hasSkill(String skill) { + return skills.contains(skill.toLowerCase()); + } + diff --git a/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java b/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java index 98c05fd3..3564c77a 100644 --- a/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java +++ b/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java @@ -187,5 +187,33 @@ public class VehicleImplTest { assertTrue(!v.equals(v2)); } + @Test + public void whenAddingSkills_theyShouldBeAddedCorrectly(){ + VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("type").build(); + Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setType(type1).setEndLocationId("start") + .addSkill("drill").addSkill("screwdriver").build(); + assertTrue(v.getSkills().contains("drill")); + assertTrue(v.hasSkill("drill")); + assertTrue(v.hasSkill("screwdriver")); + } + + @Test + public void whenAddingSkillsCaseSens_theyShouldBeAddedCorrectly(){ + VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("type").build(); + Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setType(type1).setEndLocationId("start") + .addSkill("drill").addSkill("screwdriver").build(); + assertTrue(v.getSkills().contains("drill")); + assertTrue(v.hasSkill("dRill")); + assertTrue(v.hasSkill("ScrewDriver")); + } + + @Test + public void whenAddingSkillsCaseSensV2_theyShouldBeAddedCorrectly(){ + VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("type").build(); + Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setType(type1).setEndLocationId("start") + .addSkill("drill").build(); + assertFalse(v.hasSkill("ScrewDriver")); + } + }