@Override public Optional<LengthData> read(String data) { final Map<String, String> map = parseDataAsMap(data); if (map.containsKey(DISTANCE)) { final double len = Double.parseDouble(map.get(DISTANCE) .replaceAll(QUOTE, "")); return Optional.of(LengthData.create(len)); } return Optional.absent(); } }
@Override public Optional<LengthData> read(String data) { final Map<String, String> map = parseDataAsMap(data); if (map.containsKey(DISTANCE)) { final double len = Double.parseDouble(map.get(DISTANCE) .replaceAll(QUOTE, "")); return Optional.of(LengthData.create(len)); } return Optional.absent(); } }
/** * Test a change of a conn such that it becomes too short. */ @Test public void testDetectChangeConnInvalidLive() { // this is allowed graph.setConnectionData(NW, SW, LengthData.create(1d)); boolean fail = false; try { // this is too short graph.setConnectionData(NW, SW, LengthData.create(0.99)); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof LengthData) { LengthData that = (LengthData) o; return (this.length.equals(that.getLength())); } return false; }
@Test public void setConnData() { final Point N = new Point(0, 5); final Point E = new Point(5, 0); final Point S = new Point(0, -5); final Point W = new Point(-5, 0); Graphs.addBiPath(graph, N, E, S, W, N); assertFalse(graph.setConnectionData(N, E, LengthData.create(100)) .isPresent()); assertEquals(LengthData.create(100), graph.removeConnectionData(N, E).get()); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof LengthData) { LengthData that = (LengthData) o; return (this.length.equals(that.getLength())); } return false; }
/** * The shortest path changes based on the connection data. */ @Test public void shortestPathConnData() { final Point a = new Point(0, 0); final Point b = new Point(10, 0); final Point c = new Point(5, 5); Graphs.addBiPath(graph, a, b, c, a); assertEquals(asList(a, b), Graphs.shortestPathEuclideanDistance(graph, a, b)); graph.setConnectionData(a, c, LengthData.create(1d)); graph.setConnectionData(c, b, LengthData.create(1d)); assertEquals(asList(a, c, b), Graphs.shortestPathEuclideanDistance(graph, a, b)); }
@Parameters public static Collection<Object[]> configs() { // Graph should be constructed before model, // since GraphRoadModelSnapshot should be consistent // with the actual graph in a static case. final ImmutableMultimap graphAsMap = ImmutableMultimap.builder().put(SW, SE).put(SE, NE).put(NE, NW).build(); final Table<Point, Point, Connection<LengthData>> graphAsTable = HashBasedTable.create(); graphAsTable.put(SW, SE, Connection.create(SW, SE, LengthData.create(10))); graphAsTable.put(SE, NE, Connection.create(SE, NE, LengthData.create(10))); graphAsTable.put(NE, NW, Connection.create(NE, NW, LengthData.create(10))); return Arrays.asList(new Object[][] { {RoadModelBuilders.staticGraph(MultimapGraph.supplier(graphAsMap))}, {RoadModelBuilders.staticGraph(MultimapGraph.supplier(graphAsMap)) .withCache()}, {RoadModelBuilders .staticGraph(TableGraph.<LengthData>supplier(ImmutableTable .<Point, Point, Connection<LengthData>>copyOf(graphAsTable)))}, {RoadModelBuilders .staticGraph(TableGraph.supplier(ImmutableTable .<Point, Point, Connection<LengthData>>copyOf(graphAsTable))) .withCache()}, {RoadModelBuilders.dynamicGraph(ListenableGraph .supplier(TableGraph.supplier(ImmutableTable .<Point, Point, Connection<LengthData>>copyOf(graphAsTable))))} }); }
assertEquals(asList(ADD_CONNECTION), history.getEventTypeHistory()); graph.addConnection(b, a, LengthData.create(2)); assertEquals(asList(ADD_CONNECTION, ADD_CONNECTION), history.getEventTypeHistory()); Connection.create(a, d, LengthData.create(10d)), Connection.create(d, e, LengthData.create(7d)))); assertEquals( asList(ADD_CONNECTION, ADD_CONNECTION, ADD_CONNECTION, ADD_CONNECTION), history.getEventTypeHistory()); graph.setConnectionData(a, d, LengthData.create(16d)); assertEquals( asList(ADD_CONNECTION, ADD_CONNECTION, ADD_CONNECTION, ADD_CONNECTION,
@Test public void connDataUsage() { final Point A = new Point(0, 0), B = new Point(0, 1), C = new Point(1, 0); graph.addConnection(A, B); graph.addConnection(Connection.create(B, A, LengthData.create(1.5))); graph.addConnection(B, C, LengthData.create(2)); // explicit empty value graph.addConnection(A, C); assertFalse("existing but empty", graph.connectionData(A, B).isPresent()); assertFalse("non existing", graph.connectionData(C, A).isPresent()); assertTrue("existing B->A", graph.connectionData(B, A).isPresent()); assertTrue("existing B->C", graph.connectionData(B, C).isPresent()); // use of the connection data assertEquals(1, graph.connectionLength(A, B), DELTA); assertEquals(1.5, graph.connectionLength(B, A), DELTA); assertEquals(2, graph.connectionLength(B, C), DELTA); try { graph.connectionLength(C, B); fail(); } catch (final IllegalArgumentException e) {} }
@SuppressWarnings("unchecked") @Test public void getMaxSpeed() { final Point A = new Point(0, 0); final Point B = new Point(10, 0); final Graph<LengthData> g = new MultimapGraph<>(); final GraphRoadModelImpl rm = RoadModelBuilders.staticGraph(g).build( mock(DependencyProvider.class)); g.addConnection(A, B, LengthData.create(3)); final UnitConverter conv = NonSI.KILOMETERS_PER_HOUR .getConverterTo(RoadUnits.INTERNAL_SPEED_UNIT); assertEquals(conv.convert(10), rm.getMaxSpeed(new SpeedyRoadUser(10), A, B), EPSILON); ((Graph<MultiAttributeData>) graph).addConnection(SE, SW, MultiAttributeData.builder().setLength(3).setMaxSpeed(5d).build()); assertEquals(conv.convert(5), model.getMaxSpeed(new SpeedyRoadUser(10), SE, SW), EPSILON); ((Graph<MultiAttributeData>) graph).addConnection(NE, SE, MultiAttributeData.builder().setLength(3).build()); assertEquals(conv.convert(10), model.getMaxSpeed(new SpeedyRoadUser(10), NE, SE), EPSILON); }
simpleLDGraph.addConnection(a, c, LengthData.create(10));