Search in sources :

Example 11 with PredicatesHolder

use of org.unipop.query.predicates.PredicatesHolder in project unipop by unipop-graph.

the class ConcatenateFieldPropertySchema method toPredicate.

@Override
public PredicatesHolder toPredicate(HasContainer has) {
    Object value = has.getValue();
    Set<PredicatesHolder> predicates = new HashSet<>();
    if (value instanceof String) {
        String valueString = value.toString();
        predicates.add(stringValueToPredicate(valueString, has, false));
    } else if (value instanceof Collection) {
        Collection collection = (Collection) value;
        collection.forEach(v -> predicates.add(stringValueToPredicate(v.toString(), has, true)));
        Map<String, List<HasContainer>> collect = predicates.stream().flatMap(p -> p.getPredicates().stream()).collect(Collectors.groupingBy(p -> p.getKey()));
        if (collect.size() == 0)
            return PredicatesHolderFactory.abort();
        predicates.clear();
        collect.forEach((key, hasContainers) -> {
            List<Object> values = hasContainers.stream().map(HasContainer::getValue).map(l -> ((Collection) l).iterator().next()).collect(Collectors.toList());
            predicates.add(PredicatesHolderFactory.predicate(new HasContainer(key, new P(has.getBiPredicate(), values))));
        });
    }
    return PredicatesHolderFactory.and(predicates);
}
Also used : PredicatesHolderFactory(org.unipop.query.predicates.PredicatesHolderFactory) PropertySchemaFactory(org.unipop.util.PropertySchemaFactory) java.util(java.util) JSONObject(org.json.JSONObject) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) PropertyType(org.unipop.schema.property.type.PropertyType) Collectors(java.util.stream.Collectors) P(org.apache.tinkerpop.gremlin.process.traversal.P) JSONArray(org.json.JSONArray) P(org.apache.tinkerpop.gremlin.process.traversal.P) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) JSONObject(org.json.JSONObject)

Example 12 with PredicatesHolder

use of org.unipop.query.predicates.PredicatesHolder in project unipop by unipop-graph.

the class PredicatesUtil method collectPredicates.

public static void collectPredicates(ReceivesPredicatesHolder step, Traversal.Admin traversal) {
    Step nextStep = step.getNextStep();
    Set<PredicatesHolder> predicates = new HashSet<>();
    while (true) {
        if (nextStep instanceof HasContainerHolder) {
            HasContainerHolder hasContainerHolder = (HasContainerHolder) nextStep;
            hasContainerHolder.getHasContainers().stream().map(PredicatesHolderFactory::predicate).forEach(predicates::add);
            traversal.removeStep(nextStep);
            if (collectLabels(nextStep, step))
                break;
        } else // }
        if (nextStep instanceof RangeGlobalStep) {
            RangeGlobalStep rangeGlobalStep = (RangeGlobalStep) nextStep;
            int limit = rangeGlobalStep.getHighRange() > Integer.MAX_VALUE ? -1 : (int) rangeGlobalStep.getHighRange();
            step.setLimit(limit);
            collectLabels(nextStep, step);
            break;
        } else {
            break;
        }
        nextStep = nextStep.getNextStep();
    }
    PredicatesHolder predicate = PredicatesHolderFactory.and(predicates);
    step.addPredicate(predicate);
}
Also used : PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) HasContainerHolder(org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) HashSet(java.util.HashSet)

Example 13 with PredicatesHolder

use of org.unipop.query.predicates.PredicatesHolder in project unipop by unipop-graph.

the class PredicatesTranslator method translate.

