use of org.janusgraph.graphdb.query.condition.Or 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;
}
Aggregations