static boolean validJanusGraphHas(HasContainer has) { if (has.getPredicate() instanceof ConnectiveP) { final List<? extends P<?>> predicates = ((ConnectiveP<?>) has.getPredicate()).getPredicates(); return predicates.stream().allMatch(p-> validJanusGraphHas(new HasContainer(has.getKey(), p))); } else { return JanusGraphPredicate.Converter.supports(has.getBiPredicate()); } }
@Override public AndP<V> clone() { final AndP<V> clone = (AndP<V>) super.clone(); clone.biPredicate = new AndBiPredicate(clone); return clone; }
@Override public P<V> negate() { super.negate(); return new AndP<>(this.predicates); }
public static List<Object> convert(final ConnectiveP<?> predicate, final ConnectiveJanusPredicate connectivePredicate) { final List<Object> toReturn = new ArrayList<>(); for (final P<?> p : predicate.getPredicates()){ if (p instanceof ConnectiveP) { final ConnectiveJanusPredicate subPredicate = instanceConnectiveJanusPredicate(p); toReturn.add(convert((ConnectiveP<?>)p, subPredicate)); connectivePredicate.add(subPredicate); } else { connectivePredicate.add(Converter.convert(p.getBiPredicate())); toReturn.add(p.getValue()); } } return toReturn; } }
@Override public OrP<V> clone() { final OrP<V> clone = (OrP<V>) super.clone(); clone.biPredicate = new OrBiPredicate(clone); return clone; }
@Override public P<V> negate() { super.negate(); return new OrP<>(this.predicates); }
@Override public void write(Kryo kryo, Output output, P p) { output.writeString( p instanceof ConnectiveP ? (p instanceof AndP ? "and" : "or") : p.getBiPredicate().toString()); if (p instanceof ConnectiveP || p.getValue() instanceof Collection) { output.writeByte((byte) 0); final Collection<?> coll = p instanceof ConnectiveP ? ((ConnectiveP<?>) p).getPredicates() : (Collection) p.getValue(); output.writeInt(coll.size()); coll.forEach(v -> kryo.writeClassAndObject(output, v)); } else { output.writeByte((byte) 1); kryo.writeClassAndObject(output, p.getValue()); } }
@Override public AndP<V> clone() { final AndP<V> clone = (AndP<V>) super.clone(); clone.biPredicate = new AndBiPredicate(clone); return clone; }
@Override public P<V> negate() { super.negate(); return new OrP<>(this.predicates); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private static void collectPredicates(List<P<Object>> results, List<P<?>> predicates) { for (P<?> p : predicates) { if (p instanceof ConnectiveP) { collectPredicates(results, ((ConnectiveP) p).getPredicates()); } else { results.add((P<Object>) p); } } }
@Override public OrP<V> clone() { final OrP<V> clone = (OrP<V>) super.clone(); clone.biPredicate = new OrBiPredicate(clone); return clone; }
@Override public P<V> negate() { super.negate(); return new AndP<>(this.predicates); }
private void configurePredicates(final P<Object> predicate) { if (predicate instanceof ConnectiveP) ((ConnectiveP<Object>) predicate).getPredicates().forEach(this::configurePredicates); else { final String selectKey = (String) (predicate.getValue() instanceof Collection ? ((Collection) predicate.getValue()).iterator().next() : predicate.getValue()); // hack for within("x")) this.selectKeys.add(selectKey); this.scopeKeys.add(selectKey); } }
protected StringBuilder convertPToString(final P p, final StringBuilder current) { if (p instanceof TextP) return convertTextPToString((TextP) p, current); if (p instanceof ConnectiveP) { final List<P<?>> list = ((ConnectiveP) p).getPredicates(); for (int i = 0; i < list.size(); i++) { convertPToString(list.get(i), current); if (i < list.size() - 1) current.append(p instanceof OrP ? ".or(" : ".and("); } current.append(")"); } else current.append("P.").append(p.getBiPredicate().toString()).append("(").append(convertToString(p.getValue())).append(")"); return current; }
private StringBuilder convertPToString(final P p, final StringBuilder current) { if (p instanceof TextP) return convertTextPToString((TextP) p, current); if (p instanceof ConnectiveP) { final List<P<?>> list = ((ConnectiveP) p).getPredicates(); for (int i = 0; i < list.size(); i++) { convertPToString(list.get(i), current); if (i < list.size() - 1) current.append(p instanceof OrP ? ".or_(" : ".and_("); } current.append(")"); } else current.append(convertStatic("P.")).append(p.getBiPredicate().toString()).append("(").append(convertToString(p.getValue())).append(")"); return current; }
private void setPredicateValues(final P<Object> predicate, final Traverser.Admin<S> traverser, final Iterator<String> selectKeysIterator) { if (predicate instanceof ConnectiveP) ((ConnectiveP<Object>) predicate).getPredicates().forEach(p -> this.setPredicateValues(p, traverser, selectKeysIterator)); else predicate.setValue(TraversalUtil.applyNullable((S) this.getScopeValue(Pop.last, selectKeysIterator.next(), traverser), this.traversalRing.next())); }
@Override protected ByteBuf writeValue(final T value, final ByteBufAllocator allocator, final GraphBinaryWriter context) throws SerializationException { // the predicate name is either a static method of P or an instance method when a type ConnectiveP final boolean isConnectedP = value instanceof ConnectiveP; final String predicateName = isConnectedP ? (value instanceof AndP ? "and" : "or") : value.getBiPredicate().toString(); final Object args = isConnectedP ? ((ConnectiveP<?>) value).getPredicates() : value.getValue(); final List<Object> argsAsList = args instanceof Collection ? new ArrayList<>((Collection) args) : Collections.singletonList(args); final int length = argsAsList.size(); final CompositeByteBuf result = allocator.compositeBuffer(2 + length); result.addComponent(true, context.writeValue(predicateName, allocator, false)); result.addComponent(true, context.writeValue(length, allocator, false)); for (Object o : argsAsList) { result.addComponent(true, context.write(o, allocator)); } return result; } }
@Override public <O extends OutputShim> void write(final KryoShim<?, O> kryo, final O output, final P p) { output.writeString(p instanceof ConnectiveP ? (p instanceof AndP ? "and" : "or") : p.getBiPredicate().toString()); if (p instanceof ConnectiveP || p.getValue() instanceof Collection) { output.writeByte((byte) 0); final Collection<?> coll = p instanceof ConnectiveP ? ((ConnectiveP<?>) p).getPredicates() : (Collection) p.getValue(); output.writeInt(coll.size()); coll.forEach(v -> kryo.writeClassAndObject(output, v)); } else { output.writeByte((byte) 1); kryo.writeClassAndObject(output, p.getValue()); } }
@Override public <O extends OutputShim> void write(final KryoShim<?, O> kryo, final O output, final P p) { output.writeString(p instanceof ConnectiveP ? (p instanceof AndP ? "and" : "or") : p.getBiPredicate().toString()); if (p instanceof ConnectiveP || p.getValue() instanceof Collection) { output.writeByte((byte) 0); final Collection<?> coll = p instanceof ConnectiveP ? ((ConnectiveP<?>) p).getPredicates() : (Collection) p.getValue(); output.writeInt(coll.size()); coll.forEach(v -> kryo.writeClassAndObject(output, v)); } else { output.writeByte((byte) 1); kryo.writeClassAndObject(output, p.getValue()); } }
@Override public void serialize(final P p, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeStartObject(); jsonGenerator.writeStringField(GraphSONTokens.PREDICATE, p instanceof ConnectiveP ? p instanceof AndP ? GraphSONTokens.AND : GraphSONTokens.OR : p.getBiPredicate().toString()); if (p instanceof ConnectiveP) { jsonGenerator.writeArrayFieldStart(GraphSONTokens.VALUE); for (final P<?> predicate : ((ConnectiveP<?>) p).getPredicates()) { jsonGenerator.writeObject(predicate); } jsonGenerator.writeEndArray(); } else jsonGenerator.writeObjectField(GraphSONTokens.VALUE, p.getValue()); jsonGenerator.writeEndObject(); }