@Override public <U> Property<U> property(final String key) { return this.baseVertexProperty.property(key); }
private void addPropertiesToOriginalGraph() { TinkerHelper.dropGraphComputerView(this.graph); this.computeProperties.forEach((element, properties) -> { properties.forEach((key, vertexProperties) -> { vertexProperties.forEach(vertexProperty -> { final VertexProperty<?> newVertexProperty = ((Vertex) element).property(VertexProperty.Cardinality.list, vertexProperty.key(), vertexProperty.value(), T.id, vertexProperty.id()); vertexProperty.properties().forEachRemaining(property -> { newVertexProperty.property(property.key(), property.value()); }); }); }); }); this.computeProperties.clear(); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_REMOVE_PROPERTY) public void shouldAllowIteratingAndRemovingVertexPropertyProperties() { final Vertex daniel = graph.addVertex("name", "daniel", "name", "kuppitz", "name", "big d", "name", "the german"); daniel.properties("name").forEachRemaining(vp -> { vp.<Object>property("aKey", UUID.randomUUID().toString()); vp.<Object>property("bKey", UUID.randomUUID().toString()); vp.<Object>property("cKey", UUID.randomUUID().toString()); }); assertEquals(4, IteratorUtils.count(daniel.properties())); assertEquals(12, IteratorUtils.list(IteratorUtils.map(daniel.properties(), p -> IteratorUtils.count(p.properties()))).stream().mapToInt(kv -> kv.intValue()).sum()); daniel.properties().forEachRemaining(p -> p.properties().forEachRemaining(Property::remove)); assertEquals(4, IteratorUtils.count(daniel.properties())); assertEquals(0, IteratorUtils.list(IteratorUtils.map(daniel.properties(), p -> IteratorUtils.count(p.properties()))).stream().mapToInt(kv -> kv.intValue()).sum()); daniel.properties().forEachRemaining(VertexProperty::remove); assertEquals(0, IteratorUtils.count(daniel.properties())); assertEquals(0, IteratorUtils.list(IteratorUtils.map(daniel.properties(), p -> IteratorUtils.count(p.properties()))).stream().mapToInt(kv -> kv.intValue()).sum()); }
/** * Creates a clone of the given property for the given vertex. * * @param property The property to be cloned. * @param vertex The vertex in the given graph.. * @param graph The graph that holds the given vertex. * @param g A standard traversal source for the given graph. * @return The cloned property. */ public default VertexProperty createVertexProperty(final VertexProperty<?> property, final Vertex vertex, final Graph graph, final GraphTraversalSource g) { final VertexProperty result = vertex.property(property.key(), property.value()); property.properties().forEachRemaining(metaProperty -> result.property(metaProperty.key(), metaProperty.value())); return result; }
public static Property createProperty(final Attachable<Property> attachableProperty, final Vertex hostVertex) { final Property baseProperty = attachableProperty.get(); final Element baseElement = baseProperty.element(); if (baseElement instanceof Vertex) { return Method.createVertexProperty((Attachable) attachableProperty, hostVertex); } else if (baseElement instanceof Edge) { final Iterator<Edge> edgeIterator = hostVertex.edges(Direction.OUT); if (edgeIterator.hasNext()) return edgeIterator.next().property(baseProperty.key(), baseProperty.value()); throw new IllegalStateException("Could not find edge to create the property on"); } else { // vertex property final Iterator<VertexProperty<Object>> vertexPropertyIterator = hostVertex.properties(((VertexProperty) baseElement).key()); while (vertexPropertyIterator.hasNext()) { final VertexProperty<Object> vp = vertexPropertyIterator.next(); if (ElementHelper.areEqual(vp, baseElement)) return vp.property(baseProperty.key(), baseProperty.value()); } throw new IllegalStateException("Could not find vertex property to create the attachable property on"); } } }
public static Property createProperty(final Attachable<Property> attachableProperty, final Graph hostGraph) { final Property baseProperty = attachableProperty.get(); final Element baseElement = baseProperty.element(); if (baseElement instanceof Vertex) { return Method.createVertexProperty((Attachable) attachableProperty, hostGraph); } else if (baseElement instanceof Edge) { final Iterator<Edge> edgeIterator = hostGraph.edges(baseElement.id()); if (edgeIterator.hasNext()) return edgeIterator.next().property(baseProperty.key(), baseProperty.value()); throw new IllegalStateException("Could not find edge to create the attachable property on"); } else { // vertex property final Iterator<Vertex> vertexIterator = hostGraph.vertices(((VertexProperty) baseElement).element().id()); if (vertexIterator.hasNext()) { final Vertex vertex = vertexIterator.next(); final Iterator<VertexProperty<Object>> vertexPropertyIterator = vertex.properties(((VertexProperty) baseElement).key()); while (vertexPropertyIterator.hasNext()) { final VertexProperty<Object> vp = vertexPropertyIterator.next(); if (ElementHelper.areEqual(vp, baseElement)) return vp.property(baseProperty.key(), baseProperty.value()); } } throw new IllegalStateException("Could not find vertex property to create the attachable property on"); } }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = VertexPropertyFeatures.FEATURE_REMOVE_PROPERTY, supported = false) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = VertexFeatures.FEATURE_META_PROPERTIES) @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldSupportRemovePropertyIfAPropertyCanBeRemoved() throws Exception { try { final Vertex v = graph.addVertex(); final VertexProperty p = v.property(VertexProperty.Cardinality.single, "name", "me", "test", "this"); p.property("test").remove(); fail(String.format(INVALID_FEATURE_SPECIFICATION, VertexPropertyFeatures.class.getSimpleName(), VertexPropertyFeatures.FEATURE_REMOVE_PROPERTY)); } catch (Exception ex) { validateException(Property.Exceptions.propertyRemovalNotSupported(), ex); } }
/** * {@inheritDoc} */ @Override public VertexProperty getOrCreateVertexProperty(final VertexProperty<?> property, final Vertex vertex, final Graph graph, final GraphTraversalSource g) { final VertexProperty<?> vp; final VertexProperty<?> existing = vertex.property(property.key()); if (!existing.isPresent()) { return createVertexProperty(property, vertex, graph, g); } if (!existing.value().equals(property.value())) { vp = vertex.property(property.key(), property.value()); } else { vp = existing; } property.properties().forEachRemaining(metaProperty -> { final Property<?> existing2 = vp.property(metaProperty.key()); if (!existing2.isPresent() || !existing2.value().equals(metaProperty.value())) { vp.property(metaProperty.key(), metaProperty.value()); } }); return vp; }
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @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.VertexFeatures.class, feature = VertexFeatures.FEATURE_META_PROPERTIES, supported = false) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = VertexFeatures.FEATURE_META_PROPERTIES) public void shouldSupportMetaPropertyIfPropertiesCanBePutOnPropertiesViaVertexProperty() throws Exception { try { final Vertex v = graph.addVertex("name", "stephen"); v.property("name").property("p", "on-property"); fail(String.format(INVALID_FEATURE_SPECIFICATION, VertexFeatures.class.getSimpleName(), VertexFeatures.FEATURE_META_PROPERTIES)); } catch (Exception ex) { validateException(VertexProperty.Exceptions.metaPropertiesNotSupported(), ex); } }
private Vertex getOrCreate(final Vertex vertex) { final Iterator<Vertex> vertexIterator = subgraph.vertices(vertex.id()); if (vertexIterator.hasNext()) return vertexIterator.next(); final Vertex subgraphVertex = subgraph.addVertex(T.id, vertex.id(), T.label, vertex.label()); vertex.properties().forEachRemaining(vertexProperty -> { final VertexProperty.Cardinality cardinality = parentGraphFeatures.getCardinality(vertexProperty.key()); final VertexProperty<?> subgraphVertexProperty = subgraphVertex.property(cardinality, vertexProperty.key(), vertexProperty.value(), T.id, vertexProperty.id()); // only iterate the VertexProperties if the current graph can have them and if the subgraph can support // them. unfortunately we don't have a way to write a test for this as we dont' have a graph that supports // user supplied ids and doesn't support metaproperties. if (parentGraphFeatures.supportsMetaProperties() && subgraphSupportsMetaProperties) { vertexProperty.properties().forEachRemaining(property -> subgraphVertexProperty.property(property.key(), property.value())); } }); return subgraphVertex; }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES) public void shouldReturnEmptyIfNoMetaProperties() { final Vertex v = graph.addVertex(); final VertexProperty<String> vp = v.property(VertexProperty.Cardinality.single, "name", "marko"); assertEquals(Property.empty(), vp.property("name")); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldReadWriteVertexPropertyWithMetaProperties() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); // select any vertexproperty that has both start/end time final VertexProperty p = (VertexProperty) g.V(convertToVertexId("marko")).properties("location").as("p").has("endTime").select("p").next(); writer.writeVertexProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readVertexProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); assertEquals(IteratorUtils.count(p.properties()), IteratorUtils.count(propertyAttachable.get().properties())); assertEquals(p.property("startTime").value(), ((Property) propertyAttachable.get().properties("startTime").next()).value()); assertEquals(p.property("endTime").value(), ((Property) propertyAttachable.get().properties("endTime").next()).value()); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } }
public static VertexProperty createVertexProperty(final Attachable<VertexProperty> attachableVertexProperty, final Graph hostGraph) { final VertexProperty<Object> baseVertexProperty = attachableVertexProperty.get(); final Iterator<Vertex> vertexIterator = hostGraph.vertices(baseVertexProperty.element().id()); if (vertexIterator.hasNext()) { final VertexProperty vertexProperty = hostGraph.features().vertex().properties().willAllowId(baseVertexProperty.id()) ? vertexIterator.next().property(hostGraph.features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value(), T.id, baseVertexProperty.id()) : vertexIterator.next().property(hostGraph.features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value()); baseVertexProperty.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value())); return vertexProperty; } throw new IllegalStateException("Could not find vertex to create the attachable vertex property on"); }
v.property("name", "John"); VertexProperty p = v.property("value", 11); p.property("weight", 22); v.property("value", 33.3, "weight", 66.6); v.property("value", 11, "weight", 22); //same values are supported for list-properties
public static void validateVertexPropertyEquality(final VertexProperty originalVertexProperty, final VertexProperty otherVertexProperty) { assertEquals(originalVertexProperty, otherVertexProperty); assertEquals(otherVertexProperty, originalVertexProperty); if (originalVertexProperty.isPresent()) { assertEquals(originalVertexProperty.key(), otherVertexProperty.key()); assertEquals(originalVertexProperty.value(), otherVertexProperty.value()); assertEquals(originalVertexProperty.element(), otherVertexProperty.element()); final boolean originalSupportsMetaProperties = originalVertexProperty.graph().features().vertex().supportsMetaProperties(); final boolean otherSupportsMetaProperties = otherVertexProperty.graph().features().vertex().supportsMetaProperties(); // if one supports and the other doesn't then neither should have meta properties. if (originalSupportsMetaProperties && !otherSupportsMetaProperties) assertEquals(0, originalVertexProperty.keys().size()); else if (!originalSupportsMetaProperties && otherSupportsMetaProperties) assertEquals(0, otherVertexProperty.keys().size()); else { // both support it, so assert in full assertEquals(originalVertexProperty.keys().size(), otherVertexProperty.keys().size()); for (final String key : originalVertexProperty.keys()) { validatePropertyEquality(originalVertexProperty.property(key), otherVertexProperty.property(key)); } } } }
v.property("name", "John"); VertexProperty p = v.property("value", 11); p.property("weight", 22); v.property("value", 33.3, "weight", 66.6); v.property("value", 11, "weight", 22); //same values are supported for list-properties
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; }
public static VertexProperty createVertexProperty(final Attachable<VertexProperty> attachableVertexProperty, final Vertex hostVertex) { final VertexProperty<Object> baseVertexProperty = attachableVertexProperty.get(); final VertexProperty vertexProperty = hostVertex.graph().features().vertex().properties().willAllowId(baseVertexProperty.id()) ? hostVertex.property(hostVertex.graph().features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value(), T.id, baseVertexProperty.id()) : hostVertex.property(hostVertex.graph().features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value()); baseVertexProperty.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value())); return vertexProperty; }