org.apache.tinkerpop.gremlin.structure.Vertex类的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(15.7k)|赞(0)|评价(0)|浏览(99)

本文整理了Java中org.apache.tinkerpop.gremlin.structure.Vertex类的一些代码示例,展示了Vertex类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Vertex类的具体详情如下:
包路径:org.apache.tinkerpop.gremlin.structure.Vertex
类名称:Vertex

Vertex介绍

[英]A Vertex maintains pointers to both a set of incoming and outgoing Edge objects. The outgoing edges are those edges for which the Vertex is the tail. The incoming edges are those edges for which the Vertex is the head.

Diagrammatically:

---inEdges---> vertex ---outEdges--->.

[中]顶点维护指向一组传入和传出边对象的指针。输出边是顶点为尾部的边。传入边是顶点为头部的边。
从图表上看:

---inEdges---> vertex ---outEdges--->.

代码示例

代码示例来源:origin: thinkaurelius/titan

@Override
public void execute(final Vertex vertex, Messenger<Long> messenger, final Memory memory) {
  if (memory.isInitialIteration()) {
    if (vertex.id().equals(Long.valueOf(seed).longValue())) {
      // The seed sends a single message to start the computation
      log.debug("Sent initial message from {}", vertex.id());
      // The seed's distance to itself is zero
      vertex.property(VertexProperty.Cardinality.single, DISTANCE, 0L);
      messenger.sendMessage(incidentMessageScope, 0L);
    }
  } else {
    Iterator<Long> distances = messenger.receiveMessages();
    // Find minimum distance among all incoming messages, or null if no messages came in
    Long shortestDistanceSeenOnThisIteration =
        IteratorUtils.stream(distances).reduce((a, b) -> Math.min(a, b)).orElse(null);
    if (null == shortestDistanceSeenOnThisIteration)
      return; // no messages to process or forward on this superstep
    VertexProperty<Long> currentShortestVP = vertex.property(DISTANCE);
    if (!currentShortestVP.isPresent() ||
        currentShortestVP.value() > shortestDistanceSeenOnThisIteration) {
      // First/shortest distance seen by this vertex: store it and forward to neighbors
      vertex.property(VertexProperty.Cardinality.single, DISTANCE, shortestDistanceSeenOnThisIteration);
      messenger.sendMessage(incidentMessageScope, shortestDistanceSeenOnThisIteration);
    }
    // else: no new winner, ergo no reason to send message to neighbors
  }
}

代码示例来源:origin: thinkaurelius/titan

@Test
public void testSimpleTinkerPopTraversal() {
  Vertex v1 = graph.addVertex("name", "josh");
  Vertex v2 = graph.addVertex("name", "lop");
  v1.addEdge("created", v2);
  //graph.tx().commit();
  Object id = graph.traversal().V().has("name", "josh").outE("created").as("e").inV().has("name", "lop").<Edge>select("e").next().id();
  assertNotNull(id);
}

代码示例来源:origin: thinkaurelius/titan

@Test
public void testNestedTransactions() {
  Vertex v1 = graph.addVertex();
  newTx();
  Vertex v2 = tx.addVertex();
  v2.property("name", "foo");
  tx.commit();
  v1.addEdge("related", graph.traversal().V(v2).next());
  graph.tx().commit();
  assertCount(1, v1.edges(OUT));
}

代码示例来源:origin: thinkaurelius/titan

@Override
public void map(Vertex vertex, MapEmitter<Long, Integer> emitter) {
  emitter.emit((Long)vertex.id(),vertex.value(DegreeCounter.DEGREE));
}

代码示例来源:origin: thinkaurelius/titan

@Override
public void map(final Vertex vertex, final MapEmitter<Object, Long> emitter) {
  final Property distance = vertex.property(ShortestDistanceVertexProgram.DISTANCE);
  if (distance.isPresent()) {
    emitter.emit(vertex.id(), (Long) distance.value());
  }
}

代码示例来源:origin: JanusGraph/janusgraph

