Java Code Examples for org.apache.tinkerpop.gremlin.structure.Vertex#equals()

The following examples show how to use org.apache.tinkerpop.gremlin.structure.Vertex#equals() . 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: StarGraph.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Edge addEdge(final String label, final Vertex inVertex, final Object... keyValues) {
    final Edge edge = this.addOutEdge(label, inVertex, keyValues);
    if (inVertex.equals(this)) {
        if (ElementHelper.getIdValue(keyValues).isPresent()) {
            // reuse edge ID from method params
            this.addInEdge(label, this, keyValues);
        } else {
            // copy edge ID that we just allocated with addOutEdge
            final Object[] keyValuesWithId = Arrays.copyOf(keyValues, keyValues.length + 2);
            keyValuesWithId[keyValuesWithId.length - 2] = T.id;
            keyValuesWithId[keyValuesWithId.length - 1] = edge.id();
            this.addInEdge(label, this, keyValuesWithId);
        }
    }
    return edge;
}
 
Example 2
Source File: ShortestPathVertexProgram.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private void processEdges(final Vertex vertex, final Path currentPath, final Number currentDistance,
                          final Messenger<Triplet<Path, Edge, Number>> messenger) {

    final Traversal.Admin<Vertex, Edge> edgeTraversal = this.edgeTraversal.getPure();
    edgeTraversal.addStart(edgeTraversal.getTraverserGenerator().generate(vertex, edgeTraversal.getStartStep(), 1));

    while (edgeTraversal.hasNext()) {
        final Edge edge = edgeTraversal.next();
        final Number distance = getDistance(edge);

        Vertex otherV = edge.inVertex();
        if (otherV.equals(vertex))
            otherV = edge.outVertex();

        // only send message if the adjacent vertex is not yet part of the current path
        if (!currentPath.objects().contains(otherV)) {
            messenger.sendMessage(MessageScope.Global.of(otherV),
                    Triplet.with(currentPath, this.includeEdges ? edge : null,
                            NumberHelper.add(currentDistance, distance)));
        }
    }
}
 
Example 3
Source File: SelectTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_chooseXoutE_count_isX0X__asXaX__asXbXX_chooseXselectXaX__selectXaX__selectXbXX() {
    final Traversal<Vertex, Vertex> traversal = get_g_V_chooseXoutE_count_isX0X__asXaX__asXbXX_chooseXselectXaX__selectXaX__selectXbXX();
    printTraversalForm(traversal);
    int counter = 0;
    int xCounter = 0;
    int yCounter = 0;
    while (traversal.hasNext()) {
        counter++;
        final Vertex vertex = traversal.next();
        if (vertex.equals(convertToVertex(graph, "vadas")) || vertex.equals(convertToVertex(graph, "lop")) || vertex.equals(convertToVertex(graph, "ripple")))
            xCounter++;
        else if (vertex.equals(convertToVertex(graph, "marko")) || vertex.equals(convertToVertex(graph, "josh")) || vertex.equals(convertToVertex(graph, "peter")))
            yCounter++;
        else
            fail("This state should not have occurred");
    }
    assertEquals(6, counter);
    assertEquals(3, yCounter);
    assertEquals(3, xCounter);
}
 
Example 4
Source File: TestGremlinCompileV.java    From sqlg with MIT License 6 votes vote down vote up
private void tetOutOut_assert(SqlgGraph sqlgGraph, Vertex a, Vertex c, Vertex d1, Vertex d2) {
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) vertexTraversal(sqlgGraph, a).out().out();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(4, vertices.size());
    Assert.assertTrue(vertices.contains(c));
    Assert.assertTrue(vertices.contains(d1));
    Assert.assertTrue(vertices.contains(d2));
    int count = 0;
    for (Vertex vertex : vertices) {
        if (vertex.equals(c)) {
            count++;
        }
    }
    Assert.assertEquals(2, count);
    Assert.assertEquals("c", vertices.get(vertices.indexOf(c)).value("nAmE"));
    Assert.assertEquals("d1", vertices.get(vertices.indexOf(d1)).value("NAME"));
    Assert.assertEquals("d2", vertices.get(vertices.indexOf(d2)).value("NAME"));
}
 
