@Override public GraphTraversalSource traversal(final Graph graph) { return graph.traversal(); }
@Override public GraphTraversalSource traversal(final Graph graph, final TraversalStrategy... strategies) { return graph.traversal().withStrategies(strategies); }
private void updateTraversalSource(String graphName, Graph graph, GremlinExecutor gremlinExecutor, JanusGraphManager graphManager){ gremlinExecutor.getScriptEngineManager().put(graphName, graph); String traversalName = graphName + "_traversal"; TraversalSource traversalSource = graph.traversal(); gremlinExecutor.getScriptEngineManager().put(traversalName, traversalSource); graphManager.putTraversalSource(traversalName, traversalSource); }
/** * Create a {@link GraphTraversalSource} from a {@link Graph} instance. The default implementation does not * use {@link GraphComputer} so providers should override as necessary if their implementation is testing * something that requires a different engine type, like {@link GraphComputer}. */ public default GraphTraversalSource traversal(final Graph graph) { return graph.traversal(); }
@Test public void testReadWideVertexWithManyProperties() { int numProps = 1 << 16; long numV = 1; mgmt.makePropertyKey("p").cardinality(Cardinality.LIST).dataType(Integer.class).make(); mgmt.commit(); finishSchema(); for (int j = 0; j < numV; j++) { Vertex v = graph.addVertex(); for (int i = 0; i < numProps; i++) { v.property("p", i); } } graph.tx().commit(); assertEquals(numV, (long) graph.traversal().V().count().next()); Map<String, Object> propertiesOnVertex = graph.traversal().V().valueMap().next(); List<?> valuesOnP = (List)propertiesOnVertex.values().iterator().next(); assertEquals(numProps, valuesOnP.size()); Graph g = GraphFactory.open("target/test-classes/cassandra-read.properties"); GraphTraversalSource t = g.traversal(GraphTraversalSource.computer(SparkGraphComputer.class)); assertEquals(numV, (long) t.V().count().next()); propertiesOnVertex = t.V().valueMap().next(); valuesOnP = (List)propertiesOnVertex.values().iterator().next(); assertEquals(numProps, valuesOnP.size()); }
/** * Converts SPARQL to a Gremlin traversal. * * @param graph the {@link Graph} instance to execute the traversal from * @param sparqlQuery the query to compile to Gremlin */ public static GraphTraversal<Vertex, ?> compile(final Graph graph, final String sparqlQuery) { return compile(graph.traversal(), sparqlQuery); }
@Test public void testReadGraphOfTheGods() { GraphOfTheGodsFactory.load(graph, null, true); assertEquals(12L, (long) graph.traversal().V().count().next()); Graph g = GraphFactory.open("target/test-classes/cassandra-read.properties"); GraphTraversalSource t = g.traversal(GraphTraversalSource.computer(SparkGraphComputer.class)); assertEquals(12L, (long) t.V().count().next()); }
@Setup(Level.Invocation) public void prepare() { graph = TinkerGraph.open(); g = graph.traversal(); } }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowBytecodeEvalWithInvalidBinding() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); final Graph graph = EmptyGraph.instance(); final GraphTraversalSource g = graph.traversal(); // purposefully use "x" to match the name of the traversal source binding for "x" below and // thus tests the alias added for "x" final GraphTraversal t = getTraversalWithLambda(g); final Bindings bindings = new SimpleBindings(); bindings.put("z", g); bindings.put("x", "invalid-binding-for-x-given-x-should-be-traversal-source"); scriptEngine.eval(t.asAdmin().getBytecode(), bindings, "x"); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowBytecodeEvalWithAliasInBindings() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); final Graph graph = EmptyGraph.instance(); final GraphTraversalSource g = graph.traversal(); // purposefully use "x" to match the name of the traversal source binding for "x" below and // thus tests the alias added for "x" final GraphTraversal t = getTraversalWithLambda(g); final Bindings bindings = new SimpleBindings(); bindings.put("x", g); bindings.put(GremlinScriptEngine.HIDDEN_G, g); scriptEngine.eval(t.asAdmin().getBytecode(), bindings, "x"); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowBytecodeEvalWithMissingBinding() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); final Graph graph = EmptyGraph.instance(); final GraphTraversalSource g = graph.traversal(); // purposefully use "x" to match the name of the traversal source binding for "x" below and // thus tests the alias added for "x" final GraphTraversal t = getTraversalWithLambda(g); final Bindings bindings = new SimpleBindings(); bindings.put("z", g); scriptEngine.eval(t.asAdmin().getBytecode(), bindings, "x"); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowBytecodeEvalWithAliasAsTraversalSource() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); final Graph graph = EmptyGraph.instance(); final GraphTraversalSource g = graph.traversal(); // purposefully use "x" to match the name of the traversal source binding for "x" below and // thus tests the alias added for "x" final GraphTraversal t = getTraversalWithLambda(g); final Bindings bindings = new SimpleBindings(); bindings.put("x", g); scriptEngine.eval(t.asAdmin().getBytecode(), bindings, GremlinScriptEngine.HIDDEN_G); }
@Test public void shouldEvalBytecode() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); final Graph graph = EmptyGraph.instance(); final GraphTraversalSource g = graph.traversal(); // purposefully use "x" to match the name of the traversal source binding for "x" below and // thus tests the alias added for "x" final GraphTraversal t = getTraversalWithLambda(g); final Bindings bindings = new SimpleBindings(); bindings.put("x", g); final Traversal evald = scriptEngine.eval(t.asAdmin().getBytecode(), bindings, "x"); assertTraversals(t, evald); assertThat(manager.getBindings().containsKey(GremlinScriptEngine.HIDDEN_G), is(false)); }
@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); } }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldGenerateDefaultIdOnGraphAddVWithGeneratedDefaultId() throws Exception { final ElementIdStrategy strategy = ElementIdStrategy.build().create(); final GraphTraversalSource sg = create(strategy); final Vertex v = sg.addV().property("name", "stephen").next(); assertEquals("stephen", v.value("name")); final Traversal t1 = graph.traversal().V(v); t1.asAdmin().applyStrategies(); logger.info(t1.toString()); final Traversal t2 = sg.V(v); t2.asAdmin().applyStrategies(); logger.info(t2.toString()); assertNotNull(UUID.fromString(sg.V(v).id().next().toString())); }
@Test @LoadGraphWith(MODERN) public void shouldSupportMultipleScopes() throws ExecutionException, InterruptedException { final ComputerResult result = graphProvider.getGraphComputer(graph).program(new MultiScopeVertexProgram()).submit().get(); assertEquals(result.graph().traversal().V().has("name", "josh").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 0L); assertEquals(result.graph().traversal().V().has("name", "lop").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 1L); assertEquals(result.graph().traversal().V().has("name", "ripple").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 1L); assertEquals(result.graph().traversal().V().has("name", "marko").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 2L); }
private GraphTraversal<Vertex, Vertex> runMPTest(final Direction direction) throws Exception { final VertexProgramR svp = VertexProgramR.build().direction(direction).create(); final ComputerResult result = graphProvider.getGraphComputer(graph).program(svp).vertices(__.hasLabel(VertexProgramR.VERTEX_LABEL)).submit().get(); return result.graph().traversal().V().hasLabel(VertexProgramR.VERTEX_LABEL); }
@Test @LoadGraphWith(MODERN) public void shouldSupportMultipleScopesWithEdgeFunction() throws ExecutionException, InterruptedException { final ComputerResult result = graphProvider.getGraphComputer(graph).program(new MultiScopeVertexWithEdgeFunctionProgram()).submit().get(); assertEquals(result.graph().traversal().V().has("name", "josh").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 0L); assertEquals(result.graph().traversal().V().has("name", "lop").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 4L); assertEquals(result.graph().traversal().V().has("name", "ripple").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 10L); assertEquals(result.graph().traversal().V().has("name", "marko").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 20L); }
@Test @LoadGraphWith(MODERN) @IgnoreEngine(TraversalEngine.Type.COMPUTER) // we can't modify the graph in computer mode public void shouldProperlyHandleMetaProperties() throws Exception { graph.traversal().V().has("name", "marko").properties("name").property("alias", "okram").iterate(); final BulkLoaderVertexProgram blvp = BulkLoaderVertexProgram.build() .userSuppliedIds(true) .writeGraph(getWriteGraphConfiguration()).create(graph); graphProvider.getGraphComputer(graph).workers(1).program(blvp).submit().get(); assertGraphEquality(graph, getWriteGraph()); }
private void testOr(final Graph aGraph) { final GraphTraversalSource g = aGraph.traversal(); final Vertex hiro = g.addV().property("name", "Hiro").property("age", 2).property("length", 90).next(); final Vertex totoro = g.addV().property("name", "Totoro").property("age", 1).next();