1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

added skills to vehicle

This commit is contained in:
oblonski 2014-06-29 19:40:07 +02:00
parent 2cd2f54b27
commit d093e090ef
3 changed files with 111 additions and 7 deletions

View file

@ -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<String> getSkills();
public abstract boolean hasSkill(String string);
}

View file

@ -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<String> skills = new HashSet<String>();
/**
* 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<String> 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<String> getSkills() {
return Collections.unmodifiableSet(skills);
}
@Override
public boolean hasSkill(String skill) {
return skills.contains(skill.toLowerCase());
}

View file

@ -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"));
}
}