From 32f99dba49cfcae7d7cef58eee30a8255b71a97f Mon Sep 17 00:00:00 2001 From: Le Botlan Didier Date: Fri, 22 Mar 2024 17:07:18 +0100 Subject: [PATCH] Nodes are no longer comparable (confusion with Label comparison). Predefined ArcInspectors easier to get. --- .../graphs/algorithm/ArcInspectorFactory.java | 295 ++++++++---------- .../graphs/algorithm/utils/BinaryHeap.java | 2 +- .../gui/drawing/components/BasicDrawing.java | 2 +- .../main/java/org/insa/graphs/model/Node.java | 11 +- 4 files changed, 144 insertions(+), 166 deletions(-) diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/ArcInspectorFactory.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/ArcInspectorFactory.java index ab42c7d..ddbcb63 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/ArcInspectorFactory.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/ArcInspectorFactory.java @@ -12,6 +12,137 @@ import org.insa.graphs.model.AccessRestrictions.AccessRestriction; public class ArcInspectorFactory { + + // No filter (all arcs allowed): + static ArcInspector allArcsL = new ArcInspector() { + @Override + public boolean isAllowed(Arc arc) { + return true; + } + + @Override + public double getCost(Arc arc) { + return arc.getLength(); + } + + /* + @Override + public int getMaximumSpeed() { + return GraphStatistics.NO_MAXIMUM_SPEED; + } + */ + + @Override + public Mode getMode() { + return Mode.LENGTH; + } + + @Override + public String toString() { + return "Shortest path, all roads allowed"; + } + } ; + + // Only road allowed for cars and length: + static ArcInspector forCarsL = new ArcInspector() { + + @Override + public boolean isAllowed(Arc arc) { + return arc.getRoadInformation().getAccessRestrictions() + .isAllowedForAny(AccessMode.MOTORCAR, EnumSet.complementOf(EnumSet + .of(AccessRestriction.FORBIDDEN, AccessRestriction.PRIVATE))); + } + + @Override + public double getCost(Arc arc) { + return arc.getLength(); + } + + /* + @Override + public int getMaximumSpeed() { + return GraphStatistics.NO_MAXIMUM_SPEED; + } + */ + + @Override + public Mode getMode() { + return Mode.LENGTH; + } + + @Override + public String toString() { + return "Shortest path, only roads open for cars"; + } + } ; + + // Only road allowed for cars and time: + static ArcInspector forCarsT = new ArcInspector() { + + @Override + public boolean isAllowed(Arc arc) { + return true; + } + + @Override + public double getCost(Arc arc) { + return arc.getMinimumTravelTime() ; + } + + /* + @Override + public int getMaximumSpeed() { + return GraphStatistics.NO_MAXIMUM_SPEED; + } + */ + + @Override + public Mode getMode() { + return Mode.TIME ; + } + + @Override + public String toString() { + return "Fastest path, all roads allowed"; + } + } ; + + // Non-private roads for pedestrian and bicycle: + static ArcInspector forBicyclesT = new ArcInspector() { + + static final int maxPedestrianSpeed = 5 ; + + @Override + public boolean isAllowed(Arc arc) { + return arc.getRoadInformation().getAccessRestrictions() + .isAllowedForAny(AccessMode.FOOT, EnumSet.complementOf(EnumSet + .of(AccessRestriction.FORBIDDEN, AccessRestriction.PRIVATE))); + } + + @Override + public double getCost(Arc arc) { + return arc.getTravelTime( + Math.min(maxPedestrianSpeed, arc.getRoadInformation().getMaximumSpeed())); + } + + @Override + public String toString() { + return "Fastest path for pedestrian"; + } + + /* + @Override + public int getMaximumSpeed() { + return 5; + } + */ + + @Override + public Mode getMode() { + return Mode.TIME; + } + } ; + /** * @return List of all arc filters in this factory. */ @@ -20,165 +151,11 @@ public class ArcInspectorFactory { // Common filters: - // No filter (all arcs allowed): - filters.add(new ArcInspector() { - @Override - public boolean isAllowed(Arc arc) { - return true; - } - - @Override - public double getCost(Arc arc) { - return arc.getLength(); - } - - /* - @Override - public int getMaximumSpeed() { - return GraphStatistics.NO_MAXIMUM_SPEED; - } - */ - - @Override - public Mode getMode() { - return Mode.LENGTH; - } - - @Override - public String toString() { - return "Shortest path, all roads allowed"; - } - }); - - // Only road allowed for cars and length: - filters.add(new ArcInspector() { - @Override - public boolean isAllowed(Arc arc) { - return arc.getRoadInformation().getAccessRestrictions() - .isAllowedForAny(AccessMode.MOTORCAR, EnumSet.complementOf(EnumSet - .of(AccessRestriction.FORBIDDEN, AccessRestriction.PRIVATE))); - } - - @Override - public double getCost(Arc arc) { - return arc.getLength(); - } - - /* - @Override - public int getMaximumSpeed() { - return GraphStatistics.NO_MAXIMUM_SPEED; - } - */ - - @Override - public Mode getMode() { - return Mode.LENGTH; - } - - @Override - public String toString() { - return "Shortest path, only roads open for cars"; - } - }); - - // Only road allowed for cars and time: - - filters.add(new ArcInspector() { - @Override - public boolean isAllowed(Arc arc) { - return true; - } - - @Override - public double getCost(Arc arc) { - return arc.getMinimumTravelTime(); - } - - /* - @Override - public int getMaximumSpeed() { - return GraphStatistics.NO_MAXIMUM_SPEED; - } - */ - - @Override - public Mode getMode() { - return Mode.TIME; - } - - @Override - public String toString() { - return "Fastest path, all roads allowed"; - } - }); - - filters.add(new ArcInspector() { - @Override - public boolean isAllowed(Arc arc) { - return arc.getRoadInformation().getAccessRestrictions() - .isAllowedForAny(AccessMode.MOTORCAR, EnumSet.complementOf(EnumSet - .of(AccessRestriction.FORBIDDEN, AccessRestriction.PRIVATE))); - } - - @Override - public double getCost(Arc arc) { - return arc.getMinimumTravelTime(); - } - - /* - @Override - public int getMaximumSpeed() { - return GraphStatistics.NO_MAXIMUM_SPEED; - } - */ - - @Override - public Mode getMode() { - return Mode.TIME; - } - - @Override - public String toString() { - return "Fastest path, only roads open for cars"; - } - }); - - // Non-private roads for pedestrian and bicycle: - filters.add(new ArcInspector() { - - static final int maxPedestrianSpeed = 5 ; - - @Override - public boolean isAllowed(Arc arc) { - return arc.getRoadInformation().getAccessRestrictions() - .isAllowedForAny(AccessMode.FOOT, EnumSet.complementOf(EnumSet - .of(AccessRestriction.FORBIDDEN, AccessRestriction.PRIVATE))); - } - - @Override - public double getCost(Arc arc) { - return arc.getTravelTime( - Math.min(maxPedestrianSpeed, arc.getRoadInformation().getMaximumSpeed())); - } - - @Override - public String toString() { - return "Fastest path for pedestrian"; - } - - /* - @Override - public int getMaximumSpeed() { - return 5; - } - */ - - @Override - public Mode getMode() { - return Mode.TIME; - } - }); + + filters.add(allArcsL) ; + filters.add(forCarsL) ; + filters.add(forCarsT) ; + filters.add(forBicyclesT); // Add your own filters here (do not forget to implement toString() // to get an understandable output!): diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java index 2c1a239..dd66c26 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java @@ -16,7 +16,7 @@ public class BinaryHeap> implements PriorityQueue { // Number of elements in heap. private int currentSize; - // The heap array. + // The heap array, which can be larger than currentSize. protected final ArrayList array; /** diff --git a/be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/components/BasicDrawing.java b/be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/components/BasicDrawing.java index 6d51a05..5c54600 100644 --- a/be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/components/BasicDrawing.java +++ b/be-graphes-gui/src/main/java/org/insa/graphs/gui/drawing/components/BasicDrawing.java @@ -684,7 +684,7 @@ public class BasicDrawing extends JPanel implements Drawing { // Draw arcs only if there are one-way arcs or if origin is lower than // destination, avoid drawing two-ways arc twice. if (arc.getRoadInformation().isOneWay() - || arc.getOrigin().compareTo(arc.getDestination()) < 0) { + || arc.getOrigin().getId() < arc.getDestination().getId()) { drawArc(arc, palette, false); } } diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/Node.java b/be-graphes-model/src/main/java/org/insa/graphs/model/Node.java index 37f20b0..cca2434 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/Node.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/Node.java @@ -19,7 +19,7 @@ import java.util.List; *

* */ -public final class Node implements Comparable { +public final class Node { //implements Comparable { /** *

@@ -144,6 +144,7 @@ public final class Node implements Comparable { return false; } + // Do we really need to compare nodes ? Confusion with Label comparisons. /** * Compare the ID of this node with the ID of the given node. * @@ -151,9 +152,9 @@ public final class Node implements Comparable { * * @see java.lang.Comparable#compareTo(java.lang.Object) */ - @Override - public int compareTo(Node other) { - return Integer.compare(getId(), other.getId()); - } + // @Override + // public int compareTo(Node other) { + // return Integer.compare(getId(), other.getId()); + // } }