Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal#next()

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal#next() . 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: GraphMutateBenchmark.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Benchmark
public Vertex testAddVWithPropsChained() {
    // construct a traversal that adds 100 vertices with 32 properties each
    GraphTraversal<Vertex, Vertex> t = null;
    for (int ix = 0; ix < 100; ix++) {
        if (null == t)
            t = g.addV("person");
        else
            t = t.addV("person");

        for (int iy = 0; iy < 32; iy++) {
            if (iy % 2 == 0)
                t = t.property("x" + String.valueOf(iy), iy * ix);
            else
                t = t.property("x" + String.valueOf(iy), String.valueOf(iy + ix));
        }
    }

    return t.next();
}
 
Example 2
Source File: QueryRunner.java    From janusgraph_tutorial with Apache License 2.0 6 votes vote down vote up
public void printTimeline(GraphTraversal<Vertex, Map<String, Map<String, Object>>> traversal) {
  int count = 0;
  resetTimer();
  while (traversal.hasNext()) {
    Map<String, Map<String, Object>> item = traversal.next();
    Vertex user = (Vertex) item.get(userVertex);
    Edge posts = (Edge) item.get(postsEdge);
    Vertex statusUpdate = (Vertex) item.get(statusUpdateVertex);
    LOGGER.info(
        " {}: @{} {}: {}",
        count++,
        user.value(USER_NAME),
        formatTimestamp(posts.value(CREATED_AT)),
        statusUpdate.value(CONTENT)
    );
  }

  LOGGER.info("Printed {} element(s) in {}ms", count, duration());
}
 
Example 3
Source File: TestComplex.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testProject() {
    Map<String, Object> aValues = new HashMap<>();
    aValues.put("name", "root");
    Vertex vA = sqlgGraph.addVertex("A", aValues);
    Map<String, Object> iValues = new HashMap<>();
    iValues.put("name", "item1");
    Vertex vI = sqlgGraph.addVertex("I", iValues);
    vA.addEdge("likes", vI, "howMuch", 5, "who", "Joe");
    this.sqlgGraph.tx().commit();
    Object id0 = vI.id();
    GraphTraversal<Vertex, Map<String, Object>> gt = sqlgGraph.traversal().V()
            .hasLabel("A")
            .has("name", "root")
            .outE("likes")
            .project("stars", "user", "item")
            .by("howMuch")
            .by("who")
            .by(__.inV().id())
            .select("user", "stars", "item");
    Assert.assertTrue(gt.hasNext());
    Map<String, Object> m = gt.next();
    Assert.assertEquals(new Integer(5), m.get("stars"));
    Assert.assertEquals("Joe", m.get("user"));
    Assert.assertEquals(id0, m.get("item"));
}
 
Example 4
Source File: GraphMutateBenchmark.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Benchmark
public Edge testAddVAddEWithPropsChained() {
    // construct a traversal that adds 100 vertices with 32 properties each as well as 300 edges with 8
    // properties each
    final Random rand = new Random(584545454L);

    GraphTraversal<Vertex, ?> t = null;
    for (int ix = 0; ix < 10; ix++) {
        if (null == t)
            t = g.addV("person");
        else
            t = t.addV("person");

        for (int iy = 0; iy < 32; iy++) {
            if (iy % 2 == 0)
                t = t.property("x" + String.valueOf(iy), iy * ix);
            else
                t = t.property("x" + String.valueOf(iy), String.valueOf(iy + ix));
        }

        t = t.as("person" + ix);

        if (ix > 0) {
            int edgeCount = ix == 9 ? 6 : 3;
            for (int ie = 0; ie < edgeCount; ie++) {
                t = t.addE("knows").from("person" + ix).to("person" + rand.nextInt(ix));

                for (int iy = 0; iy < 8; iy++) {
                    if (iy % 2 == 0)
                        t = t.property("x" + String.valueOf(iy), iy * ie);
                    else
                        t = t.property("x" + String.valueOf(iy), String.valueOf(iy + ie));
                }
            }
        }
    }

    final Edge e = (Edge) t.next();
    return e;
}
 
