本文整理了Java中org.apache.tinkerpop.gremlin.structure.Graph.compute()
方法的一些代码示例,展示了Graph.compute()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graph.compute()
方法的具体详情如下:
包路径:org.apache.tinkerpop.gremlin.structure.Graph
类名称:Graph
方法名:compute
[英]Generate a GraphComputer using the default engine of the underlying graph system. This is a shorthand method for the more involved method that uses Graph#compute(Class).
[中]使用基础图形系统的默认引擎生成图形计算机。这是使用Graph#compute(类)的更复杂方法的简写方法。
代码示例来源:origin: apache/tinkerpop
/**
* Create a {@link GraphComputer} from the {@link Graph} instance. The default implementation simply calls {@code graph.compute()}.
*
* @param graph the graph to get the graph computer from
* @return a new graph computer
*/
public default GraphComputer getGraphComputer(final Graph graph) {
return graph.compute();
}
代码示例来源:origin: apache/tinkerpop
public GraphComputer apply(final Graph graph) {
GraphComputer computer = this.graphComputerClass.equals(GraphComputer.class) ? graph.compute() : graph.compute(this.graphComputerClass);
for (final Map.Entry<String, Object> entry : this.configuration.entrySet()) {
computer = computer.configure(entry.getKey(), entry.getValue());
}
if (-1 != this.workers)
computer = computer.workers(this.workers);
if (null != this.persist)
computer = computer.persist(this.persist);
if (null != this.resultGraph)
computer = computer.result(this.resultGraph);
if (null != this.vertices)
computer = computer.vertices(this.vertices);
if (null != this.edges)
computer.edges(this.edges);
return computer;
}
代码示例来源:origin: apache/tinkerpop
@Test
@LoadGraphWith(MODERN)
public void shouldFindDirectedShortestPathsWithEdgesIncluded() throws Exception {
final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()).
program(ShortestPathVertexProgram.build().edgeTraversal(__.outE()).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)
.filter(p -> (p[0].equals("marko") && !p[p.length - 1].equals("peter"))
|| (p[0].equals("vadas") && p.length == 1)
|| (p[0].equals("lop") && p.length == 1)
|| (p[0].equals("josh") && Arrays.asList("lop", "josh", "ripple").contains(p[p.length - 1]))
|| (p[0].equals("ripple") && p.length == 1)
|| (p[0].equals("peter") && Arrays.asList("lop", "peter").contains(p[p.length - 1])))
.map(p -> helper.makePath(true, p)).collect(Collectors.toList());
helper.checkResults(expected, shortestPaths);
}
代码示例来源:origin: apache/tinkerpop
/**
* A {@link org.apache.tinkerpop.gremlin.structure.Graph} that does not support {@link org.apache.tinkerpop.gremlin.structure.Graph.Features.GraphFeatures#FEATURE_COMPUTER} must call
* {@link org.apache.tinkerpop.gremlin.structure.Graph.Exceptions#graphComputerNotSupported()}.
*/
@Test
@FeatureRequirement(featureClass = GraphFeatures.class, feature = FEATURE_COMPUTER, supported = false)
public void shouldSupportComputerIfAGraphCanCompute() throws Exception {
try {
graph.compute();
fail(String.format(INVALID_FEATURE_SPECIFICATION, GraphFeatures.class.getSimpleName(), FEATURE_COMPUTER));
} catch (Exception e) {
validateException(Graph.Exceptions.graphComputerNotSupported(), e);
}
}
代码示例来源:origin: apache/tinkerpop
@Test
@LoadGraphWith(MODERN)
public void shouldFindInDirectedShortestPaths() throws Exception {
final List<ShortestPathVertexProgram> programs = Arrays.asList(
ShortestPathVertexProgram.build().edgeTraversal(__.inE()).create(graph),
ShortestPathVertexProgram.build().edgeDirection(Direction.IN).create(graph));
for (final ShortestPathVertexProgram program : programs) {
final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()).
program(program).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 == 1)
|| (p[0].equals("vadas") && Arrays.asList("marko", "vadas").contains(p[p.length - 1]))
|| (p[0].equals("lop") && Arrays.asList("marko", "lop", "josh", "peter").contains(p[p.length - 1]))
|| (p[0].equals("josh") && Arrays.asList("marko", "josh").contains(p[p.length - 1]))
|| (p[0].equals("ripple") && Arrays.asList("marko", "josh", "ripple").contains(p[p.length - 1]))
|| (p[0].equals("peter") && p.length == 1))
.map(helper::makePath).collect(Collectors.toList());
helper.checkResults(expected, shortestPaths);
}
}
代码示例来源:origin: apache/tinkerpop
@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);
}
代码示例来源:origin: apache/tinkerpop
@Test
@LoadGraphWith(MODERN)
public void shouldFindOutDirectedShortestPaths() throws Exception {
final List<ShortestPathVertexProgram> programs = Arrays.asList(
ShortestPathVertexProgram.build().edgeTraversal(__.outE()).create(graph),
ShortestPathVertexProgram.build().edgeDirection(Direction.OUT).create(graph));
for (final ShortestPathVertexProgram program : programs) {
final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()).
program(program).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[p.length - 1].equals("peter"))
|| (p[0].equals("vadas") && p.length == 1)
|| (p[0].equals("lop") && p.length == 1)
|| (p[0].equals("josh") && Arrays.asList("lop", "josh", "ripple").contains(p[p.length - 1]))
|| (p[0].equals("ripple") && p.length == 1)
|| (p[0].equals("peter") && Arrays.asList("lop", "peter").contains(p[p.length - 1])))
.map(helper::makePath).collect(Collectors.toList());
helper.checkResults(expected, shortestPaths);
}
}
代码示例来源:origin: apache/tinkerpop
@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);
}
代码示例来源:origin: apache/tinkerpop
@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);
}
代码示例来源:origin: apache/tinkerpop
@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);
}
代码示例来源:origin: apache/tinkerpop
@Test
@LoadGraphWith(MODERN)
public void shouldRespectMaxCustomDistance() throws Exception {
final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()).
program(ShortestPathVertexProgram.build()
.source(__.has("name", "vadas"))
.distanceProperty("weight").maxDistance(1.3).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 = Stream.concat(Arrays.stream(ALL_SHORTEST_PATHS)
.filter(p -> p[0].equals("vadas") &&
Arrays.asList("vadas", "marko", "lop", "peter").contains(p[p.length - 1]))
.map(helper::makePath),
Stream.of(helper.makePath("vadas", "marko", "lop", "josh")))
.collect(Collectors.toList());
helper.checkResults(expected, shortestPaths);
}
代码示例来源:origin: apache/tinkerpop
@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);
}
代码示例来源:origin: apache/tinkerpop
@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);
}
代码示例来源:origin: apache/tinkerpop
@Test
@LoadGraphWith(MODERN)
public void shouldExecutePageRankWithEnergyConservation() throws Exception {
final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()).program(PageRankVertexProgram.build().create(graph)).submit().get();
final double sum = result.graph().traversal().V().values(PageRankVertexProgram.PAGE_RANK).sum().next().doubleValue();
assertEquals(1.0d, sum, 0.01d);
}
}
代码示例来源:origin: apache/tinkerpop
@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));
}
代码示例来源:origin: apache/tinkerpop
public void shouldExecutePageRankWithEpsilonBreak() throws Exception {
if (graphProvider.getGraphComputer(graph).features().supportsResultGraphPersistCombination(GraphComputer.ResultGraph.NEW, GraphComputer.Persist.VERTEX_PROPERTIES)) {
final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()).
program(PageRankVertexProgram.build().epsilon(0.00001d).iterations(30).create(graph)).submit().get(); // by using epsilon 0.00001, we should get iterations 11
result.graph().traversal().V().forEachRemaining(v -> {
代码示例来源:origin: apache/tinkerpop
public void shouldExecutePageRankWithIterationsBreak() throws Exception {
if (graphProvider.getGraphComputer(graph).features().supportsResultGraphPersistCombination(GraphComputer.ResultGraph.NEW, GraphComputer.Persist.VERTEX_PROPERTIES)) {
final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()).
program(PageRankVertexProgram.build().epsilon(0.0d).iterations(30).create(graph)).submit().get(); // by using epsilon 0.0, we guarantee iterations 30
result.graph().traversal().V().forEachRemaining(v -> {
代码示例来源:origin: apache/tinkerpop
@Test
@LoadGraphWith(CREW)
public void shouldFindEqualLengthPaths() throws Exception {
final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()).
program(ShortestPathVertexProgram.build()
.edgeTraversal(__.bothE("uses"))
.source(__.has("name", "daniel"))
.target(__.has("name", "stephen")).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.asList(
helper.makePath("daniel", "gremlin", "stephen"),
helper.makePath("daniel", "tinkergraph", "stephen"));
helper.checkResults(expected, shortestPaths);
}
代码示例来源:origin: apache/tinkerpop
@Test
@LoadGraphWith(MODERN)
public void shouldNotAllowBadGraphComputers() {
try {
graph.compute(BadGraphComputer.class);
fail("Providing a bad graph computer class should fail");
} catch (Exception ex) {
validateException(Graph.Exceptions.graphDoesNotSupportProvidedGraphComputer(BadGraphComputer.class), ex);
}
if (!new BadGraphComputer().features().supportsGraphFilter()) {
try {
new BadGraphComputer().vertices(__.hasLabel("software"));
fail("Should throw an unsupported operation exception");
} catch (final UnsupportedOperationException e) {
assertEquals(GraphComputer.Exceptions.graphFilterNotSupported().getMessage(), e.getMessage());
}
try {
new BadGraphComputer().edges(__.bothE());
fail("Should throw an unsupported operation exception");
} catch (final UnsupportedOperationException e) {
assertEquals(GraphComputer.Exceptions.graphFilterNotSupported().getMessage(), e.getMessage());
}
} else {
fail("Should not support graph filter: " + BadGraphComputer.class);
}
}
代码示例来源:origin: apache/tinkerpop
@Test
@LoadGraphWith(GRATEFUL)
public void shouldFindEqualLengthPathsUsingDistanceProperty() throws Exception {
final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()).
program(ShortestPathVertexProgram.build()
.edgeTraversal(__.outE("followedBy"))
.source(__.has("song", "name", "MIGHT AS WELL"))
.target(__.has("song", "name", "MAYBE YOU KNOW HOW I FEEL"))
.distanceProperty("weight")
.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.asList(
helper.makePath("MIGHT AS WELL", "DRUMS", "MAYBE YOU KNOW HOW I FEEL"),
helper.makePath("MIGHT AS WELL", "SHIP OF FOOLS", "MAYBE YOU KNOW HOW I FEEL"));
helper.checkResults(expected, shortestPaths);
}
内容来源于网络,如有侵权,请联系作者删除!