Search in sources :

Example 1 with Contain

use of com.thinkaurelius.titan.core.attribute.Contain in project titan by thinkaurelius.

the class QueryUtil method constraints2QNF.

/**
     * Prepares the constraints from the query builder into a QNF compliant condition.
     * If the condition is invalid or trivially false, it returns null.
     *
     * @param tx
     * @param constraints
     * @param <E>
     * @return
     * @see #isQueryNormalForm(com.thinkaurelius.titan.graphdb.query.condition.Condition)
     */
public static <E extends TitanElement> And<E> constraints2QNF(StandardTitanTx tx, List<PredicateCondition<String, E>> constraints) {
    And<E> conditions = new And<E>(constraints.size() + 4);
    for (PredicateCondition<String, E> atom : constraints) {
        RelationType type = getType(tx, atom.getKey());
        if (type == null) {
            if (atom.getPredicate() == Cmp.EQUAL && atom.getValue() == null || (atom.getPredicate() == Cmp.NOT_EQUAL && atom.getValue() != null))
                //Ignore condition, its trivially satisfied
                continue;
            return null;
        }
        Object value = atom.getValue();
        TitanPredicate predicate = atom.getPredicate();
        if (type.isPropertyKey()) {
            PropertyKey key = (PropertyKey) type;
            assert predicate.isValidCondition(value);
            Preconditions.checkArgument(key.dataType() == Object.class || predicate.isValidValueType(key.dataType()), "Data type of key is not compatible with condition");
        } else {
            //its a label
            Preconditions.checkArgument(((EdgeLabel) type).isUnidirected());
            Preconditions.checkArgument(predicate.isValidValueType(TitanVertex.class), "Data type of key is not compatible with condition");
        }
        if (predicate instanceof Contain) {
            //Rewrite contains conditions
            Collection values = (Collection) value;
            if (predicate == Contain.NOT_IN) {
                //Simply ignore since trivially satisfied
                if (values.isEmpty())
                    continue;
                for (Object invalue : values) addConstraint(type, Cmp.NOT_EQUAL, invalue, conditions, tx);
            } else {
                Preconditions.checkArgument(predicate == Contain.IN);
                if (values.isEmpty()) {
                    //Cannot be satisfied
                    return null;
                }
                if (values.size() == 1) {
                    addConstraint(type, Cmp.EQUAL, values.iterator().next(), conditions, tx);
                } else {
                    Or<E> nested = new Or<E>(values.size());
                    for (Object invalue : values) addConstraint(type, Cmp.EQUAL, invalue, nested, tx);
                    conditions.add(nested);
                }
            }
        } else {
            addConstraint(type, predicate, value, conditions, tx);
        }
    }
    return conditions;
}
Also used : InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) Contain(com.thinkaurelius.titan.core.attribute.Contain)

Aggregations

Contain (com.thinkaurelius.titan.core.attribute.Contain)1 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)1