org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ComputerVerificationStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBeVerifiedIllegal() {

    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(ComputerVerificationStrategy.instance());
    this.traversal.asAdmin().setParent(new TraversalVertexProgramStep(EmptyTraversal.instance(), EmptyTraversal.instance())); // trick it
    this.traversal.asAdmin().setStrategies(strategies);
    try {
        this.traversal.asAdmin().applyStrategies();
        if (!this.legal)
            fail("The traversal should not be allowed: " + this.traversal);
    } catch (final VerificationException ise) {
        if (this.legal)
            fail("The traversal should be allowed: " + this.traversal);
    }
}
 
Example #2
Source File: TinkerGraphCountStrategyTest.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> generateTestParameters() {
    return Arrays.asList(new Object[][]{
            {__.V().count(), countStep(Vertex.class), Collections.emptyList()},
            {__.V().count(), countStep(Vertex.class), TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().as("a").count(), countStep(Vertex.class), TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().count().as("a"), countStep(Vertex.class), TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().map(out()).count().as("a"), null, TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().map(out()).identity().count().as("a"), null, TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().map(out().groupCount()).identity().count().as("a"), null, TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().label().map(s -> s.get().length()).count(), null, TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().as("a").map(select("a")).count(), null, TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            //
            {__.V(), null, Collections.emptyList()},
            {__.V().out().count(), null, Collections.emptyList()},
            {__.V(1).count(), null, Collections.emptyList()},
            {__.count(), null, Collections.emptyList()},
            {__.V().map(out().groupCount("m")).identity().count().as("a"), null, Collections.emptyList()},
    });
}
 
Example #3
Source File: SubgraphStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void doTest() {
    final TraversalStrategies originalStrategies = new DefaultTraversalStrategies();
    originalStrategies.addStrategies(SubgraphStrategy.build().
            vertices(__.and(has("name", "marko"), has("age", 29))).
            edges(hasLabel("knows")).
            vertexProperties(__.<VertexProperty, Long>values().count().and(is(P.lt(10)), is(0))).create());
    originalStrategies.addStrategies(InlineFilterStrategy.instance());
    originalStrategies.addStrategies(StandardVerificationStrategy.instance());
    this.original.asAdmin().setStrategies(originalStrategies);
    this.original.asAdmin().applyStrategies();
    final TraversalStrategies optimizedStrategies = new DefaultTraversalStrategies();
    optimizedStrategies.addStrategies(InlineFilterStrategy.instance());
    this.optimized.asAdmin().setStrategies(optimizedStrategies);
    this.optimized.asAdmin().applyStrategies();
    assertEquals(this.optimized, this.original);
}
 
Example #4
Source File: TinkerGraphCountStrategyTest.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
public void doTest() {
    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(TinkerGraphCountStrategy.instance());
    for (final TraversalStrategy strategy : this.otherStrategies) {
        strategies.addStrategies(strategy);
    }
    if (this.optimized == null) {
        this.optimized = this.original.asAdmin().clone();
        this.optimized.asAdmin().setStrategies(strategies);
        this.optimized.asAdmin().applyStrategies();
    }
    this.original.asAdmin().setStrategies(strategies);
    this.original.asAdmin().applyStrategies();
    assertEquals(this.optimized, this.original);
}
 
Example #5
Source File: LambdaRestrictionStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBeVerifiedIllegal() {
    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(ProfileStrategy.instance());
    strategies.addStrategies(LambdaRestrictionStrategy.instance());
    traversal.asAdmin().setStrategies(strategies);
    if (allow) {
        traversal.asAdmin().applyStrategies();
    } else {
        try {
            traversal.asAdmin().applyStrategies();
            fail("The strategy should not allow lambdas: " + this.traversal);
        } catch (VerificationException ise) {
            assertTrue(ise.getMessage().contains("lambda"));
        }
    }
}
 
Example #6
Source File: ReservedKeysVerificationStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldOnlyThrow() {
    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(ReservedKeysVerificationStrategy.build().throwException().create());
    final Traversal traversal = this.traversal.asAdmin().clone();
    traversal.asAdmin().setStrategies(strategies);
    if (allow) {
        traversal.asAdmin().applyStrategies();
    } else {
        try {
            traversal.asAdmin().applyStrategies();
            fail("The strategy should not allow vertex steps with unspecified edge labels: " + this.traversal);
        } catch (VerificationException ise) {
            assertTrue(MSG_PREDICATE.test(ise.getMessage()));
        }
    }
    assertTrue(logAppender.isEmpty());
}
 
Example #7
Source File: TinkerGraphCountStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> generateTestParameters() {
    return Arrays.asList(new Object[][]{
            {__.V().count(), countStep(Vertex.class), Collections.emptyList()},
            {__.V().count(), countStep(Vertex.class), TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().as("a").count(), countStep(Vertex.class), TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().count().as("a"), countStep(Vertex.class), TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().map(out()).count().as("a"), null, TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().map(out()).identity().count().as("a"), null, TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().map(out().groupCount()).identity().count().as("a"), null, TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().label().map(s -> s.get().length()).count(), null, TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            {__.V().as("a").map(select("a")).count(), null, TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList()},
            //
            {__.V(), null, Collections.emptyList()},
            {__.V().out().count(), null, Collections.emptyList()},
            {__.V(1).count(), null, Collections.emptyList()},
            {__.count(), null, Collections.emptyList()},
            {__.V().map(out().groupCount("m")).identity().count().as("a"), null, Collections.emptyList()},
    });
}
 
Example #8
Source File: TinkerGraphCountStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void doTest() {
    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(TinkerGraphCountStrategy.instance());
    for (final TraversalStrategy strategy : this.otherStrategies) {
        strategies.addStrategies(strategy);
    }
    if (this.optimized == null) {
        this.optimized = this.original.asAdmin().clone();
        this.optimized.asAdmin().setStrategies(strategies);
        this.optimized.asAdmin().applyStrategies();
    }
    this.original.asAdmin().setStrategies(strategies);
    this.original.asAdmin().applyStrategies();
    assertEquals(this.optimized, this.original);
}
 
Example #9
Source File: DefaultTraversalStrategiesTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void testWellDefinedDependency3() {
    //Dependency well defined
    TraversalStrategies s = new DefaultTraversalStrategies();
    s.addStrategies(d, c, a, e, b);
    assertEquals(5, s.toList().size());
    assertEquals(a, s.toList().get(0));
    assertEquals(b, s.toList().get(1));
    assertEquals(d, s.toList().get(2));
    assertEquals(c, s.toList().get(3));
    assertEquals(e, s.toList().get(4));
    s = s.clone();
    assertEquals(5, s.toList().size());
    assertEquals(a, s.toList().get(0));
    assertEquals(b, s.toList().get(1));
    assertEquals(d, s.toList().get(2));
    assertEquals(c, s.toList().get(3));
    assertEquals(e, s.toList().get(4));
}
 
Example #10
Source File: EdgeLabelVerificationStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldThrowAndLog() {
    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(EdgeLabelVerificationStrategy.build().throwException().logWarning().create());
    final Traversal traversal = this.traversal.asAdmin().clone();
    traversal.asAdmin().setStrategies(strategies);
    if (allow) {
        traversal.asAdmin().applyStrategies();
        assertTrue(logAppender.isEmpty());
    } else {
        try {
            traversal.asAdmin().applyStrategies();
            fail("The strategy should not allow vertex steps with unspecified edge labels: " + this.traversal);
        } catch (VerificationException ise) {
            assertTrue(MSG_PREDICATE.test(ise.getMessage()));
        }
        assertTrue(String.format("Expected log entry not found in %s", logAppender.messages),
                logAppender.messages().anyMatch(MSG_PREDICATE));
    }
}
 
Example #11
Source File: ConnectedComponentVertexProgramStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public ConnectedComponentVertexProgram generateProgram(final Graph graph, final Memory memory) {
    final Traversal.Admin<Vertex, Edge> detachedTraversal = this.edgeTraversal.getPure();
    detachedTraversal.setStrategies(TraversalStrategies.GlobalCache.getStrategies(graph.getClass()));

    final ConnectedComponentVertexProgram.Builder builder = ConnectedComponentVertexProgram.build().
            edges(detachedTraversal).
            property(this.clusterProperty);

    if (memory.exists(TraversalVertexProgram.HALTED_TRAVERSERS)) {
        final TraverserSet<?> haltedTraversers = memory.get(TraversalVertexProgram.HALTED_TRAVERSERS);
        if (!haltedTraversers.isEmpty()) {
            Object haltedTraversersValue;
            try {
                haltedTraversersValue = Base64.getEncoder().encodeToString(Serializer.serializeObject(haltedTraversers));
            } catch (final IOException ignored) {
                haltedTraversersValue = haltedTraversers;
            }
            builder.configure(TraversalVertexProgram.HALTED_TRAVERSERS, haltedTraversersValue);
        }
    }

    return builder.create(graph);
}
 
Example #12
Source File: ReservedKeysVerificationStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldThrowAndLog() {
    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(ReservedKeysVerificationStrategy.build().throwException().logWarning().create());
    final Traversal traversal = this.traversal.asAdmin().clone();
    traversal.asAdmin().setStrategies(strategies);
    if (allow) {
        traversal.asAdmin().applyStrategies();
        assertTrue(logAppender.isEmpty());
    } else {
        try {
            traversal.asAdmin().applyStrategies();
            fail("The strategy should not allow vertex steps with unspecified edge labels: " + this.traversal);
        } catch (VerificationException ise) {
            assertTrue(MSG_PREDICATE.test(ise.getMessage()));
        }
        assertTrue(String.format("Expected log entry not found in %s", logAppender.messages),
                logAppender.messages().anyMatch(MSG_PREDICATE));
    }
}
 
Example #13
Source File: TraversalVertexProgramStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@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()),
            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);
}
 
