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);
}
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);
}
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;
}
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();
}
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);
}
Aggregations