Skip to content
Snippets Groups Projects
Commit e6222a50 authored by Nikolaus Krismer's avatar Nikolaus Krismer
Browse files

source formatting (sorted methods)

reworked method visibility
parent ed612273
No related branches found
No related tags found
No related merge requests found
...@@ -33,6 +33,8 @@ public abstract class Isochrone { ...@@ -33,6 +33,8 @@ public abstract class Isochrone {
protected Database<ConfigDataset> db; protected Database<ConfigDataset> db;
// Constructors
/** /**
* Instantiates a new isochrone object. * Instantiates a new isochrone object.
* *
...@@ -47,69 +49,32 @@ public abstract class Isochrone { ...@@ -47,69 +49,32 @@ public abstract class Isochrone {
initDateCodes(); 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 { public abstract Link getLink(int linkId);
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 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) { public abstract Node getNode(int nodeId);
for (final Integer id : nodeIds) {
updateQueue(initializeNode(id));
}
}
/** /**
* 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 * @return a collection of all nodes stored in the network
* @param locations The locations from which the isochrone should start.
*/ */
private void exploreInitialLocations(final Output output, final Collection<Location> locations) { public abstract Collection<Node> getNodes();
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()) { // Public methods
node.setDistance(distance);
updateQueue(node);
}
}
}
/** /**
* Computes the isochrone. * Computes the isochrone.
...@@ -131,9 +96,9 @@ public abstract class Isochrone { ...@@ -131,9 +96,9 @@ public abstract class Isochrone {
output.beforeCalculation(); output.beforeCalculation();
if (!query.getNodes().isEmpty()) { if (!query.getNodes().isEmpty()) {
exploreInitialNodes(query.getNodes()); initializeStartNodes(query.getNodes());
} else { } else {
exploreInitialLocations(output, query.getLocations()); initializeStartLocations(output, query.getLocations());
} }
Node node = null; Node node = null;
...@@ -165,38 +130,14 @@ public abstract class Isochrone { ...@@ -165,38 +130,14 @@ public abstract class Isochrone {
return output; 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) * 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 adjusts the link offsets based on the distance of the current Node
* and the maximum duration of the query. * and the maximum duration of the query.
* *
* @param node * @param node current node
* current Node * @param link continuous link, that is connected to the node
* @param link * @return another node, if it can be reached in the available time (or null if time has run out)
* 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) { public Node expandContinuousLink(final Node node, final Link link) {
final int duration = query.getDuration().intValue(); final int duration = query.getDuration().intValue();
...@@ -241,9 +182,7 @@ public abstract class Isochrone { ...@@ -241,9 +182,7 @@ public abstract class Isochrone {
* *
* @param node current Node * @param node current Node
* @param link discrete link connected to the node * @param link discrete link connected to the node
* @return another node, if it can be reached in the avaliable * @return another node, if it can be reached in the available time (or null if there is no time to reach another node)
* time. Or null if there is no time to reach another
* node.
*/ */
public Node expandDiscreteLink(final Node node, final Link link) { public Node expandDiscreteLink(final Node node, final Link link) {
final Long duration = query.getDuration(); final Long duration = query.getDuration();
...@@ -270,38 +209,6 @@ public abstract class Isochrone { ...@@ -270,38 +209,6 @@ public abstract class Isochrone {
return null; 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. * Get the cost to travel to an adjacent node.
* *
...@@ -328,11 +235,108 @@ public abstract class Isochrone { ...@@ -328,11 +235,108 @@ public abstract class Isochrone {
return db.getDateCodes(time); 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);
}
}
} }
...@@ -20,6 +20,8 @@ public class MDijkstra extends Isochrone { ...@@ -20,6 +20,8 @@ public class MDijkstra extends Isochrone {
private final Map<Integer, Link> links; private final Map<Integer, Link> links;
private final Map<Integer, Node> nodes; private final Map<Integer, Node> nodes;
// Constructors
public MDijkstra(final ConfigDataset config, final Query query) throws AlgorithmException { public MDijkstra(final ConfigDataset config, final Query query) throws AlgorithmException {
super(config, query); super(config, query);
...@@ -29,13 +31,7 @@ public class MDijkstra extends Isochrone { ...@@ -29,13 +31,7 @@ public class MDijkstra extends Isochrone {
db.readNetwork(nodes, links); db.readNetwork(nodes, links);
} }
@Override // Public methods
public Node initializeNode(final int nodeId) {
final Node node = getNode(nodeId);
node.setDistance(0.0);
return node;
}
@Override @Override
public Link getLink(final int linkId) { public Link getLink(final int linkId) {
...@@ -58,7 +54,14 @@ public class MDijkstra extends Isochrone { ...@@ -58,7 +54,14 @@ public class MDijkstra extends Isochrone {
} }
@Override @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(); final Collection<Link> adjLinks = getNode(nodeId).getAdjLinks();
if (adjLinks == null) { if (adjLinks == null) {
return Collections.emptyList(); return Collections.emptyList();
...@@ -68,12 +71,15 @@ public class MDijkstra extends Isochrone { ...@@ -68,12 +71,15 @@ public class MDijkstra extends Isochrone {
} }
@Override @Override
public Collection<Node> getNodes() { protected void removeNode(final int id) {
return nodes.values(); // Nothing to do here
} }
@Override @Override
protected void removeNode(final int id) { protected Node initializeNode(final int nodeId) {
// Nothing to do here final Node node = getNode(nodeId);
node.setDistance(0.0);
return node;
} }
} }
...@@ -10,24 +10,20 @@ import java.util.HashMap; ...@@ -10,24 +10,20 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
* The MineX isochrone algorithm. Loads the nodes and links from the * The MineX isochrone algorithm.
* database as soon as they are needed. * Loads the nodes and links from the database as soon as they are needed.
*/ */
public class MineX extends Isochrone { public class MineX extends Isochrone {
private final Map<Integer, Node> nodes; private final Map<Integer, Node> nodes;
// Constructors
public MineX(final ConfigDataset config, final Query query) throws AlgorithmException { public MineX(final ConfigDataset config, final Query query) throws AlgorithmException {
super(config, query); super(config, query);
nodes = new HashMap<Integer, Node>(); nodes = new HashMap<Integer, Node>();
} }
@Override // Public methods
public Node initializeNode(final int nodeId) {
final Node node = getNode(nodeId);
node.setDistance(0.0);
return node;
}
@Override @Override
public Link getLink(final int linkId) { public Link getLink(final int linkId) {
...@@ -59,14 +55,24 @@ public class MineX extends Isochrone { ...@@ -59,14 +55,24 @@ public class MineX extends Isochrone {
return nodes.values(); return nodes.values();
} }
// Protected methods
@Override @Override
public void removeNode(final int nodeId) { protected Collection<Link> calcAdjLinks(final int nodeId) {
nodes.remove(nodeId); return db.getAdjLinks(nodeId, nodes);
} }
@Override @Override
public Collection<Link> calcAdjLinks(final int nodeId) { protected Node initializeNode(final int nodeId) {
return db.getAdjLinks(nodeId, nodes); final Node node = getNode(nodeId);
node.setDistance(0.0);
return node;
}
@Override
protected void removeNode(final int nodeId) {
nodes.remove(nodeId);
} }
} }
...@@ -14,8 +14,8 @@ import java.util.List; ...@@ -14,8 +14,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* The MrneX isochrone algorithm. Loads the data in circles * The MrneX isochrone algorithm.
* surrounding the currently explored node. * Loads the data in circles surrounding the currently explored node.
*/ */
public class MrneX extends Isochrone { public class MrneX extends Isochrone {
private final Map<Integer, List<Link>> adjList; private final Map<Integer, List<Link>> adjList;
...@@ -45,18 +45,58 @@ public class MrneX extends Isochrone { ...@@ -45,18 +45,58 @@ public class MrneX extends Isochrone {
// Public methods // Public methods
@Override @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); final Node node = getNode(nodeId);
if (adjList.get(nodeId) != null) { if (adjList.get(nodeId) != null) {
/* /*
* Check if adjacent continuous links are empty * 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 * load the adjacent continuous links, because
* density may have been to small, so only the * density may have been to small, so only the
* discrete links were loaded. * discrete links were loaded.
* *
* Most likely this is a non-issue, because * 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 * but that's always false (java initializes
* variables as false and it's never set) and * variables as false and it's never set) and
* therefore continuous links are never added * therefore continuous links are never added
...@@ -96,45 +136,7 @@ public class MrneX extends Isochrone { ...@@ -96,45 +136,7 @@ public class MrneX extends Isochrone {
} }
@Override @Override
public Link getLink(final int linkId) { protected Node initializeNode(final int nodeId) {
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) {
final Node node = getNode(nodeId); final Node node = getNode(nodeId);
node.setDistance(0.0); node.setDistance(0.0);
node.setRadius(calcRemainingDistance(node)); node.setRadius(calcRemainingDistance(node));
...@@ -142,12 +144,10 @@ public class MrneX extends Isochrone { ...@@ -142,12 +144,10 @@ public class MrneX extends Isochrone {
} }
@Override @Override
public void removeNode(final int nodeId) { protected void removeNode(final int nodeId) {
nodes.remove(nodeId); nodes.remove(nodeId);
} }
// Protected methods
/** /**
* Do we have unlimited memory available (or don't we care * Do we have unlimited memory available (or don't we care
* about how much memory we will be using)? * about how much memory we will be using)?
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment