Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource#V

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource#V . 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: ElementIdStrategyProcessTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@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()));
}
 
Example 2
Source File: Neo4jLuceneEntityFetcher.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
public GraphTraversal<Vertex, Vertex> getEntity(GraphTraversalSource source, UUID id, Integer rev,
                                                String collectionName) {
  if (rev == null) {
    Optional<Vertex> foundVertex = getVertexByIndex(source, id, collectionName);

    if (foundVertex.isPresent()) {
      return source.V(foundVertex.get().id());
    }
  }
  return super.getEntity(source, id, rev, collectionName);
}
 
Example 3
Source File: SparqlToGremlinCompiler.java    From sparql-gremlin with Apache License 2.0 4 votes vote down vote up
private SparqlToGremlinCompiler(final GraphTraversalSource g) {
    this(g.V());
}
 
Example 4
Source File: SubgraphStrategyProcessTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldFilterEdgeCriterion() 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 SubgraphStrategy strategy = SubgraphStrategy.build().edges(edgeCriterion).create();
    final GraphTraversalSource sg = g.withStrategies(strategy);

    // all vertices are here
    assertEquals(6, g.V().count().next().longValue());
    final Traversal t = sg.V();
    t.hasNext();
    printTraversalForm(t);
    CloseableIterator.closeIterator(t);
    assertEquals(6, sg.V().count().next().longValue());

    // only the given edges are included
    assertEquals(6, g.E().count().next().longValue());
    assertEquals(3, sg.E().count().next().longValue());

    assertEquals(2, g.V(convertToVertexId("marko")).outE("knows").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("marko")).outE("knows").count().next().longValue());

    // wrapped Traversal<Vertex, Vertex> takes into account the edges it must pass through
    assertEquals(2, g.V(convertToVertexId("marko")).out("knows").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("marko")).out("knows").count().next().longValue());
    assertEquals(2, g.V(convertToVertexId("josh")).out("created").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).out("created").count().next().longValue());

    // from vertex

    assertEquals(2, g.V(convertToVertexId("josh")).outE().count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).outE().count().next().longValue());
    assertEquals(2, g.V(convertToVertexId("josh")).out().count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).out().count().next().longValue());

    assertEquals(1, g.V(convertToVertexId("josh")).inE().count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).inE().count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).in().count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).in().count().next().longValue());

    assertEquals(3, g.V(convertToVertexId("josh")).bothE().count().next().longValue());
    assertEquals(2, sg.V(convertToVertexId("josh")).bothE().count().next().longValue());
    assertEquals(3, g.V(convertToVertexId("josh")).both().count().next().longValue());
    assertEquals(2, sg.V(convertToVertexId("josh")).both().count().next().longValue());

    // with label

    assertEquals(2, g.V(convertToVertexId("josh")).outE("created").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).outE("created").count().next().longValue());
    assertEquals(2, g.V(convertToVertexId("josh")).out("created").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).out("created").count().next().longValue());
    assertEquals(2, g.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
    assertEquals(2, g.V(convertToVertexId("josh")).both("created").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).both("created").count().next().longValue());

    assertEquals(1, g.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).in("knows").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).in("knows").count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).both("knows").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).both("knows").count().next().longValue());

    // with branch factor

    assertEquals(1, g.V(convertToVertexId("josh")).limit(1).local(bothE().limit(1)).count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).limit(1).local(bothE().limit(1)).count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).limit(1).local(bothE().limit(1)).inV().count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).limit(1).local(bothE().limit(1)).inV().count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).inV().count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).inV().count().next().longValue());

    // from edge

    assertEquals(2, g.E(convertToEdgeId("marko", "knows", "josh")).bothV().count().next().longValue());
    assertEquals(2, sg.E(convertToEdgeId("marko", "knows", "josh")).bothV().count().next().longValue());

    assertEquals(3, g.E(convertToEdgeId("marko", "knows", "josh")).outV().outE().count().next().longValue());
    assertEquals(2, sg.E(convertToEdgeId("marko", "knows", "josh")).outV().outE().count().next().longValue());
}
 
