use of org.opentripplanner.routing.error.GraphNotFoundException in project OpenTripPlanner by opentripplanner.
the class GraphScanner method autoScan.
private void autoScan() {
LOG.debug("Auto discovering graphs under {}", basePath);
/*
* There is no need to synchronize scan and registration here. If a graph file is removed
* between scan and register, registering will fail but it's safe. It a graph file is
* created, we'll wait for the next scan to register it.
*/
Set<String> graphOnDisk = new HashSet<String>();
/* First check for a root graph */
File rootGraphFile = new File(basePath, InputStreamGraphSource.GRAPH_FILENAME);
if (rootGraphFile.exists() && rootGraphFile.canRead()) {
graphOnDisk.add("");
}
/* Then graph in sub-directories */
for (String sub : basePath.list()) {
File subPath = new File(basePath, sub);
if (subPath.isDirectory()) {
File graphFile = new File(subPath, InputStreamGraphSource.GRAPH_FILENAME);
if (graphFile.exists() && graphFile.canRead()) {
graphOnDisk.add(sub);
}
}
}
Set<String> graphRegistered = new HashSet<>(graphService.getRouterIds());
Set<String> graphToRegister = new HashSet<>(graphOnDisk);
graphToRegister.removeAll(graphRegistered);
if (!graphToRegister.isEmpty()) {
LOG.info("Found new routers to register: {}", Arrays.toString(graphToRegister.toArray()));
for (String routerId : graphToRegister) {
InputStreamGraphSource graphSource = InputStreamGraphSource.newFileGraphSource(routerId, getBasePath(routerId), loadLevel);
// Can be null here if the file has been removed in the meantime.
graphService.registerGraph(routerId, graphSource);
}
}
/*
* Note: We do not automatically evict removed graph. They will be evicted only in
* auto-reload mode, and that's the behavior we want.
*/
Collection<String> routerIds = graphService.getRouterIds();
if (routerIds.isEmpty()) {
LOG.warn("No graphs have been loaded/registered. " + "You must place one or more graphs before routing.");
} else {
try {
// Check if we still have a default graph.
graphService.getRouter();
} catch (GraphNotFoundException e) {
// Let's see which one we want to take by default
if (routerIds.contains("")) {
// If we have a root graph, this should be a good default
LOG.info("Setting default routerId to root graph ''");
graphService.setDefaultRouterId("");
} else {
// Otherwise take first one present
String defRouterId = routerIds.iterator().next();
if (routerIds.size() > 1)
LOG.warn("Setting default routerId to arbitrary one '{}'", defRouterId);
else
LOG.info("Setting default routerId to '{}'", defRouterId);
graphService.setDefaultRouterId(defRouterId);
}
}
}
}
use of org.opentripplanner.routing.error.GraphNotFoundException in project OpenTripPlanner by opentripplanner.
the class GraphService method getRouter.
/**
* @return the graph object for the given router ID
*/
public Router getRouter(String routerId) throws GraphNotFoundException {
if (routerId == null || routerId.isEmpty() || routerId.equalsIgnoreCase("default")) {
routerId = defaultRouterId;
LOG.debug("routerId not specified, set to default of '{}'", routerId);
}
/*
* Here we should not synchronize on graphSource as it may block for a while (during
* reload/autoreload). For normal operations a simple get do not need to be synchronized so
* we should be safe.
*/
GraphSource graphSource = graphSources.get(routerId);
if (graphSource == null) {
LOG.error("no graph registered with the routerId '{}'", routerId);
throw new GraphNotFoundException();
}
Router router = graphSource.getRouter();
if (router == null) {
evictRouter(routerId);
throw new GraphNotFoundException();
}
return router;
}
use of org.opentripplanner.routing.error.GraphNotFoundException in project OpenTripPlanner by opentripplanner.
the class GraphServiceTest method testGraphServiceMemory.
@Test
public final void testGraphServiceMemory() {
GraphService graphService = new GraphService();
graphService.registerGraph("A", new MemoryGraphSource("A", emptyGraph));
assertEquals(1, graphService.getRouterIds().size());
Graph graph = graphService.getRouter("A").graph;
assertNotNull(graph);
assertEquals(emptyGraph, graph);
assertEquals("A", emptyGraph.routerId);
try {
graph = graphService.getRouter("inexistant").graph;
// Should not be there
assertTrue(false);
} catch (GraphNotFoundException e) {
}
graphService.setDefaultRouterId("A");
graph = graphService.getRouter().graph;
assertEquals(emptyGraph, graph);
graphService.registerGraph("B", new MemoryGraphSource("B", smallGraph));
assertEquals(2, graphService.getRouterIds().size());
graph = graphService.getRouter("B").graph;
assertNotNull(graph);
assertEquals(smallGraph, graph);
assertEquals("B", graph.routerId);
graphService.evictRouter("A");
assertEquals(1, graphService.getRouterIds().size());
try {
graph = graphService.getRouter("A").graph;
// Should not be there
assertTrue(false);
} catch (GraphNotFoundException e) {
}
try {
graph = graphService.getRouter().graph;
// Should not be there
assertTrue(false);
} catch (GraphNotFoundException e) {
}
graphService.evictAll();
assertEquals(0, graphService.getRouterIds().size());
}
Aggregations