Search in sources :

Example 46 with Graph

use of com.graphhopper.storage.Graph in project graphhopper by graphhopper.

the class PathBidirRefTest method testExtract.

@Test
public void testExtract() {
    Graph g = createGraph();
    g.edge(1, 2, 10, true);
    PathBidirRef pw = new PathBidirRef(g, new FastestWeighting(carEncoder));
    EdgeExplorer explorer = g.createEdgeExplorer(carOutEdges);
    EdgeIterator iter = explorer.setBaseNode(1);
    iter.next();
    pw.sptEntry = new SPTEntry(iter.getEdge(), 2, 0);
    pw.sptEntry.parent = new SPTEntry(EdgeIterator.NO_EDGE, 1, 10);
    pw.edgeTo = new SPTEntry(EdgeIterator.NO_EDGE, 2, 0);
    Path p = pw.extract();
    assertEquals(Helper.createTList(1, 2), p.calcNodes());
    assertEquals(10, p.getDistance(), 1e-4);
}
Also used : SPTEntry(com.graphhopper.storage.SPTEntry) EdgeIterator(com.graphhopper.util.EdgeIterator) Graph(com.graphhopper.storage.Graph) EdgeExplorer(com.graphhopper.util.EdgeExplorer) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.Test)

Example 47 with Graph

use of com.graphhopper.storage.Graph in project graphhopper by graphhopper.

the class GraphHopperAPITest method testConcurrentGraphChange.

@Test
public void testConcurrentGraphChange() throws InterruptedException {
    final GraphHopperStorage graph = new GraphBuilder(encodingManager).create();
    initGraph(graph);
    graph.edge(1, 2, 10, true);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger checkPointCounter = new AtomicInteger(0);
    final GraphHopper graphHopper = new GraphHopper() {

        @Override
        protected ChangeGraphHelper createChangeGraphHelper(Graph graph, LocationIndex locationIndex) {
            return new ChangeGraphHelper(graph, locationIndex) {

                @Override
                public long applyChanges(EncodingManager em, Collection<JsonFeature> features) {
                    // force sleep inside the lock and let the main thread run until the lock barrier
                    latch.countDown();
                    try {
                        Thread.sleep(400);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    checkPointCounter.incrementAndGet();
                    return super.applyChanges(em, features);
                }
            };
        }
    }.setStoreOnFlush(false).setEncodingManager(encodingManager).setCHEnabled(false).loadGraph(graph);
    GHResponse rsp = graphHopper.route(new GHRequest(42, 10.4, 42, 10));
    assertFalse(rsp.toString(), rsp.hasErrors());
    assertEquals(1800, rsp.getBest().getTime());
    final List<JsonFeature> list = new ArrayList<>();
    Map<String, Object> properties = new HashMap<>();
    properties.put("speed", 5);
    list.add(new JsonFeature("1", "bbox", new BBox(10.399, 10.4, 42.0, 42.001), null, properties));
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    executorService.submit(new Runnable() {

        @Override
        public void run() {
            graphHopper.changeGraph(list);
            checkPointCounter.incrementAndGet();
        }
    });
    latch.await();
    assertEquals(0, checkPointCounter.get());
    rsp = graphHopper.route(new GHRequest(42, 10.4, 42, 10));
    assertFalse(rsp.toString(), rsp.hasErrors());
    assertEquals(8400, rsp.getBest().getTime());
    executorService.shutdown();
    executorService.awaitTermination(3, TimeUnit.SECONDS);
    assertEquals(2, checkPointCounter.get());
}
Also used : ChangeGraphHelper(com.graphhopper.storage.change.ChangeGraphHelper) GraphBuilder(com.graphhopper.storage.GraphBuilder) EncodingManager(com.graphhopper.routing.util.EncodingManager) CountDownLatch(java.util.concurrent.CountDownLatch) LocationIndex(com.graphhopper.storage.index.LocationIndex) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) JsonFeature(com.graphhopper.json.geo.JsonFeature) Graph(com.graphhopper.storage.Graph) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BBox(com.graphhopper.util.shapes.BBox) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Aggregations

Graph (com.graphhopper.storage.Graph)47 Test (org.junit.Test)40 GraphBuilder (com.graphhopper.storage.GraphBuilder)21 NodeAccess (com.graphhopper.storage.NodeAccess)18 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)9 EdgeExplorer (com.graphhopper.util.EdgeExplorer)8 Dijkstra (com.graphhopper.routing.Dijkstra)7 Path (com.graphhopper.routing.Path)7 EncodingManager (com.graphhopper.routing.util.EncodingManager)6 RAMDirectory (com.graphhopper.storage.RAMDirectory)5 ReaderWay (com.graphhopper.reader.ReaderWay)4 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)4 GHRequest (com.graphhopper.GHRequest)3 GHResponse (com.graphhopper.GHResponse)3 GHIntHashSet (com.graphhopper.coll.GHIntHashSet)3 DefaultEdgeFilter (com.graphhopper.routing.util.DefaultEdgeFilter)3 Weighting (com.graphhopper.routing.weighting.Weighting)3 CHGraph (com.graphhopper.storage.CHGraph)3 BBox (com.graphhopper.util.shapes.BBox)3 GHPoint (com.graphhopper.util.shapes.GHPoint)3