Example #14
Source File: EdgeLabelVerificationStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldOnlyThrow() {
    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(EdgeLabelVerificationStrategy.build().throwException().create());
    final Traversal traversal = this.traversal.asAdmin().clone();
    traversal.asAdmin().setStrategies(strategies);
    if (allow) {
        traversal.asAdmin().applyStrategies();
    } else {
        try {
            traversal.asAdmin().applyStrategies();
            fail("The strategy should not allow vertex steps with unspecified edge labels: " + this.traversal);
        } catch (VerificationException ise) {
            assertTrue(MSG_PREDICATE.test(ise.getMessage()));
        }
    }
    assertTrue(logAppender.isEmpty());
}
 
Example #15
Source File: PathProcessorStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> generateTestParameters() {

    return Arrays.asList(new Object[][]{
            // select("a")
            {__.select("a"), __.select("a"), Collections.emptyList()},
            {__.select("a").by(), __.select("a").by(), Collections.emptyList()},
            {__.select("a").by(__.outE().count()), __.select("a").map(__.outE().count()), Collections.emptyList()},
            {__.select("a").by("name"), __.select("a").map(new ValueTraversal<>("name")), Collections.emptyList()},
            {__.select("a").out(), __.select("a").out(), Collections.emptyList()},
            {__.select(Pop.all, "a").by(__.values("name")), __.select(Pop.all, "a").by("name"), TraversalStrategies.GlobalCache.getStrategies(Graph.class).toList()},
            {__.select(Pop.last, "a").by(__.values("name")), __.select(Pop.last, "a").map(__.values("name")), TraversalStrategies.GlobalCache.getStrategies(Graph.class).toList()},
            {__.select(Pop.first, "a").by(__.values("name")), __.select(Pop.first, "a").map(__.values("name")), TraversalStrategies.GlobalCache.getStrategies(Graph.class).toList()},
            // select("a","b")
            {__.select("a", "b"), __.select("a", "b"), Collections.emptyList()},
            {__.select("a", "b").by(), __.select("a", "b").by(), Collections.emptyList()},
            {__.select("a", "b", "c").by(), __.select("a", "b", "c").by(), Collections.emptyList()},
            {__.select("a", "b").by().by("age"), __.select("b").map(new ValueTraversal<>("age")).as("b").select("a").map(new IdentityTraversal<>()).as("a").select(Pop.last, "a", "b"), TraversalStrategies.GlobalCache.getStrategies(Graph.class).toList()},
            {__.select("a", "b").by("name").by("age"), __.select("b").map(new ValueTraversal<>("age")).as("b").select("a").map(new ValueTraversal<>("name")).as("a").select(Pop.last, "a", "b"), Collections.emptyList()},
            {__.select("a", "b", "c").by("name").by(__.outE().count()), __.select("c").map(new ValueTraversal<>("name")).as("c").select("b").map(__.outE().count()).as("b").select("a").map(new ValueTraversal<>("name")).as("a").select(Pop.last, "a", "b", "c"), TraversalStrategies.GlobalCache.getStrategies(Graph.class).toList()},
            {__.select(Pop.first, "a", "b").by("name").by("age"), __.select(Pop.first, "b").map(new ValueTraversal<>("age")).as("b").select(Pop.first, "a").map(new ValueTraversal<>("name")).as("a").select(Pop.last, "a", "b"), Collections.emptyList()},
            {__.select(Pop.last, "a", "b").by("name").by("age"), __.select(Pop.last, "b").map(new ValueTraversal<>("age")).as("b").select(Pop.last, "a").map(new ValueTraversal<>("name")).as("a").select(Pop.last, "a", "b"), TraversalStrategies.GlobalCache.getStrategies(Graph.class).toList()},
            {__.select(Pop.all, "a", "b").by("name").by("age"), __.select(Pop.all, "a", "b").by("name").by("age"), Collections.emptyList()},
            {__.select(Pop.mixed, "a", "b").by("name").by("age"), __.select(Pop.mixed, "a", "b").by("name").by("age"), Collections.emptyList()},
            // where(as("a")...)
            {__.where(__.out("knows")), __.where(__.outE("knows")), TraversalStrategies.GlobalCache.getStrategies(Graph.class).toList()},
            {__.where(__.as("a").out("knows")), __.identity().as("xyz").select(Pop.last, "a").filter(__.out("knows")).select(Pop.last, "xyz"), Collections.emptyList()},
            {__.where(__.as("a").has("age", P.gt(10))), __.identity().as("xyz").select(Pop.last, "a").has("age", P.gt(10)).select(Pop.last, "xyz"), TraversalStrategies.GlobalCache.getStrategies(Graph.class).toList()},
            {__.select("b").where(__.as("a").has("age", P.gt(10))), __.select("b").as("xyz").select(Pop.last, "a").has("age", P.gt(10)).select(Pop.last, "xyz"), TraversalStrategies.GlobalCache.getStrategies(Graph.class).toList()},
            {__.where(__.as("a").out("knows").as("b")), __.identity().as("xyz").select(Pop.last, "a").where(__.out("knows").as("b")).select(Pop.last, "xyz"), Collections.emptyList()},
            {__.where("a", P.eq("b")), __.where("a", P.eq("b")), Collections.emptyList()},
            {__.as("a").out().where(__.as("a").has("age", P.gt(10))).in(), __.as("a").out().as("xyz").select(Pop.last, "a").has("age", P.gt(10)).select(Pop.last, "xyz").in(), TraversalStrategies.GlobalCache.getStrategies(Graph.class).toList()},
            {__.as("a").out().where(__.as("a").has("age", P.gt(10))).in().path(), __.as("a").out().where(__.as("a").has("age", P.gt(10))).in().path(), TraversalStrategies.GlobalCache.getStrategies(Graph.class).toList()},
    });
}
 
