use of org.janusgraph.core.attribute.Contain in project janusgraph by JanusGraph.
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(org.janusgraph.graphdb.query.condition.Condition)
*/
public static <E extends JanusGraphElement> And<E> constraints2QNF(StandardJanusGraphTx tx, List<PredicateCondition<String, E>> constraints) {
final And<E> conditions = new And<>(constraints.size() + 4);
for (final PredicateCondition<String, E> atom : constraints) {
final RelationType type = getType(tx, atom.getKey());
if (type == null) {
if (atom.getPredicate() == Cmp.EQUAL && atom.getValue() == null)
// Ignore condition, its trivially satisfied
continue;
return null;
}
final Object value = atom.getValue();
final JanusGraphPredicate predicate = atom.getPredicate();
if (type.isPropertyKey()) {
final 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(JanusGraphVertex.class), "Data type of key is not compatible with condition");
}
if (predicate instanceof Contain) {
// Rewrite contains conditions
final Collection values = (Collection) value;
if (predicate == Contain.NOT_IN) {
// Simply ignore since trivially satisfied
if (values.isEmpty())
continue;
for (final 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 {
final Or<E> nested = new Or<>(values.size());
for (final Object invalue : values) addConstraint(type, Cmp.EQUAL, invalue, nested, tx);
conditions.add(nested);
}
}
} else if (predicate instanceof AndJanusPredicate) {
if (addConstraint(type, (AndJanusPredicate) (predicate), (List<Object>) (value), conditions, tx) == null) {
return null;
}
} else if (predicate instanceof OrJanusPredicate) {
final List<Object> values = (List<Object>) (value);
// FIXME: this might generate a non QNF-compliant form, e.g. nested = PredicateCondition OR (PredicateCondition AND PredicateCondition)
final Or<E> nested = addConstraint(type, (OrJanusPredicate) predicate, values, new Or<>(values.size()), tx);
if (nested == null) {
return null;
}
conditions.add(nested);
} else {
addConstraint(type, predicate, value, conditions, tx);
}
}
return conditions;
}
use of org.janusgraph.core.attribute.Contain in project janusgraph by JanusGraph.
the class QueryUtil method addConstraint.
private static <E extends JanusGraphElement> And<E> addConstraint(final RelationType type, AndJanusPredicate predicate, List<Object> values, And<E> and, StandardJanusGraphTx tx) {
for (int i = 0; i < values.size(); i++) {
final JanusGraphPredicate janusGraphPredicate = predicate.get(i);
if (janusGraphPredicate instanceof Contain) {
// Rewrite contains conditions
final Collection childValues = (Collection) values.get(i);
if (janusGraphPredicate == Contain.NOT_IN) {
// Simply ignore since trivially satisfied
if (childValues.isEmpty())
continue;
for (final Object inValue : childValues) addConstraint(type, Cmp.NOT_EQUAL, inValue, and, tx);
} else {
Preconditions.checkArgument(janusGraphPredicate == Contain.IN);
if (childValues.isEmpty()) {
// Cannot be satisfied
return null;
}
if (childValues.size() == 1) {
addConstraint(type, Cmp.EQUAL, childValues.iterator().next(), and, tx);
} else {
final Or<E> nested = new Or<>(childValues.size());
for (final Object inValue : childValues) addConstraint(type, Cmp.EQUAL, inValue, nested, tx);
and.add(nested);
}
}
} else if (janusGraphPredicate instanceof AndJanusPredicate) {
if (addConstraint(type, (AndJanusPredicate) (janusGraphPredicate), (List<Object>) (values.get(i)), and, tx) == null) {
return null;
}
} else if (predicate.get(i) instanceof OrJanusPredicate) {
final List<Object> childValues = (List<Object>) (values.get(i));
final Or<E> nested = addConstraint(type, (OrJanusPredicate) (janusGraphPredicate), childValues, new Or<>(childValues.size()), tx);
if (nested == null) {
return null;
}
and.add(nested);
} else {
addConstraint(type, janusGraphPredicate, values.get(i), and, tx);
}
}
return and;
}
use of org.janusgraph.core.attribute.Contain in project janusgraph by JanusGraph.
the class QueryUtil method addConstraint.
private static <E extends JanusGraphElement> Or<E> addConstraint(final RelationType type, OrJanusPredicate predicate, List<Object> values, Or<E> or, StandardJanusGraphTx tx) {
for (int i = 0; i < values.size(); i++) {
final JanusGraphPredicate janusGraphPredicate = predicate.get(i);
if (janusGraphPredicate instanceof Contain) {
// Rewrite contains conditions
final Collection childValues = (Collection) values.get(i);
if (janusGraphPredicate == Contain.NOT_IN) {
if (childValues.size() == 1) {
addConstraint(type, Cmp.NOT_EQUAL, childValues.iterator().next(), or, tx);
}
// Don't need to handle the case where childValues is empty, because it defaults to
// an or(and()) is added, which is a tautology
final And<E> nested = new And<>(childValues.size());
for (final Object inValue : childValues) {
addConstraint(type, Cmp.NOT_EQUAL, inValue, nested, tx);
}
or.add(nested);
} else {
Preconditions.checkArgument(janusGraphPredicate == Contain.IN);
if (childValues.isEmpty()) {
// Handle any unsatisfiable condition that occurs within an OR statement like it does not exist
continue;
}
for (final Object inValue : childValues) {
addConstraint(type, Cmp.EQUAL, inValue, or, tx);
}
}
} else if (janusGraphPredicate instanceof AndJanusPredicate) {
final List<Object> childValues = (List<Object>) (values.get(i));
final And<E> nested = addConstraint(type, (AndJanusPredicate) janusGraphPredicate, childValues, new And<>(childValues.size()), tx);
if (nested == null) {
return null;
}
or.add(nested);
} else if (janusGraphPredicate instanceof OrJanusPredicate) {
if (addConstraint(type, (OrJanusPredicate) janusGraphPredicate, (List<Object>) (values.get(i)), or, tx) == null) {
return null;
}
} else {
addConstraint(type, janusGraphPredicate, values.get(i), or, tx);
}
}
return or;
}
Aggregations