public static SubgraphStrategy create(final Configuration configuration) { final Builder builder = SubgraphStrategy.build(); if (configuration.containsKey(VERTICES)) builder.vertices((Traversal) configuration.getProperty(VERTICES)); if (configuration.containsKey(EDGES)) builder.edges((Traversal) configuration.getProperty(EDGES)); if (configuration.containsKey(VERTEX_PROPERTIES)) builder.vertexProperties((Traversal) configuration.getProperty(VERTEX_PROPERTIES)); if (configuration.containsKey(CHECK_ADJACENT_VERTICES)) builder.checkAdjacentVertices(configuration.getBoolean(CHECK_ADJACENT_VERTICES)); return builder.create(); }
public SubgraphStrategy create() { if (null == this.vertexCriterion && null == this.edgeCriterion && null == this.vertexPropertyCriterion) throw new IllegalStateException("A subgraph must be filtered by a vertex, edge, or vertex property criterion"); return new SubgraphStrategy(this); } }
if (step instanceof TraversalParent) { for (final Traversal.Admin t : ((TraversalParent) step).getLocalChildren()) { this.apply(t); t.getStartStep().addLabel(MARKER); this.apply(t); t.getStartStep().addLabel(MARKER); vertexStepsToInsertFilterAfter.addAll(TraversalHelper.getStepsOfAssignableClass(AddVertexStartStep.class, traversal)); vertexStepsToInsertFilterAfter.addAll(graphSteps.stream().filter(GraphStep::returnsVertex).collect(Collectors.toList())); applyCriterion(vertexStepsToInsertFilterAfter, traversal, this.vertexCriterion); edgeStepsToInsertFilterAfter.addAll(graphSteps.stream().filter(GraphStep::returnsEdge).collect(Collectors.toList())); edgeStepsToInsertFilterAfter.addAll(vertexSteps.stream().filter(VertexStep::returnsEdge).collect(Collectors.toList())); applyCriterion(edgeStepsToInsertFilterAfter, traversal, this.edgeCriterion); if (step instanceof TraversalParent) { if (step instanceof PropertyMapStep) { final char propertyType = processesPropertyType(step.getPreviousStep()); if ('p' != propertyType) { final Traversal.Admin<?, ?> temp = new DefaultTraversal<>(); .filter(t -> t instanceof ElementValueTraversal) .forEach(t -> { final char propertyType = processesPropertyType(step.getPreviousStep()); if ('p' != propertyType) { final Traversal.Admin<?, ?> temp = new DefaultTraversal<>(); final char propertyType = processesPropertyType(step.getPreviousStep());
@Test @LoadGraphWith(CREW) public void shouldFilterVertexProperties() throws Exception { GraphTraversalSource sg = g.withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{ put(SubgraphStrategy.VERTEX_PROPERTIES, __.has("startTime", P.gt(2005))); }}))); sg = g.withStrategies(SubgraphStrategy.build().vertexProperties(has("startTime", P.gt(2005))).create()); checkResults(Arrays.asList("purcellville", "baltimore", "oakland", "seattle", "aachen"), sg.V().as("a").properties("location").as("b").select("a").outE().properties().select("b").value().dedup()); checkResults(Arrays.asList("purcellville", "baltimore", "oakland", "seattle", "aachen"), sg.V().as("a").values("location").as("b").select("a").outE().properties().select("b").dedup()); sg = g.withStrategies(SubgraphStrategy.build().vertices(has("name", P.neq("stephen"))).vertexProperties(has("startTime", P.gt(2005))).create()); checkResults(Arrays.asList("baltimore", "oakland", "seattle", "aachen"), sg.V().properties("location").value()); checkResults(Arrays.asList("baltimore", "oakland", "seattle", "aachen"), sg.V().values("location")); sg = g.withStrategies(SubgraphStrategy.build().vertices(has("name", P.not(P.within("stephen", "daniel")))).vertexProperties(has("startTime", P.gt(2005))).create()); checkResults(Arrays.asList("baltimore", "oakland", "seattle"), sg.V().properties("location").value()); checkResults(Arrays.asList("baltimore", "oakland", "seattle"), sg.V().values("location")); sg = g.withStrategies(SubgraphStrategy.build().vertices(has("name", P.eq("matthias"))).vertexProperties(has("startTime", P.gte(2014))).create()); checkResults(makeMapList(1, "seattle", 1L), sg.V().groupCount().by("location")); sg = g.withStrategies(SubgraphStrategy.build().vertices(has("location")).vertexProperties(hasNot("endTime")).create()); checkOrderedResults(Arrays.asList("aachen", "purcellville", "santa fe", "seattle"), sg.V().order().by("location", Order.asc).values("location")); sg = g.withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{ put(SubgraphStrategy.VERTICES, __.has("location")); put(SubgraphStrategy.VERTEX_PROPERTIES, __.hasNot("endTime")); sg = g.withStrategies(SubgraphStrategy.build().edges(__.<Edge>hasLabel("uses").has("skill", 5)).create());
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldHandleStrategies() throws Exception { final GraphTraversalSource g = graph.traversal().withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{ put(SubgraphStrategy.VERTICES, __.has("name", "marko")); }}))); final Bindings bindings = new SimpleBindings(); bindings.put("g", g); Traversal.Admin<Vertex, Object> traversal = engine.eval(g.V().values("name").asAdmin().getBytecode(), bindings, "g"); assertEquals("marko", traversal.next()); assertFalse(traversal.hasNext()); // traversal = engine.eval(g.withoutStrategies(SubgraphStrategy.class).V().count().asAdmin().getBytecode(), bindings, "g"); assertEquals(new Long(6), traversal.next()); assertFalse(traversal.hasNext()); // traversal = engine.eval(g.withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{ put(SubgraphStrategy.VERTICES, __.has("name", "marko")); }})), ReadOnlyStrategy.instance()).V().values("name").asAdmin().getBytecode(), bindings, "g"); assertEquals("marko", traversal.next()); assertFalse(traversal.hasNext()); }
@Test @LoadGraphWith(CREW) public void shouldFilterVertexProperties() throws Exception { GraphTraversalSource sg = g.withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{ put(SubgraphStrategy.VERTEX_PROPERTIES, __.has("startTime", P.gt(2005))); }}))); sg = g.withStrategies(SubgraphStrategy.build().vertexProperties(has("startTime", P.gt(2005))).create()); checkResults(Arrays.asList("purcellville", "baltimore", "oakland", "seattle", "aachen"), sg.V().as("a").properties("location").as("b").select("a").outE().properties().select("b").value().dedup()); checkResults(Arrays.asList("purcellville", "baltimore", "oakland", "seattle", "aachen"), sg.V().as("a").values("location").as("b").select("a").outE().properties().select("b").dedup()); sg = g.withStrategies(SubgraphStrategy.build().vertices(has("name", P.neq("stephen"))).vertexProperties(has("startTime", P.gt(2005))).create()); checkResults(Arrays.asList("baltimore", "oakland", "seattle", "aachen"), sg.V().properties("location").value()); checkResults(Arrays.asList("baltimore", "oakland", "seattle", "aachen"), sg.V().values("location")); sg = g.withStrategies(SubgraphStrategy.build().vertices(has("name", P.not(P.within("stephen", "daniel")))).vertexProperties(has("startTime", P.gt(2005))).create()); checkResults(Arrays.asList("baltimore", "oakland", "seattle"), sg.V().properties("location").value()); checkResults(Arrays.asList("baltimore", "oakland", "seattle"), sg.V().values("location")); sg = g.withStrategies(SubgraphStrategy.build().vertices(has("name", P.eq("matthias"))).vertexProperties(has("startTime", P.gte(2014))).create()); checkResults(makeMapList(1, "seattle", 1L), sg.V().groupCount().by("location")); sg = g.withStrategies(SubgraphStrategy.build().vertices(has("location")).vertexProperties(hasNot("endTime")).create()); checkOrderedResults(Arrays.asList("aachen", "purcellville", "santa fe", "seattle"), sg.V().order().by("location", Order.asc).values("location")); sg = g.withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{ put(SubgraphStrategy.VERTICES, __.has("location")); put(SubgraphStrategy.VERTEX_PROPERTIES, __.hasNot("endTime")); sg = g.withStrategies(SubgraphStrategy.build().edges(__.<Edge>hasLabel("uses").has("skill", 5)).create());
@Test @LoadGraphWith(MODERN) public void shouldFilterComplexVertexCriterion() throws Exception { checkResults(Arrays.asList("vadas", "josh"), g.withStrategies(SubgraphStrategy.build().vertices(__.<Vertex>in("knows").has("name", "marko")).create()). V().values("name")); checkResults(Arrays.asList("vadas", "josh", "lop"), g.withStrategies(SubgraphStrategy.build().vertices(__.<Vertex>in().has("name", "marko")).create()). V().values("name")); checkResults(Arrays.asList("vadas", "josh"), g.withStrategies(SubgraphStrategy.build().vertices(__.<Vertex>in("knows").where(out("created").has("name", "lop"))).create()). V().values("name")); checkResults(Arrays.asList("vadas", "josh", "lop"), g.withStrategies(SubgraphStrategy.build().vertices(__.<Vertex>in().where(has("name", "marko").out("created").has("name", "lop"))).create()). V().values("name")); checkResults(Arrays.asList("marko", "vadas", "josh", "lop"), g.withStrategies(SubgraphStrategy.build().vertices(__.or(both().has("name", "marko"), has("name", "marko"))).create()). V().where(bothE().count().is(P.neq(0))).values("name")); }
if (step instanceof TraversalParent) { for (final Traversal.Admin t : ((TraversalParent) step).getLocalChildren()) { this.apply(t); t.getStartStep().addLabel(MARKER); this.apply(t); t.getStartStep().addLabel(MARKER); vertexStepsToInsertFilterAfter.addAll(TraversalHelper.getStepsOfAssignableClass(AddVertexStartStep.class, traversal)); vertexStepsToInsertFilterAfter.addAll(graphSteps.stream().filter(GraphStep::returnsVertex).collect(Collectors.toList())); applyCriterion(vertexStepsToInsertFilterAfter, traversal, this.vertexCriterion); edgeStepsToInsertFilterAfter.addAll(graphSteps.stream().filter(GraphStep::returnsEdge).collect(Collectors.toList())); edgeStepsToInsertFilterAfter.addAll(vertexSteps.stream().filter(VertexStep::returnsEdge).collect(Collectors.toList())); applyCriterion(edgeStepsToInsertFilterAfter, traversal, this.edgeCriterion); if (step instanceof TraversalParent) { if (step instanceof PropertyMapStep) { final char propertyType = processesPropertyType(step.getPreviousStep()); if ('p' != propertyType) { final Traversal.Admin<?, ?> temp = new DefaultTraversal<>(); .filter(t -> t instanceof ElementValueTraversal) .forEach(t -> { final char propertyType = processesPropertyType(step.getPreviousStep()); if ('p' != propertyType) { final Traversal.Admin<?, ?> temp = new DefaultTraversal<>(); final char propertyType = processesPropertyType(step.getPreviousStep());
public SubgraphStrategy create() { if (null == this.vertexCriterion && null == this.edgeCriterion && null == this.vertexPropertyCriterion) throw new IllegalStateException("A subgraph must be filtered by a vertex, edge, or vertex property criterion"); return new SubgraphStrategy(this); } }
@Test(expected = NoSuchElementException.class) @LoadGraphWith(MODERN) public void shouldGetExcludedVertex() throws Exception { final Traversal<Vertex, ?> vertexCriterion = has("name", P.within("josh", "lop", "ripple")); final GraphTraversalSource sg = g.withStrategies(SubgraphStrategy.build().vertices(vertexCriterion).create()); sg.V(convertToVertexId("marko")).next(); }
@Test @LoadGraphWith(MODERN) public void shouldFilterVertexCriterionAndKeepLabels() throws Exception { // this will exclude "peter" final Traversal<Vertex, ?> vertexCriterion = has("name", P.within("ripple", "josh", "marko")); final GraphTraversalSource sg = g.withStrategies(SubgraphStrategy.build().vertices(vertexCriterion).create()); assertEquals(9, g.V().as("a").out().in().as("b").dedup("a", "b").count().next().intValue()); assertEquals(2, sg.V().as("a").out().in().as("b").dedup("a", "b").count().next().intValue()); final List<Object> list = sg.V().as("a").out().in().as("b").dedup("a", "b").values("name").toList(); assertThat(list, hasItems("marko", "josh")); }
@Benchmark public GraphTraversal.Admin<Vertex,Vertex> testTranslationWithStrategy() { return translator.translate(g.withStrategies(ReadOnlyStrategy.instance()) .withStrategies(SubgraphStrategy.build().vertices(hasLabel("person")).create()) .V().out().in("link").out().in("link").asAdmin().getBytecode()); } }
@Test(expected = NoSuchElementException.class) @LoadGraphWith(MODERN) public void shouldGetExcludedEdge() throws Exception { final Traversal<Edge, ?> edgeCriterion = __.or( has("weight", 1.0d).hasLabel("knows"), // 8 has("weight", 0.4d).hasLabel("created").outV().has("name", "marko"), // 9 has("weight", 1.0d).hasLabel("created") // 10 ); final GraphTraversalSource sg = g.withStrategies(SubgraphStrategy.build().edges(edgeCriterion).create()); sg.E(sg.E(convertToEdgeId("marko", "knows", "vadas")).next()).next(); }
); final SubgraphStrategy strategy = SubgraphStrategy.build().edges(edgeCriterion).vertices(vertexCriterion).create(); final GraphTraversalSource sg = g.withStrategies(strategy);
final Traversal<Vertex, ?> vertexCriterion = has("name", P.within("josh", "lop", "ripple")); final GraphTraversalSource sg = g.withStrategies(SubgraphStrategy.build().vertices(vertexCriterion).create());
); final SubgraphStrategy strategy = SubgraphStrategy.build(). checkAdjacentVertices(false). edges(edgeCriterion).vertices(vertexCriterion).create();
); final SubgraphStrategy strategy = SubgraphStrategy.build().edges(edgeCriterion).create(); final GraphTraversalSource sg = g.withStrategies(strategy);
SubgraphStrategy strategy; strategy = SubgraphStrategy.build().vertices(has("name", P.within("josh", "lop", "ripple"))).create(); sg = g.withStrategies(strategy); assertTrue(traversal.getTraverserGenerator() instanceof B_LP_O_P_S_SE_SL_TraverserGenerator); strategy = SubgraphStrategy.build().vertices(__.filter(__.simplePath())).create(); sg = g.withStrategies(strategy);
public static SubgraphStrategy create(final Configuration configuration) { final Builder builder = SubgraphStrategy.build(); if (configuration.containsKey(VERTICES)) builder.vertices((Traversal) configuration.getProperty(VERTICES)); if (configuration.containsKey(EDGES)) builder.edges((Traversal) configuration.getProperty(EDGES)); if (configuration.containsKey(VERTEX_PROPERTIES)) builder.vertexProperties((Traversal) configuration.getProperty(VERTEX_PROPERTIES)); if (configuration.containsKey(CHECK_ADJACENT_VERTICES)) builder.checkAdjacentVertices(configuration.getBoolean(CHECK_ADJACENT_VERTICES)); return builder.create(); }
@Test @LoadGraphWith(MODERN) public void shouldFilterComplexVertexCriterion() throws Exception { checkResults(Arrays.asList("vadas", "josh"), g.withStrategies(SubgraphStrategy.build().vertices(__.<Vertex>in("knows").has("name", "marko")).create()). V().values("name")); checkResults(Arrays.asList("vadas", "josh", "lop"), g.withStrategies(SubgraphStrategy.build().vertices(__.<Vertex>in().has("name", "marko")).create()). V().values("name")); checkResults(Arrays.asList("vadas", "josh"), g.withStrategies(SubgraphStrategy.build().vertices(__.<Vertex>in("knows").where(out("created").has("name", "lop"))).create()). V().values("name")); checkResults(Arrays.asList("vadas", "josh", "lop"), g.withStrategies(SubgraphStrategy.build().vertices(__.<Vertex>in().where(has("name", "marko").out("created").has("name", "lop"))).create()). V().values("name")); checkResults(Arrays.asList("marko", "vadas", "josh", "lop"), g.withStrategies(SubgraphStrategy.build().vertices(__.or(both().has("name", "marko"), has("name", "marko"))).create()). V().where(bothE().count().is(P.neq(0))).values("name")); }