Example #16
Source File: TinkerGraphNoStrategyProvider.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public GraphTraversalSource traversal(final Graph graph) {
    final List<Class> toRemove = TraversalStrategies.GlobalCache.getStrategies(TinkerGraph.class).toList().stream()
            .map(TraversalStrategy::getClass)
            .filter(clazz -> !REQUIRED_STRATEGIES.contains(clazz))
            .collect(Collectors.toList());
    return graph.traversal().withoutStrategies(toRemove.toArray(new Class[toRemove.size()]));
}
 
Example #17
Source File: RepeatUnrollStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void doTest() {
    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(RepeatUnrollStrategy.instance());
    for (final TraversalStrategy strategy : this.otherStrategies) {
        strategies.addStrategies(strategy);
    }
    this.original.asAdmin().setStrategies(strategies);
    this.original.asAdmin().applyStrategies();
    assertEquals(this.optimized, this.original);
}
 
Example #18
Source File: DefaultTraversalStrategiesTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoDependency() {
    //No dependency
    TraversalStrategies s = new DefaultTraversalStrategies();
    s.addStrategies(c, a);
    assertEquals(2, s.toList().size());
}
 
Example #19
Source File: IncidentToAdjacentStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void doTest() {
    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(IncidentToAdjacentStrategy.instance());
    this.original.asAdmin().setStrategies(strategies);
    this.original.asAdmin().applyStrategies();
    assertEquals(this.optimized, this.original);
}
 
