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

The following examples show how to use org.apache.tinkerpop.gremlin.structure.Vertex#keys() . 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: ObjectVertexTest.java    From act-platform with ISC License 6 votes vote down vote up
@Test
public void testGetPropertyKeysOnVertex() {
  Vertex vertex = createVertex();
  // Test that the following properties exists on the vertex.
  Map<String, String> expected = MapUtils.map(
          T("value", "value")
  );

  Set<String> keys = vertex.keys();
  Set<VertexProperty<Object>> properties = SetUtils.set(vertex.properties());

  assertEquals(expected.size(), keys.size());
  assertEquals(expected.size(), properties.size());

  for (Map.Entry<String, String> entry : expected.entrySet()) {
    assertTrue(keys.contains(entry.getKey()));

    VertexProperty<Object> property = vertex.property(entry.getKey());
    assertNotNull(property.id());
    assertEquals(entry.getValue(), property.value());
    assertEquals(property.key(), property.label());
    assertEquals(StringFactory.propertyString(property), property.toString());
    assertSame(vertex, property.element());
  }
}
 
Example 2
Source File: ObjectVertexTest.java    From act-platform with ISC License 6 votes vote down vote up
@Test
public void testGetPropertyKeysOnVertex() {
  Vertex vertex = createVertex(list(new PropertyEntry<>("value", "someObjectValue")));
  // Test that the following properties exists on the vertex.
  Map<String, String> expected = map(
          T("value", "someObjectValue")
  );

  Set<String> keys = vertex.keys();
  Set<VertexProperty<Object>> properties = set(vertex.properties());

  assertEquals(expected.size(), keys.size());
  assertEquals(expected.size(), properties.size());

  for (Map.Entry<String, String> entry : expected.entrySet()) {
    assertTrue(keys.contains(entry.getKey()));

    VertexProperty<Object> property = vertex.property(entry.getKey());
    assertNotNull(property.id());
    assertEquals(entry.getValue(), property.value());
    assertEquals(property.key(), property.label());
    assertEquals(StringFactory.propertyString(property), property.toString());
    assertSame(vertex, property.element());
  }
}
 
Example 3
Source File: GraphMLWriter.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static Map<String, String> determineVertexTypes(final Graph graph) {
    final Map<String, String> vertexKeyTypes = new HashMap<>();
    final Iterator<Vertex> vertices = graph.vertices();
    try {
        while (vertices.hasNext()) {
            final Vertex vertex = vertices.next();
            for (String key : vertex.keys()) {
                if (!vertexKeyTypes.containsKey(key)) {
                    final VertexProperty<Object> currentValue = getCheckedVertexProperty(vertex, key);

                    vertexKeyTypes.put(key, GraphMLWriter.getStringType(currentValue.value()));
                }
            }
        }
    } finally {
        CloseableIterator.closeIterator(vertices);
    }

    return vertexKeyTypes;
}
 
Example 4
Source File: AbstractCompatibilityTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
protected void assertVertex(final Vertex expected, final Vertex actual) {
    assertEquals(expected.id(), actual.id());
    assertEquals(expected.label(), actual.label());

    if (!(getCompatibility() instanceof GraphBinaryCompatibility)) {
        assertEquals(IteratorUtils.count(expected.properties()), IteratorUtils.count(actual.properties()));
        for (String k : expected.keys()) {
            final Iterator<VertexProperty<Object>> expectedVps = expected.properties(k);
            final List<VertexProperty<Object>> actualVps = IteratorUtils.list(actual.properties(k));
            while (expectedVps.hasNext()) {
                final VertexProperty expectedVp = expectedVps.next();
                final VertexProperty<Object> found = actualVps.stream()
                        .filter(vp -> vp.id().equals(expectedVp.id()))
                        .findFirst()
                        .orElseThrow(() -> new RuntimeException("Could not find VertexProperty for " + expectedVp.id()));
                assertVertexProperty(expectedVp, found);
            }
        }
    }
}
 
Example 5
Source File: SetInPropertiesHandler.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Getter
 */
private static Set<String> handleGetter(Vertex vertex, Method method, Object[] args, SetInProperties ann)
{
    if (args != null && args.length != 0)
        throw new WindupException("Method must take zero arguments");

    Set<String> set = new HashSet<>();
    String prefix = preparePrefix(ann);

    Set<String> keys = vertex.keys();
    for (String key : keys)
    {
        String tail = key;
        if (!prefix.isEmpty())
        {
            if (!key.startsWith(prefix))
                continue;
            else
                tail = key.substring(prefix.length());
        }

        set.add(tail);
    }

    return set;
}
 
Example 6
Source File: GraphSONSerializersV1d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private void writeProperties(final Vertex vertex, final JsonGenerator jsonGenerator,
                                    final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException {
    jsonGenerator.writeObjectFieldStart(GraphSONTokens.PROPERTIES);
    if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName());

    final List<String> keys = normalize ?
            IteratorUtils.list(vertex.keys().iterator(), Comparator.naturalOrder()) : new ArrayList<>(vertex.keys());
    for (String key : keys) {
        final Iterator<VertexProperty<Object>> vertexProperties = normalize ?
                IteratorUtils.list(vertex.properties(key), Comparators.PROPERTY_COMPARATOR).iterator() : vertex.properties(key);

        if (vertexProperties.hasNext()) {
            jsonGenerator.writeArrayFieldStart(key);
            if (typeSerializer != null) {
                jsonGenerator.writeString(ArrayList.class.getName());
                jsonGenerator.writeStartArray();
            }

            while (vertexProperties.hasNext()) {
                serializerVertexProperty(vertexProperties.next(), jsonGenerator, serializerProvider, typeSerializer, normalize, false);
            }

            jsonGenerator.writeEndArray();
            if (typeSerializer != null) jsonGenerator.writeEndArray();
        }
    }

    jsonGenerator.writeEndObject();
}
 
Example 7
Source File: TestHelper.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static void validateVertexEquality(final Vertex originalVertex, final Vertex otherVertex, boolean testEdges) {
    assertEquals(originalVertex, otherVertex);
    assertEquals(otherVertex, originalVertex);
    assertEquals(originalVertex.id(), otherVertex.id());
    assertEquals(originalVertex.label(), otherVertex.label());
    assertEquals(originalVertex.keys().size(), otherVertex.keys().size());
    for (final String key : originalVertex.keys()) {
        final List<VertexProperty<Object>> originalVertexProperties = IteratorUtils.list(originalVertex.properties(key));
        final List<VertexProperty<Object>> otherVertexProperties = IteratorUtils.list(otherVertex.properties(key));
        assertEquals(originalVertexProperties.size(), otherVertexProperties.size());
        for (VertexProperty<Object> originalVertexProperty : originalVertexProperties) {
            final VertexProperty<Object> otherVertexProperty = otherVertexProperties.parallelStream().filter(vp -> vp.equals(originalVertexProperty)).findAny().get();
            validateVertexPropertyEquality(originalVertexProperty, otherVertexProperty);
        }
    }
    if (testEdges) {
        Iterator<Edge> originalEdges = IteratorUtils.list(originalVertex.edges(Direction.OUT), Comparators.ELEMENT_COMPARATOR).iterator();
        Iterator<Edge> otherEdges = IteratorUtils.list(otherVertex.edges(Direction.OUT), Comparators.ELEMENT_COMPARATOR).iterator();
        while (originalEdges.hasNext()) {
            validateEdgeEquality(originalEdges.next(), otherEdges.next());
        }
        assertFalse(otherEdges.hasNext());

        originalEdges = IteratorUtils.list(originalVertex.edges(Direction.IN), Comparators.ELEMENT_COMPARATOR).iterator();
        otherEdges = IteratorUtils.list(otherVertex.edges(Direction.IN), Comparators.ELEMENT_COMPARATOR).iterator();
        while (originalEdges.hasNext()) {
            validateEdgeEquality(originalEdges.next(), otherEdges.next());
        }
        assertFalse(otherEdges.hasNext());
    }

}
 
