All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.unipop.schema.element.VertexSchema Maven / Gradle / Ivy

There is a newer version: 0.2.1
Show newest version
package org.unipop.schema.element;

import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.unipop.query.predicates.PredicatesHolder;
import org.unipop.query.predicates.PredicatesHolderFactory;
import org.unipop.structure.UniVertex;

import java.util.*;
import java.util.stream.Collectors;

public interface VertexSchema extends ElementSchema {

    @Override
    default Collection fromFields(Map fields) {
        Vertex element = createElement(fields);
        if(element == null) return null;
        return Collections.singleton(element);
    }

    Vertex createElement(Map fields);

    default PredicatesHolder toPredicates(List vertices) {
        if(vertices == null || vertices.size() == 0) return PredicatesHolderFactory.abort();
        HashSet predicates = new HashSet<>();
        vertices.stream().collect(Collectors.groupingBy(Vertex::label)).forEach((label, labelVertices) -> {
            HasContainer labelPredicate = new HasContainer(T.label.getAccessor(), P.eq(label));
            HasContainer ids = new HasContainer(T.id.getAccessor(),
                    P.within(labelVertices.stream().map(Vertex::id).collect(Collectors.toSet())));
            PredicatesHolder labelPredicates = PredicatesHolderFactory.and(ids, labelPredicate);
            PredicatesHolder toPredicates = toPredicates(labelPredicates);
            predicates.add(toPredicates);
        });
        return PredicatesHolderFactory.or(predicates);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy