Java Code Examples for org.apache.tinkerpop.gremlin.structure.VertexProperty#Cardinality

The following examples show how to use org.apache.tinkerpop.gremlin.structure.VertexProperty#Cardinality . 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: Neo4jVertex.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public <V> VertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues) {
    ElementHelper.validateProperty(key, value);
    if (ElementHelper.getIdValue(keyValues).isPresent())
        throw Vertex.Exceptions.userSuppliedIdsNotSupported();
    this.graph.tx().readWrite();

    if (cardinality != VertexProperty.Cardinality.single)
        throw VertexProperty.Exceptions.multiPropertiesNotSupported();

    if (null == value) {
        properties(key).forEachRemaining(VertexProperty::remove);
        return VertexProperty.empty();
    }

    if (keyValues.length > 0)
        throw VertexProperty.Exceptions.metaPropertiesNotSupported();
    try {
        this.baseElement.setProperty(key, value);
        return new Neo4jVertexProperty<>(this, key, value);
    } catch (final IllegalArgumentException iae) {
        throw Property.Exceptions.dataTypeOfPropertyValueNotSupported(value, iae);
    }
}
 
Example 2
Source File: Song.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
protected <V> VertexProperty<V> updateSpecificProperty(
  VertexProperty.Cardinality cardinality, String key, V value) {
    if (NAME.equals(key)) {
        this.name = (String) value;
    } else if (SONG_TYPE.equals(key)) {
        this.songType = (String) value;
    } else if (PERFORMANCES.equals(key)) {
        this.performances = ((Integer) value);
    } else if (TEST_PROP.equals(key)) {
        this.testProp = (int[]) value;
    } else {
        throw new RuntimeException("property with key=" + key + " not (yet) supported by " + this.getClass().getName());
    }
    return property(key);
}
 