Example 5
Source File: TestGremlinCompileV.java    From sqlg with MIT License 6 votes vote down vote up
private void testOutOutWithLabels_assert(SqlgGraph sqlgGraph, Vertex a, Vertex c, Vertex d1, Vertex d2) {
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) vertexTraversal(sqlgGraph, a)
            .out("outB", "outE").out("outC", "outD");
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(4, vertices.size());
    Assert.assertTrue(vertices.contains(c));
    Assert.assertTrue(vertices.contains(d1));
    Assert.assertTrue(vertices.contains(d2));
    int count = 0;
    for (Vertex vertex : vertices) {
        if (vertex.equals(c)) {
            count++;
        }
    }
    Assert.assertEquals(2, count);
    Assert.assertEquals("c", vertices.get(vertices.indexOf(c)).value("nAmE"));
    Assert.assertEquals("d1", vertices.get(vertices.indexOf(d1)).value("NAME"));
    Assert.assertEquals("d2", vertices.get(vertices.indexOf(d2)).value("NAME"));
}
 
Example 6
Source File: TestGremlinCompileV.java    From sqlg with MIT License 6 votes vote down vote up
private void testOutOutWithLabels2_assert(SqlgGraph sqlgGraph, Vertex a, Vertex c) {
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) vertexTraversal(sqlgGraph, a).out("outB").out("outC");
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(2, vertices.size());
    Assert.assertTrue(vertices.contains(c));
    int count = 0;
    for (Vertex vertex : vertices) {
        if (vertex.equals(c)) {
            count++;
        }
    }
    Assert.assertEquals(2, count);
    Assert.assertEquals("c", vertices.get(vertices.indexOf(c)).value("nAmE"));
}
 
Example 7
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public static Iterator<Edge> filterResult(Vertex vertex,
                                          Directions dir,
                                          Iterator<Edge> edges) {
    return new FilterIterator<>(edges, edge -> {
        if (dir == Directions.OUT && vertex.equals(edge.outVertex()) ||
            dir == Directions.IN && vertex.equals(edge.inVertex())) {
            return true;
        }
        return false;
    });
}
 
Example 8
Source File: BufferGraphConnector.java    From egeria with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the vertex of the schema element in the output of a process.
 * @param endingVertex - The vertex that is being checked if it is the output schema element
 * @param g - Graph traversal object
 * @param startingVertex - The vertex of the input schema element
 * @return Return a vertex of the output schema element
 */
private Vertex findPathForOutputAsset(Vertex endingVertex, GraphTraversalSource g,Vertex startingVertex)  {
    final String VERTEX = "vertex";

    try{
        Iterator<Vertex> end =  g.V(endingVertex.id())
                .or(__.out(ATTRIBUTE_FOR_SCHEMA).out(ASSET_SCHEMA_TYPE)
                        .has(PROPERTY_KEY_LABEL,DATA_FILE).store(VERTEX),
                        __.out(NESTED_SCHEMA_ATTRIBUTE).has(PROPERTY_KEY_LABEL,RELATIONAL_TABLE)
                                .store(VERTEX)).select(VERTEX).unfold();

        if (!end.hasNext()) {
            List<Vertex> next = g.V(endingVertex.id()).both(LINEAGE_MAPPING).toList();
            Vertex nextVertex = null;
            for(Vertex vert: next){
                if(vert.equals(startingVertex)){
                    continue;
                }
                nextVertex = vert;
            }


            return findPathForOutputAsset(nextVertex, g,endingVertex);
        }

        return endingVertex;
    }
    catch (Exception e){
        if (log.isDebugEnabled()) {
            log.debug("Vertex does not exist {}",startingVertex.id());
        }
        return null;
    }
}
 
Example 9
Source File: VertexElementImpl.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Sets the value of a property with the added restriction that no other vertex can have that property.
 *
 * @param key   The key of the unique property to mutate
 * @param value The new value of the unique property
 */
@Override
public void propertyUnique(Schema.VertexProperty key, String value) {
    Iterator<VertexElement> verticesWithProperty = elementFactory.getVerticesWithProperty(key, value).iterator();

    if (verticesWithProperty.hasNext()) {
        Vertex vertex = verticesWithProperty.next().element();
        if (!vertex.equals(element()) || verticesWithProperty.hasNext()) {
            if (verticesWithProperty.hasNext()) vertex = verticesWithProperty.next().element();
            throw PropertyNotUniqueException.cannotChangeProperty(element(), vertex, key, value);
        }
    }

    property(key, value);
}
 