Example #20
Source File: DefaultTraversalStrategiesTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void testWellDefinedDependency() {
    //Dependency well defined
    TraversalStrategies s = new DefaultTraversalStrategies();
    s.addStrategies(b, a);
    assertEquals(2, s.toList().size());
    assertEquals(a, s.toList().get(0));
    assertEquals(b, s.toList().get(1));
}
 
Example #21
Source File: FilterRankingStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void doTest() {
    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(FilterRankingStrategy.instance());
    for (final TraversalStrategy strategy : this.otherStrategies) {
        strategies.addStrategies(strategy);
    }
    this.original.asAdmin().setStrategies(strategies);
    this.original.asAdmin().applyStrategies();
    assertEquals(this.optimized, this.original);
}
 
Example #22
Source File: TinkerGraphStepStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void doTest() {
    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(TinkerGraphStepStrategy.instance());
    for (final TraversalStrategy strategy : this.otherStrategies) {
        strategies.addStrategies(strategy);
    }
    this.original.asAdmin().setStrategies(strategies);
    this.original.asAdmin().applyStrategies();
    assertEquals(this.optimized, this.original);
}
 
Example #23
Source File: VertexProgramStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public void addGraphComputerStrategies(final TraversalSource traversalSource) {
    Class<? extends GraphComputer> graphComputerClass;
    if (this.computer.getGraphComputerClass().equals(GraphComputer.class)) {
        try {
            graphComputerClass = this.computer.apply(traversalSource.getGraph()).getClass();
        } catch (final Exception e) {
            graphComputerClass = GraphComputer.class;
        }
    } else
        graphComputerClass = this.computer.getGraphComputerClass();
    final List<TraversalStrategy<?>> graphComputerStrategies = TraversalStrategies.GlobalCache.getStrategies(graphComputerClass).toList();
    traversalSource.getStrategies().addStrategies(graphComputerStrategies.toArray(new TraversalStrategy[graphComputerStrategies.size()]));
}
 