Example 5
Source File: VreIniter.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
public Vertex upsertVre(String vreName, String vreLabel, String fileName) {
  final Vertex result;
  try (Transaction tx = wrapper.getGraph().tx()) {
    final GraphTraversal<Vertex, Vertex> vre = getVreTraversal(vreName);
    if (vre.hasNext()) {
      result = vre.next();
      if (result.property(TinkerpopSaver.SAVED_MAPPING_STATE).isPresent()) {
        result.property(TinkerpopSaver.SAVED_MAPPING_STATE).remove();
      }
      wrapper.getGraph().traversal().V(result.id())
        .out(TinkerpopSaver.RAW_COLLECTION_EDGE_NAME)
        .union(
          __.out(TinkerpopSaver.RAW_ITEM_EDGE_NAME),
          __.out(TinkerpopSaver.RAW_PROPERTY_EDGE_NAME),
          __.identity() //the collection
        )
        .drop()
        .toList();//force traversal and thus side-effects
    } else {
      result = wrapper.getGraph()
        .addVertex(T.label, Vre.DATABASE_LABEL, Vre.VRE_NAME_PROPERTY_NAME, vreName);
    }
    result.property(Vre.VRE_LABEL_PROPERTY_NAME, vreLabel);
    result.property(Vre.UPLOADED_FILE_NAME, fileName);
    result.property(Vre.PUBLISH_STATE_PROPERTY_NAME, Vre.PublishState.UPLOADING.toString());
    tx.commit();
  }

  vres.reload();
  return result;
}
 
Example 6
Source File: Vre.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
private Vertex findOrCreateVreVertex(Graph graph) {
  // Look for existing VRE vertex
  Vertex vreVertex;
  GraphTraversal<Vertex, Vertex> existing = graph.traversal().V().hasLabel(DATABASE_LABEL)
                                                 .has(VRE_NAME_PROPERTY_NAME, vreName);
  // Create new if does not exist
  if (existing.hasNext()) {
    vreVertex = existing.next();
    LOG.debug("Replacing existing vertex {}.", vreVertex);
  } else {
    vreVertex = graph.addVertex(DATABASE_LABEL);
    LOG.debug("Creating new vertex");
  }
  return vreVertex;
}
 
Example 7
Source File: GraphMutateBenchmark.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Benchmark
public Vertex testAddVWithProps() {
    GraphTraversal<Vertex, Vertex> t = g.addV("test");
    for (int iy = 0; iy < 32; iy++) {
        if (iy % 2 == 0)
            t = t.property("x" + String.valueOf(iy), iy);
        else
            t = t.property("x" + String.valueOf(iy), String.valueOf(iy));
    }
    return t.next();
}
 
Example 8
Source File: AtlasGraphSONReader.java    From atlas with Apache License 2.0 5 votes vote down vote up
private void postProcess(long startIndex) {
    LOG.info("postProcess: Starting... : counter at: {}", counter.get());

    try {
        PostProcessManager.WorkItemsManager wim   = PostProcessManager.create(bulkLoadGraph, relationshipCache.getPropertiesToPostProcess(), batchSize, numWorkers);
        GraphTraversal                      query = bulkLoadGraph.traversal().V();

        while (query.hasNext()) {
            handleInterrupt(bulkLoadGraph, counter.incrementAndGet());

            if(shouldSkip(startIndex, counter.get())) {
                continue;
            }

            Vertex v = (Vertex) query.next();

            wim.produce(v.id());

            updateStatusConditionally(bulkLoadGraph, counter.get());
        }

        wim.shutdown();
    } catch (Exception ex) {
        LOG.error("postProcess: failed!", ex);
    } finally {
        LOG.info("postProcess: Done! : [{}]", counter.get());

        readerStatusManager.update(bulkLoadGraph, counter.get(), true);
    }
}
 
Example 9
Source File: Collection.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
private Vertex findOrCreateCollectionVertex(Graph graph) {
  Vertex collectionVertex;
  GraphTraversal<Vertex, Vertex> existing = graph.traversal().V().hasLabel(DATABASE_LABEL)
                                                 .has(COLLECTION_NAME_PROPERTY_NAME, collectionName);

  // Create new if does not exist
  if (existing.hasNext()) {
    collectionVertex = existing.next();
    LOG.debug("Replacing existing vertex {}.", collectionVertex);
  } else {
    collectionVertex = graph.addVertex(DATABASE_LABEL);
    LOG.debug("Creating new vertex");
  }
  return collectionVertex;
}
 