Example 10
Source File: VertexDeserializer.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private static Boolean isLoopAdded(Vertex vertex, String label) {
    Iterator<Vertex> adjacentVertices = vertex.vertices(Direction.BOTH, label);

    while (adjacentVertices.hasNext()) {
        Vertex adjacentVertex = adjacentVertices.next();

        if (adjacentVertex.equals(vertex)) {
            return true;
        }
    }

    return false;
}
 
Example 11
Source File: TitanVertexDeserializer.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private static Boolean isLoopAdded(Vertex vertex, String label) {
    Iterator<Vertex> adjacentVertices = vertex.vertices(Direction.BOTH, label);

    while (adjacentVertices.hasNext()) {
        Vertex adjacentVertex = adjacentVertices.next();

        if(adjacentVertex.equals(vertex)){
            return true;
        }
    }

    return false;
}
 
Example 12
Source File: StarGraph.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Edge addEdge(final String label, final Vertex inVertex, final Object... keyValues) {
    if (inVertex.equals(starVertex))
        return starVertex.addInEdge(label, this, keyValues);
    else
        throw GraphComputer.Exceptions.adjacentVertexEdgesAndVerticesCanNotBeReadOrUpdated();
}
 
Example 13
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 4 votes vote down vote up
/**
 * Checks sequentially vertices and edges of both graphs. Will check sequentially Vertex IDs, Vertex Properties IDs
 * and values and classes. Then same for edges. To use when serializing a Graph and deserializing the supposedly
 * same Graph.
 */
private boolean approximateGraphsCheck(Graph g1, Graph g2) {
    final Iterator<Vertex> itV = g1.vertices();
    final Iterator<Vertex> itVRead = g2.vertices();

    while (itV.hasNext()) {
        final Vertex v = itV.next();
        final Vertex vRead = itVRead.next();

        // Will only check IDs but that's 'good' enough.
        if (!v.equals(vRead)) {
            return false;
        }

        final Iterator itVP = v.properties();
        final Iterator itVPRead = vRead.properties();
        while (itVP.hasNext()) {
            final VertexProperty vp = (VertexProperty) itVP.next();
            final VertexProperty vpRead = (VertexProperty) itVPRead.next();
            if (!vp.value().equals(vpRead.value())
                    || !vp.equals(vpRead)) {
                return false;
            }
        }
    }

    final Iterator<Edge> itE = g1.edges();
    final Iterator<Edge> itERead = g2.edges();

    while (itE.hasNext()) {
        final Edge e = itE.next();
        final Edge eRead = itERead.next();
        // Will only check IDs but that's good enough.
        if (!e.equals(eRead)) {
            return false;
        }

        final Iterator itEP = e.properties();
        final Iterator itEPRead = eRead.properties();
        while (itEP.hasNext()) {
            final Property ep = (Property) itEP.next();
            final Property epRead = (Property) itEPRead.next();
            if (!ep.value().equals(epRead.value())
                    || !ep.equals(epRead)) {
                return false;
            }
        }
    }
    return true;
}
 
