@Override public Optional<MultiAttributeData> read(String data) { final Map<String, String> map = parseDataAsMap(data); final Builder b = MultiAttributeData.builder(); if (map.containsKey(DISTANCE)) { b.setLength(Double.parseDouble(map.get(DISTANCE))); map.remove(DISTANCE); } if (map.containsKey(MAX_SPEED)) { b.setMaxSpeed(Double.parseDouble(map.get(MAX_SPEED))); map.remove(MAX_SPEED); } b.addAllAttributes(map); if (b.getAttributes().isEmpty() && !b.getLength().isPresent() && !b.getMaxSpeed().isPresent()) { return Optional.absent(); } return Optional.of(b.build()); } }
@Override public void handleTimedEvent(ChangeConnectionSpeedEvent event, SimulatorAPI simulator) { final Graph<MultiAttributeData> graph = (Graph<MultiAttributeData>) ((Simulator) simulator) .getModelProvider().getModel(PDPDynamicGraphRoadModel.class) .getGraph(); final MultiAttributeData data = graph.connectionData(event.getFrom(), event.getTo()).get(); graph.setConnectionData( event.getFrom(), event.getTo(), MultiAttributeData.builder() .addAllAttributes(data.getAttributes()) .setLength(data.getLength().get()) .setMaxSpeed(data.getMaxSpeed().get() * event.getFactor()) .build()); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof MultiAttributeData) { MultiAttributeData that = (MultiAttributeData) o; return (this.length.equals(that.getLength())) && (this.maxSpeed.equals(that.getMaxSpeed())) && (this.attributes.equals(that.getAttributes())); } return false; }
@Override public void renderDynamic(GC gc, ViewPort vp, long time) { helper.adapt(gc, vp); if (showRelativeSpeedDynamic) { final Set<Connection<? extends ConnectionData>> set = ImmutableSet.copyOf(updatedConnections); for (final Connection<? extends ConnectionData> e : set) { final double dist = Point.distance(e.from(), e.to()); final Point f = PointUtil.on(e, dist * ARROW_REL_FROM_TO.x); final Point t = PointUtil.on(e, dist * ARROW_REL_FROM_TO.y); final MultiAttributeData data = (MultiAttributeData) e.data().get(); final double ratio = data.getMaxSpeed().get() / Double.parseDouble((String) data.getAttributes().get("ts")); final Color color = new Color(gc.getDevice(), (int) Math.max(Math.min(-510 * ratio + 510, 255), 0), (int) Math.max(Math.min(510 * ratio, 255), 0), 0); helper.setBackgroundSysCol(color); helper.drawArrow(f, t, ARROW_HEAD_SIZE, ARROW_HEAD_SIZE); color.dispose(); } // updatedConnections.clear(); } }
@Override double getSpeed(Graph<?> graph, Point from, Point to) { final MultiAttributeData data = getData(graph, from, to); if (data != null) { final Optional<Double> mxSpeed = data.getMaxSpeed(); if (mxSpeed.isPresent()) { return mxSpeed.get(); } } return defaultMaxSpeed; }
@Override double getSpeed(Graph<?> graph, Point from, Point to) { final MultiAttributeData data = getData(graph, from, to); if (data != null && data.getAttributes() .containsKey(MultiAttributeData.THEORETICAL_SPEED_ATTRIBUTE)) { return Double.parseDouble(data.getAttributes() .get(MultiAttributeData.THEORETICAL_SPEED_ATTRIBUTE) .toString()); } return defaultMaxSpeed; }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof MultiAttributeData) { MultiAttributeData that = (MultiAttributeData) o; return (this.length.equals(that.getLength())) && (this.maxSpeed.equals(that.getMaxSpeed())) && (this.attributes.equals(that.getAttributes())); } return false; }
@Override double getSpeed(Graph<?> graph, Point from, Point to) { final MultiAttributeData data = getData(graph, from, to); if (data != null) { final Optional<Double> mxSpeed = data.getMaxSpeed(); if (mxSpeed.isPresent()) { return mxSpeed.get(); } } return defaultMaxSpeed; }
final MultiAttributeData data = (MultiAttributeData) e.data().get(); final double ratio = data.getMaxSpeed().get() / Double.parseDouble((String) data.getAttributes() .get(MultiAttributeData.THEORETICAL_SPEED_ATTRIBUTE)); final Color color =
@Override double getSpeed(Graph<?> graph, Point from, Point to) { final MultiAttributeData data = getData(graph, from, to); if (data != null && data.getAttributes() .containsKey(MultiAttributeData.THEORETICAL_SPEED_ATTRIBUTE)) { return Double.parseDouble(data.getAttributes() .get(MultiAttributeData.THEORETICAL_SPEED_ATTRIBUTE) .toString()); } return defaultMaxSpeed; }
@Override public Optional<MultiAttributeData> read(String data) { final Map<String, String> map = parseDataAsMap(data); final Builder b = MultiAttributeData.builder(); if (map.containsKey(DISTANCE)) { b.setLength(Double.parseDouble(map.get(DISTANCE))); map.remove(DISTANCE); } if (map.containsKey(MAX_SPEED)) { b.setMaxSpeed(Double.parseDouble(map.get(MAX_SPEED))); map.remove(MAX_SPEED); } b.addAllAttributes(map); if (b.getAttributes().isEmpty() && !b.getLength().isPresent() && !b.getMaxSpeed().isPresent()) { return Optional.absent(); } return Optional.of(b.build()); } }
@Override public void handleTimedEvent(ChangeConnectionSpeedEvent event, SimulatorAPI simulator) { final Graph<MultiAttributeData> graph = (Graph<MultiAttributeData>) ((Simulator) simulator) .getModelProvider().getModel(PDPDynamicGraphRoadModel.class) .getGraph(); final MultiAttributeData data = graph.connectionData(event.getFrom(), event.getTo()).get(); graph.setConnectionData( event.getFrom(), event.getTo(), MultiAttributeData.builder() .addAllAttributes(data.getAttributes()) .setLength(data.getLength().get()) .setMaxSpeed(data.getMaxSpeed().get() * event.getFactor()) .build()); }
@Override public void write(StringBuilder sb, MultiAttributeData data) { final Map<String, String> map = new LinkedHashMap<>(); if (data.getLength().isPresent()) { map.put(DISTANCE, Double.toString(data.getLength().get())); } if (data.getMaxSpeed().isPresent()) { map.put(MAX_SPEED, Double.toString(data.getMaxSpeed().get())); } for (final Entry<String, Object> entry : data.getAttributes() .entrySet()) { checkArgument(!entry.getKey().equals(DISTANCE) && !entry.getKey().equals(MAX_SPEED), "Attribute key: '%s' is reserved and should not be used.", entry.getKey()); map.put(entry.getKey(), entry.getValue().toString()); } if (!map.isEmpty()) { sb.append(DATA_START); Joiner.on(LIST_ITEM_SEPARATOR).withKeyValueSeparator("=") .appendTo(sb, map); sb.append(DATA_END); } }
/** * Compute speed of the object taking into account the speed limits of the * object. * @param object traveling object * @param from The point on the graph the object is located * @param to The next point on the path it wants to reach * @return The maximum speed in the internal unit, must be ≥ 0. */ protected double getMaxSpeed(MovingRoadUser object, Point from, Point to) { final double objSpeed = unitConversion.toInSpeed(object.getSpeed()); if (!from.equals(to)) { final Connection<?> conn = getConnection(from, to); if (conn.data().isPresent() && conn.data().get() instanceof MultiAttributeData) { final MultiAttributeData maed = (MultiAttributeData) conn.data().get(); if (maed.getMaxSpeed().isPresent()) { return Math.min(unitConversion.toInSpeed(maed.getMaxSpeed().get()), objSpeed); } return objSpeed; } } return objSpeed; }
/** * The fastest path changes based on the maximal allowed speed */ @Test public void fastestPathConnData() { final Graph<MultiAttributeData> attributeGraph = new TableGraph<>(); Point A, B, C, D; A = new Point(0, 0); B = new Point(0, 10); C = new Point(10, 10); D = new Point(10, 0); attributeGraph.addConnection(A, B, MultiAttributeData.builder().setMaxSpeed(2).build()); attributeGraph.addConnection(B, C, MultiAttributeData.builder().setMaxSpeed(2).build()); attributeGraph.addConnection(A, D, MultiAttributeData.builder().setMaxSpeed(1).build()); attributeGraph.addConnection(D, C, MultiAttributeData.builder().setMaxSpeed(1).build()); assertEquals(asList(A, B, C), Graphs.shortestPath(attributeGraph, A, C, GeomHeuristics.time(50d))); attributeGraph.setConnectionData(A, D, MultiAttributeData.builder().setMaxSpeed(10).build()); attributeGraph.setConnectionData(D, C, MultiAttributeData.builder().setMaxSpeed(10).build()); assertEquals(asList(A, D, C), Graphs.shortestPath(attributeGraph, A, C, GeomHeuristics.time(50d))); }
@Override public void write(StringBuilder sb, MultiAttributeData data) { final Map<String, String> map = new LinkedHashMap<>(); if (data.getLength().isPresent()) { map.put(DISTANCE, Double.toString(data.getLength().get())); } if (data.getMaxSpeed().isPresent()) { map.put(MAX_SPEED, Double.toString(data.getMaxSpeed().get())); } for (final Entry<String, Object> entry : data.getAttributes() .entrySet()) { checkArgument(!entry.getKey().equals(DISTANCE) && !entry.getKey().equals(MAX_SPEED), "Attribute key: '%s' is reserved and should not be used.", entry.getKey()); map.put(entry.getKey(), entry.getValue().toString()); } if (!map.isEmpty()) { sb.append(DATA_START); Joiner.on(LIST_ITEM_SEPARATOR).withKeyValueSeparator("=") .appendTo(sb, map); sb.append(DATA_END); } }
RoadModels.computeTravelTime( Measure.valueOf( ((MultiAttributeData) conn.data().get()).getMaxSpeed().get(), roadModel.getSpeedUnit()), distance, timeUnit));
MultiAttributeData.builder().setMaxSpeed(100).build()); connB = Connection.create(B, C, MultiAttributeData.builder().setMaxSpeed(100).build()); connC = Connection.create(C, D, MultiAttributeData.builder().setMaxSpeed(100).build()); connD = Connection.create(D, E, MultiAttributeData.builder().setMaxSpeed(100).build()); connE = Connection.create(E, F, MultiAttributeData.builder().setMaxSpeed(100).build());
/** * Tests reading a dot file in legacy format. * @throws IOException In case of IO problems. */ @SuppressWarnings("static-method") @Test public void testLegacy() throws IOException { final Graph<LengthData> ldGraph = DotGraphIO.getLengthGraphIO().read( new StringReader(Joiner.on("\n").join(LEGACY_FORMAT))); final Graph<MultiAttributeData> maGraph = DotGraphIO.getMultiAttributeGraphIO() .read(new StringReader(Joiner.on("\n").join(LEGACY_FORMAT))); testLegacyFormat(ldGraph); testLegacyFormat(maGraph); final Point n0 = new Point(3296724.2131123254, 2.5725043247255992E7); final Point n19663 = new Point(3296782.7337179, 2.5724994399343655E7); for (final Connection<MultiAttributeData> conn : maGraph.getConnections()) { if (conn.from().equals(n0) && conn.to().equals(n19663)) { assertFalse(conn.data().get().getMaxSpeed().isPresent()); } else { assertEquals(50000d, conn.data().get().getMaxSpeed().get() .doubleValue(), 0); } } }
MultiAttributeData.builder().setMaxSpeed(100).build()); connB = Connection.create(B, C, MultiAttributeData.builder().setMaxSpeed(100).build()); connC = Connection.create(C, D, MultiAttributeData.builder().setMaxSpeed(100).build()); connD = Connection.create(D, E, MultiAttributeData.builder().setMaxSpeed(100).build());