From e6222a50f29162a2de8602bac551bd7d4d1050e2 Mon Sep 17 00:00:00 2001 From: Nikolaus Krismer <niko@krismer.de> Date: Tue, 25 Mar 2014 11:47:31 +0100 Subject: [PATCH] source formatting (sorted methods) reworked method visibility --- .../inf/isochrone/algorithm/Isochrone.java | 240 +++++++++--------- .../inf/isochrone/algorithm/MDijkstra.java | 30 ++- .../unibz/inf/isochrone/algorithm/MineX.java | 32 ++- .../unibz/inf/isochrone/algorithm/MrneX.java | 94 +++---- 4 files changed, 206 insertions(+), 190 deletions(-) diff --git a/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java b/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java index 26a41172..65d93457 100644 --- a/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java +++ b/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java @@ -33,6 +33,8 @@ public abstract class Isochrone { protected Database<ConfigDataset> db; + // Constructors + /** * Instantiates a new isochrone object. * @@ -47,69 +49,32 @@ public abstract class Isochrone { initDateCodes(); } + // Public abstract methods + /** - * Initialize the datecodes for the isochrone. + * Gets a single link from the network. * - * @throws AlgorithmException thrown if there are no date codes for the query time in the database + * @param linkId the id of the requested link + * @return the requested link */ - protected void initDateCodes() throws AlgorithmException { - codes = getDateCodes(query.getTime()); - - if (codes.size() == 0) { - // was solved in uniBz version by switching to unimodal mode... we do not do this any more - // (since user does not get informated about the change) - throw new AlgorithmException("Couldn't find datecodes in the databse for the given date"); - } - } + public abstract Link getLink(int linkId); /** - * Initializes all the nodes from which the isochrone should start. + * Gets a single node from the network. * - * @param nodeIds All the identifiers of the nodes to be initialized. + * @param nodeId the id of the requested node + * @return the node requested */ - private void exploreInitialNodes(final Collection<Integer> nodeIds) { - for (final Integer id : nodeIds) { - updateQueue(initializeNode(id)); - } - } + public abstract Node getNode(int nodeId); /** - * Initializes all the locations from which the isochrone should be calculated. + * Returns all nodes in the network. * - * @param output the output class in which the initial links are stored - * @param locations The locations from which the isochrone should start. + * @return a collection of all nodes stored in the network */ - private void exploreInitialLocations(final Output output, final Collection<Location> locations) { - for (final Location location : locations) { - final Link link = getLink(location.getLinkId()); - final double locationOffset = location.getOffset(); - double distance; - Node node; - - if (query.getDir() == Direction.INCOMING) { - node = getNode(link.getStartNode()); - distance = locationOffset / query.getWalkingSpeed(); - if (locationOffset > 0 && locationOffset < link.getLength()) { - link.setStartOffset(Math.max(0, locationOffset - query.getDuration() * query.getWalkingSpeed())); - link.setEndOffset(locationOffset); - output.addLink(link); - } - } else { - node = getNode(link.getEndNode()); - distance = (link.getLength() - locationOffset) / query.getWalkingSpeed(); - if (locationOffset > 0 && locationOffset < link.getLength()) { - link.setStartOffset(locationOffset); - link.setEndOffset(Math.min(link.getLength(), Math.abs(link.getLength() - locationOffset - query.getDuration() * query.getWalkingSpeed()))); - output.addLink(link); - } - } + public abstract Collection<Node> getNodes(); - if (distance <= query.getDuration() && distance < node.getDistance()) { - node.setDistance(distance); - updateQueue(node); - } - } - } + // Public methods /** * Computes the isochrone. @@ -131,9 +96,9 @@ public abstract class Isochrone { output.beforeCalculation(); if (!query.getNodes().isEmpty()) { - exploreInitialNodes(query.getNodes()); + initializeStartNodes(query.getNodes()); } else { - exploreInitialLocations(output, query.getLocations()); + initializeStartLocations(output, query.getLocations()); } Node node = null; @@ -165,38 +130,14 @@ public abstract class Isochrone { return output; } - /** - * Remove a node from the in-memory network, only used in the - * minex and mrnex algorithms. - * - * @param id The id of the node to be removed - */ - protected abstract void removeNode(int id); - - /** - * Set an updated node into the right place in the priority - * queue. - * - * @param node The node that was changed - */ - private void updateQueue(final Node node) { - if (node != null) { - queue.remove(node); - queue.offer(node); - } - } - /** * Expand the given continuous link (that is connected to the given node) * and adjusts the link offsets based on the distance of the current Node * and the maximum duration of the query. * - * @param node - * current Node - * @param link - * continuous link, that is connected to the Node - * @return another Node, if it can be reached in the available time (or null - * if time has run out) + * @param node current node + * @param link continuous link, that is connected to the node + * @return another node, if it can be reached in the available time (or null if time has run out) */ public Node expandContinuousLink(final Node node, final Link link) { final int duration = query.getDuration().intValue(); @@ -241,9 +182,7 @@ public abstract class Isochrone { * * @param node current Node * @param link discrete link connected to the node - * @return another node, if it can be reached in the avaliable - * time. Or null if there is no time to reach another - * node. + * @return another node, if it can be reached in the available time (or null if there is no time to reach another node) */ public Node expandDiscreteLink(final Node node, final Link link) { final Long duration = query.getDuration(); @@ -270,38 +209,6 @@ public abstract class Isochrone { return null; } - /** - * Initialize a node and returns it. - * - * @param nodeId the id of the node that should be initialized and returned - * @return the initialized node - */ - public abstract Node initializeNode(int nodeId); - - /** - * Gets a single link from the network. - * - * @param linkId the id of the requested link - * @return the requested link - */ - public abstract Link getLink(int linkId); - - /** - * Gets a single node from the network. - * - * @param nodeId the id of the requested node - * @return the node requested - */ - public abstract Node getNode(int nodeId); - - /** - * Gets all links adjacent to a given node. - * - * @param nodeId the id of the node for which the adjacent links should be retrieved. - * @return the adjacent link collection - */ - public abstract Collection<Link> calcAdjLinks(int nodeId); - /** * Get the cost to travel to an adjacent node. * @@ -328,11 +235,108 @@ public abstract class Isochrone { return db.getDateCodes(time); } + // Protected abstract methods /** - * Returns all nodes in the network. + * Gets all links adjacent to a given node. * - * @return A collection of all nodes stored in the network + * @param nodeId the id of the node for which the adjacent links should be retrieved. + * @return the adjacent link collection */ - public abstract Collection<Node> getNodes(); + protected abstract Collection<Link> calcAdjLinks(int nodeId); + + /** + * Initialize a node and returns it. + * + * @param nodeId the id of the node that should be initialized and returned + * @return the initialized node + */ + protected abstract Node initializeNode(int nodeId); + + /** + * Remove a node from the in-memory network, only used in the + * minex and mrnex algorithms. + * + * @param id The id of the node to be removed + */ + protected abstract void removeNode(int id); + + // Private methods + + /** + * Initialize the datecodes for the isochrone. + * + * @throws AlgorithmException thrown if there are no date codes for the query time in the database + */ + private void initDateCodes() throws AlgorithmException { + codes = getDateCodes(query.getTime()); + + if (codes.size() == 0) { + // was solved in uniBz version by switching to unimodal mode... we do not do this any more + // (since user does not get informated about the change) + throw new AlgorithmException("Couldn't find datecodes in the databse for the given date"); + } + } + + /** + * Initializes all the locations from which the isochrone should be calculated. + * + * @param output the output class in which the initial links are stored + * @param locations The locations from which the isochrone should start. + */ + private void initializeStartLocations(final Output output, final Collection<Location> locations) { + for (final Location location : locations) { + final Link link = getLink(location.getLinkId()); + final double locationOffset = location.getOffset(); + double distance; + Node node; + + if (query.getDir() == Direction.INCOMING) { + node = getNode(link.getStartNode()); + distance = locationOffset / query.getWalkingSpeed(); + if (locationOffset > 0 && locationOffset < link.getLength()) { + link.setStartOffset(Math.max(0, locationOffset - query.getDuration() * query.getWalkingSpeed())); + link.setEndOffset(locationOffset); + output.addLink(link); + } + } else { + node = getNode(link.getEndNode()); + distance = (link.getLength() - locationOffset) / query.getWalkingSpeed(); + if (locationOffset > 0 && locationOffset < link.getLength()) { + link.setStartOffset(locationOffset); + link.setEndOffset(Math.min(link.getLength(), Math.abs(link.getLength() - locationOffset - query.getDuration() * query.getWalkingSpeed()))); + output.addLink(link); + } + } + + if (distance <= query.getDuration() && distance < node.getDistance()) { + node.setDistance(distance); + updateQueue(node); + } + } + } + + /** + * Initializes all the nodes from which the isochrone should start. + * + * @param nodeIds All the identifiers of the nodes to be initialized. + */ + private void initializeStartNodes(final Collection<Integer> nodeIds) { + for (final Integer id : nodeIds) { + updateQueue(initializeNode(id)); + } + } + + /** + * Set an updated node into the right place in the priority + * queue. + * + * @param node The node that was changed + */ + private void updateQueue(final Node node) { + if (node != null) { + queue.remove(node); + queue.offer(node); + } + } } diff --git a/src/main/java/it/unibz/inf/isochrone/algorithm/MDijkstra.java b/src/main/java/it/unibz/inf/isochrone/algorithm/MDijkstra.java index f86e5f9d..03022765 100644 --- a/src/main/java/it/unibz/inf/isochrone/algorithm/MDijkstra.java +++ b/src/main/java/it/unibz/inf/isochrone/algorithm/MDijkstra.java @@ -20,6 +20,8 @@ public class MDijkstra extends Isochrone { private final Map<Integer, Link> links; private final Map<Integer, Node> nodes; + // Constructors + public MDijkstra(final ConfigDataset config, final Query query) throws AlgorithmException { super(config, query); @@ -29,13 +31,7 @@ public class MDijkstra extends Isochrone { db.readNetwork(nodes, links); } - @Override - public Node initializeNode(final int nodeId) { - final Node node = getNode(nodeId); - node.setDistance(0.0); - - return node; - } + // Public methods @Override public Link getLink(final int linkId) { @@ -58,7 +54,14 @@ public class MDijkstra extends Isochrone { } @Override - public Collection<Link> calcAdjLinks(final int nodeId) { + public Collection<Node> getNodes() { + return nodes.values(); + } + + // Protected methods + + @Override + protected Collection<Link> calcAdjLinks(final int nodeId) { final Collection<Link> adjLinks = getNode(nodeId).getAdjLinks(); if (adjLinks == null) { return Collections.emptyList(); @@ -68,12 +71,15 @@ public class MDijkstra extends Isochrone { } @Override - public Collection<Node> getNodes() { - return nodes.values(); + protected void removeNode(final int id) { + // Nothing to do here } @Override - protected void removeNode(final int id) { - // Nothing to do here + protected Node initializeNode(final int nodeId) { + final Node node = getNode(nodeId); + node.setDistance(0.0); + + return node; } } diff --git a/src/main/java/it/unibz/inf/isochrone/algorithm/MineX.java b/src/main/java/it/unibz/inf/isochrone/algorithm/MineX.java index 224cdc27..03db5dd6 100644 --- a/src/main/java/it/unibz/inf/isochrone/algorithm/MineX.java +++ b/src/main/java/it/unibz/inf/isochrone/algorithm/MineX.java @@ -10,24 +10,20 @@ import java.util.HashMap; import java.util.Map; /** - * The MineX isochrone algorithm. Loads the nodes and links from the - * database as soon as they are needed. + * The MineX isochrone algorithm. + * Loads the nodes and links from the database as soon as they are needed. */ public class MineX extends Isochrone { private final Map<Integer, Node> nodes; + // Constructors + public MineX(final ConfigDataset config, final Query query) throws AlgorithmException { super(config, query); nodes = new HashMap<Integer, Node>(); } - @Override - public Node initializeNode(final int nodeId) { - final Node node = getNode(nodeId); - node.setDistance(0.0); - - return node; - } + // Public methods @Override public Link getLink(final int linkId) { @@ -59,14 +55,24 @@ public class MineX extends Isochrone { return nodes.values(); } + // Protected methods + @Override - public void removeNode(final int nodeId) { - nodes.remove(nodeId); + protected Collection<Link> calcAdjLinks(final int nodeId) { + return db.getAdjLinks(nodeId, nodes); } @Override - public Collection<Link> calcAdjLinks(final int nodeId) { - return db.getAdjLinks(nodeId, nodes); + protected Node initializeNode(final int nodeId) { + final Node node = getNode(nodeId); + node.setDistance(0.0); + + return node; + } + + @Override + protected void removeNode(final int nodeId) { + nodes.remove(nodeId); } } diff --git a/src/main/java/it/unibz/inf/isochrone/algorithm/MrneX.java b/src/main/java/it/unibz/inf/isochrone/algorithm/MrneX.java index ac966c3c..6430a471 100644 --- a/src/main/java/it/unibz/inf/isochrone/algorithm/MrneX.java +++ b/src/main/java/it/unibz/inf/isochrone/algorithm/MrneX.java @@ -14,8 +14,8 @@ import java.util.List; import java.util.Map; /** - * The MrneX isochrone algorithm. Loads the data in circles - * surrounding the currently explored node. + * The MrneX isochrone algorithm. + * Loads the data in circles surrounding the currently explored node. */ public class MrneX extends Isochrone { private final Map<Integer, List<Link>> adjList; @@ -45,18 +45,58 @@ public class MrneX extends Isochrone { // Public methods @Override - public Collection<Link> calcAdjLinks(final int nodeId) { + public Link getLink(final int linkId) { + final Link link = db.getLink(linkId, nodes); + if (link == null) { + throw new RuntimeException("LinkId not present: " + linkId); + } + + return link; + } + + @Override + public Node getNode(final int nodeId) { + Node node = nodes.get(nodeId); + if (node == null) { + node = db.getNode(nodeId); + if (node == null) { + throw new RuntimeException("NodeId not present: " + nodeId); + } + nodes.put(nodeId, node); + } + + return node; + } + + @Override + public Collection<Node> getNodes() { + return nodes.values(); + } + + /** + * Gets the number of the nodes loaded in memory. + * + * @return the number of nodes in memory + */ + public int getNodeSize() { + return nodes.size(); + } + + // Protected methods + + @Override + protected Collection<Link> calcAdjLinks(final int nodeId) { final Node node = getNode(nodeId); if (adjList.get(nodeId) != null) { /* * Check if adjacent continuous links are empty - * and the node is preexplored. In that case + * and the node is pre-explored. In that case * load the adjacent continuous links, because * density may have been to small, so only the * discrete links were loaded. * * Most likely this is a non-issue, because - * the old code checks if a node is preexplored, + * the old code checks if a node is pre-explored, * but that's always false (java initializes * variables as false and it's never set) and * therefore continuous links are never added @@ -96,45 +136,7 @@ public class MrneX extends Isochrone { } @Override - public Link getLink(final int linkId) { - final Link link = db.getLink(linkId, nodes); - if (link == null) { - throw new RuntimeException("LinkId not present: " + linkId); - } - - return link; - } - - @Override - public Node getNode(final int nodeId) { - Node node = nodes.get(nodeId); - if (node == null) { - node = db.getNode(nodeId); - if (node == null) { - throw new RuntimeException("NodeId not present: " + nodeId); - } - nodes.put(nodeId, node); - } - - return node; - } - - @Override - public Collection<Node> getNodes() { - return nodes.values(); - } - - /** - * Gets the number of the nodes loaded in memory. - * - * @return the number of nodes in memory - */ - public int getNodeSize() { - return nodes.size(); - } - - @Override - public Node initializeNode(final int nodeId) { + protected Node initializeNode(final int nodeId) { final Node node = getNode(nodeId); node.setDistance(0.0); node.setRadius(calcRemainingDistance(node)); @@ -142,12 +144,10 @@ public class MrneX extends Isochrone { } @Override - public void removeNode(final int nodeId) { + protected void removeNode(final int nodeId) { nodes.remove(nodeId); } - // Protected methods - /** * Do we have unlimited memory available (or don't we care * about how much memory we will be using)? -- GitLab