Example #24
Source File: SackStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void traversersWithUnmergeableSacksShouldGenerateDiversifiedHashCodes() {

    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(SackStrategy.build().initialValue(() -> 0).create());

    final Integer[] starts = IntStream.range(0, 8192).boxed().toArray(Integer[]::new);
    final GraphTraversal<Integer, Long> traversal = __.inject(starts).sack(Operator.mult).sack().
            map(Traverser::hashCode).dedup().count();

    traversal.asAdmin().setStrategies(strategies);
    traversal.asAdmin().applyStrategies();
    assertThat(traversal.next(), is(greaterThan(8100L))); // allow a few hash collisions
}
 
Example #25
Source File: Neo4jGraphStepStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void doTest() {
    final TraversalStrategies strategies = new DefaultTraversalStrategies();
    strategies.addStrategies(Neo4jGraphStepStrategy.instance());
    for (final TraversalStrategy strategy : this.otherStrategies) {
        strategies.addStrategies(strategy);
    }
    this.original.asAdmin().setStrategies(strategies);
    this.original.asAdmin().applyStrategies();
    assertEquals(this.optimized, this.original);
}
 
Example #26
Source File: Neo4jGraphStepStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> generateTestParameters() {
    final int LAZY_SIZE = 2500;
    return Arrays.asList(new Object[][]{
            {__.V().out(), g_V().out(), Collections.emptyList()},
            {__.V().has("name", "marko").out(), g_V("name", eq("marko")).out(), Collections.emptyList()},
            {__.V().has("name", "marko").has("age", gt(31).and(lt(10))).out(),
                    g_V("name", eq("marko"), "age", gt(31), "age", lt(10)).out(), Collections.emptyList()},
            {__.V().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"),
                    g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))), Collections.singletonList(FilterRankingStrategy.instance())},
            {__.V().has("name", "marko").as("a").or(has("age"), has("age", gt(32))).has("lang", "java"),
                    g_V("name", eq("marko")).as("a").or(has("age"), has("age", gt(32))).has("lang", "java"), Collections.emptyList()},
            {__.V().has("name", "marko").as("a").or(has("age"), has("age", gt(32))).has("lang", "java"),
                    g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).as("a"), Collections.singletonList(FilterRankingStrategy.instance())},
            {__.V().dedup().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"),
                    g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).dedup(), Collections.singletonList(FilterRankingStrategy.instance())},
            {__.V().as("a").dedup().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"),
                    g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).dedup().as("a"), Collections.singletonList(FilterRankingStrategy.instance())},
            {__.V().as("a").has("name", "marko").as("b").or(has("age"), has("age", gt(32))).has("lang", "java"),
                    g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).as("b", "a"), Collections.singletonList(FilterRankingStrategy.instance())},
            {__.V().as("a").dedup().has("name", "marko").or(has("age"), has("age", gt(32))).filter(has("name", "bob")).has("lang", "java"),
                    g_V("name", eq("marko"), "lang", eq("java"), "name", eq("bob")).or(has("age"), has("age", gt(32))).dedup().as("a"), Arrays.asList(InlineFilterStrategy.instance(), FilterRankingStrategy.instance())},
            {__.V().as("a").dedup().has("name", "marko").or(has("age", 10), has("age", gt(32))).filter(has("name", "bob")).has("lang", "java"),
                    g_V("name", eq("marko"), "lang", eq("java"), "name", eq("bob"), "age", eq(10).or(gt(32))).dedup().as("a"), TraversalStrategies.GlobalCache.getStrategies(Neo4jGraph.class).toList()},
            {__.V().has("name", "marko").or(not(has("age")), has("age", gt(32))).has("name", "bob").has("lang", "java"),
                    g_V("name", eq("marko"), "name", eq("bob"), "lang", eq("java")).or(not(filter(properties("age"))), has("age", gt(32))), TraversalStrategies.GlobalCache.getStrategies(Neo4jGraph.class).toList()},
            {__.V().has("name", P.eq("marko").or(P.eq("bob").and(P.eq("stephen")))).out("knows"),
                    g_V("name", eq("marko").or(P.eq("bob").and(P.eq("stephen")))).out("knows"), Collections.emptyList()},
            {__.V().has("name", P.eq("marko").and(P.eq("bob").and(P.eq("stephen")))).out("knows"),
                    g_V("name", eq("marko"), "name", eq("bob"), "name", eq("stephen")).out("knows"), Collections.emptyList()},
            {__.V().has("name", P.eq("marko").and(P.eq("bob").or(P.eq("stephen")))).out("knows"),
                    g_V("name", eq("marko"), "name", P.eq("bob").or(eq("stephen"))).out("knows"), Collections.emptyList()},
            ///////
            {__.V().out().out().V().has("name", "marko").out(), g_V().out().barrier(LAZY_SIZE).out().barrier(LAZY_SIZE).asAdmin().addStep(V("name", eq("marko"))).barrier(LAZY_SIZE).out(), Arrays.asList(InlineFilterStrategy.instance(), FilterRankingStrategy.instance(), LazyBarrierStrategy.instance())},
            {__.V().out().out().V().has("name", "marko").as("a").out(), g_V().out().barrier(LAZY_SIZE).out().barrier(LAZY_SIZE).asAdmin().addStep(V("name", eq("marko"))).barrier(LAZY_SIZE).as("a").out(), Arrays.asList(InlineFilterStrategy.instance(), FilterRankingStrategy.instance(), LazyBarrierStrategy.instance())},
            {__.V().out().V().has("age", gt(32)).barrier(10).has("name", "marko").as("a"), g_V().out().barrier(LAZY_SIZE).asAdmin().addStep(V("age", gt(32), "name", eq("marko"))).barrier(LAZY_SIZE).barrier(10).as("a"), Arrays.asList(InlineFilterStrategy.instance(), FilterRankingStrategy.instance(), LazyBarrierStrategy.instance())},
            {__.V().out().V().has("age", gt(32)).barrier(10).has("name", "marko").as("a"), g_V().out().barrier(LAZY_SIZE).asAdmin().addStep(V("age", gt(32), "name", eq("marko"))).barrier(LAZY_SIZE).barrier(10).as("a"), TraversalStrategies.GlobalCache.getStrategies(Neo4jGraph.class).toList()},
    });
}
 