Example 10
Source File: DisplayNameHelper.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
public static Optional<String> getDisplayname(GraphTraversalSource traversalSource, Vertex vertex,
                                              Collection targetCollection) {
  ReadableProperty displayNameProperty = targetCollection.getDisplayName();
  if (displayNameProperty != null) {
    GraphTraversal<Vertex, Try<JsonNode>> displayNameGetter = traversalSource.V(vertex.id()).union(
      targetCollection.getDisplayName().traversalJson()
    );
    if (displayNameGetter.hasNext()) {
      Try<JsonNode> traversalResult = displayNameGetter.next();
      if (!traversalResult.isSuccess()) {
        LOG.debug(databaseInvariant, "Retrieving displayname failed", traversalResult.getCause());
      } else {
        if (traversalResult.get() == null) {
          LOG.debug(databaseInvariant, "Displayname was null");
        } else {
          if (!traversalResult.get().isTextual()) {
            LOG.debug(databaseInvariant, "Displayname was not a string but " + traversalResult.get().toString());
          } else {
            return Optional.of(traversalResult.get().asText());
          }
        }
      }
    } else {
      LOG.debug(databaseInvariant, "Displayname traversal resulted in no results: " + displayNameGetter);
    }
  } else {
    LOG.debug("No displayname configured for " + targetCollection.getEntityTypeName());
    //FIXME: deze wordt gegooid tijdens de finish. da's raar
  }
  return Optional.empty();
}
 
Example 11
Source File: D3GraphGeneratorService.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
public D3Graph get(String type, UUID uuid, List<String> relationNames, int depth) throws NotFoundException {

    final String vreId = (String) graphWrapper.getGraph().traversal().V()
                                              .hasLabel(Collection.DATABASE_LABEL)
                                              .has(Collection.ENTITY_TYPE_NAME_PROPERTY_NAME, type)
                                              .in(Vre.HAS_COLLECTION_RELATION_NAME).next()
                                              .property(Vre.VRE_NAME_PROPERTY_NAME).value();

    final String relationTypeName = (String) graphWrapper.getGraph().traversal().V()
                                     .hasLabel(Collection.DATABASE_LABEL)
                                     .has(Collection.ENTITY_TYPE_NAME_PROPERTY_NAME, type)
                                     .in(Vre.HAS_COLLECTION_RELATION_NAME).out(Vre.HAS_COLLECTION_RELATION_NAME)
                                     .where(__.has(Collection.IS_RELATION_COLLECTION_PROPERTY_NAME, true)).next()
                                     .property(Collection.ENTITY_TYPE_NAME_PROPERTY_NAME).value();


    GraphTraversal<Vertex, Vertex> result = graphWrapper.getGraph().traversal().V()
            .has("tim_id", uuid.toString()).filter(
              x -> ((String) x.get().property("types").value()).contains("\"" + type + "\"")
            ).has("isLatest", true)
            .not(__.has("deleted", true));

    if (!result.hasNext()) {
      throw new NotFoundException();
    }


    Vertex vertex = result.next();
    D3Graph d3Graph = new D3Graph();

    generateD3Graph(relationTypeName, vreId, d3Graph, vertex, relationNames, depth, 1);

    return d3Graph;
  }
 
Example 12
Source File: ComputeWeightVertexProgram.java    From janusgraph_tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * Execute this marvelous code, going from the Content to Users.
 *
 * Internally uses the RecommendationForNewUser class to build the recommendation as we want, pretty and
 * simple.
 *
 * @param vertex
 * @param messenger
 * @param memory
 */
@Override
public void execute(Vertex vertex, Messenger<Tuple> messenger, Memory memory) {
  try {
    HadoopQueryRunner runner = new HadoopQueryRunner(g, vertex.value(Schema.USER_NAME));
    GraphTraversal<Vertex, Edge> t = g.V(vertex.id()).inE(Schema.FOLLOWS);

    while(t.hasNext()) {
      Edge followsEdge = t.next();

      long commonFollowedUsers = runner.countCommonFollowedUsers(followsEdge.outVertex());
      long postsPerDaySince = runner.countPostsPerDaySince(sevenDaysAgo);
      long weight = (3 * commonFollowedUsers + postsPerDaySince) / 4;
      if(min == -10 || min > weight) {
        min = (int) weight;
      }
      if(max < weight) {
        max = (int) weight;
      }
      count++;

      followsEdge.property(CreateWeightIndex.WEIGHT, weight);
    }
  } catch (Exception e){
    e.printStackTrace();
    LOGGER.error("while processing " + vertex.id() + ": " + e.getClass().toString() + "(" + e.getMessage() + ")");
    return;
  }
}
 
Example 13
Source File: TestBatchServerSideEdgeCreation.java    From sqlg with MIT License 5 votes vote down vote up
private void testBulkEdges2_assert(SqlgGraph sqlgGraph, String uuid1Cache, String uuid2Cache) {
    GraphTraversal<Vertex, Vertex> has = sqlgGraph.traversal().V().hasLabel("Person").has("id", uuid1Cache);
    Assert.assertTrue(has.hasNext());
    Vertex person50 = has.next();

    GraphTraversal<Vertex, Vertex> has1 = sqlgGraph.traversal().V().hasLabel("Person").has("id", uuid2Cache);
    Assert.assertTrue(has1.hasNext());
    Vertex person250 = has1.next();
    Assert.assertTrue(sqlgGraph.traversal().V(person50.id()).out().hasNext());
    Vertex person250Please = sqlgGraph.traversal().V(person50.id()).out().next();
    assertEquals(person250, person250Please);
}
 