Example 3
Source File: ElementHelper.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Assign key/value pairs as properties to a {@link Vertex}. If the value of {@link T#id} or {@link T#label} is
 * in the set of pairs, then they are ignored.
 *
 * @param vertex            the vertex to attach the properties to
 * @param cardinality       the cardinality of the key value pair settings
 * @param propertyKeyValues the key/value pairs to assign to the {@code element}
 * @throws ClassCastException       if the value of the key is not a {@link String}
 * @throws IllegalArgumentException if the value of {@code element} is null
 */
public static void attachProperties(final Vertex vertex, final VertexProperty.Cardinality cardinality,
                                    final Object... propertyKeyValues) {
    if (null == vertex)
        throw Graph.Exceptions.argumentCanNotBeNull("vertex");

    final boolean allowNullPropertyValues = vertex.graph().features().vertex().supportsNullPropertyValues();

    for (int i = 0; i < propertyKeyValues.length; i = i + 2) {
        if (!propertyKeyValues[i].equals(T.id) && !propertyKeyValues[i].equals(T.label))
            if (!allowNullPropertyValues && null == propertyKeyValues[i + 1])
                vertex.properties(((String) propertyKeyValues[i])).forEachRemaining(VertexProperty::remove);
            else
                vertex.property(cardinality, (String) propertyKeyValues[i], propertyKeyValues[i + 1]);
    }
}
 
Example 4
Source File: SerializerTestVertex.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
protected <V> VertexProperty<V> updateSpecificProperty(
  VertexProperty.Cardinality cardinality, String key, V value) {
    if (STRING_PROPERTY.equals(key)) {
        this.stringProperty = (String) value;
    } else if (STRING_LIST_PROPERTY.equals(key)) {
        if (value instanceof List) {
            this.stringListProperty = (List) value;
        } else {
            if (this.stringListProperty == null) this.stringListProperty = new ArrayList<>();
            this.stringListProperty.add((String) value);
        }
    } else if (INT_PROPERTY.equals(key)) {
        this.intProperty = (Integer) value;
    } else if (INT_LIST_PROPERTY.equals(key)) {
        if (value instanceof List) {
            this.intListProperty = (List) value;
        } else {
            if (this.intListProperty == null) this.intListProperty = new ArrayList<>();
            this.intListProperty.add((Integer) value);
        }
    } else {
        throw new RuntimeException("property with key=" + key + " not (yet) supported by " + this.getClass().getName());
    }
    return property(key);
}
 
Example 5
Source File: VertexDeserializer.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
private VertexProperty.Cardinality getPropertyKeyCardinality(String name) {
    RelationType rt = typeManager.getRelationType(name);
    if (null == rt || !rt.isPropertyKey()) {
        return VertexProperty.Cardinality.single;
    }
    PropertyKey pk = typeManager.getExistingPropertyKey(rt.longId());
    switch (pk.cardinality()) {
        case SINGLE:
            return VertexProperty.Cardinality.single;
        case LIST:
            return VertexProperty.Cardinality.list;
        case SET:
            return VertexProperty.Cardinality.set;
        default:
            throw new IllegalStateException("Unknown cardinality " + pk.cardinality());
    }
}
 
Example 6
Source File: AbstractVertex.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public <V> JanusGraphVertexProperty<V> property(VertexProperty.Cardinality cardinality, String key, V value, Object... keyValues) {
    //NOTE that cardinality is ignored as we are enforcing these cheks at Grakn level
    JanusGraphVertexProperty<V> p = tx().addProperty(it(), tx().getOrCreatePropertyKey(key, value), value);
    ElementHelper.attachProperties(p, keyValues);
    return p;
}
 
Example 7
Source File: TitanVertexDeserializer.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private VertexProperty.Cardinality getPropertyKeyCardinality(String name) {
    RelationType rt = typeManager.getRelationType(name);
    if (null == rt || !rt.isPropertyKey())
        return VertexProperty.Cardinality.single;
    PropertyKey pk = typeManager.getExistingPropertyKey(rt.longId());
    switch (pk.cardinality()) {
        case SINGLE: return VertexProperty.Cardinality.single;
        case LIST: return VertexProperty.Cardinality.list;
        case SET: return VertexProperty.Cardinality.set;
        default: throw new IllegalStateException("Unknown cardinality " + pk.cardinality());
    }
}
 
Example 8
Source File: BitsyVertex.java    From bitsy with Apache License 2.0 5 votes vote down vote up
@Override
public <V> VertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues) {
	if (cardinality != Cardinality.single) {
		// For some reason, TP3 tests fail with this exception
		//throw new BitsyException(BitsyErrorCodes.NO_MULTI_PROPERTY_SUPPORT, "Encountered cardinality: " + cardinality.toString());
	} else if (keyValues.length != 0) {
		throw new UnsupportedOperationException("Encountered key values: " + keyValues.toString(), new BitsyException(BitsyErrorCodes.NO_META_PROPERTY_SUPPORT));
	}

	return property(key, value);
}
 
Example 9
Source File: StarGraph.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public <V> VertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues) {
    throw GraphComputer.Exceptions.adjacentVertexPropertiesCanNotBeReadOrUpdated();
}
 
Example 10
Source File: ParameterizedGroovyTranslatorTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldHandleCardinality() {
    VertexProperty.Cardinality cardinality = VertexProperty.Cardinality.set;
    assertNonParameterizedTranslation(String.format("VertexProperty.Cardinality.%s", cardinality), cardinality);
}
 