v1.property(property, value1);
v1 = getV(graph, v1.id());
v1.property(property, value2);
v1 = getV(graph, v1.id());
v1.properties(property).forEachRemaining(p -> {
  if (p.value().equals(value1)) {
    p.remove();
v1 = getV(graph, v1.id());
v1.property(property, value1);
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
v1 = getV(graph, v1.id());
v1.property(property, value1);
v1.property(property, value1);
v1.property(property, value2);
g.V().drop().iterate();
v1 = g.addV().property(property, value1).property(property, value2).next();
g.addV().property(property, value1).property(property, value2).next();

代码示例来源:origin: apache/tinkerpop

@Test
@LoadGraphWith(MODERN)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
public void g_V_hasXname_markoX_asXaX_outEXcreatedX_asXbX_inV_addEXselectXbX_labelX_toXaX() {
  final Traversal<Vertex, Edge> traversal = get_g_V_hasXname_markoX_asXaX_outEXcreatedX_asXbX_inV_addEXselectXbX_labelX_toXaX();
  printTraversalForm(traversal);
  final Edge edge = traversal.next();
  assertFalse(traversal.hasNext());
  assertEquals("created", edge.label());
  assertEquals(convertToVertexId("marko"), edge.inVertex().id());
  assertEquals(convertToVertexId("lop"), edge.outVertex().id());
  assertEquals(6L, g.V().count().next().longValue());
  assertEquals(7L, g.E().count().next().longValue());
}

代码示例来源:origin: apache/tinkerpop

@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);
}

代码示例来源:origin: thinkaurelius/titan

@Test
public void testTinkerPopCardinality() {
  PropertyKey id = mgmt.makePropertyKey("id").cardinality(Cardinality.SINGLE).dataType(Integer.class).make();
  PropertyKey name = mgmt.makePropertyKey("name").cardinality(Cardinality.SINGLE).dataType(String.class).make();
  PropertyKey names = mgmt.makePropertyKey("names").cardinality(Cardinality.LIST).dataType(String.class).make();
  mgmt.buildIndex("byId", Vertex.class).addKey(id).buildCompositeIndex();
  finishSchema();
  GraphTraversalSource gts;
  Vertex v;
  v = graph.addVertex("id", 1);
  v.property(single, "name", "t1");
  graph.addVertex("id", 2, "names", "n1", "names", "n2");
  graph.tx().commit();
  gts = graph.traversal();
  v = gts.V().has("id", 1).next();
  v.property(single, "name", "t2");
  v = gts.V().has("id", 1).next();
  v.property(single, "name", "t3");
  assertCount(1, gts.V(v).properties("name"));
  assertCount(2, gts.V().has("id", 2).properties("names"));
  assertCount(2, gts.V().hasLabel("vertex"));
}

代码示例来源:origin: hugegraph/hugegraph

@Test
public void testDeleteAndInsertVertex() {
  HugeGraph graph = graph();
  graph.addVertex(T.label, "author", "id", 1,
          "name", "Tom", "lived", "Beijing");
  graph.traversal().V().hasLabel("author").has("id", 1).next().remove();
  graph.addVertex(T.label, "author", "id", 1,
          "name", "Tom", "lived", "Shanghai");
  graph.tx().commit();
  Vertex vertex = vertex("author", "id", 1);
  Assert.assertTrue(vertex.property("lived").isPresent());
  Assert.assertEquals("Shanghai", vertex.property("lived").value());
}

代码示例来源:origin: apache/tinkerpop

public static Vertex createVertex(final Attachable<Vertex> attachableVertex, final Graph hostGraph) {
  final Vertex baseVertex = attachableVertex.get();
  final Vertex vertex = hostGraph.features().vertex().willAllowId(baseVertex.id()) ?
      hostGraph.addVertex(T.id, baseVertex.id(), T.label, baseVertex.label()) :
      hostGraph.addVertex(T.label, baseVertex.label());
  baseVertex.properties().forEachRemaining(vp -> {
    final VertexProperty vertexProperty = hostGraph.features().vertex().properties().willAllowId(vp.id()) ?
        vertex.property(hostGraph.features().vertex().getCardinality(vp.key()), vp.key(), vp.value(), T.id, vp.id()) :
        vertex.property(hostGraph.features().vertex().getCardinality(vp.key()), vp.key(), vp.value());
    vp.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value()));
  });
  return vertex;
}

代码示例来源:origin: JanusGraph/janusgraph

private void updateVertexWithProperties(String propertyKey, Object propertyValue, Map<Object, Object> map) {
  if (graph.traversal().V().has(propertyKey, propertyValue).hasNext()) {
    final Vertex v = graph.traversal().V().has(propertyKey, propertyValue).next();
    map.forEach((key, value) -> v.property((String) key, value));
    graph.tx().commit();
  }
}

代码示例来源:origin: apache/tinkerpop