Example 8
Source File: MapInPropertiesHandler.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Getter
 */
private static Map<String, Object> handleGetter(Vertex vertex, Method method, Object[] args, MapInProperties ann)
{
    if (args != null && args.length != 0)
        throw new WindupException("Method must take zero arguments");

    Map<String, Object> map = new HashMap<>();
    String prefix = preparePrefix(ann);

    Set<String> keys = vertex.keys();
    for (String key : keys)
    {
        if (!key.startsWith(prefix))
            continue;

        // Skip the type property
        if (key.equals(WindupFrame.TYPE_PROP))
            continue;

        final Property<Object> val = vertex.property(key);
        if (!ann.propertyType().isAssignableFrom(val.value().getClass()))
        {
            log.warning("@InProperties is meant for Map<String," + ann.propertyType().getName() + ">, but the value was: " + val.getClass());
        }

        map.put(key.substring(prefix.length()), val.value());
    }

    return map;
}
 
Example 9
Source File: MapInAdjacentPropertiesHandler.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Getter
 */
private static Map<String, Serializable> handleGetter(Vertex vertex, Method method, Object[] args,
            MapInAdjacentProperties ann)
{
    if (args != null && args.length != 0)
        throw new WindupException("Method must take no arguments: " + method.getName());

    // Find the map vertex.
    Map<String, Serializable> map = new HashMap<>();
    Iterator<Vertex> it = vertex.vertices(Direction.OUT, ann.label());
    Vertex mapVertex = null;
    if (!it.hasNext())
    {
        // No map yet.
        return map;
    }
    else
    {
        mapVertex = it.next();
        if (it.hasNext())
        {
            // Multiple vertices behind edges with given label.
            log.warning("Found multiple vertices for a map, using only first one; for: " + method.getName());
        }
    }

    Set<String> keys = mapVertex.keys();
    for (String key : keys)
    {
        final Property<Object> val = mapVertex.property(key);
        if (!val.isPresent() || !(val.value() instanceof String))
            log.warning("@InProperties is meant for Map<String,Serializable>, but the value was: " + val.getClass());
        map.put(key, "" + val.value());
    }
    return map;
}
 
Example 10
Source File: SetInPropertiesHandler.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Setter
 */
private static void handleSetter(Vertex vertex, Method method, Object[] args, SetInProperties ann)
{
    // Argument.
    if (args == null || args.length != 1)
        throw new WindupException("Method must take one argument: " + method.getName());

    if (!(args[0] instanceof Set))
        throw new WindupException("Argument of " + method.getName() + " must be a Set<String>, but is: " + args[0].getClass());

    @SuppressWarnings("unchecked")
    Set<String> newSet = (Set<String>) args[0];

    String prefix = preparePrefix(ann);

    // For all keys in the old set...
    Set<String> vertKeys = vertex.keys();
    for (String vertKey : vertKeys)
    {
        if (!vertKey.startsWith(prefix))
            continue;
        if (WindupVertexFrame.TYPE_PROP.equals(vertKey)) // Leave the "type" property.
            continue;
        if (vertKey.startsWith("w:")) // Leave windup internal properties. TODO: Get the prefix from somewhere.
            continue;

        String subKey = vertKey.substring(prefix.length());
        // ...either change to the new value,
        if (newSet.contains(subKey))
        {
            vertex.property(vertKey, SET_VERTEX_PROP_VALUE);
            newSet.remove(subKey);
        }
        // or remove the old.
        else
            vertex.property(vertKey).remove();
    }

    // Add the new entries.
    for (String item : newSet)
    {
        if (!(item instanceof String))
            throw new WindupException("Argument of " + method.getName() + " must be a Set<String>, but it contains: " + item.getClass());
        vertex.property(prefix + item, "1");
    }
}