SolverValidator { public static GlobalStateObject validateInputs(GlobalStateObject state) { checkArgument(state.getTime() >= 0, "Time must be >= 0, is %s.", state.getTime()); checkArgument(!state.getVehicles().isEmpty(), "At least one vehicle must be present."); final Set<Parcel> inventoryParcels = newHashSet(); final boolean routeIsPresent = state.getVehicles().get(0).getRoute() .isPresent(); final Set<Parcel> allParcels = newHashSet(); for (int i = 0; i < state.getVehicles().size(); i++) { final VehicleStateObject vs = state.getVehicles().get(i); checkArgument( vs.getRoute().isPresent() == routeIsPresent, "Either a route should be present for all vehicles, or no route " + "should be present for all vehicles."); if (vs.getRoute().isPresent()) { for (final Parcel p : vs.getRoute().get()) { checkArgument( !allParcels.contains(p), "Found parcel which is already present in the route of another " + "vehicle. %s is found in vehicle %s.", p, i); } allParcels.addAll(vs.getRoute().get()); } } for (int i = 0; i < state.getVehicles().size(); i++) { final VehicleStateObject vs = state.getVehicles().get(i); checkArgument(vs.getRemainingServiceTime() >= 0, "Remaining service time must be >= 0, is %s.", vs.getRemainingServiceTime()); final Set<Parcel> intersection = Sets.intersection( state.getAvailableParcels(), vs.getContents()); checkArgument( intersection.isEmpty(), "Parcels can not be available AND in the inventory of a vehicle, " + "found: %s.", intersection); final Set<Parcel> inventoryIntersection = Sets.intersection( inventoryParcels, vs.getContents()); checkArgument( inventoryIntersection.isEmpty(), "Parcels can not be in the inventory of two vehicles at the same " + "time, found: %s.", inventoryIntersection); inventoryParcels.addAll(vs.getContents()); if (vs.getDestination().isPresent()) { final boolean isAvailable = state.getAvailableParcels() .contains(vs.getDestination().get()); final boolean isInCargo = vs.getContents() .contains(vs.getDestination().get()); checkArgument( isAvailable != isInCargo, "Destination must be either available (%s) or in the current " + "vehicle's cargo (%s), but not both (i.e. XOR). Destination: " + "%s, vehicle: %s (out of %s), remaining service time: %s.", isAvailable, isInCargo, vs.getDestination(), i, state.getVehicles().size(), vs.getRemainingServiceTime()); } if (vs.getRoute().isPresent()) { checkRoute(vs, i); } } return state; } private  SolverValidator(); static Solver wrap(Solver delegate); static StochasticSupplier<Solver> wrap(
      StochasticSupplier<? extends Solver> sup); static GlobalStateObject validateInputs(GlobalStateObject state); static void checkRoute(VehicleStateObject vs, int i); static ImmutableList<ImmutableList<Parcel>> validateOutputs(
      ImmutableList<ImmutableList<Parcel>> routes, GlobalStateObject state);  }