Example 11
Source File: HugeVertex.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Watched(prefix = "vertex")
@Override
@SuppressWarnings("unchecked") // (VertexProperty<V>) prop
public <V> VertexProperty<V> property(
           VertexProperty.Cardinality cardinality,
           String key, V value, Object... objects) {
    if (objects.length != 0 && objects[0].equals(T.id)) {
        throw VertexProperty.Exceptions.userSuppliedIdsNotSupported();
    }
    // TODO: extra props: objects
    if (objects.length != 0) {
        throw VertexProperty.Exceptions.metaPropertiesNotSupported();
    }

    PropertyKey propertyKey = this.graph().propertyKey(key);
    /*
     * g.AddV("xxx").property("key1", val1).property("key2", val2)
     * g.AddV("xxx").property(single, "key1", val1)
     *              .property(list, "key2", val2)
     *
     * The cardinality single may be user supplied single, it may also be
     * that user doesn't supplied cardinality, when it is latter situation,
     * we shouldn't check it. Because of this reason, we are forced to
     * give up the check of user supplied cardinality single.
     * The cardinality not single must be user supplied, so should check it
     */
    if (cardinality != VertexProperty.Cardinality.single) {
        E.checkArgument(propertyKey.cardinality() ==
                        Cardinality.convert(cardinality),
                        "Invalid cardinalty '%s' for property key '%s', " +
                        "expect '%s'", cardinality, key,
                        propertyKey.cardinality().string());
    }

    // Check key in vertex label
    E.checkArgument(this.label.properties().contains(propertyKey.id()),
                    "Invalid property '%s' for vertex label '%s'",
                    key, this.label());
    // Primary-Keys can only be set once
    if (this.schemaLabel().primaryKeys().contains(propertyKey.id())) {
        E.checkArgument(!this.hasProperty(propertyKey.id()),
                        "Can't update primary key: '%s'", key);
    }
    return (VertexProperty<V>) this.addProperty(propertyKey, value, true);
}
 
Example 12
Source File: TinkerGraph.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public VertexProperty.Cardinality getCardinality(final String key) {
    return defaultVertexPropertyCardinality;
}
 
Example 13
Source File: TitanVertex.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
@Override
public <V> TitanVertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues);
 