Example 14
Source File: ElementGraph.java    From gremlin-ogm with Apache License 2.0 4 votes vote down vote up
/**
 * Complete the traversal by returning the next (and only) element.
 */
protected <E> E complete(GraphTraversal<?, E> traversal) {
  return traversal.next();
}
 
Example 15
Source File: TestOptionalWithOrder.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testOrder() {

    Vertex a1 = sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex b1 = sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex b2 = sqlgGraph.addVertex(T.label, "B", "name", "b2");

    a1.addEdge("e_a", b1);
    a2.addEdge("e_a", b2);

    GraphTraversal<Vertex, Path> t = sqlgGraph.traversal().V(a1, a2).order().by("name").optional(__.out()).path();
    Assert.assertTrue(t.hasNext());
    Path p = t.next();
    Assert.assertEquals(2, p.size());
    Vertex v1 = p.get(0);
    Assert.assertEquals("a1", v1.property("name").value());
    Vertex v2 = p.get(1);
    Assert.assertEquals("b1", v2.property("name").value());
    Assert.assertTrue(t.hasNext());
    p = t.next();
    Assert.assertEquals(2, p.size());
    v1 = p.get(0);
    Assert.assertEquals("a2", v1.property("name").value());
    v2 = p.get(1);
    Assert.assertEquals("b2", v2.property("name").value());

    b1.remove();
    this.sqlgGraph.tx().commit();

    List<Path> paths  = sqlgGraph.traversal().V(a2, a1).order().by("name").optional(__.out()).path().toList();
    Assert.assertEquals(2, paths.size());
    Assert.assertEquals(1, paths.get(0).size());

    t = sqlgGraph.traversal().V(a2, a1).order().by("name").optional(__.out()).path();
    Assert.assertTrue(t.hasNext());
    p = t.next();
    Assert.assertEquals(1, p.size());
    v1 = p.get(0);
    Assert.assertEquals("a1", v1.property("name").value());
    Assert.assertTrue(t.hasNext());
    p = t.next();
    Assert.assertEquals(2, p.size());
    v1 = p.get(0);
    Assert.assertEquals("a2", v1.property("name").value());
    v2 = p.get(1);
    Assert.assertEquals("b2", v2.property("name").value());
}
 
Example 16
Source File: TransactionImpl.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
private void mergeAttributeEdge(Vertex mergeTargetV, Vertex ent, GraphTraversal<Vertex, Edge> attributeEdge) {
    Edge edge = attributeEdge.next();
    Object[] properties = propertiesToArray(Lists.newArrayList(edge.properties()));
    ent.addEdge(Schema.EdgeLabel.ATTRIBUTE.getLabel(), mergeTargetV, properties);
    edge.remove();
}
 
Example 17
Source File: ReaderStatusManager.java    From atlas with Apache License 2.0 4 votes vote down vote up
private static Vertex fetchUsingTypeName(GraphTraversalSource g) {
    GraphTraversal src = g.V().has(Constants.ENTITY_TYPE_PROPERTY_KEY, MIGRATION_STATUS_TYPE_NAME);
    return src.hasNext() ? (Vertex) src.next() : null;
}
 
Example 18
Source File: GraphOMRSMetadataStore.java    From egeria with Apache License 2.0 4 votes vote down vote up
synchronized void removeEntityProxyFromStore(String entityGUID)
{
    final String methodName = "removeEntityProxyFromStore";
    // TODO - could capture existing entity and move it to 'history'

    // Look in the graph

    GraphTraversalSource g = instanceGraph.traversal();


    GraphTraversal<Vertex, Vertex> gt = g.V().hasLabel("Entity").has(PROPERTY_KEY_ENTITY_GUID, entityGUID);

    // Only looking for proxy entities:
    gt = gt.has(PROPERTY_KEY_ENTITY_IS_PROXY, true);

    if (gt.hasNext()) {
        Vertex vertex = gt.next();

        Boolean isProxy = entityMapper.isProxy(vertex);
        if (isProxy) {

            log.debug("{} found entity proxy vertex {} to be removed", methodName, vertex);

            // Look for associated classifications.
            Iterator<Edge> classifierEdges = vertex.edges(Direction.OUT, "Classifier");
            while (classifierEdges.hasNext()) {
                Edge classifierEdge = classifierEdges.next();
                Vertex classificationVertex = classifierEdge.inVertex();
                // Get the classification's name for debug/info only
                Classification existingClassification = new Classification();
                try {
                    classificationMapper.mapVertexToClassification(classificationVertex, existingClassification);
                } catch (Exception e) {
                    log.error("{} caught exception from classification mapper for classification {}", methodName, existingClassification.getName());
                    // Nothing you can do - just keep going
                }
                log.debug("{} removing classification {} from entity proxy", methodName, existingClassification.getName());
                classifierEdge.remove();
                classificationVertex.remove();
            }

            // Finally remove the entity vertex...
            vertex.remove();

            log.debug("{} removed entity proxy vertex with guid {}", methodName, entityGUID);
        }
    }
    g.tx().commit();

}
 
