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

Merge pull request #348 from balage1541/master

#347 - Custom properties
This commit is contained in:
Stefan Schröder 2017-06-01 07:34:14 +02:00 committed by GitHub
commit 29fbdd6e66
16 changed files with 583 additions and 263 deletions

View file

@ -26,7 +26,9 @@ import com.graphhopper.jsprit.core.problem.job.Job;
public abstract class AbstractJob implements Job {
private int index;
private Object userData;
@Override
public int getIndex() {
return index;
}
@ -35,4 +37,15 @@ public abstract class AbstractJob implements Job {
this.index = index;
}
/**
* @return User-specific domain data associated by the job
*/
public Object getUserData() {
return userData;
}
protected void setUserData(Object userData) {
this.userData = userData;
}
}

View file

@ -30,6 +30,7 @@ public abstract class AbstractVehicle implements Vehicle {
private int index;
@Override
public int getIndex() {
return index;
}
@ -44,6 +45,21 @@ public abstract class AbstractVehicle implements Vehicle {
private VehicleTypeKey vehicleIdentifier;
private Object userData;
/**
* @return User-specific domain data associated with the vehicle
*/
@Override
public Object getUserData() {
return userData;
}
protected void setUserData(Object userData) {
this.userData = userData;
}
@Override
public int getIndex() {
return index;
}
@ -52,6 +68,7 @@ public abstract class AbstractVehicle implements Vehicle {
this.index = index;
}
@Override
public VehicleTypeKey getVehicleTypeIdentifier() {
return vehicleIdentifier;
}

View file

@ -66,10 +66,30 @@ public final class Location implements HasIndex, HasId {
private String name = "";
private Object userData;
public static Builder newInstance() {
return new Builder();
}
/**
* Sets user specific domain data associated with the object.
*
* <p>
* The user data is a black box for the framework, it only stores it,
* but never interacts with it in any way.
* </p>
*
* @param userData
* any object holding the domain specific user data
* associated with the object.
* @return builder
*/
public Builder setUserData(Object userData) {
this.userData = userData;
return this;
}
/**
* Sets location index
*
@ -140,13 +160,23 @@ public final class Location implements HasIndex, HasId {
private final String name;
private Object userData;
private Location(Builder builder) {
this.userData = builder.userData;
this.index = builder.index;
this.coordinate = builder.coordinate;
this.id = builder.id;
this.name = builder.name;
}
/**
* @return User-specific domain data associated by the job
*/
public Object getUserData() {
return userData;
}
@Override
public String getId() {
return id;

View file

@ -17,6 +17,8 @@
*/
package com.graphhopper.jsprit.core.problem.job;
import java.util.Collection;
import com.graphhopper.jsprit.core.problem.AbstractJob;
import com.graphhopper.jsprit.core.problem.Capacity;
import com.graphhopper.jsprit.core.problem.Location;
@ -26,8 +28,6 @@ import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindows;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindowsImpl;
import com.graphhopper.jsprit.core.util.Coordinate;
import java.util.Collection;
/**
* Service implementation of a job.
* <p>
@ -89,6 +89,7 @@ public class Service extends AbstractJob {
private boolean twAdded = false;
private int priority = 2;
protected Object userData;
Builder(String id){
this.id = id;
@ -137,6 +138,24 @@ public class Service extends AbstractJob {
return this;
}
/**
* Sets user specific domain data associated with the object.
*
* <p>
* The user data is a black box for the framework, it only stores it,
* but never interacts with it in any way.
* </p>
*
* @param userData
* any object holding the domain specific user data
* associated with the object.
* @return builder
*/
public Builder<T> setUserData(Object userData) {
this.userData = userData;
return this;
}
/**
* Adds capacity dimension.
*
@ -247,7 +266,8 @@ public class Service extends AbstractJob {
private final int priority;
Service(Builder builder) {
Service(Builder<?> builder) {
setUserData(builder.userData);
id = builder.id;
serviceTime = builder.serviceTime;
timeWindow = builder.timeWindow;
@ -367,6 +387,7 @@ public class Service extends AbstractJob {
*
* @return priority
*/
@Override
public int getPriority() {
return priority;
}

View file

@ -17,6 +17,8 @@
*/
package com.graphhopper.jsprit.core.problem.job;
import java.util.Collection;
import com.graphhopper.jsprit.core.problem.AbstractJob;
import com.graphhopper.jsprit.core.problem.Capacity;
import com.graphhopper.jsprit.core.problem.Location;
@ -24,8 +26,6 @@ import com.graphhopper.jsprit.core.problem.Skills;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindow;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindowsImpl;
import java.util.Collection;
/**
* Shipment is an implementation of Job and consists of a pickup and a delivery of something.
@ -89,6 +89,8 @@ public class Shipment extends AbstractJob {
private int priority = 2;
public Object userData;
/**
* Returns new instance of this builder.
*
@ -108,10 +110,29 @@ public class Shipment extends AbstractJob {
deliveryTimeWindows.add(deliveryTimeWindow);
}
/**
* Sets user specific domain data associated with the object.
*
* <p>
* The user data is a black box for the framework, it only stores it,
* but never interacts with it in any way.
* </p>
*
* @param userData
* any object holding the domain specific user data
* associated with the object.
* @return builder
*/
public Builder setUserData(Object userData) {
this.userData = userData;
return this;
}
/**
* Sets pickup location.
*
* @param pickupLocation pickup location
* @param pickupLocation
* pickup location
* @return builder
*/
public Builder setPickupLocation(Location pickupLocation) {
@ -311,6 +332,7 @@ public class Shipment extends AbstractJob {
private final int priority;
Shipment(Builder builder) {
setUserData(builder.userData);
this.id = builder.id;
this.pickupServiceTime = builder.pickupServiceTime;
this.pickupTimeWindow = builder.pickupTimeWindow;
@ -438,6 +460,7 @@ public class Shipment extends AbstractJob {
*
* @return priority
*/
@Override
public int getPriority() {
return priority;
}

View file

@ -72,6 +72,14 @@ public interface Vehicle extends HasId, HasIndex {
public abstract VehicleTypeKey getVehicleTypeIdentifier();
public abstract Skills getSkills();
/**
* @return User-specific domain data associated with the vehicle
*/
public Object getUserData();
public abstract Break getBreak();
// Switch to this as soon as we switct to Java 8:
// default Object getUserData() {
// return null;
// };
}

View file

@ -17,12 +17,13 @@
*/
package com.graphhopper.jsprit.core.problem.vehicle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.graphhopper.jsprit.core.problem.AbstractVehicle;
import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.Skills;
import com.graphhopper.jsprit.core.problem.job.Break;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
@ -130,6 +131,8 @@ public class VehicleImpl extends AbstractVehicle {
private Break aBreak;
private Object userData;
private Builder(String id) {
super();
this.id = id;
@ -148,16 +151,39 @@ public class VehicleImpl extends AbstractVehicle {
return this;
}
/**
* Sets user specific domain data associated with the object.
*
* <p>
* The user data is a black box for the framework, it only stores it,
* but never interacts with it in any way.
* </p>
*
* @param userData
* any object holding the domain specific user data
* associated with the object.
* @return builder
*/
public Builder setUserData(Object userData) {
this.userData = userData;
return this;
}
/**
* Sets the flag whether the vehicle must return to depot or not.
* <p>
* <p>If returnToDepot is true, the vehicle must return to specified end-location. If you
* omit specifying the end-location, vehicle returns to start-location (that must to be set). If
* you specify it, it returns to specified end-location.
* <p>
* <p>If returnToDepot is false, the end-location of the vehicle is endogenous.
* If returnToDepot is true, the vehicle must return to specified
* end-location. If you omit specifying the end-location, vehicle
* returns to start-location (that must to be set). If you specify it,
* it returns to specified end-location.
* <p>
* <p>
* If returnToDepot is false, the end-location of the vehicle is
* endogenous.
*
* @param returnToDepot true if vehicle need to return to depot, otherwise false
* @param returnToDepot
* true if vehicle need to return to depot, otherwise false
* @return this builder
*/
public Builder setReturnToDepot(boolean returnToDepot) {
@ -239,9 +265,8 @@ public class VehicleImpl extends AbstractVehicle {
if (startLocation != null && endLocation == null) {
endLocation = startLocation;
}
if (startLocation == null && endLocation == null) {
if (startLocation == null && endLocation == null)
throw new IllegalArgumentException("vehicle requires startLocation. but neither locationId nor locationCoord nor startLocationId nor startLocationCoord has been set");
}
skills = skillBuilder.build();
return new VehicleImpl(this);
}
@ -297,6 +322,7 @@ public class VehicleImpl extends AbstractVehicle {
private final Break aBreak;
private VehicleImpl(Builder builder) {
setUserData(builder.userData);
id = builder.id;
type = builder.type;
earliestDeparture = builder.earliestStart;
@ -306,7 +332,7 @@ public class VehicleImpl extends AbstractVehicle {
endLocation = builder.endLocation;
startLocation = builder.startLocation;
aBreak = builder.aBreak;
// setVehicleIdentifier(new VehicleTypeKey(type.getTypeId(),startLocation.getId(),endLocation.getId(),earliestDeparture,latestArrival,skills));
// setVehicleIdentifier(new VehicleTypeKey(type.getTypeId(),startLocation.getId(),endLocation.getId(),earliestDeparture,latestArrival,skills));
setVehicleIdentifier(new VehicleTypeKey(type.getTypeId(), startLocation.getId(), endLocation.getId(), earliestDeparture, latestArrival, skills, returnToDepot));
}
@ -346,6 +372,7 @@ public class VehicleImpl extends AbstractVehicle {
return id;
}
@Override
public boolean isReturnToDepot() {
return returnToDepot;
}

View file

@ -56,4 +56,13 @@ public interface VehicleType {
public String getProfile();
/**
* @return User-specific domain data associated with the vehicle type
*/
public Object getUserData();
// Switch to this as soon as we switct to Java 8:
// default Object getUserData() {
// return null;
// };
}

View file

@ -116,16 +116,39 @@ public class VehicleTypeImpl implements VehicleType {
private boolean dimensionAdded = false;
private Object userData;
private Builder(String id) {
this.id = id;
}
/**
* Sets the maximum velocity this vehicle-type can go [in meter per seconds].
* Sets user specific domain data associated with the object.
*
* <p>
* The user data is a black box for the framework, it only stores it,
* but never interacts with it in any way.
* </p>
*
* @param userData
* any object holding the domain specific user data
* associated with the object.
* @return builder
*/
public Builder setUserData(Object userData) {
this.userData = userData;
return this;
}
/**
* Sets the maximum velocity this vehicle-type can go [in meter per
* seconds].
*
* @param inMeterPerSeconds
* @return this builder
* @throws IllegalArgumentException if velocity is smaller than zero
* @throws IllegalArgumentException
* if velocity is smaller than zero
*/
public VehicleTypeImpl.Builder setMaxVelocity(double inMeterPerSeconds) {
if (inMeterPerSeconds < 0.0) throw new IllegalArgumentException("velocity cannot be smaller than zero");
@ -314,12 +337,15 @@ public class VehicleTypeImpl implements VehicleType {
private final double maxVelocity;
private Object userData;
/**
* priv constructor constructing vehicle-type
*
* @param builder
*/
private VehicleTypeImpl(VehicleTypeImpl.Builder builder) {
this.userData = builder.userData;
typeId = builder.id;
capacity = builder.capacity;
maxVelocity = builder.maxVelo;
@ -328,6 +354,14 @@ public class VehicleTypeImpl implements VehicleType {
profile = builder.profile;
}
/**
* @return User-specific domain data associated with the vehicle
*/
@Override
public Object getUserData() {
return userData;
}
/* (non-Javadoc)
* @see basics.route.VehicleType#getTypeId()
*/

View file

@ -18,11 +18,18 @@
package com.graphhopper.jsprit.core.problem;
import com.graphhopper.jsprit.core.util.Coordinate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import com.graphhopper.jsprit.core.util.Coordinate;
/**
* Created by schroeder on 16.12.14.
*/
@ -82,7 +89,7 @@ public class LocationTest {
@Test
public void whenCoordinateSetWithFactory_returnCorrectLocation() {
// Location l = Location.Builder.newInstance().setCoordinate(Coordinate.newInstance(10,20)).build();
// Location l = Location.Builder.newInstance().setCoordinate(Coordinate.newInstance(10,20)).build();
Location l = Location.newInstance(10, 20);
Assert.assertEquals(10., l.getCoordinate().getX(),0.001);
Assert.assertEquals(20., l.getCoordinate().getY(),0.001);
@ -90,4 +97,16 @@ public class LocationTest {
}
@Test
public void whenSettingUserData_itIsAssociatedWithTheLocation() {
Location one = Location.Builder.newInstance().setCoordinate(Coordinate.newInstance(10, 20))
.setUserData(new HashMap<String, Object>()).build();
Location two = Location.Builder.newInstance().setIndex(1).setUserData(42).build();
Location three = Location.Builder.newInstance().setIndex(2).build();
assertTrue(one.getUserData() instanceof Map);
assertEquals(42, two.getUserData());
assertNull(three.getUserData());
}
}

View file

@ -17,11 +17,18 @@
*/
package com.graphhopper.jsprit.core.problem.job;
import com.graphhopper.jsprit.core.problem.Location;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
import com.graphhopper.jsprit.core.problem.Location;
public class DeliveryTest {
@ -104,4 +111,16 @@ public class DeliveryTest {
}
@Test
public void whenSettingUserData_itIsAssociatedWithTheJob() {
Delivery one = Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setUserData(new HashMap<String, Object>()).build();
Delivery two = Delivery.Builder.newInstance("s2").setLocation(Location.newInstance("loc")).setUserData(42)
.build();
Delivery three = Delivery.Builder.newInstance("s3").setLocation(Location.newInstance("loc")).build();
assertTrue(one.getUserData() instanceof Map);
assertEquals(42, two.getUserData());
assertNull(three.getUserData());
}
}

View file

@ -17,11 +17,18 @@
*/
package com.graphhopper.jsprit.core.problem.job;
import com.graphhopper.jsprit.core.problem.Location;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
import com.graphhopper.jsprit.core.problem.Location;
public class PickupTest {
@ -105,4 +112,15 @@ public class PickupTest {
Assert.assertEquals(2, s.getPriority());
}
@Test
public void whenSettingUserData_itIsAssociatedWithTheJob() {
Pickup one = Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setUserData(new HashMap<String, Object>()).build();
Pickup two = Pickup.Builder.newInstance("s2").setLocation(Location.newInstance("loc")).setUserData(42).build();
Pickup three = Pickup.Builder.newInstance("s3").setLocation(Location.newInstance("loc")).build();
assertTrue(one.getUserData() instanceof Map);
assertEquals(42, two.getUserData());
assertNull(three.getUserData());
}
}

View file

@ -17,17 +17,25 @@
*/
package com.graphhopper.jsprit.core.problem.job;
import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindow;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.*;
import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindow;
public class ServiceTest {
@ -53,7 +61,7 @@ public class ServiceTest {
Service one = Service.Builder.newInstance("service").addSizeDimension(0, 10).setLocation(Location.newInstance("foo")).build();
Service two = Service.Builder.newInstance("service").addSizeDimension(0, 10).setLocation(Location.newInstance("fo")).build();
serviceSet.add(one);
// assertTrue(serviceSet.contains(two));
// assertTrue(serviceSet.contains(two));
serviceSet.remove(two);
assertTrue(serviceSet.isEmpty());
}
@ -278,4 +286,17 @@ public class ServiceTest {
}
@Test
public void whenSettingUserData_itIsAssociatedWithTheJob() {
Service one = Service.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setUserData(new HashMap<String, Object>()).build();
Service two = Service.Builder.newInstance("s2").setLocation(Location.newInstance("loc")).setUserData(42)
.build();
Service three = Service.Builder.newInstance("s3").setLocation(Location.newInstance("loc")).build();
assertTrue(one.getUserData() instanceof Map);
assertEquals(42, two.getUserData());
assertNull(three.getUserData());
}
}

View file

@ -17,16 +17,25 @@
*/
package com.graphhopper.jsprit.core.problem.job;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindow;
import com.graphhopper.jsprit.core.util.Coordinate;
import com.graphhopper.jsprit.core.util.TestUtils;
import org.junit.Assert;
import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.*;
public class ShipmentTest {
@ -433,4 +442,17 @@ public class ShipmentTest {
}
@Test
public void whenSettingUserData_itIsAssociatedWithTheJob() {
Shipment one = Shipment.Builder.newInstance("s").setPickupLocation(Location.newInstance("loc"))
.setDeliveryLocation(Location.newInstance("loc")).setUserData(new HashMap<String, Object>()).build();
Shipment two = Shipment.Builder.newInstance("s2").setPickupLocation(Location.newInstance("loc"))
.setDeliveryLocation(Location.newInstance("loc")).setUserData(42).build();
Shipment three = Shipment.Builder.newInstance("s3").setPickupLocation(Location.newInstance("loc"))
.setDeliveryLocation(Location.newInstance("loc")).build();
assertTrue(one.getUserData() instanceof Map);
assertEquals(42, two.getUserData());
assertNull(three.getUserData());
}
}

View file

@ -18,12 +18,20 @@
package com.graphhopper.jsprit.core.problem.vehicle;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.job.Break;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindow;
import org.junit.Test;
import static org.junit.Assert.*;
public class VehicleImplTest {
@ -39,7 +47,7 @@ public class VehicleImplTest {
@Test
public void whenAddingDriverBreak_itShouldBeAddedCorrectly() {
VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("type").build();
Break aBreak = (Break) Break.Builder.newInstance("break").setTimeWindow(TimeWindow.newInstance(100, 200)).setServiceTime(30).build();
Break aBreak = Break.Builder.newInstance("break").setTimeWindow(TimeWindow.newInstance(100, 200)).setServiceTime(30).build();
Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance("start"))
.setType(type1).setEndLocation(Location.newInstance("start"))
.setBreak(aBreak).build();
@ -237,5 +245,19 @@ public class VehicleImplTest {
assertFalse(v.getSkills().containsSkill("ScrewDriver"));
}
@Test
public void whenSettingUserData_itIsAssociatedWithTheVehicle() {
VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("type").build();
Vehicle one = VehicleImpl.Builder.newInstance("v").setType(type1)
.setStartLocation(Location.newInstance("start")).setUserData(new HashMap<String, Object>()).build();
Vehicle two = VehicleImpl.Builder.newInstance("v").setType(type1)
.setStartLocation(Location.newInstance("start")).setUserData(42).build();
Vehicle three = VehicleImpl.Builder.newInstance("v").setType(type1)
.setStartLocation(Location.newInstance("start")).build();
assertTrue(one.getUserData() instanceof Map);
assertEquals(42, two.getUserData());
assertNull(three.getUserData());
}
}

View file

@ -17,9 +17,15 @@
*/
package com.graphhopper.jsprit.core.problem.vehicle;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class VehicleTypeImplTest {
@ -152,4 +158,15 @@ public class VehicleTypeImplTest {
}
@Test
public void whenSettingUserData_itIsAssociatedWithTheVehicleType() {
VehicleType one = VehicleTypeImpl.Builder.newInstance("type").setUserData(new HashMap<String, Object>())
.build();
VehicleType two = VehicleTypeImpl.Builder.newInstance("type").setUserData(42).build();
VehicleType three = VehicleTypeImpl.Builder.newInstance("type").build();
assertTrue(one.getUserData() instanceof Map);
assertEquals(42, two.getUserData());
assertNull(three.getUserData());
}
}