Example 5
Source File: SparqlToGremlinCompiler.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
private SparqlToGremlinCompiler(final GraphTraversalSource g) {
    this(g.V());
}
 
Example 6
Source File: TestAndStepBarrier.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void shouldFilterEdgeCriterion() {
    loadModern();
    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
    );

    GraphTraversalSource g = this.sqlgGraph.traversal();
    final SubgraphStrategy strategy = SubgraphStrategy.build().edges(edgeCriterion).create();
    final GraphTraversalSource sg = g.withStrategies(strategy);

    // all vertices are here
    Assert.assertEquals(6, g.V().count().next().longValue());
    final Traversal t = sg.V();
    t.hasNext();
    printTraversalForm(t);


    Assert.assertEquals(6, sg.V().count().next().longValue());

    // only the given edges are included
    Assert.assertEquals(6, g.E().count().next().longValue());
    Assert.assertEquals(3, sg.E().count().next().longValue());

    Assert.assertEquals(2, g.V(convertToVertexId("marko")).outE("knows").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("marko")).outE("knows").count().next().longValue());

    // wrapped Traversal<Vertex, Vertex> takes into account the edges it must pass through
    Assert.assertEquals(2, g.V(convertToVertexId("marko")).out("knows").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("marko")).out("knows").count().next().longValue());
    Assert.assertEquals(2, g.V(convertToVertexId("josh")).out("created").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).out("created").count().next().longValue());

    // from vertex

    Assert.assertEquals(2, g.V(convertToVertexId("josh")).outE().count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).outE().count().next().longValue());
    Assert.assertEquals(2, g.V(convertToVertexId("josh")).out().count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).out().count().next().longValue());

    Assert.assertEquals(1, g.V(convertToVertexId("josh")).inE().count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).inE().count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).in().count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).in().count().next().longValue());

    Assert.assertEquals(3, g.V(convertToVertexId("josh")).bothE().count().next().longValue());
    Assert.assertEquals(2, sg.V(convertToVertexId("josh")).bothE().count().next().longValue());
    Assert.assertEquals(3, g.V(convertToVertexId("josh")).both().count().next().longValue());
    Assert.assertEquals(2, sg.V(convertToVertexId("josh")).both().count().next().longValue());

    // with label

    Assert.assertEquals(2, g.V(convertToVertexId("josh")).outE("created").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).outE("created").count().next().longValue());
    Assert.assertEquals(2, g.V(convertToVertexId("josh")).out("created").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).out("created").count().next().longValue());
    Assert.assertEquals(2, g.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
    Assert.assertEquals(2, g.V(convertToVertexId("josh")).both("created").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).both("created").count().next().longValue());

    Assert.assertEquals(1, g.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).in("knows").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).in("knows").count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).both("knows").count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).both("knows").count().next().longValue());

    // with branch factor

    Assert.assertEquals(1, g.V(convertToVertexId("josh")).limit(1).local(__.bothE().limit(1)).count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).limit(1).local(__.bothE().limit(1)).count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).limit(1).local(__.bothE().limit(1)).inV().count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).limit(1).local(__.bothE().limit(1)).inV().count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).local(__.bothE("knows", "created").limit(1)).count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).local(__.bothE("knows", "created").limit(1)).count().next().longValue());
    Assert.assertEquals(1, g.V(convertToVertexId("josh")).local(__.bothE("knows", "created").limit(1)).inV().count().next().longValue());
    Assert.assertEquals(1, sg.V(convertToVertexId("josh")).local(__.bothE("knows", "created").limit(1)).inV().count().next().longValue());

    // from edge

    Assert.assertEquals(2, g.E(convertToEdgeId(this.sqlgGraph, "marko", "knows", "josh")).bothV().count().next().longValue());
    Assert.assertEquals(2, sg.E(convertToEdgeId(this.sqlgGraph, "marko", "knows", "josh")).bothV().count().next().longValue());

    Assert.assertEquals(3, g.E(convertToEdgeId(this.sqlgGraph, "marko", "knows", "josh")).outV().outE().count().next().longValue());
    Assert.assertEquals(2, sg.E(convertToEdgeId(this.sqlgGraph, "marko", "knows", "josh")).outV().outE().count().next().longValue());
}