Example 19
Source File: GraphOMRSMetadataStore.java    From egeria with Apache License 2.0 4 votes vote down vote up
synchronized void removeEntityFromStore(String entityGUID)
{
    final String methodName = "removeEntityFromStore";

    // Look in the graph
    String guid = entityGUID;
    GraphTraversalSource g = instanceGraph.traversal();

    GraphTraversal<Vertex, Vertex> gt = g.V().hasLabel("Entity").has(PROPERTY_KEY_ENTITY_GUID, entityGUID);

    // Only looking for non-proxy entities:
    gt = gt.has(PROPERTY_KEY_ENTITY_IS_PROXY, false);

    if (gt.hasNext()) {
        Vertex vertex = gt.next();

        Boolean isProxy = entityMapper.isProxy(vertex);
        if (!isProxy) {

            log.debug("{} found entity vertex {} to be removed", methodName, vertex);

            // Look for associated classifications.
            Iterator<Edge> classifierEdges = vertex.edges(Direction.OUT, "Classifier");
            while (classifierEdges.hasNext()) {
                Edge classifierEdge = classifierEdges.next();
                Vertex classificationVertex = classifierEdge.inVertex();
                // Get the classification's name for debug/info only
                Classification existingClassification = new Classification();
                try {
                    classificationMapper.mapVertexToClassification(classificationVertex, existingClassification);
                } catch (Exception e) {
                    log.error("{} caught exception from classification mapper for classification {}", methodName, existingClassification.getName());
                    // Nothing you can do - just keep going
                }
                log.debug("{} removing classification {} from entity", methodName, existingClassification.getName());
                classifierEdge.remove();
                classificationVertex.remove();
            }

            // Finally remove the entity vertex...
            vertex.remove();

            log.debug("{} removed entity vertex with guid {}", methodName, entityGUID);
        }
    }
    g.tx().commit();

}
 
Example 20
Source File: GraphOMRSMetadataStore.java    From egeria with Apache License 2.0 4 votes vote down vote up
synchronized EntitySummary getEntitySummaryFromStore(String guid)
        throws
        EntityNotKnownException,
        RepositoryErrorException
{

    String methodName = "getEntitySummaryFromStore";


    EntitySummary entity = null;

    // Look in the graph
    GraphTraversalSource g = instanceGraph.traversal();

    GraphTraversal<Vertex, Vertex> gt = g.V().hasLabel("Entity").has(PROPERTY_KEY_ENTITY_GUID, guid);

    if (gt.hasNext()) {
        Vertex vertex = gt.next();
        log.debug("{} found vertex {}", methodName, vertex);

        try {
            if (vertex != null) {
                log.debug("{} found entity vertex {}", methodName, vertex);
                entity = new EntitySummary();
                entityMapper.mapVertexToEntitySummary(vertex, entity);
            }

        }
        catch (RepositoryErrorException e) {

            log.error("{} Caught exception {}", methodName, e.getMessage());
            g.tx().rollback();

            throw new RepositoryErrorException(GraphOMRSErrorCode.ENTITY_NOT_FOUND.getMessageDefinition(guid, methodName,
                                                                                                        this.getClass().getName(),
                                                                                                        repositoryName),
                    this.getClass().getName(),
                    methodName, e);
        }

    } else {

        // Entity was not found by GUID
        log.error("{} entity with GUID {} not found", methodName, guid);
        g.tx().rollback();

        throw new EntityNotKnownException(GraphOMRSErrorCode.ENTITY_NOT_FOUND.getMessageDefinition(guid, methodName,
                                                                                                   this.getClass().getName(),
                                                                                                   repositoryName),
                this.getClass().getName(),
                methodName);
    }


    g.tx().commit();

    return entity;
}