final List<Vertex> vertices = DetachedFactory.detach(g.V().hasLabel("software").fold().next(), true);
for (final Vertex v : vertices) {
  assertTrue(v instanceof DetachedVertex);
  assertEquals("java", v.value("lang"));
final List<List<Vertex>> lists = DetachedFactory.detach(g.V().hasLabel("software").fold().fold().next(), true);
for (final Vertex v : lists.get(0)) {
  assertTrue(v instanceof DetachedVertex);
  assertEquals("java", v.value("lang"));
for (final Vertex v : set.iterator().next()) {
  assertTrue(v instanceof DetachedVertex);
  assertEquals("java", v.value("lang"));
Map<Vertex, List<Edge>> map = DetachedFactory.detach(g.V().hasLabel("software").group().by().by(inE().fold()).next(), true);
for (final Map.Entry<Vertex, List<Edge>> entry : map.entrySet()) {
  assertTrue(entry.getKey() instanceof DetachedVertex);
  assertEquals("java", entry.getKey().value("lang"));
  for (final Edge edge : entry.getValue()) {
    assertTrue(edge instanceof DetachedEdge);
    assertTrue(edge.property("weight").isPresent());
map = DetachedFactory.detach(g.V().hasLabel("software").group("m").by().by(inE().fold()).identity().cap("m").next(), true);
for (final Map.Entry<Vertex, List<Edge>> entry : map.entrySet()) {
  assertTrue(entry.getKey() instanceof DetachedVertex);
  assertEquals("java", entry.getKey().value("lang"));
  for (final Edge edge : entry.getValue()) {
    assertTrue(edge instanceof DetachedEdge);
    assertTrue(edge.property("weight").isPresent());

代码示例来源:origin: JanusGraph/janusgraph

private void removeVertex(String property, Object value) {
  final GraphTraversal<Vertex, Vertex> traversal = graph.traversal().V().has(property, value);
  if (traversal.hasNext()) {
    traversal.next().remove();
    graph.tx().commit();
  }
}

代码示例来源:origin: thinkaurelius/titan

v1.property(property, value1);
v1 = getV(graph, v1.id());
v1.property(property, value2);
v1 = getV(graph, v1.id());
v1.properties(property).forEachRemaining(p -> {
  if (p.value().equals(value1)) {
    p.remove();
v1 = getV(graph, v1.id());
v1.property(property, value1);
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
v1 = getV(graph, v1.id());
v1.property(property, value1);
graph.vertices().forEachRemaining(v -> v.remove());
v1 = graph.addVertex();
v1.property(property, value1);
v1.property(property, value2);

代码示例来源:origin: thinkaurelius/titan

@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());
}

代码示例来源:origin: apache/tinkerpop

/**
 * {@inheritDoc}
 */
@Override
public Vertex getVertex(final Vertex vertex, final Graph graph, final GraphTraversalSource g) {
  return useUserSuppliedIds()
      ? getVertexById(vertex.id(), graph, g)
      : g.V().has(vertex.label(), bulkLoaderVertexId, vertex.id().toString()).next();
}

代码示例来源:origin: apache/tinkerpop

@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_PROPERTY)
public void shouldHandleBothEdgesGraphFilterOnSelfLoop() {
  assertEquals(0l, IteratorUtils.count(graph.vertices()));
  assertEquals(0l, IteratorUtils.count(graph.edges()));
  // these vertex label, edge label, and property names/values were copied from existing tests
  StarGraph starGraph = StarGraph.open();
  Vertex vertex = starGraph.addVertex(T.label, "person", "name", "furnace");
  Edge edge = vertex.addEdge("self", vertex);
  edge.property("acl", "private");
  // traversing a self-loop should yield the edge once for inE/outE
  // and the edge twice for bothE (one edge emitted two times, not two edges)
  assertEquals(1L, IteratorUtils.count(starGraph.traversal().V().inE()));
  assertEquals(1L, IteratorUtils.count(starGraph.traversal().V().outE()));
  assertEquals(2L, IteratorUtils.count(starGraph.traversal().V().bothE()));
  
  // Try a filter that retains BOTH
  GraphFilter graphFilter = new GraphFilter();
  graphFilter.setEdgeFilter(__.bothE("self"));
  starGraph = starGraph.applyGraphFilter(graphFilter).get();
  // Retest traversal counts after filtering
  assertEquals(1L, IteratorUtils.count(starGraph.traversal().V().inE()));
  assertEquals(1L, IteratorUtils.count(starGraph.traversal().V().outE()));
  assertEquals(2L, IteratorUtils.count(starGraph.traversal().V().bothE()));
}

代码示例来源:origin: apache/tinkerpop

@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_CUSTOM_IDS)
public void shouldIterateVerticesWithCustomIdSupportUsingVertexIds() {
  final Vertex v1 = graph.addVertex();
  final Vertex v2 = graph.addVertex();
  graph.addVertex();
  tryCommit(graph, graph -> {
    assertEquals(2, IteratorUtils.count(graph.vertices(v1.id(), v2.id())));
  });
}

代码示例来源:origin: thinkaurelius/titan

assertEquals(6.0,v.<Double>value("weight").doubleValue(),0.00001);
VertexProperty p = getOnlyElement(v.properties("weight"));
assertEquals(wintx,p.<Integer>value("sig").intValue());
p = getOnlyElement(v.properties("name"));
assertEquals("Bob",p.value());
assertEquals(wintx,p.<Integer>value("sig").intValue());
p = getOnlyElement(v.properties("value"));
assertEquals(rs[2].longId(),getId(p));
assertEquals(wintx,e.<Integer>value("sig").intValue());
assertNotEquals(rs[6].longId(),getId(e));
assertEquals(wintx,e.<Integer>value("sig").intValue());
assertEquals(rs[7].longId(), getId(e));
e = getOnlyElement(v.query().direction(OUT).labels("o2m").edges());
assertEquals(wintx,e.<Integer>value("sig").intValue());
assertNotEquals(rs[8].longId(),getId(e));
e = getOnlyElement(v.query().direction(OUT).labels("em").edges());
for (Edge ee : v.query().direction(OUT).labels("emf").edges()) {
  assertNotEquals(rs[5].longId(),getId(ee));
  assertEquals(uid,ee.inVertex().id());

相关文章