SolverValidator { public static ImmutableList<ImmutableList<Parcel>> validateOutputs( ImmutableList<ImmutableList<Parcel>> routes, GlobalStateObject state) { checkArgument( routes.size() == state.getVehicles().size(), "There must be exactly one route for every vehicle, found %s routes " + "with %s vehicles.", routes.size(), state.getVehicles().size()); final Set<Parcel> inputParcels = newHashSet(state.getAvailableParcels()); final Set<Parcel> outputParcels = newHashSet(); for (int i = 0; i < routes.size(); i++) { final List<Parcel> route = routes.get(i); final Set<Parcel> routeSet = ImmutableSet.copyOf(route); checkArgument( routeSet.containsAll(state.getVehicles().get(i).getContents()), "The route of vehicle %s doesn't visit all parcels in its cargo.", i); inputParcels.addAll(state.getVehicles().get(i).getContents()); if (state.getVehicles().get(i).getDestination().isPresent()) { checkArgument( state.getVehicles().get(i).getDestination().asSet() .contains(route.get(0)), "The route of vehicle %s should start with its current destination:" + " %s.", i, state.getVehicles().get(i).getDestination()); } for (final Parcel p : route) { checkArgument(!outputParcels.contains(p), "Found a parcel which is already in another route: %s.", p); final int frequency = Collections.frequency(route, p); if (state.getAvailableParcels().contains(p)) { checkArgument( frequency == 2, "Route %s: a parcel that is picked up needs to be delivered as " + "well, so it should occur twice in the route, found %s " + "occurence(s) of parcel %s.", i, frequency, p); } else { checkArgument( state.getVehicles().get(i).getContents().contains(p), "The parcel in this route is not available, which means it should" + " be in the contents of this vehicle. Parcel: %s.", p); checkArgument( frequency == 1, "A parcel that is already in cargo should occur once in the " + "route, found %s occurences of parcel %s.", frequency, p); } } outputParcels.addAll(route); } checkArgument( inputParcels.equals(outputParcels), "The number of distinct parcels in the routes should equal the number " + "of parcels in the input, parcels that should be added in routes:" + " %s, parcels that should be removed from routes: %s.", Sets.difference(inputParcels, outputParcels), Sets.difference(outputParcels, inputParcels)); return routes; } 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 validateInvalidNumberOfRoutes() { final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), POINT, absent, ImmutableSet.<Parcel>of(), 0, null, null); final ImmutableList<ImmutableList<Parcel>> routes = ImmutableList.of(); final GlobalStateObject state = GlobalStateObject.create( ImmutableSet.<Parcel>of(), ImmutableList.of(vs1), 0, SI.SECOND, SI.METERS_PER_SECOND, SI.CENTIMETER, mock(RoadModelSnapshot.class)); boolean fail = false; try { SolverValidator.validateOutputs(routes, state); } catch (final IllegalArgumentException e) { assertThat(e.getMessage()).contains("found 0 routes with 1 vehicles"); fail = true; } assertThat(fail).isTrue(); }
@Test public void validateParcelInTwoRoutes() { final ImmutableSet<Parcel> empty = ImmutableSet.of(); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, empty, 0, null, null); final VehicleStateObject vs2 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, empty, 0, null, null); final ImmutableList<ImmutableList<Parcel>> routes = ImmutableList.of( ImmutableList.of(p1, p1), ImmutableList.of(p1, p1)); final ImmutableSet<Parcel> availableParcels = ImmutableSet.of(p1); final ImmutableList<VehicleStateObject> vehicles = ImmutableList.of(vs1, vs2); final GlobalStateObject state = GlobalStateObject.create(availableParcels, vehicles, 0, SI.SECOND, SI.METERS_PER_SECOND, SI.CENTIMETER, mock(RoadModelSnapshot.class)); boolean fail = false; try { SolverValidator.validateOutputs(routes, state); } catch (final IllegalArgumentException e) { assertThat(e.getMessage()) .contains("Found a parcel which is already in another route"); fail = true; } assertThat(fail).isTrue(); }
@Test public void validateParcelTooManyTimes1() { final ImmutableSet<Parcel> empty = ImmutableSet.of(); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, empty, 0, null, null); final ImmutableList<ImmutableList<Parcel>> routes = ImmutableList .of(ImmutableList.of(p1, p1, p1)); final ImmutableSet<Parcel> availableParcels = ImmutableSet.of(p1); final ImmutableList<VehicleStateObject> vehicles = ImmutableList.of(vs1); final GlobalStateObject state = GlobalStateObject.create(availableParcels, vehicles, 0, SI.SECOND, SI.METERS_PER_SECOND, SI.CENTIMETER, mock(RoadModelSnapshot.class)); boolean fail = false; try { SolverValidator.validateOutputs(routes, state); } catch (final IllegalArgumentException e) { assertThat(e.getMessage()).contains("found 3 occurence(s) of parcel p1"); fail = true; } assertThat(fail).isTrue(); }
@Test public void validateParcelTooManyTimes2() { final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, ImmutableSet.of(p1), 0, null, null); final ImmutableList<ImmutableList<Parcel>> routes = ImmutableList .of(ImmutableList.of(p1, p1)); final ImmutableSet<Parcel> availableParcels = ImmutableSet.of(); final ImmutableList<VehicleStateObject> vehicles = ImmutableList.of(vs1); final GlobalStateObject state = GlobalStateObject.create(availableParcels, vehicles, 0, SI.SECOND, SI.METERS_PER_SECOND, SI.CENTIMETER, mock(RoadModelSnapshot.class)); boolean fail = false; try { SolverValidator.validateOutputs(routes, state); } catch (final IllegalArgumentException e) { assertThat(e.getMessage()).contains("found 2 occurences of parcel p1"); fail = true; } assertThat(fail).isTrue(); }
@Test public void validateParcelNotInCargo() { final ImmutableSet<Parcel> empty = ImmutableSet.of(); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, empty, 0, null, null); final ImmutableList<ImmutableList<Parcel>> routes = ImmutableList .of(ImmutableList.of(p1)); final ImmutableSet<Parcel> availableParcels = ImmutableSet.of(); final ImmutableList<VehicleStateObject> vehicles = ImmutableList.of(vs1); final GlobalStateObject state = GlobalStateObject.create(availableParcels, vehicles, 0, SI.SECOND, SI.METERS_PER_SECOND, SI.CENTIMETER, mock(RoadModelSnapshot.class)); boolean fail = false; try { SolverValidator.validateOutputs(routes, state); } catch (final IllegalArgumentException e) { fail = true; assertThat(e.getMessage()) .contains("The parcel in this route is not available"); } assertThat(fail).isTrue(); }
@Test public void validateUnknownParcelInRoute() { final ImmutableSet<Parcel> empty = ImmutableSet.of(); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, empty, 0, null, null); final VehicleStateObject vs2 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, empty, 0, null, null); final ImmutableList<ImmutableList<Parcel>> routes = ImmutableList.of( ImmutableList.of(p1, p1), ImmutableList.of(p2, p3, p3, p2)); final ImmutableSet<Parcel> availableParcels = ImmutableSet.of(p1, p2); final ImmutableList<VehicleStateObject> vehicles = ImmutableList.of(vs1, vs2); final GlobalStateObject state = GlobalStateObject.create(availableParcels, vehicles, 0, SI.SECOND, SI.METERS_PER_SECOND, SI.CENTIMETER, mock(RoadModelSnapshot.class)); boolean fail = false; try { SolverValidator.validateOutputs(routes, state); } catch (final IllegalArgumentException e) { assertThat(e.getMessage()) .contains("parcel in this route is not available"); fail = true; } assertThat(fail).isTrue(); }
@Test public void validateIncompleteRoute1() { final ImmutableSet<Parcel> empty = ImmutableSet.of(); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, empty, 0, null, null); final VehicleStateObject vs2 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, empty, 0, null, null); final ImmutableList<ImmutableList<Parcel>> routes = ImmutableList.of( ImmutableList.of(p1, p1), ImmutableList.of(p2, p2)); final ImmutableSet<Parcel> availableParcels = ImmutableSet .of(p1, p2, p3); final ImmutableList<VehicleStateObject> vehicles = ImmutableList.of(vs1, vs2); final GlobalStateObject state = GlobalStateObject.create(availableParcels, vehicles, 0, SI.SECOND, SI.METERS_PER_SECOND, SI.CENTIMETER, mock(RoadModelSnapshot.class)); boolean fail = false; try { SolverValidator.validateOutputs(routes, state); } catch (final IllegalArgumentException e) { assertThat(e.getMessage()).contains( "number of distinct parcels in the routes should equal the number of parcels"); fail = true; } assertThat(fail).isTrue(); }
@Test public void validateIncompleteRouteForVehicle() { final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, ImmutableSet.of(p1), 0, null, null); final ImmutableList<Parcel> empty = ImmutableList.of(); final ImmutableList<ImmutableList<Parcel>> routes = ImmutableList .of(empty); final ImmutableSet<Parcel> availableParcels = ImmutableSet.of(); final ImmutableList<VehicleStateObject> vehicles = ImmutableList.of(vs1); final GlobalStateObject state = GlobalStateObject.create(availableParcels, vehicles, 0, SI.SECOND, SI.METERS_PER_SECOND, SI.CENTIMETER, mock(RoadModelSnapshot.class)); boolean fail = false; try { SolverValidator.validateOutputs(routes, state); } catch (final IllegalArgumentException e) { assertThat(e.getMessage()) .contains("doesn't visit all parcels in its cargo"); fail = true; } assertThat(fail).isTrue(); }
@Test public void validateOutputDestinationNotFirstInRoute() { final ImmutableSet<Parcel> empty = ImmutableSet.of(); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, empty, 0, p1, null); final ImmutableList<ImmutableList<Parcel>> routes = ImmutableList .of(ImmutableList.of(p2, p1, p1, p2)); final ImmutableSet<Parcel> availableParcels = ImmutableSet.of(p1, p2); final ImmutableList<VehicleStateObject> vehicles = ImmutableList.of(vs1); final GlobalStateObject state = GlobalStateObject.create(availableParcels, vehicles, 0, SI.SECOND, SI.METERS_PER_SECOND, SI.CENTIMETER, mock(RoadModelSnapshot.class)); boolean fail = false; try { SolverValidator.validateOutputs(routes, state); } catch (final IllegalArgumentException e) { assertThat(e.getMessage()) .contains("should start with its current destination"); fail = true; } assertThat(fail).isTrue(); }
@SuppressWarnings("null") @Test public void validateCorrectOutput() { final ImmutableSet<Parcel> empty = ImmutableSet.of(); final VehicleStateObject vs1 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, empty, 0, null, null); final VehicleStateObject vs2 = VehicleStateObject.create(vdto(), new Point(0, 0), absent, ImmutableSet.of(p3), 0, null, null); final ImmutableList<ImmutableList<Parcel>> routes = ImmutableList.of( ImmutableList.of(p1, p1), ImmutableList.of(p2, p3, p2)); final ImmutableSet<Parcel> availableParcels = ImmutableSet.of(p1, p2); final ImmutableList<VehicleStateObject> vehicles = ImmutableList.of(vs1, vs2); final GlobalStateObject state = GlobalStateObject.create(availableParcels, vehicles, 0, SI.SECOND, SI.METERS_PER_SECOND, SI.CENTIMETER, mock(RoadModelSnapshot.class)); SolverValidator.validateOutputs(routes, state); }