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

com.tinkerpop.gremlin.structure.strategy.SequenceGraphStrategy Maven / Gradle / Ivy

package com.tinkerpop.gremlin.structure.strategy;

import com.tinkerpop.gremlin.process.graph.GraphTraversal;
import com.tinkerpop.gremlin.structure.Edge;
import com.tinkerpop.gremlin.structure.Property;
import com.tinkerpop.gremlin.structure.Vertex;
import com.tinkerpop.gremlin.util.function.STriFunction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

/**
 * @author Stephen Mallette (http://stephen.genoprime.com)
 */
public class SequenceGraphStrategy implements GraphStrategy {
    private final List graphStrategySequence;

    public SequenceGraphStrategy(final GraphStrategy... strategies) {
        this.graphStrategySequence = new ArrayList<>(Arrays.asList(strategies));
    }

    @Override
    public GraphTraversal applyStrategyToTraversal(final GraphTraversal traversal) {
        final UnaryOperator composedStrategy = graphStrategySequence.stream().map(SequenceGraphStrategy::convertToUnaryOperator)
                .reduce(null, (acc, next) -> acc == null ? next : toUnaryOp(acc.compose(next)));
        return composedStrategy.apply(traversal);
    }

    public static UnaryOperator convertToUnaryOperator(final GraphStrategy s) {
        return s::applyStrategyToTraversal;
    }

    @Override
    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getAddVertexStrategy(ctx));
    }

    @Override
    public UnaryOperator> getAddEdgeStrategy(final Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getAddEdgeStrategy(ctx));
    }

    @Override
    public UnaryOperator> getRemoveElementStrategy(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getRemoveElementStrategy(ctx));
    }

    @Override
    public  UnaryOperator> getRemovePropertyStrategy(Strategy.Context> ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getRemovePropertyStrategy(ctx));
    }

    @Override
    public  UnaryOperator>> getElementGetProperty(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getElementGetProperty(ctx));
    }

    @Override
    public  UnaryOperator>> getElementProperty(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getElementProperty(ctx));
    }

    @Override
    public UnaryOperator> getGraphvStrategy(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getGraphvStrategy(ctx));
    }

    @Override
    public UnaryOperator> getGrapheStrategy(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getGrapheStrategy(ctx));
    }

    @Override
    public UnaryOperator> getElementId(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getElementId(ctx));
    }

    @Override
    public UnaryOperator> getElementLabel(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getElementLabel(ctx));
    }

    @Override
    public UnaryOperator> getElementPropertiesSetter(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getElementId(ctx));
    }

    @Override
    public UnaryOperator>> getElementPropertiesGetter(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getElementId(ctx));
    }

    @Override
    public UnaryOperator>> getElementHiddens(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getElementId(ctx));
    }

    @Override
    public UnaryOperator>> getElementKeys(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getElementKeys(ctx));
    }

    @Override
    public UnaryOperator>> getElementHiddenKeys(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getElementHiddenKeys(ctx));
    }

    @Override
    public UnaryOperator>> getElementValues(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getElementValues(ctx));
    }

    @Override
    public UnaryOperator>> getElementHiddenValues(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getElementHiddenValues(ctx));
    }

    @Override
    public  UnaryOperator> getElementValue(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getElementValue(ctx));
    }

    @Override
    public UnaryOperator>> getVariableKeysStrategy(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getVariableKeysStrategy(ctx));
    }

    @Override
    public  UnaryOperator>> getVariableGetStrategy(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getVariableGetStrategy(ctx));
    }

    @Override
    public UnaryOperator> getVariableRemoveStrategy(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getVariableRemoveStrategy(ctx));
    }

    @Override
    public UnaryOperator> getVariableSetStrategy(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getVariableSetStrategy(ctx));
    }

    @Override
    public UnaryOperator>> getVariableAsMapStrategy(Strategy.Context ctx) {
        return this.composeStrategyUnaryOperator(s -> s.getVariableAsMapStrategy(ctx));
    }

    @Override
    public String toString() {
        return String.join("->", graphStrategySequence.stream().map(Object::toString)
                .map(String::toLowerCase).collect(Collectors.toList()));
    }

    /**
     * Compute a new strategy function from the sequence of supplied {@link GraphStrategy} objects.
     *
     * @param f a {@link java.util.function.Function} that extracts a particular strategy implementation from a {@link GraphStrategy}
     * @return a newly constructed {@link java.util.function.UnaryOperator} that applies each extracted strategy implementation in
     * the order supplied
     */
    private UnaryOperator composeStrategyUnaryOperator(final Function f) {
        return this.graphStrategySequence.stream().map(f).reduce(null,
                (acc, next) -> acc == null ? next : toUnaryOp(acc.compose(next)));
    }

    /**
     * Converts a {@link java.util.function.Function} to a {@link java.util.function.UnaryOperator} since the call to
     * {@link java.util.function.UnaryOperator#andThen(java.util.function.Function)} doesn't return {@link java.util.function.UnaryOperator} and can't
     * be casted to one.
     *
     * @param f a {@link java.util.function.Function} that has the same argument and return type
     * @return a {@link java.util.function.UnaryOperator} of the supplied {@code f}
     */
    private static  UnaryOperator toUnaryOp(final Function f) {
        return new UnaryOperator() {
            @Override
            public T apply(T t) {
                return f.apply(t);
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy