Search in sources :

Example 31 with HasContainer

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);
}
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 32 with HasContainer

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;
}
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 33 with HasContainer

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();
}
Also used : Configuration(org.apache.commons.configuration.Configuration) java.util(java.util) TraversalStrategies(org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies) GraphComputer(org.apache.tinkerpop.gremlin.process.computer.GraphComputer) PropertyTypeFactory(org.unipop.util.PropertyTypeFactory) ElementHelper(org.apache.tinkerpop.gremlin.structure.util.ElementHelper) StandardStrategyProvider(org.unipop.process.strategyregistrar.StandardStrategyProvider) StringUtils(org.apache.commons.lang3.StringUtils) PredicatesHolderFactory(org.unipop.query.predicates.PredicatesHolderFactory) org.apache.tinkerpop.gremlin.structure(org.apache.tinkerpop.gremlin.structure) TraversalFilter(org.unipop.structure.traversalfilter.TraversalFilter) UnipopGraphProvider(org.unipop.test.UnipopGraphProvider) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) ConversionUtils(org.unipop.util.ConversionUtils) P(org.apache.tinkerpop.gremlin.process.traversal.P) PropertySchema(org.unipop.schema.property.PropertySchema) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AddVertexQuery(org.unipop.query.mutation.AddVertexQuery) Collectors(java.util.stream.Collectors) ConfigurationControllerManager(org.unipop.query.controller.ConfigurationControllerManager) StrategyProvider(org.unipop.process.strategyregistrar.StrategyProvider) DefaultTraversalFilter(org.unipop.structure.traversalfilter.DefaultTraversalFilter) Stream(java.util.stream.Stream) ControllerManager(org.unipop.query.controller.ControllerManager) StringFactory(org.apache.tinkerpop.gremlin.structure.util.StringFactory) SearchQuery(org.unipop.query.search.SearchQuery) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) org.unipop.schema.property.type(org.unipop.schema.property.type) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)

Example 34 with HasContainer

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();
}
Also used : PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) SearchVertexQuery(org.unipop.query.search.SearchVertexQuery)

Example 35 with HasContainer

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();
}
Also used : java.util(java.util) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AddVertexQuery(org.unipop.query.mutation.AddVertexQuery) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SimpleController(org.unipop.query.controller.SimpleController) T(org.apache.tinkerpop.gremlin.structure.T) Maps(com.google.common.collect.Maps) EmptyIterator(org.apache.tinkerpop.gremlin.util.iterator.EmptyIterator) Collectors(java.util.stream.Collectors) AddEdgeQuery(org.unipop.query.mutation.AddEdgeQuery) Element(org.apache.tinkerpop.gremlin.structure.Element) Stream(java.util.stream.Stream) RemoveQuery(org.unipop.query.mutation.RemoveQuery) NotImplementedException(org.apache.commons.lang.NotImplementedException) ElementSchema(org.unipop.schema.element.ElementSchema) UniGraph(org.unipop.structure.UniGraph) PropertyQuery(org.unipop.query.mutation.PropertyQuery) UniEdge(org.unipop.structure.UniEdge) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) DeferredVertexQuery(org.unipop.query.search.DeferredVertexQuery) SearchQuery(org.unipop.query.search.SearchQuery) Edge(org.apache.tinkerpop.gremlin.structure.Edge) SearchVertexQuery(org.unipop.query.search.SearchVertexQuery) UniVertex(org.unipop.structure.UniVertex) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) EmptyIterator(org.apache.tinkerpop.gremlin.util.iterator.EmptyIterator)

Aggregations

HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)56 AndOrHasContainer (org.umlg.sqlg.sql.parse.AndOrHasContainer)13 P (org.apache.tinkerpop.gremlin.process.traversal.P)9 Collectors (java.util.stream.Collectors)7 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)6 AndP (org.apache.tinkerpop.gremlin.process.traversal.util.AndP)6 FullText (org.umlg.sqlg.predicate.FullText)6 PredicatesHolder (org.unipop.query.predicates.PredicatesHolder)6 java.util (java.util)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 BiPredicate (java.util.function.BiPredicate)5 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)5 JSONObject (org.json.JSONObject)5 GraphStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep)4 BasicVertexCentricQueryBuilder (org.janusgraph.graphdb.query.vertex.BasicVertexCentricQueryBuilder)4 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)3 HasContainerHolder (org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder)3 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)3 ConnectiveP (org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP)3