Search in sources :

Example 1 with GraphService

use of org.opentripplanner.routing.services.GraphService in project OpenTripPlanner by opentripplanner.

the class GraphServiceTest method testGraphServiceAutoscan.

@Test
public final void testGraphServiceAutoscan() throws IOException {
    // Check for no graphs
    GraphService graphService = new GraphService(false);
    GraphScanner graphScanner = new GraphScanner(graphService, basePath, true);
    graphScanner.startup();
    assertEquals(0, graphService.getRouterIds().size());
    System.out.println("------------------------------------------");
    // Add a single default graph
    InputStreamGraphSource.FileFactory graphSourceFactory = new InputStreamGraphSource.FileFactory(basePath);
    graphSourceFactory.save("", new ByteArrayInputStream(smallGraphData));
    // Check that the single graph is there
    graphService = new GraphService(false);
    graphScanner = new GraphScanner(graphService, basePath, true);
    graphScanner.startup();
    assertEquals(1, graphService.getRouterIds().size());
    assertEquals("", graphService.getRouter().graph.routerId);
    assertEquals("", graphService.getRouter("").graph.routerId);
    System.out.println("------------------------------------------");
    // Add another graph in a sub-directory
    graphSourceFactory.save("A", new ByteArrayInputStream(smallGraphData));
    graphService = new GraphService(false);
    graphScanner = new GraphScanner(graphService, basePath, true);
    graphScanner.startup();
    assertEquals(2, graphService.getRouterIds().size());
    assertEquals("", graphService.getRouter().graph.routerId);
    assertEquals("A", graphService.getRouter("A").graph.routerId);
    System.out.println("------------------------------------------");
    // Remove default Graph
    new File(basePath, InputStreamGraphSource.GRAPH_FILENAME).delete();
    // Check that default is A this time
    graphService = new GraphService(false);
    graphScanner = new GraphScanner(graphService, basePath, true);
    graphScanner.startup();
    assertEquals(1, graphService.getRouterIds().size());
    assertEquals("A", graphService.getRouter().graph.routerId);
    assertEquals("A", graphService.getRouter("A").graph.routerId);
}
Also used : GraphService(org.opentripplanner.routing.services.GraphService) Test(org.junit.Test)

Example 2 with GraphService

use of org.opentripplanner.routing.services.GraphService in project OpenTripPlanner by opentripplanner.

the class GraphServiceTest method testGraphServiceMemoryRouterConfig.

