ComputerResult result = computer.submit().get(); assertTrue(result.memory().exists(OLAPTest.DegreeMapper.DEGREE_RESULT)); Map<Long,Integer> degrees = result.memory().get(OLAPTest.DegreeMapper.DEGREE_RESULT); assertNotNull(degrees);
@Test public void degreeCounting() throws Exception { int numV = 200; int numE = generateRandomGraph(numV); clopen(); final TitanGraphComputer computer = graph.compute(); computer.resultMode(TitanGraphComputer.ResultMode.NONE); computer.workers(4); computer.program(new DegreeCounter()); computer.mapReduce(new DegreeMapper()); ComputerResult result = computer.submit().get(); System.out.println("Execution time (ms) ["+numV+"|"+numE+"]: " + result.memory().getRuntime()); assertTrue(result.memory().exists(DegreeMapper.DEGREE_RESULT)); Map<Long,Integer> degrees = result.memory().get(DegreeMapper.DEGREE_RESULT); assertNotNull(degrees); assertEquals(numV,degrees.size()); int totalCount = 0; for (Map.Entry<Long,Integer> entry : degrees.entrySet()) { int degree = entry.getValue(); TitanVertex v = getV(tx, entry.getKey().longValue()); int count = v.value("uid"); assertEquals(count,degree); totalCount+= degree; } assertEquals(numV*(numV+1)/2,totalCount); assertEquals(1,result.memory().getIteration()); }
ComputerResult result = computer.submit().get(); assertTrue(result.memory().exists(OLAPTest.DegreeMapper.DEGREE_RESULT)); Map<Long,Integer> degrees = result.memory().get(OLAPTest.DegreeMapper.DEGREE_RESULT); assertNotNull(degrees);
@Test public void degreeCounting() throws Exception { int numV = 200; int numE = generateRandomGraph(numV); clopen(); final JanusGraphComputer computer = graph.compute(); computer.resultMode(JanusGraphComputer.ResultMode.NONE); computer.workers(4); computer.program(new DegreeCounter()); computer.mapReduce(new DegreeMapper()); ComputerResult result = computer.submit().get(); System.out.println("Execution time (ms) ["+numV+"|"+numE+"]: " + result.memory().getRuntime()); assertTrue(result.memory().exists(DegreeMapper.DEGREE_RESULT)); Map<Long,Integer> degrees = result.memory().get(DegreeMapper.DEGREE_RESULT); assertNotNull(degrees); assertEquals(numV,degrees.size()); int totalCount = 0; for (Map.Entry<Long,Integer> entry : degrees.entrySet()) { int degree = entry.getValue(); final JanusGraphVertex v = getV(tx, entry.getKey()); int count = v.value("uid"); assertEquals(count,degree); totalCount+= degree; } assertEquals(numV*(numV+1)/2,totalCount); assertEquals(1,result.memory().getIteration()); }
@Override public void setup(final Memory memory) { assertFalse(memory.exists("m1")); // or assertFalse(memory.exists("m2")); // and assertFalse(memory.exists("m3")); // long assertFalse(memory.exists("m4")); // int memory.set("m1", false); memory.set("m2", true); memory.set("m3", 0l); memory.set("m4", 0); }
@Override public <V> V get(final String key) throws IllegalArgumentException { return (null != this.memory && this.memory.exists(key)) ? this.memory.get(key) : this.sideEffects.get(key); }
@Test @LoadGraphWith(MODERN) public void shouldFindShortestPathsWithStartVertexFilter() throws Exception { final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()). program(ShortestPathVertexProgram.build().source(__.has("name", "marko")).create(graph)).submit().get(); assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS)); final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS); final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS) .filter(p -> p[0].equals("marko")).map(helper::makePath).collect(Collectors.toList()); helper.checkResults(expected, shortestPaths); }
@Test @LoadGraphWith(MODERN) public void shouldFindShortestPathsWithEndVertexFilter() throws Exception { final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()). program(ShortestPathVertexProgram.build().target(__.has("name", "marko")).create(graph)).submit().get(); assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS)); final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS); final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS) .filter(p -> p[p.length - 1].equals("marko")).map(helper::makePath).collect(Collectors.toList()); helper.checkResults(expected, shortestPaths); }
@Test @LoadGraphWith(MODERN) public void shouldFindAllShortestPathsWithDefaultParameters() throws Exception { final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()). program(ShortestPathVertexProgram.build().create(graph)).submit().get(); assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS)); final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS); final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS).map(helper::makePath).collect(Collectors.toList()); helper.checkResults(expected, shortestPaths); }
@Override public void workerIterationEnd(final Memory memory) { assertFalse(memory.exists(TraversalVertexProgram.HALTED_TRAVERSERS)); MemoryTraversalSideEffects.setMemorySideEffects(this.traversal.get(), memory, ProgramPhase.WORKER_ITERATION_END); checkSideEffects(); }
@Test @LoadGraphWith(MODERN) public void shouldFindShortestPathsWithStartEndVertexFilter() throws Exception { final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()). program(ShortestPathVertexProgram.build() .source(__.has("name", "marko")) .target(__.hasLabel("software")).create(graph)).submit().get(); assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS)); final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS); final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS) .filter(p -> p[0].equals("marko") && Arrays.asList("lop", "ripple").contains(p[p.length - 1])) .map(helper::makePath).collect(Collectors.toList()); helper.checkResults(expected, shortestPaths); }
@Test @LoadGraphWith(MODERN) public void shouldRespectMaxDistance() throws Exception { final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()). program(ShortestPathVertexProgram.build() .source(__.has("name", "marko")) .maxDistance(1).create(graph)).submit().get(); assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS)); final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS); final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS) .filter(p -> p[0].equals("marko") && p.length <= 2).map(helper::makePath).collect(Collectors.toList()); helper.checkResults(expected, shortestPaths); }
@Test @LoadGraphWith(MODERN) public void shouldFindAllShortestPathsWithEdgesIncluded() throws Exception { final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()). program(ShortestPathVertexProgram.build().includeEdges(true).create(graph)).submit().get(); assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS)); final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS); final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS).map(p -> helper.makePath(true, p)) .collect(Collectors.toList()); helper.checkResults(expected, shortestPaths); }
@Test @LoadGraphWith(MODERN) public void shouldSupportBroadcastKeys() throws Exception { final ComputerResult result = graphProvider.getGraphComputer(graph).program(new VertexProgramP()).submit().get(); assertTrue(result.memory().exists("m1")); assertFalse(result.memory().exists("m2")); assertFalse(result.memory().exists("m3")); assertTrue(result.memory().exists("m4")); assertTrue(result.memory().get("m1")); assertEquals(-18, result.memory().<Integer>get("m4").intValue()); assertEquals(2, result.memory().keys().size()); }
@Override protected Traverser.Admin<S> processNextStart() throws NoSuchElementException { while (true) { if (this.currentIterator.hasNext()) return this.currentIterator.next(); else { final ComputerResult result = this.starts.next().get(); this.currentIterator = attach(result.memory().exists(TraversalVertexProgram.HALTED_TRAVERSERS) ? result.memory().<TraverserSet<S>>get(TraversalVertexProgram.HALTED_TRAVERSERS).iterator() : EmptyIterator.instance(), result.graph()); } } }
@Override public void workerIterationStart(final Memory memory) { assertNotNull(this.haltedTraversers); this.haltedTraversers.clear(); assertFalse(memory.exists(TraversalVertexProgram.HALTED_TRAVERSERS)); MemoryTraversalSideEffects.setMemorySideEffects(this.traversal.get(), memory, ProgramPhase.WORKER_ITERATION_START); checkSideEffects(); }
@Test @LoadGraphWith(MODERN) public void shouldUseCustomDistanceProperty() throws Exception { final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()). program(ShortestPathVertexProgram.build() .source(__.has("name", "marko")) .target(__.has("name", "josh")) .distanceProperty("weight").create(graph)).submit().get(); assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS)); final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS); assertEquals(1, shortestPaths.size()); assertEquals(helper.makePath("marko", "lop", "josh"), shortestPaths.get(0)); }
@Override public TraversalVertexProgram generateProgram(final Graph graph, final Memory memory) { final Traversal.Admin<?, ?> computerSpecificTraversal = this.computerTraversal.getPure(); final TraversalStrategies computerSpecificStrategies = this.getTraversal().getStrategies().clone(); IteratorUtils.filter(TraversalStrategies.GlobalCache.getStrategies(graph.getClass()).toList(), s -> s instanceof TraversalStrategy.ProviderOptimizationStrategy).forEach(computerSpecificStrategies::addStrategies); computerSpecificTraversal.setStrategies(computerSpecificStrategies); computerSpecificTraversal.setSideEffects(new MemoryTraversalSideEffects(this.getTraversal().getSideEffects())); computerSpecificTraversal.setParent(this); final TraversalVertexProgram.Builder builder = TraversalVertexProgram.build().traversal(computerSpecificTraversal); if (memory.exists(TraversalVertexProgram.HALTED_TRAVERSERS)) builder.haltedTraversers(memory.get(TraversalVertexProgram.HALTED_TRAVERSERS)); return builder.create(graph); }
@Override public VertexProgram generateProgram(final Graph graph, final Memory memory) { final MapConfiguration base = new MapConfiguration(this.configuration); base.setDelimiterParsingDisabled(true); PureTraversal.storeState(base, ROOT_TRAVERSAL, TraversalHelper.getRootTraversal(this.getTraversal()).clone()); base.setProperty(STEP_ID, this.getId()); if (memory.exists(TraversalVertexProgram.HALTED_TRAVERSERS)) TraversalVertexProgram.storeHaltedTraversers(base, memory.get(TraversalVertexProgram.HALTED_TRAVERSERS)); return VertexProgram.createVertexProgram(graph, base); }