From 3e6bcb0665acd8f619390734693050a3f7c6ee51 Mon Sep 17 00:00:00 2001 From: Mikael Capelle Date: Fri, 9 Mar 2018 14:12:21 +0100 Subject: [PATCH 1/4] Start updating length (int -> float). --- src/main/org/insa/graph/Arc.java | 2 +- src/main/org/insa/graph/ArcBackward.java | 2 +- src/main/org/insa/graph/ArcForward.java | 6 +++--- src/main/org/insa/graph/GraphStatistics.java | 6 +++--- src/main/org/insa/graph/Node.java | 2 +- src/main/org/insa/graph/io/BinaryGraphReader.java | 10 ++++++++-- src/main/org/insa/graphics/PathsPanel.java | 4 ++-- 7 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main/org/insa/graph/Arc.java b/src/main/org/insa/graph/Arc.java index 5140884..5eed3fe 100644 --- a/src/main/org/insa/graph/Arc.java +++ b/src/main/org/insa/graph/Arc.java @@ -31,7 +31,7 @@ public interface Arc { /** * @return Length of this arc, in meters. */ - public int getLength(); + public float getLength(); /** * @return Minimum time required to travel this arc, in seconds. diff --git a/src/main/org/insa/graph/ArcBackward.java b/src/main/org/insa/graph/ArcBackward.java index 4a0aaaf..0c87bf0 100644 --- a/src/main/org/insa/graph/ArcBackward.java +++ b/src/main/org/insa/graph/ArcBackward.java @@ -37,7 +37,7 @@ class ArcBackward implements Arc { } @Override - public int getLength() { + public float getLength() { return this.originalArc.getLength(); } diff --git a/src/main/org/insa/graph/ArcForward.java b/src/main/org/insa/graph/ArcForward.java index 4f906c2..edd891a 100644 --- a/src/main/org/insa/graph/ArcForward.java +++ b/src/main/org/insa/graph/ArcForward.java @@ -15,7 +15,7 @@ class ArcForward implements Arc { private final Node origin, destination; // Length of the road (in meters). - private final int length; + private final float length; // Road information. private final RoadInformation info; @@ -32,7 +32,7 @@ class ArcForward implements Arc { * @param roadInformation Road information for this arc. * @param points Points representing this arc. */ - protected ArcForward(Node origin, Node dest, int length, RoadInformation roadInformation, + protected ArcForward(Node origin, Node dest, float length, RoadInformation roadInformation, ArrayList points) { this.origin = origin; this.destination = dest; @@ -53,7 +53,7 @@ class ArcForward implements Arc { @Override - public int getLength() { + public float getLength() { return length; } diff --git a/src/main/org/insa/graph/GraphStatistics.java b/src/main/org/insa/graph/GraphStatistics.java index fa6d115..9e98023 100644 --- a/src/main/org/insa/graph/GraphStatistics.java +++ b/src/main/org/insa/graph/GraphStatistics.java @@ -14,7 +14,7 @@ public class GraphStatistics { private final int maximumSpeed; // Maximum length of any arc on this graph. - private final int maximumLength; + private final float maximumLength; /** * Create a new GraphStatistics instance with the given value. @@ -23,7 +23,7 @@ public class GraphStatistics { * be used to indicate that this graph has no maximum limitation. * @param maximumLength Maximum length of any arc of the graph. */ - public GraphStatistics(int maximumSpeed, int maximumLength) { + public GraphStatistics(int maximumSpeed, float maximumLength) { this.maximumLength = maximumLength; this.maximumSpeed = maximumSpeed; } @@ -39,7 +39,7 @@ public class GraphStatistics { /** * @return Maximum length of any arc in the graph. */ - public int getMaximumLength() { + public float getMaximumLength() { return this.maximumLength; } diff --git a/src/main/org/insa/graph/Node.java b/src/main/org/insa/graph/Node.java index 9620cfe..33ee579 100644 --- a/src/main/org/insa/graph/Node.java +++ b/src/main/org/insa/graph/Node.java @@ -31,7 +31,7 @@ public class Node implements Comparable { * * @return The newly created forward arc (origin to destination). */ - public static Arc linkNodes(Node origin, Node destination, int length, + public static Arc linkNodes(Node origin, Node destination, float length, RoadInformation roadInformation, ArrayList points) { ArcForward arc = new ArcForward(origin, destination, length, roadInformation, points); origin.addSuccessor(arc); diff --git a/src/main/org/insa/graph/io/BinaryGraphReader.java b/src/main/org/insa/graph/io/BinaryGraphReader.java index 5e15450..4495c27 100644 --- a/src/main/org/insa/graph/io/BinaryGraphReader.java +++ b/src/main/org/insa/graph/io/BinaryGraphReader.java @@ -212,7 +212,7 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader { checkByteOrThrow(254); // Read successors and convert to arcs. - int maxLength = 0; + float maxLength = 0; final int copyNbTotalSuccesors = nbTotalSuccessors; // Stupid Java... observers.forEach((observer) -> observer.notifyStartReadingArcs(copyNbTotalSuccesors)); for (int node = 0; node < nbNodes; ++node) { @@ -225,7 +225,13 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader { int descrNum = this.read24bits(); // Length of the arc. - int length = dis.readUnsignedShort(); + float length; + if (getCurrentVersion() < 8) { + length = dis.readUnsignedShort(); + } + else { + length = dis.readInt() / 1000.0f; + } maxLength = Math.max(length, maxLength); // Number of segments. diff --git a/src/main/org/insa/graphics/PathsPanel.java b/src/main/org/insa/graphics/PathsPanel.java index 7bb0f6e..fcb8301 100644 --- a/src/main/org/insa/graphics/PathsPanel.java +++ b/src/main/org/insa/graphics/PathsPanel.java @@ -126,9 +126,9 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh String info = ""; // Display length - int length = path.getLength(); + float length = path.getLength(); if (length < 2000) { - info += String.format("Length = %d meters", length); + info += String.format("Length = %.1f meters", length); } else { info += String.format("Length = %.3f kilometers", length / 1000.); From a765e58f847c50da32db9d1cf3352c82fdf51462 Mon Sep 17 00:00:00 2001 From: Mikael Capelle Date: Fri, 9 Mar 2018 21:07:08 +0100 Subject: [PATCH 2/4] Fix issue with path test. --- src/test/org/insa/graph/PathTest.java | 54 +++++++++++++-------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/test/org/insa/graph/PathTest.java b/src/test/org/insa/graph/PathTest.java index b74a0db..ee4d327 100644 --- a/src/test/org/insa/graph/PathTest.java +++ b/src/test/org/insa/graph/PathTest.java @@ -22,7 +22,7 @@ public class PathTest { // List of arcs in the graph, a2b is the arc from node A (0) to B (1). @SuppressWarnings("unused") - private static ArcForward a2b, a2c, a2e, b2c, c2d_1, c2d_2, c2d_3, c2a, d2a, d2e, e2d; + private static Arc a2b, a2c, a2e, b2c, c2d_1, c2d_2, c2d_3, c2a, d2a, d2e, e2d; // Some paths... private static Path emptyPath, shortPath, longPath, loopPath, longLoopPath, invalidPath; @@ -41,26 +41,26 @@ public class PathTest { } // Add arcs... - a2b = new ArcForward(nodes[0], nodes[1], 10, speed10, null); - a2c = new ArcForward(nodes[0], nodes[2], 15, speed10, null); - a2e = new ArcForward(nodes[0], nodes[4], 15, speed20, null); - b2c = new ArcForward(nodes[1], nodes[2], 10, speed10, null); - c2d_1 = new ArcForward(nodes[2], nodes[3], 20, speed10, null); - c2d_2 = new ArcForward(nodes[2], nodes[3], 10, speed10, null); - c2d_3 = new ArcForward(nodes[2], nodes[3], 15, speed20, null); - d2a = new ArcForward(nodes[3], nodes[0], 15, speed10, null); - d2e = new ArcForward(nodes[3], nodes[4], 20, speed20, null); - e2d = new ArcForward(nodes[4], nodes[0], 10, speed10, null); + a2b = Node.linkNodes(nodes[0], nodes[1], 10, speed10, null); + a2c = Node.linkNodes(nodes[0], nodes[2], 15, speed10, null); + a2e = Node.linkNodes(nodes[0], nodes[4], 15, speed20, null); + b2c = Node.linkNodes(nodes[1], nodes[2], 10, speed10, null); + c2d_1 = Node.linkNodes(nodes[2], nodes[3], 20, speed10, null); + c2d_2 = Node.linkNodes(nodes[2], nodes[3], 10, speed10, null); + c2d_3 = Node.linkNodes(nodes[2], nodes[3], 15, speed20, null); + d2a = Node.linkNodes(nodes[3], nodes[0], 15, speed10, null); + d2e = Node.linkNodes(nodes[3], nodes[4], 20, speed20, null); + e2d = Node.linkNodes(nodes[4], nodes[0], 10, speed10, null); graph = new Graph("ID", "", Arrays.asList(nodes), null); emptyPath = new Path(graph, new ArrayList()); - shortPath = new Path(graph, Arrays.asList(new ArcForward[]{ a2b, b2c, c2d_1 })); - longPath = new Path(graph, Arrays.asList(new ArcForward[]{ a2b, b2c, c2d_1, d2e })); - loopPath = new Path(graph, Arrays.asList(new ArcForward[]{ a2b, b2c, c2d_1, d2a })); + shortPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1 })); + longPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2e })); + loopPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2a })); longLoopPath = new Path(graph, - Arrays.asList(new ArcForward[]{ a2b, b2c, c2d_1, d2a, a2c, c2d_3, d2a, a2b, b2c })); - invalidPath = new Path(graph, Arrays.asList(new ArcForward[]{ a2b, c2d_1, d2e })); + Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2a, a2c, c2d_3, d2a, a2b, b2c })); + invalidPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, c2d_1, d2e })); } @@ -103,11 +103,11 @@ public class PathTest { @Test public void testGetLength() { - assertEquals(emptyPath.getLength(), 0); - assertEquals(shortPath.getLength(), 40); - assertEquals(longPath.getLength(), 60); - assertEquals(loopPath.getLength(), 55); - assertEquals(longLoopPath.getLength(), 120); + assertEquals(emptyPath.getLength(), 0, 1e-6); + assertEquals(shortPath.getLength(), 40, 1e-6); + assertEquals(longPath.getLength(), 60, 1e-6); + assertEquals(loopPath.getLength(), 55, 1e-6); + assertEquals(longLoopPath.getLength(), 120, 1e-6); } @Test @@ -122,12 +122,12 @@ public class PathTest { @Test public void testCreateFastestPathFromNodes() { Path path; - ArcForward[] expected; + Arc[] expected; // Simple construction path = Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2] })); - expected = new ArcForward[]{ a2b, b2c }; + expected = new Arc[]{ a2b, b2c }; assertEquals(expected.length, path.getArcs().size()); for (int i = 0; i < expected.length; ++i) { assertEquals(expected[i], path.getArcs().get(i)); @@ -136,7 +136,7 @@ public class PathTest { // Not so simple construction path = Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2], nodes[3] })); - expected = new ArcForward[]{ a2b, b2c, c2d_3 }; + expected = new Arc[]{ a2b, b2c, c2d_3 }; assertEquals(expected.length, path.getArcs().size()); for (int i = 0; i < expected.length; ++i) { assertEquals(expected[i], path.getArcs().get(i)); @@ -146,12 +146,12 @@ public class PathTest { @Test public void testCreateShortestPathFromNodes() { Path path; - ArcForward[] expected; + Arc[] expected; // Simple construction path = Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2] })); - expected = new ArcForward[]{ a2b, b2c }; + expected = new Arc[]{ a2b, b2c }; assertEquals(expected.length, path.getArcs().size()); for (int i = 0; i < expected.length; ++i) { assertEquals(expected[i], path.getArcs().get(i)); @@ -160,7 +160,7 @@ public class PathTest { // Not so simple construction path = Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2], nodes[3] })); - expected = new ArcForward[]{ a2b, b2c, c2d_2 }; + expected = new Arc[]{ a2b, b2c, c2d_2 }; assertEquals(expected.length, path.getArcs().size()); for (int i = 0; i < expected.length; ++i) { assertEquals(expected[i], path.getArcs().get(i)); From 7dcbdcb408d0aa901a4729f8b36ee85d08eca806 Mon Sep 17 00:00:00 2001 From: Mikael Capelle Date: Fri, 9 Mar 2018 21:07:49 +0100 Subject: [PATCH 3/4] Correct typing error. --- src/main/org/insa/algo/AbstractInputData.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/org/insa/algo/AbstractInputData.java b/src/main/org/insa/algo/AbstractInputData.java index 655a31a..4eff040 100644 --- a/src/main/org/insa/algo/AbstractInputData.java +++ b/src/main/org/insa/algo/AbstractInputData.java @@ -20,7 +20,7 @@ public abstract class AbstractInputData { } /** - * Filtering inteface for arcs - This class can be used to indicate to an + * Filtering interface for arcs - This class can be used to indicate to an * algorithm which arc can be used. * */ From f2bdc462a3f4622ca205d0fb0c1036f05c838106 Mon Sep 17 00:00:00 2001 From: Mikael Capelle Date: Fri, 9 Mar 2018 21:08:00 +0100 Subject: [PATCH 4/4] Fix .equals for Node. --- src/main/org/insa/graph/Node.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/org/insa/graph/Node.java b/src/main/org/insa/graph/Node.java index 33ee579..8aadf84 100644 --- a/src/main/org/insa/graph/Node.java +++ b/src/main/org/insa/graph/Node.java @@ -95,6 +95,9 @@ public class Node implements Comparable { @Override public boolean equals(Object other) { + if (other == null) { + return false; + } if (other instanceof Node) { return getId() == ((Node) other).getId(); }