@Test public void validateNegativeTime() { final GlobalStateObject state = GlobalStateObject.create( ImmutableSet.<Parcel>of(), ImmutableList.<VehicleStateObject>of(), -1, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); boolean fail = false; try { SolverValidator.validateInputs(state); } catch (final IllegalArgumentException e) { fail = true; assertThat(e.getMessage()).contains("Time must be >= 0"); } assertThat(fail).isTrue(); }
@Test public void validateNegativeRemainingTime() { final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), POINT, absent, ImmutableSet.of(p1), -1, p2, ImmutableList.<Parcel>of()); final GlobalStateObject state = GlobalStateObject.create( ImmutableSet.<Parcel>of(), ImmutableList.of(vs1), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); boolean fail = false; try { SolverValidator.validateInputs(state); } catch (final IllegalArgumentException e) { assertThat(e.getMessage()) .contains("Remaining service time must be >= 0"); fail = true; } assertThat(fail).isTrue(); }
@Test public void validateParcelAvailableAndInInventory() { final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), POINT, absent, ImmutableSet.of(p1), 0, p2, ImmutableList.<Parcel>of()); final GlobalStateObject state = GlobalStateObject.create( ImmutableSet.of(p1), ImmutableList.of(vs1), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); boolean fail = false; try { SolverValidator.validateInputs(state); } catch (final IllegalArgumentException e) { fail = true; assertThat(e.getMessage()).contains( "Parcels can not be available AND in the inventory of a vehicle"); } assertThat(fail).isTrue(); }
@Test(expected = IllegalArgumentException.class) public void validateParcelInTwoInventories() { final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), POINT, absent, ImmutableSet.of(p1), 0, p2, ImmutableList.<Parcel>of()); final VehicleStateObject vs2 = VehicleStateObject.create(vdto(), POINT, absent, ImmutableSet.of(p1), 0, p2, ImmutableList.<Parcel>of()); final ImmutableSet<Parcel> empty = ImmutableSet.of(); final GlobalStateObject state = GlobalStateObject.create(empty, ImmutableList.of(vs1, vs2), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); SolverValidator.validateInputs(state); }
@Test(expected = IllegalArgumentException.class) public void valiateInputsDestinationNotInContents() { final ImmutableSet<Parcel> empty = ImmutableSet.of(); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), POINT, absent, empty, 0, p1, ImmutableList.<Parcel>of()); final VehicleStateObject vs2 = VehicleStateObject.create(vdto(), POINT, absent, ImmutableSet.of(p2, p1), 0, p1, ImmutableList.<Parcel>of()); final GlobalStateObject state = GlobalStateObject.create(empty, ImmutableList.of(vs1, vs2), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); SolverValidator.validateInputs(state); }
@Test(expected = IllegalArgumentException.class) public void validateInvalidCurrentRoute1() { final Point p = new Point(0, 0); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), p, absent, ImmutableSet.of(p1), 0, p1, ImmutableList.of(p1)); final VehicleStateObject vs2 = VehicleStateObject.create(vdto(), p, absent, ImmutableSet.of(p2), 0, null, null); final ImmutableSet<Parcel> available = ImmutableSet.of(p3); final GlobalStateObject state = GlobalStateObject.create(available, ImmutableList.of(vs1, vs2), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); SolverValidator.validateInputs(state); }
@Test(expected = IllegalArgumentException.class) public void validateInvalidCurrentRoute2() { final Point p = new Point(0, 0); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), p, absent, ImmutableSet.<Parcel>of(), 0, p1, ImmutableList.of(p1, p2, p1, p2)); final VehicleStateObject vs2 = VehicleStateObject.create(vdto(), p, absent, ImmutableSet.<Parcel>of(), 0, null, ImmutableList.of(p2, p2)); final ImmutableSet<Parcel> available = ImmutableSet.of(p1, p2); final GlobalStateObject state = GlobalStateObject.create(available, ImmutableList.of(vs1, vs2), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); SolverValidator.validateInputs(state); }
@Test(expected = IllegalArgumentException.class) public void validateInvalidCurrentRoute3() { final Point p = new Point(0, 0); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), p, absent, ImmutableSet.of(p1), 0, p1, ImmutableList.of(p3)); final ImmutableSet<Parcel> available = ImmutableSet.of(p3); final GlobalStateObject state = GlobalStateObject.create(available, ImmutableList.of(vs1), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); SolverValidator.validateInputs(state); }
@Test(expected = IllegalArgumentException.class) public void validateInvalidCurrentRoute4a() { final Point p = new Point(0, 0); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), p, absent, ImmutableSet.of(p1), 0, p1, ImmutableList.of(p3, p1, p3)); final ImmutableSet<Parcel> available = ImmutableSet.of(p3); final GlobalStateObject state = GlobalStateObject.create(available, ImmutableList.of(vs1), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); SolverValidator.validateInputs(state); }
@Test(expected = IllegalArgumentException.class) public void validateInvalidCurrentRoute4b() { final Point p = new Point(0, 0); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), p, absent, ImmutableSet.<Parcel>of(), 0, p1, ImmutableList.<Parcel>of()); final ImmutableSet<Parcel> available = ImmutableSet.of(p1); final GlobalStateObject state = GlobalStateObject.create(available, ImmutableList.of(vs1), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); SolverValidator.validateInputs(state); }
@Test(expected = IllegalArgumentException.class) public void validateInvalidCurrentRoute5() { final Point p = new Point(0, 0); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), p, absent, ImmutableSet.of(p1), 0, p1, ImmutableList.of(p1, p1, p3)); final ImmutableSet<Parcel> available = ImmutableSet.of(p3); final GlobalStateObject state = GlobalStateObject.create(available, ImmutableList.of(vs1), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); SolverValidator.validateInputs(state); }
@Test(expected = IllegalArgumentException.class) public void validateInvalidCurrentRoute6a() { final Point p = new Point(0, 0); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), p, absent, ImmutableSet.of(p1), 0, p1, ImmutableList.of(p1, p2)); final ImmutableSet<Parcel> available = ImmutableSet.of(p2); final GlobalStateObject state = GlobalStateObject.create(available, ImmutableList.of(vs1), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); SolverValidator.validateInputs(state); }
@Test(expected = IllegalArgumentException.class) public void validateInvalidCurrentRoute6b() { final Point p = new Point(0, 0); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), p, absent, ImmutableSet.of(p1), 0, p1, ImmutableList.of(p1, p2, p2, p2)); final ImmutableSet<Parcel> available = ImmutableSet.of(p2); final GlobalStateObject state = GlobalStateObject.create(available, ImmutableList.of(vs1), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.METER, mock(RoadModelSnapshot.class)); SolverValidator.validateInputs(state); }
@Test public void validateValidCurrentRoutes() { final ImmutableSet<Parcel> empty = ImmutableSet.of(); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), POINT, absent, ImmutableSet.of(p1), 0, p1, ImmutableList.of(p1)); final VehicleStateObject vs2 = VehicleStateObject.create(vdto(), POINT, absent, ImmutableSet.of(p2), 0, null, ImmutableList.of(p2)); final VehicleStateObject vs3 = VehicleStateObject.create(vdto(), POINT, absent, empty, 0, p3, ImmutableList.<Parcel>of(p3, p3)); final ImmutableSet<Parcel> available = ImmutableSet.of(p3); final GlobalStateObject state = GlobalStateObject.create(available, ImmutableList.of(vs1, vs2, vs3), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.CENTIMETER, mock(RoadModelSnapshot.class)); SolverValidator.validateInputs(state); }
@SuppressWarnings("null") @Test public void validateCorrectInputs() { final ImmutableSet<Parcel> empty = ImmutableSet.of(); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), POINT, absent, ImmutableSet.of(p1), 0, p1, null); final VehicleStateObject vs2 = VehicleStateObject.create(vdto(), POINT, absent, ImmutableSet.of(p2), 0, null, null); final VehicleStateObject vs3 = VehicleStateObject.create(vdto(), POINT, absent, empty, 0, p3, null); final ImmutableSet<Parcel> available = ImmutableSet.of(p3); final GlobalStateObject state = GlobalStateObject.create(available, ImmutableList.of(vs1, vs2, vs3), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.CENTIMETER, mock(RoadModelSnapshot.class)); SolverValidator.validateInputs(state); }