Example 14
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
/**
 * Sets a {@link Property} value and related meta properties if supplied, if supported by the {@link Graph}
 * and if the {@link Element} is a {@link VertexProperty}.  This method is the long-hand version of
 * {@link #property(Object, Object, Object...)} with the difference that the {@link VertexProperty.Cardinality}
 * can be supplied.
 * <p/>
 * Generally speaking, this method will append an {@link AddPropertyStep} to the {@link Traversal} but when
 * possible, this method will attempt to fold key/value pairs into an {@link AddVertexStep}, {@link AddEdgeStep} or
 * {@link AddVertexStartStep}.  This potential optimization can only happen if cardinality is not supplied
 * and when meta-properties are not included.
 *
 * @param cardinality the specified cardinality of the property where {@code null} will allow the {@link Graph}
 *                    to use its default settings
 * @param key         the key for the property
 * @param value       the value for the property
 * @param keyValues   any meta properties to be assigned to this property
 * @return the traversal with the last step modified to add a property
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#addproperty-step" target="_blank">AddProperty Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> property(final VertexProperty.Cardinality cardinality, final Object key, final Object value, final Object... keyValues) {
    if (null == cardinality)
        this.asAdmin().getBytecode().addStep(Symbols.property, key, value, keyValues);
    else
        this.asAdmin().getBytecode().addStep(Symbols.property, cardinality, key, value, keyValues);

    // if it can be detected that this call to property() is related to an addV/E() then we can attempt to fold
    // the properties into that step to gain an optimization for those graphs that support such capabilities.
    Step endStep = this.asAdmin().getEndStep();

    // always try to fold the property() into the initial "AddElementStep" as the performance will be better
    // and as it so happens with T the value must be set by way of that approach otherwise you get an error.
    // it should be safe to execute this loop this way as we'll either hit an "AddElementStep" or an "EmptyStep".
    // if empty, it will just use the regular AddPropertyStep being tacked on to the end of the traversal as usual
    while (endStep instanceof AddPropertyStep) {
        endStep = endStep.getPreviousStep();
    }

    // edge properties can always be folded as there is no cardinality/metaproperties. for a vertex mutation,
    // it's possible to fold the property() into the Mutating step if there are no metaproperties (i.e. keyValues)
    // and if (1) the key is an instance of T OR OR (3) the key is a string and the cardinality is not specifiied.
    // Note that checking for single cardinality of the argument doesn't work well because once folded we lose
    // the cardinality argument associated to the key/value pair and then it relies on the graph. that
    // means that if you do:
    //
    // g.addV().property(single, 'k',1).property(single,'k',2)
    //
    // you could end up with whatever the cardinality is for the key which might seem "wrong" if you were explicit
    // about the specification of "single". it also isn't possible to check the Graph Features for cardinality
    // as folding seems to have different behavior based on different graphs - we clearly don't have that aspect
    // of things tested/enforced well.
    //
    // of additional note is the folding that occurs if the key is a Traversal. the key here is technically
    // unknown until traversal execution as the anonymous traversal result isn't evaluated during traversal
    // construction but during iteration. not folding to AddVertexStep creates different (breaking) traversal
    // semantics than we've had in previous versions so right/wrong could be argued, but since it's a breaking
    // change we'll just arbitrarily account for it to maintain the former behavior.
    if ((endStep instanceof AddEdgeStep || endStep instanceof AddEdgeStartStep) ||
            ((endStep instanceof AddVertexStep || endStep instanceof AddVertexStartStep) &&
              keyValues.length == 0 &&
              (key instanceof T || (key instanceof String && null == cardinality) || key instanceof Traversal))) {
        ((Mutating) endStep).configure(key, value);
    } else {
        final AddPropertyStep<Element> addPropertyStep = new AddPropertyStep<>(this.asAdmin(), cardinality, key, value);
        this.asAdmin().addStep(addPropertyStep);
        addPropertyStep.configure(keyValues);
    }
    return this;
}
 
Example 15
Source File: Neo4JGraphFeatures.java    From neo4j-gremlin-bolt with Apache License 2.0 4 votes vote down vote up
@Override
public VertexProperty.Cardinality getCardinality(final String key) {
    return VertexProperty.Cardinality.single;
}
 
Example 16
Source File: Neo4jGraph.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public VertexProperty.Cardinality getCardinality(final String key) {
    return VertexProperty.Cardinality.single;
}
 
Example 17
Source File: AddPropertyStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public AddPropertyStep(final Traversal.Admin traversal, final VertexProperty.Cardinality cardinality, final Object keyObject, final Object valueObject) {
    super(traversal);
    this.parameters.set(this, T.key, keyObject, T.value, valueObject);
    this.cardinality = cardinality;
}
 
Example 18
Source File: PreloadedVertex.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
@Override
public <V> TitanVertexProperty<V> property(VertexProperty.Cardinality cardinality, String key, V value) {
    throw new UnsupportedOperationException("Provided key is not supported: " + key);
}
 
Example 19
Source File: HugeFeatures.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Override
public VertexProperty.Cardinality getCardinality(final String key) {
    return VertexProperty.Cardinality.single;
}
 
Example 20
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the key and value of a {@link Property}. If the {@link Element} is a {@link VertexProperty} and the
 * {@link Graph} supports it, meta properties can be set.  Use of this method assumes that the
 * {@link VertexProperty.Cardinality} is defaulted to {@code null} which  means that the default cardinality for
 * the {@link Graph} will be used.
 * <p/>
 * This method is effectively calls {@link #property(VertexProperty.Cardinality, Object, Object, Object...)}
 * as {@code property(null, key, value, keyValues}.
 *
 * @param key       the key for the property
 * @param value     the value for the property
 * @param keyValues any meta properties to be assigned to this property
 * @return the traversal with the last step modified to add a property
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#addproperty-step" target="_blank">AddProperty Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> property(final Object key, final Object value, final Object... keyValues) {
    return key instanceof VertexProperty.Cardinality ?
            this.property((VertexProperty.Cardinality) key, value, null == keyValues ? null : keyValues[0],
                    keyValues != null && keyValues.length > 1 ?
                            Arrays.copyOfRange(keyValues, 1, keyValues.length) :
                            new Object[]{}) :
            this.property(null, key, value, keyValues);
}