Example #27
Source File: PageRankVertexProgramStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public PageRankVertexProgram generateProgram(final Graph graph, final Memory memory) {
    final Traversal.Admin<Vertex, Edge> detachedTraversal = this.edgeTraversal.getPure();
    detachedTraversal.setStrategies(TraversalStrategies.GlobalCache.getStrategies(graph.getClass()));
    final PageRankVertexProgram.Builder builder = PageRankVertexProgram.build()
            .property(this.pageRankProperty)
            .iterations(this.times + 1)
            .alpha(this.alpha)
            .edges(detachedTraversal);
    if (this.previousTraversalVertexProgram())
        builder.initialRank(new HaltedTraversersCountTraversal());
    return builder.create(graph);
}
 
Example #28
Source File: SideEffectStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static <A> void addSideEffect(final TraversalStrategies traversalStrategies, final String key, final A value, final BinaryOperator<A> reducer) {
    SideEffectStrategy strategy = traversalStrategies.getStrategy(SideEffectStrategy.class).orElse(null);
    if (null == strategy) {
        strategy = new SideEffectStrategy();
        traversalStrategies.addStrategies(strategy);
    } else {
        final SideEffectStrategy cloneStrategy = new SideEffectStrategy();
        cloneStrategy.sideEffects.addAll(strategy.sideEffects);
        strategy = cloneStrategy;
        traversalStrategies.addStrategies(strategy);
    }
    strategy.sideEffects.add(new Triplet<>(key, null == value ? null : value instanceof Supplier ? (Supplier) value : new ConstantSupplier<>(value), reducer));
}
 
Example #29
Source File: HugeGraph.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public static void registerTraversalStrategies(Class<?> clazz) {
    TraversalStrategies strategies = null;
    strategies = TraversalStrategies.GlobalCache
                                    .getStrategies(Graph.class)
                                    .clone();
    strategies.addStrategies(HugeVertexStepStrategy.instance(),
                             HugeGraphStepStrategy.instance(),
                             HugeCountStepStrategy.instance());
    TraversalStrategies.GlobalCache.registerStrategies(clazz, strategies);
}
 
Example #30
Source File: DefaultTraversalStrategiesTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void testCircularDependency() {
    //Circular dependency => throws exception
    TraversalStrategies s = new DefaultTraversalStrategies();
    try {
        s.addStrategies(c, k, a, b);
        fail();
    } catch (IllegalStateException ex) {
        assertTrue(ex.getMessage().toLowerCase().contains("cyclic"));
    }
}