/** * Constructs a condition that is equivalent to the type constraints of this query if there are any. * * @param types * @return */ private static Condition<TitanRelation> getTypeCondition(Set<RelationType> types) { assert !types.isEmpty(); if (types.size() == 1) return new RelationTypeCondition<TitanRelation>(types.iterator().next()); Or<TitanRelation> typeCond = new Or<TitanRelation>(types.size()); for (RelationType type : types) typeCond.add(new RelationTypeCondition<TitanRelation>(type)); return typeCond; }
public static Map.Entry<RelationType,Collection> extractOrCondition(Or<TitanRelation> condition) { RelationType masterType = null; List<Object> values = new ArrayList<Object>(); for (Condition c : condition.getChildren()) { if (!(c instanceof PredicateCondition)) return null; PredicateCondition<RelationType, TitanRelation> atom = (PredicateCondition)c; if (atom.getPredicate()!=Cmp.EQUAL) return null; Object value = atom.getValue(); if (value==null) return null; RelationType type = atom.getKey(); if (masterType==null) masterType=type; else if (!masterType.equals(type)) return null; values.add(value); } if (masterType==null) return null; assert !values.isEmpty(); return new AbstractMap.SimpleImmutableEntry(masterType,values); }
@Override public boolean evaluate(E element) { if (!hasChildren()) return true; for (Condition<E> condition : this) { if (condition.evaluate(element)) return true; } return false; }
@Override public Or<E> clone() { return new Or<E>(this); }
assertEquals("doc3", result.get(0)); result = tx.query(new IndexQuery(store, And.of(Or.of(PredicateCondition.of(TIME, Cmp.EQUAL, 1001),PredicateCondition.of(TIME, Cmp.EQUAL, -500))))); assertEquals(2, result.size()); assertEquals("doc3", result.get(0)); result = tx.query(new IndexQuery(store, And.of(Or.of(PredicateCondition.of(TIME, Cmp.EQUAL, 1001),PredicateCondition.of(TIME, Cmp.EQUAL, -500)), PredicateCondition.of(TEXT, Text.CONTAINS, "world")))); assertEquals(1, result.size()); assertEquals("doc1", result.get(0));
public static <E extends TitanElement> Or<E> of(Condition<E>... elements) { return new Or<E>(elements); }
assertEquals("doc3", result.get(0)); result = tx.query(new IndexQuery(store, And.of(Or.of(PredicateCondition.of(TIME, Cmp.EQUAL, 1001),PredicateCondition.of(TIME, Cmp.EQUAL, -500))))); assertEquals(2, result.size()); assertEquals("doc3", result.get(0)); result = tx.query(new IndexQuery(store, And.of(Or.of(PredicateCondition.of(TIME, Cmp.EQUAL, 1001),PredicateCondition.of(TIME, Cmp.EQUAL, -500)), PredicateCondition.of(TEXT, Text.CONTAINS, "world")))); assertEquals(1, result.size()); assertEquals("doc1", result.get(0));
public static final<E extends TitanElement> Condition<E> transformation(Condition<E> condition, Function<Condition<E>,Condition<E>> transformation) { Condition<E> transformed = transformation.apply(condition); if (transformed!=null) return transformed; //if transformed==null we go a level deeper if (condition.getType()== Condition.Type.LITERAL) { return condition; } else if (condition instanceof Not) { return Not.of(transformation(((Not) condition).getChild(), transformation)); } else if (condition instanceof And) { And<E> newand = new And<E>(condition.numChildren()); for (Condition<E> child : condition.getChildren()) newand.add(transformation(child, transformation)); return newand; } else if (condition instanceof Or) { Or<E> newor = new Or<E>(condition.numChildren()); for (Condition<E> child : condition.getChildren()) newor.add(transformation(child, transformation)); return newor; } else throw new IllegalArgumentException("Unexpected condition type: " + condition); }
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);
/** * Query-normal-form (QNF) for Titan is a variant of CNF (conjunctive normal form) with negation inlined where possible * * @param condition * @return */ public static boolean isQueryNormalForm(Condition<?> condition) { if (isQNFLiteralOrNot(condition)) return true; else if (condition instanceof And) { for (Condition<?> child : ((And<?>) condition).getChildren()) { if (isQNFLiteralOrNot(child)) continue; else if (child instanceof Or) { for (Condition<?> child2 : ((Or<?>) child).getChildren()) { if (!isQNFLiteralOrNot(child2)) return false; } } else return false; } return true; } else return false; }
@Override public boolean evaluate(E element) { if (!hasChildren()) return true; for (Condition<E> condition : this) { if (condition.evaluate(element)) return true; } return false; }
/** * Constructs a condition that is equivalent to the type constraints of this query if there are any. * * @param types * @return */ private static Condition<TitanRelation> getTypeCondition(Set<RelationType> types) { assert !types.isEmpty(); if (types.size() == 1) return new RelationTypeCondition<TitanRelation>(types.iterator().next()); Or<TitanRelation> typeCond = new Or<TitanRelation>(types.size()); for (RelationType type : types) typeCond.add(new RelationTypeCondition<TitanRelation>(type)); return typeCond; }
@Override public Or<E> clone() { return new Or<E>(this); }
public static Map.Entry<RelationType,Collection> extractOrCondition(Or<TitanRelation> condition) { RelationType masterType = null; List<Object> values = new ArrayList<Object>(); for (Condition c : condition.getChildren()) { if (!(c instanceof PredicateCondition)) return null; PredicateCondition<RelationType, TitanRelation> atom = (PredicateCondition)c; if (atom.getPredicate()!=Cmp.EQUAL) return null; Object value = atom.getValue(); if (value==null) return null; RelationType type = atom.getKey(); if (masterType==null) masterType=type; else if (!masterType.equals(type)) return null; values.add(value); } if (masterType==null) return null; assert !values.isEmpty(); return new AbstractMap.SimpleImmutableEntry(masterType,values); }
@Override public boolean evaluate(E element) { if (!hasChildren()) return true; for (Condition<E> condition : this) { if (condition.evaluate(element)) return true; } return false; }
/** * Constructs a condition that is equivalent to the type constraints of this query if there are any. * * @param types * @return */ private static Condition<TitanRelation> getTypeCondition(Set<RelationType> types) { assert !types.isEmpty(); if (types.size() == 1) return new RelationTypeCondition<TitanRelation>(types.iterator().next()); Or<TitanRelation> typeCond = new Or<TitanRelation>(types.size()); for (RelationType type : types) typeCond.add(new RelationTypeCondition<TitanRelation>(type)); return typeCond; }
@Override public Or<E> clone() { return new Or<E>(this); }
public static Map.Entry<RelationType,Collection> extractOrCondition(Or<TitanRelation> condition) { RelationType masterType = null; List<Object> values = new ArrayList<Object>(); for (Condition c : condition.getChildren()) { if (!(c instanceof PredicateCondition)) return null; PredicateCondition<RelationType, TitanRelation> atom = (PredicateCondition)c; if (atom.getPredicate()!=Cmp.EQUAL) return null; Object value = atom.getValue(); if (value==null) return null; RelationType type = atom.getKey(); if (masterType==null) masterType=type; else if (!masterType.equals(type)) return null; values.add(value); } if (masterType==null) return null; assert !values.isEmpty(); return new AbstractMap.SimpleImmutableEntry(masterType,values); }
public static final<E extends TitanElement> Condition<E> transformation(Condition<E> condition, Function<Condition<E>,Condition<E>> transformation) { Condition<E> transformed = transformation.apply(condition); if (transformed!=null) return transformed; //if transformed==null we go a level deeper if (condition.getType()== Condition.Type.LITERAL) { return condition; } else if (condition instanceof Not) { return Not.of(transformation(((Not) condition).getChild(), transformation)); } else if (condition instanceof And) { And<E> newand = new And<E>(condition.numChildren()); for (Condition<E> child : condition.getChildren()) newand.add(transformation(child, transformation)); return newand; } else if (condition instanceof Or) { Or<E> newor = new Or<E>(condition.numChildren()); for (Condition<E> child : condition.getChildren()) newor.add(transformation(child, transformation)); return newor; } else throw new IllegalArgumentException("Unexpected condition type: " + condition); }
public static <E extends TitanElement> Or<E> of(Condition<E>... elements) { return new Or<E>(elements); }