@Test
public final void testGraphServiceMemoryRouterConfig() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode buildConfig = MissingNode.getInstance();
    ObjectNode routerConfig = mapper.createObjectNode();
    routerConfig.put("timeout", 8);
    EmbedConfig embedConfig = new EmbedConfig(buildConfig, routerConfig);
    embedConfig.buildGraph(emptyGraph, null);
    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);
    JsonNode graphRouterConfig = mapper.readTree(graph.routerConfig);
    assertEquals(graphRouterConfig, routerConfig);
    assertEquals(graphRouterConfig.get("timeout"), routerConfig.get("timeout"));
}
Also used : GraphService(org.opentripplanner.routing.services.GraphService) Graph(org.opentripplanner.routing.graph.Graph) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) EmbedConfig(org.opentripplanner.graph_builder.module.EmbedConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with GraphService

use of org.opentripplanner.routing.services.GraphService in project OpenTripPlanner by opentripplanner.

the class GraphServiceTest method testGraphServiceFile.

@Test
public final void testGraphServiceFile() throws IOException {
    // Create a GraphService and a GraphSourceFactory
    GraphService graphService = new GraphService();
    InputStreamGraphSource.FileFactory graphSourceFactory = new InputStreamGraphSource.FileFactory(basePath);
    graphSourceFactory.save("A", new ByteArrayInputStream(emptyGraphData));
    // Check if the graph has been saved
    assertTrue(new File(new File(basePath, "A"), InputStreamGraphSource.GRAPH_FILENAME).canRead());
    // Register this empty graph, reloading it from disk
    boolean registered = graphService.registerGraph("A", graphSourceFactory.createGraphSource("A"));
    assertTrue(registered);
    // Check if the loaded graph is the one we saved earlier
    Graph graph = graphService.getRouter("A").graph;
    assertNotNull(graph);
    assertEquals("A", graph.routerId);
    assertEquals(0, graph.getVertices().size());
    assertEquals(1, graphService.getRouterIds().size());
    // Save A again, with more data this time
    int verticesCount = smallGraph.getVertices().size();
    int edgesCount = smallGraph.getEdges().size();
    graphSourceFactory.save("A", new ByteArrayInputStream(smallGraphData));
    // Force a reload, get again the graph
    graphService.reloadGraphs(false, true);
    graph = graphService.getRouter("A").graph;
    // Check if loaded graph is the one modified
    assertEquals(verticesCount, graph.getVertices().size());
    assertEquals(edgesCount, graph.getEdges().size());
    // Remove the file from disk and reload
    boolean deleted = new File(new File(basePath, "A"), InputStreamGraphSource.GRAPH_FILENAME).delete();
    assertTrue(deleted);
    graphService.reloadGraphs(false, true);
    // Check that the graph have been evicted
    assertEquals(0, graphService.getRouterIds().size());
    // Register it manually
    registered = graphService.registerGraph("A", graphSourceFactory.createGraphSource("A"));
    // Check that it fails again (file still deleted)
    assertFalse(registered);
    assertEquals(0, graphService.getRouterIds().size());
    // Re-save the graph file, register it again
    graphSourceFactory.save("A", new ByteArrayInputStream(smallGraphData));
    registered = graphService.registerGraph("A", graphSourceFactory.createGraphSource("A"));
    // This time registering is OK
    assertTrue(registered);
    assertEquals(1, graphService.getRouterIds().size());
    // Evict the graph, should be OK
    boolean evicted = graphService.evictRouter("A");
    assertTrue(evicted);
    assertEquals(0, graphService.getRouterIds().size());
}
Also used : GraphService(org.opentripplanner.routing.services.GraphService) Graph(org.opentripplanner.routing.graph.Graph) Test(org.junit.Test)

Example 4 with GraphService

use of org.opentripplanner.routing.services.GraphService in project OpenTripPlanner by opentripplanner.

the class Routers method putGraphId.

/**
 * Load the graph for the specified routerId from disk.
 * @param preEvict before reloading each graph, evict the existing graph. This will prevent
 * memory usage from increasing during the reload, but routing will be unavailable on this
 * routerId for the duration of the operation.
 */
@RolesAllowed({ "ROUTERS" })
@PUT
@Path("{routerId}")
@Produces({ MediaType.TEXT_PLAIN })
public Response putGraphId(@PathParam("routerId") String routerId, @QueryParam("preEvict") @DefaultValue("true") boolean preEvict) {
    LOG.debug("Attempting to load graph '{}' from server's local filesystem.", routerId);
    GraphService graphService = otpServer.getGraphService();
    if (graphService.getRouterIds().contains(routerId)) {
        boolean success = graphService.reloadGraph(routerId, preEvict, false);
        if (success)
            return Response.status(201).entity("graph already registered, reloaded.\n").build();
        else
            return Response.status(404).entity("graph already registered, but reload failed.\n").build();
    } else {
        boolean success = graphService.registerGraph(routerId, graphService.getGraphSourceFactory().createGraphSource(routerId));
        if (success)
            return Response.status(201).entity("graph registered.\n").build();
        else
            return Response.status(404).entity("graph not found or other error.\n").build();
    }
}
Also used : GraphService(org.opentripplanner.routing.services.GraphService) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 5 with GraphService

use of org.opentripplanner.routing.services.GraphService in project OpenTripPlanner by opentripplanner.

the class Routers method postGraphOverWire.

/**
 * Deserialize a graph sent with the HTTP request as POST data, associating it with the given
 * routerId.
 */
@RolesAllowed({ "ROUTERS" })
@POST
@Path("{routerId}")
@Produces({ MediaType.TEXT_PLAIN })
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response postGraphOverWire(@PathParam("routerId") String routerId, @QueryParam("preEvict") @DefaultValue("true") boolean preEvict, @QueryParam("loadLevel") @DefaultValue("FULL") LoadLevel level, InputStream is) {
    if (preEvict) {
        LOG.debug("pre-evicting graph");
        otpServer.getGraphService().evictRouter(routerId);
    }
    LOG.debug("deserializing graph from POST data stream...");
    Graph graph;
    try {
        graph = Graph.load(is, level);
        GraphService graphService = otpServer.getGraphService();
        graphService.registerGraph(routerId, new MemoryGraphSource(routerId, graph));
        return Response.status(Status.CREATED).entity(graph.toString() + "\n").build();
    } catch (Exception e) {
        return Response.status(Status.BAD_REQUEST).entity(e.toString() + "\n").build();
    }
}
Also used : GraphService(org.opentripplanner.routing.services.GraphService) Graph(org.opentripplanner.routing.graph.Graph) MemoryGraphSource(org.opentripplanner.routing.impl.MemoryGraphSource) GraphNotFoundException(org.opentripplanner.routing.error.GraphNotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Aggregations

GraphService (org.opentripplanner.routing.services.GraphService)12 Graph (org.opentripplanner.routing.graph.Graph)9 Test (org.junit.Test)6 MemoryGraphSource (org.opentripplanner.routing.impl.MemoryGraphSource)6 CommandLineParameters (org.opentripplanner.standalone.CommandLineParameters)5 OTPServer (org.opentripplanner.standalone.OTPServer)4 RolesAllowed (javax.annotation.security.RolesAllowed)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 GraphNotFoundException (org.opentripplanner.routing.error.GraphNotFoundException)3 File (java.io.File)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 RouterInfo (org.opentripplanner.api.model.RouterInfo)2 DefaultStreetVertexIndexFactory (org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory)2 GraphPathFinder (org.opentripplanner.routing.impl.GraphPathFinder)2 Router (org.opentripplanner.standalone.Router)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1