public static Map<String, Object> translate(PredicatesHolder predicatesHolder, JSONObject opTranslator, MatcherHolder complexTranslator, boolean valuesToString) {
    List<HasContainer> predicates = predicatesHolder.getPredicates();
    Map<HasContainer, String> complexOps = predicates.stream().map(hasContainer -> {
        String match = complexTranslator.match(hasContainer);
        if (match != null)
            return Pair.of(hasContainer, match);
        return null;
    }).filter(t -> t != null).collect(Collectors.toMap(Pair::getKey, Pair::getValue));
    List<Map<String, Object>> predicatesMaps = predicates.stream().filter(has -> !complexOps.containsKey(has)).map(has -> {
        // TODO: maybe implement a simple matcher for op translator
        HashMap<String, Object> map = new HashMap<>();
        String[] split = has.getKey().split("\\.");
        map.put("key", split[split.length - 1]);
        Object value = has.getValue();
        if (value instanceof String)
            map.put("value", "\"" + value + "\"");
        else if (value instanceof Collection) {
            String finalString = "[%s]";
            List<String> values = ((Collection<Object>) value).stream().map(v -> "\"" + v.toString() + "\"").collect(Collectors.toList());
            String valuesString = "";
            String join = valuesString.join(",", values);
            map.put("value", String.format(finalString, join));
        } else {
            if (!valuesToString)
                map.put("value", value);
            else
                map.put("value", "\"" + value + "\"");
        }
        if (!opTranslator.has(has.getBiPredicate().toString()))
            return null;
        map.put("op", opTranslator.getString(has.getBiPredicate().toString()));
        return map;
    }).filter(m -> m != null).collect(Collectors.toList());
    List<Map<String, Object>> children = predicatesHolder.getChildren().stream().map(p -> translate(p, opTranslator, complexTranslator, valuesToString)).collect(Collectors.toList());
    Map<String, Object> map = new HashMap<>();
    Map<String, Object> predicateChildrenMap = new HashMap<>();
    if (predicatesMaps.size() > 0)
        predicateChildrenMap.put("predicates", predicatesMaps);
    if (complexOps.values().size() > 0)
        predicateChildrenMap.put("complex", complexOps.values());
    if (children.size() > 0)
        predicateChildrenMap.put("children", children);
    if (predicateChildrenMap.size() > 0)
        map.put(predicatesHolder.getClause().toString().toLowerCase(), predicateChildrenMap);
    Map<String, Object> result = new HashMap<>();
    if (map.size() > 0)
        result.put("predicates", map);
    return result;
}
Also used : List(java.util.List) JSONObject(org.json.JSONObject) Pair(org.apache.commons.lang3.tuple.Pair) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) Collectors(java.util.stream.Collectors) HashMap(java.util.HashMap) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) Collection(java.util.Collection) JSONObject(org.json.JSONObject) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with PredicatesHolder

use of org.unipop.query.predicates.PredicatesHolder in project unipop by unipop-graph.

the class RestEdge method getVertexPredicates.

protected PredicatesHolder getVertexPredicates(List<Vertex> vertices, Direction direction) {
    PredicatesHolder outPredicates = this.outVertexSchema.toPredicates(vertices);
    PredicatesHolder inPredicates = this.inVertexSchema.toPredicates(vertices);
    if (direction.equals(Direction.OUT) && outPredicates.notAborted())
        return outPredicates;
    if (direction.equals(Direction.IN) && inPredicates.notAborted())
        return inPredicates;
    if (outPredicates.notAborted() && inPredicates.notAborted())
        return PredicatesHolderFactory.or(inPredicates, outPredicates);
    else if (outPredicates.isAborted())
        return inPredicates;
    else if (inPredicates.isAborted())
        return outPredicates;
    else
        return PredicatesHolderFactory.abort();
}
Also used : PredicatesHolder(org.unipop.query.predicates.PredicatesHolder)

Example 15 with PredicatesHolder

use of org.unipop.query.predicates.PredicatesHolder in project unipop by unipop-graph.

the class RestVertex method getSearch.

@Override
public BaseRequest getSearch(DeferredVertexQuery query) {
    int limit = query.getOrders() == null || query.getOrders().size() > 0 ? -1 : query.getLimit();
    PredicatesHolder predicatesHolder = toPredicates(query.getVertices());
    return createSearch(predicatesHolder, limit);
}
Also used : PredicatesHolder(org.unipop.query.predicates.PredicatesHolder)

Aggregations

PredicatesHolder (org.unipop.query.predicates.PredicatesHolder)23 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)9 Collectors (java.util.stream.Collectors)7 java.util (java.util)6 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)6 Stream (java.util.stream.Stream)5 P (org.apache.tinkerpop.gremlin.process.traversal.P)5 JSONObject (org.json.JSONObject)5 PredicatesHolderFactory (org.unipop.query.predicates.PredicatesHolderFactory)4 SearchQuery (org.unipop.query.search.SearchQuery)4 AddVertexQuery (org.unipop.query.mutation.AddVertexQuery)3 ConversionUtils (org.unipop.util.ConversionUtils)3 Configuration (org.apache.commons.configuration.Configuration)2 StringUtils (org.apache.commons.lang3.StringUtils)2 GraphComputer (org.apache.tinkerpop.gremlin.process.computer.GraphComputer)2 TraversalStrategies (org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies)2 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)2 org.apache.tinkerpop.gremlin.structure (org.apache.tinkerpop.gremlin.structure)2 Element (org.apache.tinkerpop.gremlin.structure.Element)2 JSONArray (org.json.JSONArray)2