Example 14
Source File: TinkerGraphQuerySemaphoreNeighbor.java    From trainbenchmark with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Collection<TinkerGraphSemaphoreNeighborMatch> evaluate() {
	final Collection<TinkerGraphSemaphoreNeighborMatch> matches = new ArrayList<>();

	final Collection<Vertex> route1s = driver.getVertices(ModelConstants.ROUTE);

	for (final Vertex route1 : route1s) {
		// (route1:Route)-[:exit]->(semaphore:Semaphore)
		final Iterable<Vertex> semaphores = TinkerGraphUtil.getAdjacentNodes(route1, ModelConstants.EXIT, Direction.OUT,
				ModelConstants.SEMAPHORE);
		for (final Vertex semaphore : semaphores) {
			// (route1:Route)-[:requires]->(sensor1:Sensor)
			final Iterable<Vertex> sensor1s = TinkerGraphUtil.getAdjacentNodes(route1, ModelConstants.REQUIRES, Direction.OUT,
					ModelConstants.SENSOR);
			for (final Vertex sensor1 : sensor1s) {
				// (sensor1:Sensor)<-[:sensor]-(te1:TrackElement)
				final Iterable<Vertex> te1s = TinkerGraphUtil.getAdjacentNodes(sensor1, ModelConstants.MONITORED_BY, Direction.IN,
						new String[] { ModelConstants.SEGMENT, ModelConstants.SWITCH });
				for (final Vertex te1 : te1s) {
					// (te1:TrackElement)-[:connectsTo]->(te2:TrackElement)
					final Iterable<Vertex> te2s = TinkerGraphUtil.getAdjacentNodes(te1, ModelConstants.CONNECTS_TO, Direction.OUT,
							new String[] { ModelConstants.SEGMENT, ModelConstants.SWITCH });
					for (final Vertex te2 : te2s) {
						// (te2:TrackElement)-[:sensor]->(sensor2:Sensor)
						final Iterable<Vertex> sensor2s = TinkerGraphUtil.getAdjacentNodes(te2, ModelConstants.MONITORED_BY, Direction.OUT, ModelConstants.SENSOR);
						for (final Vertex sensor2 : sensor2s) {
							// (sensor2:Sensor)<-[:requires]-(route2:Route),
							final Iterable<Vertex> route2s = TinkerGraphUtil.getAdjacentNodes(sensor2, ModelConstants.REQUIRES, Direction.IN, ModelConstants.ROUTE);
							for (final Vertex route2 : route2s) {
								// route1 != route2 --> if (route1 == route2), continue
								if (route1.equals(route2)) {
									continue;
								}

								// (route2)-[:entry]->(semaphore) NAC
								if (!TinkerGraphUtil.isConnected(route2, semaphore, ModelConstants.ENTRY)) {
									matches.add(new TinkerGraphSemaphoreNeighborMatch(semaphore, route1, route2, sensor1, sensor2, te1, te2));
									break;
								}
							}
						}
					}
				}
			}
		}
	}

	return matches;
}
 
Example 15
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
/**
 * Checks sequentially vertices and edges of both graphs. Will check sequentially Vertex IDs, Vertex Properties IDs
 * and values and classes. Then same for edges. To use when serializing a Graph and deserializing the supposedly
 * same Graph.
 */
private boolean approximateGraphsCheck(Graph g1, Graph g2) {
    final Iterator<Vertex> itV = g1.vertices();
    final Iterator<Vertex> itVRead = g2.vertices();

    while (itV.hasNext()) {
        final Vertex v = itV.next();
        final Vertex vRead = itVRead.next();

        // Will only check IDs but that's 'good' enough.
        if (!v.equals(vRead)) {
            return false;
        }

        final Iterator itVP = v.properties();
        final Iterator itVPRead = vRead.properties();
        while (itVP.hasNext()) {
            final VertexProperty vp = (VertexProperty) itVP.next();
            final VertexProperty vpRead = (VertexProperty) itVPRead.next();
            if (!vp.value().equals(vpRead.value())
                    || !vp.equals(vpRead)) {
                return false;
            }
        }
    }

    final Iterator<Edge> itE = g1.edges();
    final Iterator<Edge> itERead = g2.edges();

    while (itE.hasNext()) {
        final Edge e = itE.next();
        final Edge eRead = itERead.next();
        // Will only check IDs but that's good enough.
        if (!e.equals(eRead)) {
            return false;
        }

        final Iterator itEP = e.properties();
        final Iterator itEPRead = eRead.properties();
        while (itEP.hasNext()) {
            final Property ep = (Property) itEP.next();
            final Property epRead = (Property) itEPRead.next();
            if (!ep.value().equals(epRead.value())
                    || !ep.equals(epRead)) {
                return false;
            }
        }
    }
    return true;
}
 
Example 16
Source File: TinkerGraphUtil.java    From trainbenchmark with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * The trick used in the
 * hu.bme.mit.trainbenchmark.benchmark.neo4j.util.Neo4jUtil#isConnected()
 * method will not work here as TinkerPop's Vertex class does not provide an
 * interface for efficiently accessing the degree (w.r.t. a certain
 * relationship type/direction) of the node.
 * 
 * @param source
 * @param target
 * @param edgeLabel
 * @return
 */
public static boolean isConnected(final Vertex source, final Vertex target, final String edgeLabel) {
	final Iterable<Vertex> vertices = () -> source.vertices(Direction.OUT, edgeLabel);
	for (final Vertex vertex : vertices) {
		if (vertex.equals(target)) {
			return true;
		}
	}
	return false;
}