use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer 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.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer 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.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project unipop by unipop-graph.
the class UniGraph method createIdPredicate.
public static <E extends Element> PredicatesHolder createIdPredicate(Object[] ids, Class<E> returnType) {
ElementHelper.validateMixedElementIds(returnType, ids);
// if (ids.length > 0 && Vertex.class.isAssignableFrom(ids[0].getClass())) return new ArrayIterator<>(ids);
if (ids.length > 0) {
List<Object> collect = Stream.of(ids).map(id -> {
if (id instanceof Element)
return ((Element) id).id();
return id;
}).collect(Collectors.toList());
HasContainer idPredicate = new HasContainer(T.id.getAccessor(), P.within(collect));
return PredicatesHolderFactory.predicate(idPredicate);
}
return PredicatesHolderFactory.empty();
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project unipop by unipop-graph.
the class UniVertex method edges.
@Override
public Iterator<Edge> edges(Direction direction, String... edgeLabels) {
PredicatesHolder predicatesHolder = (edgeLabels.length == 0) ? PredicatesHolderFactory.empty() : PredicatesHolderFactory.predicate(new HasContainer(T.label.getAccessor(), P.within(edgeLabels)));
SearchVertexQuery searchVertexQuery = new SearchVertexQuery(Edge.class, Arrays.asList(this), direction, predicatesHolder, -1, null, null, null, null);
return graph.getControllerManager().getControllers(SearchVertexQuery.SearchVertexController.class).stream().<Iterator<Edge>>map(controller -> controller.search(searchVertexQuery)).flatMap(ConversionUtils::asStream).iterator();
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project unipop by unipop-graph.
the class VirtualController method search.
@Override
public <E extends Element> Iterator<E> search(SearchQuery<E> uniQuery) {
if (uniQuery.getReturnType() != Vertex.class)
return EmptyIterator.instance();
PredicatesHolder predicates = uniQuery.getPredicates();
List<? extends VirtualVertexSchema> filteredSchemas = vertexSchemas.stream().filter(schema -> !schema.toPredicates(predicates).getClause().equals(PredicatesHolder.Clause.Abort)).collect(Collectors.toList());
Optional<HasContainer> ids = predicates.getPredicates().stream().filter(has -> has.getKey().equals(T.id.getAccessor())).findFirst();
Optional<HasContainer> labels = predicates.getPredicates().stream().filter(has -> has.getKey().equals(T.label.getAccessor())).findFirst();
if (!ids.isPresent() || !labels.isPresent()) {
return EmptyIterator.instance();
}
ArrayList<Map<String, Object>> elements = new ArrayList<>();
Object idObject = ids.get().getValue();
Collection<Object> idsCol = idObject instanceof Collection ? ((Collection) idObject) : Collections.singleton(idObject);
Object labelObject = labels.get().getValue();
Collection<Object> labelCol = labelObject instanceof Collection ? ((Collection) labelObject) : Collections.singleton(labelObject);
idsCol.forEach(id -> labelCol.forEach(label -> elements.add(createElement(id, label.toString()))));
return (Iterator<E>) elements.stream().flatMap(fields -> filteredSchemas.stream().flatMap(schema -> Stream.of(schema.createElement(fields)))).filter(v -> v != null).iterator();
}
Aggregations