You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2015/02/12 14:01:32 UTC
[06/77] [partial] incubator-tinkerpop git commit: moved com/tinkerpop
directories to org/apache/tinkerpop
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/strategy/SubgraphStrategy.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/strategy/SubgraphStrategy.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/strategy/SubgraphStrategy.java
new file mode 100644
index 0000000..73e6170
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/strategy/SubgraphStrategy.java
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.tinkerpop.gremlin.structure.strategy;
+
+import com.tinkerpop.gremlin.process.graph.traversal.GraphTraversal;
+import com.tinkerpop.gremlin.structure.Direction;
+import com.tinkerpop.gremlin.structure.Edge;
+import com.tinkerpop.gremlin.structure.Vertex;
+import com.tinkerpop.gremlin.structure.util.ElementHelper;
+import com.tinkerpop.gremlin.structure.util.StringFactory;
+import com.tinkerpop.gremlin.util.StreamFactory;
+import com.tinkerpop.gremlin.util.iterator.IteratorUtils;
+
+import java.util.Iterator;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.function.UnaryOperator;
+
+/**
+ * A {@link GraphStrategy} which creates a logical subgraph to selectively include vertices and edges of a
+ * {@link com.tinkerpop.gremlin.structure.Graph} according to provided criteria. A vertex is in the subgraph if
+ * it meets the specified {@link #vertexPredicate}. An edge is in the subgraph if it meets the specified
+ * {@link #edgePredicate} and its associated vertices meet the specified {@link #vertexPredicate}.
+ *
+ * @author Joshua Shinavier (http://fortytwo.net)
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public final class SubgraphStrategy implements GraphStrategy {
+
+ private final Predicate<Vertex> vertexPredicate;
+ private final Predicate<Edge> edgePredicate;
+ // TODO protected Predicate<VertexProperty> vertexPropertyPredicate;
+
+ private SubgraphStrategy(final Predicate<Vertex> vertexPredicate, final Predicate<Edge> edgePredicate) {
+ this.vertexPredicate = vertexPredicate;
+ this.edgePredicate = edgePredicate;
+ }
+
+ @Override
+ public UnaryOperator<BiFunction<Direction, String[], Iterator<Vertex>>> getVertexIteratorsVertexIteratorStrategy(final StrategyContext<StrategyVertex> ctx, final GraphStrategy composingStrategy) {
+ return (f) -> (direction, labels) -> StreamFactory
+ .stream(ctx.getCurrent().getBaseVertex().iterators().edgeIterator(direction, labels))
+ .filter(this::testEdge)
+ .map(edge -> otherVertex(direction, ctx.getCurrent(), edge))
+ .filter(this::testVertex).iterator();
+ // TODO: Note that we are not doing f.apply() like the other methods. Is this bad?
+ // by not calling f.apply() to get the iterator, we're possibly bypassing strategy methods that
+ // could have been sequenced
+ }
+
+ @Override
+ public UnaryOperator<BiFunction<Direction, String[], Iterator<Edge>>> getVertexIteratorsEdgeIteratorStrategy(final StrategyContext<StrategyVertex> ctx, final GraphStrategy composingStrategy) {
+ return (f) -> (direction, labels) -> IteratorUtils.filter(f.apply(direction, labels), this::testEdge);
+ }
+
+ @Override
+ public UnaryOperator<Function<Direction, Iterator<Vertex>>> getEdgeIteratorsVertexIteratorStrategy(final StrategyContext<StrategyEdge> ctx, final GraphStrategy composingStrategy) {
+ return (f) -> direction -> IteratorUtils.filter(f.apply(direction), this::testVertex);
+ }
+
+ @Override
+ public UnaryOperator<Function<Object[], GraphTraversal<Vertex, Vertex>>> getGraphVStrategy(final StrategyContext<StrategyGraph> ctx, final GraphStrategy composingStrategy) {
+ return (f) -> ids -> f.apply(ids).filter(t -> this.testVertex(t.get())); // TODO: we should make sure index hits go first.
+ }
+
+ @Override
+ public UnaryOperator<Function<Object[], GraphTraversal<Edge, Edge>>> getGraphEStrategy(final StrategyContext<StrategyGraph> ctx, final GraphStrategy composingStrategy) {
+ return (f) -> ids -> f.apply(ids).filter(t -> this.testEdge(t.get())); // TODO: we should make sure index hits go first.
+ }
+
+ // TODO: make this work for DSL -- we need Element predicate
+ /*public UnaryOperator<Supplier<GraphTraversal>> getGraphOfStrategy(final Strategy.Context<StrategyWrappedGraph> ctx) {
+ return (f) -> () -> f.get().filter(el);
+ }*/
+
+
+ private boolean testVertex(final Vertex vertex) {
+ return vertexPredicate.test(vertex);
+ }
+
+ private boolean testEdge(final Edge edge) {
+ // the edge must pass the edge predicate, and both of its incident vertices must also pass the vertex predicate
+ // inV() and/or outV() will be empty if they do not. it is sometimes the case that an edge is unwrapped
+ // in which case it may not be filtered. in such cases, the vertices on such edges should be tested.
+ return edgePredicate.test(edge)
+ && (edge instanceof StrategyWrapped ? edge.inV().hasNext() && edge.outV().hasNext()
+ : testVertex(edge.inV().next()) && testVertex(edge.outV().next()));
+ }
+
+ private static final Vertex otherVertex(final Direction direction, final Vertex start, final Edge edge) {
+ if (direction.equals(Direction.BOTH)) {
+ final Vertex inVertex = edge.iterators().vertexIterator(Direction.IN).next();
+ return ElementHelper.areEqual(start, inVertex) ?
+ edge.iterators().vertexIterator(Direction.OUT).next() :
+ inVertex;
+ } else {
+ return edge.iterators().vertexIterator(direction.opposite()).next();
+ }
+ }
+
+ @Override
+ public String toString() {
+ return StringFactory.graphStrategyString(this);
+ }
+
+ public static Builder build() {
+ return new Builder();
+ }
+
+ public static class Builder {
+
+ private Predicate<Vertex> vertexPredicate = v -> true;
+ private Predicate<Edge> edgePredicate = v -> true;
+
+ private Builder() {
+ }
+
+ public Builder vertexPredicate(final Predicate<Vertex> vertexPredicate) {
+ this.vertexPredicate = vertexPredicate;
+ return this;
+ }
+
+ public Builder edgePredicate(final Predicate<Edge> edgePredicate) {
+ this.edgePredicate = edgePredicate;
+ return this;
+ }
+
+ public SubgraphStrategy create() {
+ return new SubgraphStrategy(vertexPredicate, edgePredicate);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/strategy/process/graph/step/sideEffect/StrategyGraphStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/strategy/process/graph/step/sideEffect/StrategyGraphStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/strategy/process/graph/step/sideEffect/StrategyGraphStep.java
new file mode 100644
index 0000000..af247a9
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/strategy/process/graph/step/sideEffect/StrategyGraphStep.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.tinkerpop.gremlin.structure.strategy.process.graph.step.sideEffect;
+
+import com.tinkerpop.gremlin.process.Traversal;
+import com.tinkerpop.gremlin.process.graph.traversal.GraphTraversal;
+import com.tinkerpop.gremlin.process.graph.traversal.step.sideEffect.GraphStep;
+import com.tinkerpop.gremlin.structure.Element;
+import com.tinkerpop.gremlin.structure.Vertex;
+import com.tinkerpop.gremlin.structure.strategy.StrategyEdge;
+import com.tinkerpop.gremlin.structure.strategy.StrategyGraph;
+import com.tinkerpop.gremlin.structure.strategy.StrategyVertex;
+
+import java.util.Iterator;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class StrategyGraphStep<E extends Element> extends GraphStep<E> {
+
+ private final GraphTraversal<?, E> graphTraversal;
+
+ public StrategyGraphStep(final Traversal.Admin traversal, final StrategyGraph strategyGraph, final Class<E> returnClass, final GraphTraversal<?, E> graphTraversal) {
+ super(traversal, strategyGraph, returnClass);
+ this.graphTraversal = graphTraversal;
+ this.setIteratorSupplier(() -> (Iterator) (Vertex.class.isAssignableFrom(this.returnClass) ?
+ new StrategyVertex.StrategyVertexIterator((Iterator) this.graphTraversal, strategyGraph) :
+ new StrategyEdge.StrategyEdgeIterator((Iterator) this.graphTraversal, strategyGraph)));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/Comparators.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/Comparators.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/Comparators.java
new file mode 100644
index 0000000..cabe682
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/Comparators.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.tinkerpop.gremlin.structure.util;
+
+import com.tinkerpop.gremlin.structure.Edge;
+import com.tinkerpop.gremlin.structure.Element;
+import com.tinkerpop.gremlin.structure.Property;
+import com.tinkerpop.gremlin.structure.Vertex;
+
+import java.util.Comparator;
+import java.util.Map;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public final class Comparators {
+ public static final Comparator<Element> ELEMENT_COMPARATOR = Comparator.comparing(e -> e.id().toString(), String.CASE_INSENSITIVE_ORDER);
+ public static final Comparator<Vertex> VERTEX_COMPARATOR = Comparator.comparing(e -> e.id().toString(), String.CASE_INSENSITIVE_ORDER);
+ public static final Comparator<Edge> EDGE_COMPARATOR = Comparator.comparing(e -> e.id().toString(), String.CASE_INSENSITIVE_ORDER);
+ public static final Comparator<Property> PROPERTY_COMPARATOR = Comparator.comparing(Property::key, String.CASE_INSENSITIVE_ORDER);
+ public static final Comparator<Map.Entry<String, Property>> PROPERTY_ENTRY_COMPARATOR = Comparator.comparing(Map.Entry::getKey, String.CASE_INSENSITIVE_ORDER);
+ public static final Comparator<Map.Entry<String, Object>> OBJECT_ENTRY_COMPARATOR = Comparator.comparing(Map.Entry::getKey, String.CASE_INSENSITIVE_ORDER);
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java
new file mode 100644
index 0000000..f057037
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java
@@ -0,0 +1,461 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.tinkerpop.gremlin.structure.util;
+
+import com.tinkerpop.gremlin.process.T;
+import com.tinkerpop.gremlin.structure.Edge;
+import com.tinkerpop.gremlin.structure.Element;
+import com.tinkerpop.gremlin.structure.Graph;
+import com.tinkerpop.gremlin.structure.Property;
+import com.tinkerpop.gremlin.structure.Vertex;
+import com.tinkerpop.gremlin.structure.VertexProperty;
+import org.javatuples.Pair;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+/**
+ * Utility class supporting common functions for {@link com.tinkerpop.gremlin.structure.Element}.
+ *
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public final class ElementHelper {
+
+ private ElementHelper() {
+ }
+
+ /**
+ * Determine whether the Element label can be legally set. This is typically used as a pre-condition check.
+ *
+ * @param label the element label
+ * @throws IllegalArgumentException whether the label is legal and if not, a clear reason exception is provided
+ */
+ public static void validateLabel(final String label) throws IllegalArgumentException {
+ if (null == label)
+ throw Element.Exceptions.labelCanNotBeNull();
+ if (label.isEmpty())
+ throw Element.Exceptions.labelCanNotBeEmpty();
+ if (Graph.Hidden.isHidden(label))
+ throw Element.Exceptions.labelCanNotBeAHiddenKey(label);
+ }
+
+ /*public static void validateLabels(final String... labels) throws IllegalArgumentException {
+ for (final String label : labels) {
+ validateLabel(label);
+ }
+ }*/
+
+ /**
+ * Check if the vertex, by ID, exists. If it does return it, else create it and return it.
+ *
+ * @param graph the graph to check for the existence of the vertex
+ * @param id the id of the vertex to look for
+ * @param label the label of the vertex to set if the vertex does not exist
+ * @return a pre-existing vertex or a newly created vertex
+ */
+ public static Vertex getOrAddVertex(final Graph graph, final Object id, final String label) {
+ final Iterator<Vertex> iterator = graph.iterators().vertexIterator(id);
+ return iterator.hasNext() ? iterator.next() : graph.addVertex(T.id, id, T.label, label);
+ }
+
+ /**
+ * Determines whether the property key/value for the specified thing can be legally set. This is typically used as
+ * a pre-condition check prior to setting a property.
+ *
+ * @param key the key of the property
+ * @param value the value of the property
+ * @throws IllegalArgumentException whether the key/value pair is legal and if not, a clear reason exception
+ * message is provided
+ */
+ public static void validateProperty(final String key, final Object value) throws IllegalArgumentException {
+ if (null == value)
+ throw Property.Exceptions.propertyValueCanNotBeNull();
+ if (null == key)
+ throw Property.Exceptions.propertyKeyCanNotBeNull();
+ if (key.isEmpty())
+ throw Property.Exceptions.propertyKeyCanNotBeEmpty();
+ if (Graph.Hidden.isHidden(key))
+ throw Property.Exceptions.propertyKeyCanNotBeAHiddenKey(key);
+ }
+
+ /**
+ * Determines whether a list of key/values are legal, ensuring that there are an even number of values submitted
+ * and that the key values in the list of arguments are {@link String} or {@link com.tinkerpop.gremlin.structure.Element} objects.
+ *
+ * @param propertyKeyValues a list of key/value pairs
+ * @throws IllegalArgumentException if something in the pairs is illegal
+ */
+ public static void legalPropertyKeyValueArray(final Object... propertyKeyValues) throws IllegalArgumentException {
+ if (propertyKeyValues.length % 2 != 0)
+ throw Element.Exceptions.providedKeyValuesMustBeAMultipleOfTwo();
+ for (int i = 0; i < propertyKeyValues.length; i = i + 2) {
+ if (!(propertyKeyValues[i] instanceof String) && !(propertyKeyValues[i] instanceof T))
+ throw Element.Exceptions.providedKeyValuesMustHaveALegalKeyOnEvenIndices();
+ }
+ }
+
+ /**
+ * Extracts the value of the {@link com.tinkerpop.gremlin.process.T#id} key from the list of arguments.
+ *
+ * @param keyValues a list of key/value pairs
+ * @return the value associated with {@link com.tinkerpop.gremlin.process.T#id}
+ * @throws NullPointerException if the value for the {@link com.tinkerpop.gremlin.process.T#id} key is {@code null}
+ */
+ public static Optional<Object> getIdValue(final Object... keyValues) {
+ for (int i = 0; i < keyValues.length; i = i + 2) {
+ if (keyValues[i].equals(T.id))
+ return Optional.of(keyValues[i + 1]);
+ }
+ return Optional.empty();
+ }
+
+ /**
+ * Remove a key from the set of key value pairs. Assumes that validations have already taken place to
+ * assure that key positions contain strings and that there are an even number of elements. If after removal
+ * there are no values left, the key value list is returned as empty.
+ */
+ public static Optional<Object[]> remove(final String keyToRemove, final Object... keyValues) {
+ return ElementHelper.remove((Object) keyToRemove, keyValues);
+ }
+
+ public static Optional<Object[]> remove(final T accessor, final Object... keyValues) {
+ return ElementHelper.remove((Object) accessor, keyValues);
+ }
+
+
+ private static Optional<Object[]> remove(final Object keyToRemove, final Object... keyValues) {
+ final List list = Arrays.asList(keyValues);
+ final List revised = IntStream.range(0, list.size())
+ .filter(i -> i % 2 == 0)
+ .filter(i -> !keyToRemove.equals(list.get(i)))
+ .flatMap(i -> IntStream.of(i, i + 1))
+ .mapToObj(list::get)
+ .collect(Collectors.toList());
+ return revised.size() > 0 ? Optional.of(revised.toArray()) : Optional.empty();
+ }
+
+ /**
+ * Append a key/value pair to a list of existing key/values. If the key already exists in the keyValues then
+ * that value is overwritten with the provided value.
+ */
+ public static Object[] upsert(final Object[] keyValues, final String key, final Object val) {
+ if (!getKeys(keyValues).contains(key))
+ return Stream.concat(Stream.of(keyValues), Stream.of(key, val)).toArray();
+ else {
+ final Object[] kvs = new Object[keyValues.length];
+ for (int i = 0; i < keyValues.length; i = i + 2) {
+ kvs[i] = keyValues[i];
+ if (keyValues[i].equals(key))
+ kvs[i + 1] = val;
+ else
+ kvs[i + 1] = keyValues[i + 1];
+ }
+
+ return kvs;
+ }
+ }
+
+ /**
+ * Converts a set of key values to a Map. Assumes that validations have already taken place to
+ * assure that key positions contain strings and that there are an even number of elements.
+ */
+ public static Map<String, Object> asMap(final Object... keyValues) {
+ return asPairs(keyValues).stream().collect(Collectors.toMap(p -> p.getValue0(), p -> p.getValue1()));
+ }
+
+ /**
+ * Convert a set of key values to a list of Pair objects. Assumes that validations have already taken place to
+ * assure that key positions contain strings and that there are an even number of elements.
+ */
+ public static List<Pair<String, Object>> asPairs(final Object... keyValues) {
+ final List list = Arrays.asList(keyValues);
+ return IntStream.range(1, list.size())
+ .filter(i -> i % 2 != 0)
+ .mapToObj(i -> Pair.with(list.get(i - 1).toString(), list.get(i)))
+ .collect(Collectors.toList());
+ }
+
+ /**
+ * Gets the list of keys from the key values.
+ *
+ * @param keyValues a list of key/values pairs
+ */
+ public static Set<String> getKeys(final Object... keyValues) {
+ final Set<String> keys = new HashSet<>();
+ for (int i = 0; i < keyValues.length; i = i + 2) {
+ keys.add(keyValues[i].toString());
+ }
+ return keys;
+ }
+
+ /**
+ * Extracts the value of the {@link com.tinkerpop.gremlin.process.T#label} key from the list of arguments.
+ *
+ * @param keyValues a list of key/value pairs
+ * @return the value associated with {@link com.tinkerpop.gremlin.process.T#label}
+ * @throws ClassCastException if the value of the label is not a {@link String}
+ * @throws NullPointerException if the value for the {@link com.tinkerpop.gremlin.process.T#label} key is {@code null}
+ */
+ public static Optional<String> getLabelValue(final Object... keyValues) {
+ for (int i = 0; i < keyValues.length; i = i + 2) {
+ if (keyValues[i].equals(T.label)) {
+ ElementHelper.validateLabel((String) keyValues[i + 1]);
+ return Optional.of((String) keyValues[i + 1]);
+ }
+ }
+ return Optional.empty();
+ }
+
+ /**
+ * Assign key/value pairs as properties to an {@link com.tinkerpop.gremlin.structure.Element}. If the value of {@link com.tinkerpop.gremlin.process.T#id} or
+ * {@link com.tinkerpop.gremlin.process.T#label} is in the set of pairs, then they are ignored.
+ *
+ * @param element the graph element to assign the {@code propertyKeyValues}
+ * @param propertyKeyValues the key/value pairs to assign to the {@code element}
+ * @throws ClassCastException if the value of the key is not a {@link String}
+ * @throws IllegalArgumentException if the value of {@code element} is null
+ */
+ public static void attachProperties(final Element element, final Object... propertyKeyValues) {
+ if (null == element)
+ throw Graph.Exceptions.argumentCanNotBeNull("element");
+
+ for (int i = 0; i < propertyKeyValues.length; i = i + 2) {
+ if (!propertyKeyValues[i].equals(T.id) && !propertyKeyValues[i].equals(T.label))
+ element.property((String) propertyKeyValues[i], propertyKeyValues[i + 1]);
+ }
+ }
+
+ /**
+ * Assign key/value pairs as properties to an {@link com.tinkerpop.gremlin.structure.Vertex}. If the value of {@link com.tinkerpop.gremlin.process.T#id} or
+ * {@link com.tinkerpop.gremlin.process.T#label} is in the set of pairs, then they are ignored.
+ *
+ * @param vertex the vertex to assign the {@code propertyKeyValues}
+ * @param propertyKeyValues the key/value pairs to assign to the {@code vertex}
+ * @throws ClassCastException if the value of the key is not a {@link String}
+ * @throws IllegalArgumentException if the value of {@code vertex} is null
+ */
+ public static void attachSingleProperties(final Vertex vertex, final Object... propertyKeyValues) {
+ if (null == vertex)
+ throw Graph.Exceptions.argumentCanNotBeNull("vertex");
+
+ for (int i = 0; i < propertyKeyValues.length; i = i + 2) {
+ if (!propertyKeyValues[i].equals(T.id) && !propertyKeyValues[i].equals(T.label))
+ vertex.property(VertexProperty.Cardinality.single, (String) propertyKeyValues[i], propertyKeyValues[i + 1]);
+ }
+ }
+
+ /**
+ * Retrieve the properties associated with a particular element.
+ * The result is a Object[] where odd indices are String keys and even indices are the values.
+ *
+ * @param element the element to retrieve properties from
+ * @param includeId include Element.ID in the key/value list
+ * @param includeLabel include Element.LABEL in the key/value list
+ * @param propertiesToCopy the properties to include with an empty list meaning copy all properties
+ * @return a key/value array of properties where odd indices are String keys and even indices are the values.
+ */
+ public static Object[] getProperties(final Element element, final boolean includeId, final boolean includeLabel, final Set<String> propertiesToCopy) {
+ final List<Object> keyValues = new ArrayList<>();
+ if (includeId) {
+ keyValues.add(T.id);
+ keyValues.add(element.id());
+ }
+ if (includeLabel) {
+ keyValues.add(T.label);
+ keyValues.add(element.label());
+ }
+ element.keys().forEach(key -> {
+ if (propertiesToCopy.isEmpty() || propertiesToCopy.contains(key)) {
+ keyValues.add(key);
+ keyValues.add(element.value(key));
+ }
+ });
+ return keyValues.toArray(new Object[keyValues.size()]);
+ }
+
+ /**
+ * A standard method for determining if two {@link com.tinkerpop.gremlin.structure.Element} objects are equal. This method should be used by any
+ * {@link Object#equals(Object)} implementation to ensure consistent behavior. This method is used for Vertex, Edge, and VertexProperty.
+ *
+ * @param a The first {@link com.tinkerpop.gremlin.structure.Element}
+ * @param b The second {@link com.tinkerpop.gremlin.structure.Element} (as an {@link Object})
+ * @return true if elements and equal and false otherwise
+ * @throws IllegalArgumentException if either argument is null
+ */
+ public static boolean areEqual(final Element a, final Object b) {
+ if (null == a)
+ throw Graph.Exceptions.argumentCanNotBeNull("a");
+ if (null == b)
+ throw Graph.Exceptions.argumentCanNotBeNull("b");
+
+ if (a == b)
+ return true;
+ if (!((a instanceof Vertex && b instanceof Vertex) ||
+ (a instanceof Edge && b instanceof Edge) ||
+ (a instanceof VertexProperty && b instanceof VertexProperty)))
+ return false;
+ return haveEqualIds(a, (Element) b);
+ }
+
+ /**
+ * A standard method for determining if two {@link com.tinkerpop.gremlin.structure.VertexProperty} objects are equal. This method should be used by any
+ * {@link Object#equals(Object)} implementation to ensure consistent behavior.
+ *
+ * @param a the first {@link com.tinkerpop.gremlin.structure.VertexProperty}
+ * @param b the second {@link com.tinkerpop.gremlin.structure.VertexProperty}
+ * @return true if equal and false otherwise
+ */
+ public static boolean areEqual(final VertexProperty a, final Object b) {
+ return ElementHelper.areEqual((Element) a, b);
+ }
+
+ /**
+ * Simply tests if the value returned from {@link com.tinkerpop.gremlin.structure.Element#id()} are {@code equal()}.
+ *
+ * @param a the first {@link com.tinkerpop.gremlin.structure.Element}
+ * @param b the second {@link com.tinkerpop.gremlin.structure.Element}
+ * @return true if ids are equal and false otherwise
+ */
+ public static boolean haveEqualIds(final Element a, final Element b) {
+ return a.id().equals(b.id());
+ }
+
+ /**
+ * If two {@link Element} instances are equal, then they must have the same hash codes. This methods ensures consistent hashCode values.
+ *
+ * @param element the element to get the hashCode for
+ * @return the hash code of the element
+ */
+ public static int hashCode(final Element element) {
+ return element.id().hashCode();
+ }
+
+ /**
+ * If two {@link Property} instances are equal, then they must have the same hash codes. This methods ensures consistent hashCode values.
+ * For {@link VertexProperty} use {@link ElementHelper#hashCode(com.tinkerpop.gremlin.structure.Element)}.
+ *
+ * @param property the property to get the hashCode for
+ * @return the hash code of the property
+ */
+ public static int hashCode(final Property property) {
+ return property.key().hashCode() + property.value().hashCode();
+ }
+
+ /**
+ * A standard method for determining if two {@link com.tinkerpop.gremlin.structure.Property} objects are equal. This method should be used by any
+ * {@link Object#equals(Object)} implementation to ensure consistent behavior.
+ *
+ * @param a the first {@link com.tinkerpop.gremlin.structure.Property}
+ * @param b the second {@link com.tinkerpop.gremlin.structure.Property}
+ * @return true if equal and false otherwise
+ */
+ public static boolean areEqual(final Property a, final Object b) {
+ if (null == a)
+ throw Graph.Exceptions.argumentCanNotBeNull("a");
+ if (null == b)
+ throw Graph.Exceptions.argumentCanNotBeNull("b");
+
+ if (a == b)
+ return true;
+ if (!(b instanceof Property))
+ return false;
+ if (!a.isPresent() && !((Property) b).isPresent())
+ return true;
+ if (!a.isPresent() && ((Property) b).isPresent() || a.isPresent() && !((Property) b).isPresent())
+ return false;
+ return a.key().equals(((Property) b).key()) && a.value().equals(((Property) b).value()) && areEqual(a.element(), ((Property) b).element());
+
+ }
+
+ public static Map<String, Object> propertyValueMap(final Element element, final String... propertyKeys) {
+ final Map<String, Object> values = new HashMap<>();
+ element.iterators().propertyIterator(propertyKeys).forEachRemaining(property -> values.put(property.key(), property.value()));
+ return values;
+ }
+
+ public static Map<String, Property> propertyMap(final Element element, final String... propertyKeys) {
+ final Map<String, Property> propertyMap = new HashMap<>();
+ element.iterators().propertyIterator(propertyKeys).forEachRemaining(property -> propertyMap.put(property.key(), property));
+ return propertyMap;
+ }
+
+ public static Map<String, List> vertexPropertyValueMap(final Vertex vertex, final String... propertyKeys) {
+ final Map<String, List> valueMap = new HashMap<>();
+ vertex.iterators().propertyIterator(propertyKeys).forEachRemaining(property -> {
+ if (valueMap.containsKey(property.key()))
+ valueMap.get(property.key()).add(property.value());
+ else {
+ final List list = new ArrayList();
+ list.add(property.value());
+ valueMap.put(property.key(), list);
+ }
+ });
+ return valueMap;
+ }
+
+ public static Map<String, List<VertexProperty>> vertexPropertyMap(final Vertex vertex, final String... propertyKeys) {
+ final Map<String, List<VertexProperty>> propertyMap = new HashMap<>();
+ vertex.iterators().propertyIterator(propertyKeys).forEachRemaining(property -> {
+ if (propertyMap.containsKey(property.key()))
+ propertyMap.get(property.key()).add(property);
+ else {
+ final List<VertexProperty> list = new ArrayList<>();
+ list.add(property);
+ propertyMap.put(property.key(), list);
+ }
+ });
+ return propertyMap;
+ }
+
+ public static boolean keyExists(final String key, final String... providedKeys) {
+ if (Graph.Hidden.isHidden(key)) return false;
+ if (0 == providedKeys.length) return true;
+ if (1 == providedKeys.length) return key.equals(providedKeys[0]);
+ else {
+ for (final String temp : providedKeys) {
+ if (temp.equals(key))
+ return true;
+ }
+ return false;
+ }
+ }
+
+ public static boolean idExists(final Object id, final Object... providedIds) {
+ if (0 == providedIds.length) return true;
+ if (1 == providedIds.length) return id.equals(providedIds[0]);
+ else {
+ for (final Object temp : providedIds) {
+ if (temp.equals(id))
+ return true;
+ }
+ return false;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/FeatureDescriptor.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/FeatureDescriptor.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/FeatureDescriptor.java
new file mode 100644
index 0000000..01e9c70
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/FeatureDescriptor.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.tinkerpop.gremlin.structure.util;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * A annotation for feature methods.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface FeatureDescriptor {
+ /**
+ * The name of the feature which is represented as the text of the method it is annotating after the "supports"
+ * prefix
+ */
+ String name();
+
+ /**
+ * A description of the feature that will be useful to implementers if a Gremlin test fails around
+ * proper feature support.
+ */
+ String description() default "";
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/GraphFactory.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/GraphFactory.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/GraphFactory.java
new file mode 100644
index 0000000..af8c383
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/GraphFactory.java
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.tinkerpop.gremlin.structure.util;
+
+import com.tinkerpop.gremlin.structure.Graph;
+import com.tinkerpop.gremlin.util.config.YamlConfiguration;
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.MapConfiguration;
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.commons.configuration.XMLConfiguration;
+
+import java.io.File;
+import java.util.Map;
+
+/**
+ * Factory to construct new {@link com.tinkerpop.gremlin.structure.Graph} instances from a
+ * {@link org.apache.commons.configuration.Configuration} object or properties file.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class GraphFactory {
+
+ /**
+ * Open a graph. See each {@link com.tinkerpop.gremlin.structure.Graph} instance for its configuration options.
+ *
+ * @param configuration A configuration object that specifies the minimally required properties for a I
+ * {@link com.tinkerpop.gremlin.structure.Graph} instance. This minimum is determined by the
+ * {@link com.tinkerpop.gremlin.structure.Graph} instance itself.
+ * @return A {@link com.tinkerpop.gremlin.structure.Graph} instance.
+ * @throws IllegalArgumentException if {@code configuration}
+ */
+ public static Graph open(final Configuration configuration) {
+ if (null == configuration)
+ throw Graph.Exceptions.argumentCanNotBeNull("configuration");
+
+ final String clazz = configuration.getString(Graph.GRAPH, null);
+ if (null == clazz)
+ throw new RuntimeException("Configuration must contain a valid 'gremlin.graph' setting");
+
+ final Class graphClass;
+ try {
+ graphClass = Class.forName(clazz);
+ } catch (final ClassNotFoundException e) {
+ throw new RuntimeException(String.format("GraphFactory could not find [%s] - Ensure that the jar is in the classpath", clazz));
+ }
+
+ final Graph g;
+ try {
+ // will basically use Graph.open(Configuration c) to instantiate, but could
+ // technically use any method on any class with the same signature.
+ g = (Graph) graphClass.getMethod("open", Configuration.class).invoke(null, configuration);
+ } catch (final NoSuchMethodException e1) {
+ throw new RuntimeException(String.format("GraphFactory can only instantiate Graph implementations from classes that have a static open() method that takes a single Apache Commons Configuration argument - [%s] does not seem to have one", clazz));
+ } catch (final Exception e2) {
+ throw new RuntimeException(String.format("GraphFactory could not instantiate this Graph implementation [%s]", clazz), e2);
+ }
+
+ return g;
+ }
+
+ /**
+ * Open a graph. See each {@link com.tinkerpop.gremlin.structure.Graph} instance for its configuration options. This file may be XML, YAML,
+ * or a standard properties file. How the configuration is used (and which kind is required) is dependent on
+ * the implementation.
+ *
+ * @param configurationFile The location of a configuration file that specifies the minimally required properties
+ * for a {@link com.tinkerpop.gremlin.structure.Graph} instance. This minimum is determined by the {@link com.tinkerpop.gremlin.structure.Graph} instance
+ * itself.
+ * @return A {@link com.tinkerpop.gremlin.structure.Graph} instance.
+ * @throws IllegalArgumentException if {@code configurationFile} is null
+ */
+ public static Graph open(final String configurationFile) {
+ if (null == configurationFile) throw Graph.Exceptions.argumentCanNotBeNull("configurationFile");
+ return open(getConfiguration(new File(configurationFile)));
+ }
+
+ /**
+ * Open a graph. See each {@link com.tinkerpop.gremlin.structure.Graph} instance for its configuration options.
+ *
+ * @param configuration A {@link java.util.Map} based configuration that will be converted to an {@link org.apache.commons.configuration.Configuration} object
+ * via {@link org.apache.commons.configuration.MapConfiguration} and passed to the appropriate overload.
+ * @return A Graph instance.
+ */
+ public static Graph open(final Map configuration) {
+ return open(new MapConfiguration(configuration));
+ }
+
+ private static Configuration getConfiguration(final File configurationFile) {
+ if (null == configurationFile)
+ throw Graph.Exceptions.argumentCanNotBeNull("configurationFile");
+
+ if (!configurationFile.isFile())
+ throw new IllegalArgumentException(String.format("The location configuration must resolve to a file and [%s] does not", configurationFile));
+
+ try {
+ final String fileName = configurationFile.getName();
+ final String fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
+
+ switch (fileExtension) {
+ case "yml":
+ case "yaml":
+ return new YamlConfiguration(configurationFile);
+ case "xml":
+ return new XMLConfiguration(configurationFile);
+ default:
+ return new PropertiesConfiguration(configurationFile);
+ }
+ } catch (final ConfigurationException e) {
+ throw new IllegalArgumentException(String.format("Could not load configuration at: %s", configurationFile), e);
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/GraphVariableHelper.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/GraphVariableHelper.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/GraphVariableHelper.java
new file mode 100644
index 0000000..46a22ce
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/GraphVariableHelper.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.tinkerpop.gremlin.structure.util;
+
+import com.tinkerpop.gremlin.structure.Graph;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class GraphVariableHelper {
+
+ public static void validateVariable(final String variable, final Object value) throws IllegalArgumentException {
+ if (null == value)
+ throw Graph.Variables.Exceptions.variableValueCanNotBeNull();
+ if (null == variable)
+ throw Graph.Variables.Exceptions.variableKeyCanNotBeNull();
+ if (variable.isEmpty())
+ throw Graph.Variables.Exceptions.variableKeyCanNotBeEmpty();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/StringFactory.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/StringFactory.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/StringFactory.java
new file mode 100644
index 0000000..0655e16
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/StringFactory.java
@@ -0,0 +1,218 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.tinkerpop.gremlin.structure.util;
+
+import com.tinkerpop.gremlin.process.TraversalSideEffects;
+import com.tinkerpop.gremlin.process.TraversalStrategies;
+import com.tinkerpop.gremlin.process.TraversalStrategy;
+import com.tinkerpop.gremlin.process.computer.ComputerResult;
+import com.tinkerpop.gremlin.process.computer.GraphComputer;
+import com.tinkerpop.gremlin.process.computer.MapReduce;
+import com.tinkerpop.gremlin.process.computer.Memory;
+import com.tinkerpop.gremlin.process.computer.VertexProgram;
+import com.tinkerpop.gremlin.structure.Direction;
+import com.tinkerpop.gremlin.structure.Edge;
+import com.tinkerpop.gremlin.structure.Graph;
+import com.tinkerpop.gremlin.structure.Property;
+import com.tinkerpop.gremlin.structure.Vertex;
+import com.tinkerpop.gremlin.structure.VertexProperty;
+import com.tinkerpop.gremlin.structure.strategy.GraphStrategy;
+import com.tinkerpop.gremlin.structure.strategy.StrategyElement;
+import com.tinkerpop.gremlin.structure.strategy.StrategyProperty;
+import com.tinkerpop.gremlin.structure.strategy.StrategyVariables;
+import com.tinkerpop.gremlin.util.function.FunctionUtils;
+import org.javatuples.Pair;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.stream.Stream;
+
+
+/**
+ * A collection of helpful methods for creating standard {@link Object#toString()} representations of graph-related
+ * objects.
+ *
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public final class StringFactory {
+
+ private static final String V = "v";
+ private static final String E = "e";
+ private static final String P = "p";
+ private static final String VP = "vp";
+ private static final String L_BRACKET = "[";
+ private static final String R_BRACKET = "]";
+ private static final String COMMA_SPACE = ", ";
+ private static final String COLON = ":";
+ private static final String EMPTY_MAP = "{}";
+ private static final String DOTS = "...";
+ private static final String DASH = "-";
+ private static final String ARROW = "->";
+ private static final String STAR = "*";
+ private static final String EMPTY_PROPERTY = "p[empty]";
+ private static final String EMPTY_VERTEX_PROPERTY = "vp[empty]";
+ private static final String LINE_SEPARATOR = System.getProperty("line.separator");
+
+ private static final String featuresStartWith = "supports";
+ private static final int prefixLength = featuresStartWith.length();
+
+ private StringFactory() {
+ }
+
+ /**
+ * Construct the representation for a {@link com.tinkerpop.gremlin.structure.Vertex}.
+ */
+ public static String vertexString(final Vertex vertex) {
+ return V + L_BRACKET + vertex.id() + R_BRACKET;
+ }
+
+ /**
+ * Construct the representation for a {@link com.tinkerpop.gremlin.structure.Edge}.
+ */
+ public static String edgeString(final Edge edge) {
+ final Vertex inV = edge.iterators().vertexIterator(Direction.IN).next();
+ final Vertex outV = edge.iterators().vertexIterator(Direction.OUT).next();
+ return E + L_BRACKET + edge.id() + R_BRACKET + L_BRACKET + outV.id() + DASH + edge.label() + ARROW + inV.id() + R_BRACKET;
+ }
+
+ /**
+ * Construct the representation for a {@link com.tinkerpop.gremlin.structure.Property} or {@link com.tinkerpop.gremlin.structure.VertexProperty}.
+ */
+ public static String propertyString(final Property property) {
+ if (property instanceof VertexProperty) {
+ if (!property.isPresent()) return EMPTY_VERTEX_PROPERTY;
+ final String valueString = String.valueOf(property.value());
+ return VP + L_BRACKET + property.key() + ARROW + valueString.substring(0, Math.min(valueString.length(), 20)) + R_BRACKET;
+ } else {
+ if (!property.isPresent()) return EMPTY_PROPERTY;
+ final String valueString = String.valueOf(property.value());
+ return P + L_BRACKET + property.key() + ARROW + valueString.substring(0, Math.min(valueString.length(), 20)) + R_BRACKET;
+ }
+ }
+
+ /**
+ * Construct the representation for a {@link com.tinkerpop.gremlin.structure.Graph}.
+ *
+ * @param internalString a mapper {@link String} that appends to the end of the standard representation
+ */
+ public static String graphString(final Graph graph, final String internalString) {
+ return graph.getClass().getSimpleName().toLowerCase() + L_BRACKET + internalString + R_BRACKET;
+ }
+
+ /**
+ * Construct the representation for a {@link com.tinkerpop.gremlin.structure.strategy.GraphStrategy}.
+ */
+
+ public static String graphStrategyString(final GraphStrategy graphStrategy) {
+ return graphStrategy.getClass().getSimpleName().toLowerCase();
+ }
+
+ public static String graphStrategyString(final GraphStrategy graphStrategy, final Graph graph) {
+ return graphStrategy.getClass().getSimpleName().toLowerCase() + L_BRACKET + graph.toString() + R_BRACKET;
+ }
+
+ public static String graphStrategyElementString(final StrategyElement element) {
+ return element.getBaseElement() + STAR;
+ }
+
+ public static String graphStrategyPropertyString(final StrategyProperty property) {
+ return property.getBaseProperty() + STAR;
+ }
+
+ public static String graphStrategyVariables(final StrategyVariables variables) {
+ return variables.getBaseVariables() + STAR;
+ }
+
+ public static String graphVariablesString(final Graph.Variables variables) {
+ return "variables" + L_BRACKET + "size:" + variables.keys().size() + R_BRACKET;
+ }
+
+ public static String memoryString(final Memory memory) {
+ return "memory" + L_BRACKET + "size:" + memory.keys().size() + R_BRACKET;
+ }
+
+ public static String computeResultString(final ComputerResult computerResult) {
+ return "result" + L_BRACKET + computerResult.graph() + ',' + computerResult.memory() + R_BRACKET;
+ }
+
+ public static String graphComputerString(final GraphComputer graphComputer) {
+ return graphComputer.getClass().getSimpleName().toLowerCase();
+ }
+
+ public static String featureString(final Graph.Features features) {
+ final StringBuilder sb = new StringBuilder("FEATURES");
+ final Predicate<Method> supportMethods = (m) -> m.getModifiers() == Modifier.PUBLIC && m.getName().startsWith(featuresStartWith) && !m.getName().equals(featuresStartWith);
+ sb.append(LINE_SEPARATOR);
+
+ Stream.of(Pair.with(Graph.Features.GraphFeatures.class, features.graph()),
+ Pair.with(Graph.Features.VariableFeatures.class, features.graph().variables()),
+ Pair.with(Graph.Features.VertexFeatures.class, features.vertex()),
+ Pair.with(Graph.Features.VertexPropertyFeatures.class, features.vertex().properties()),
+ Pair.with(Graph.Features.EdgeFeatures.class, features.edge()),
+ Pair.with(Graph.Features.EdgePropertyFeatures.class, features.edge().properties())).forEach(p -> {
+ printFeatureTitle(p.getValue0(), sb);
+ Stream.of(p.getValue0().getMethods())
+ .filter(supportMethods)
+ .map(createTransform(p.getValue1()))
+ .forEach(sb::append);
+ });
+
+ return sb.toString();
+ }
+
+ public static String traversalSideEffectsString(final TraversalSideEffects traversalSideEffects) {
+ return "sideEffects" + L_BRACKET + "size:" + traversalSideEffects.keys().size() + R_BRACKET;
+ }
+
+ public static String traversalStrategiesString(final TraversalStrategies traversalStrategies) {
+ return "strategies" + traversalStrategies.toList();
+ }
+
+ public static String traversalStrategyString(final TraversalStrategy traversalStrategy) {
+ return traversalStrategy.getClass().getSimpleName();
+ }
+
+ public static String vertexProgramString(final VertexProgram vertexProgram, final String internalString) {
+ return vertexProgram.getClass().getSimpleName() + L_BRACKET + internalString + R_BRACKET;
+ }
+
+ public static String vertexProgramString(final VertexProgram vertexProgram) {
+ return vertexProgram.getClass().getSimpleName();
+ }
+
+ public static String mapReduceString(final MapReduce mapReduce, final String internalString) {
+ return mapReduce.getClass().getSimpleName() + L_BRACKET + internalString + R_BRACKET;
+ }
+
+ public static String mapReduceString(final MapReduce mapReduce) {
+ return mapReduce.getClass().getSimpleName();
+ }
+
+ private static Function<Method, String> createTransform(final Graph.Features.FeatureSet features) {
+ return FunctionUtils.wrapFunction((m) -> ">-- " + m.getName().substring(prefixLength) + ": " + m.invoke(features).toString() + LINE_SEPARATOR);
+ }
+
+ private static void printFeatureTitle(final Class<? extends Graph.Features.FeatureSet> featureClass, final StringBuilder sb) {
+ sb.append("> ");
+ sb.append(featureClass.getSimpleName());
+ sb.append(LINE_SEPARATOR);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1545201f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/BatchFeatures.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/BatchFeatures.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/BatchFeatures.java
new file mode 100644
index 0000000..3ab0e79
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/batch/BatchFeatures.java
@@ -0,0 +1,216 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.tinkerpop.gremlin.structure.util.batch;
+
+import com.tinkerpop.gremlin.structure.Graph;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+class BatchFeatures implements Graph.Features {
+
+ private final Graph.Features baseFeatures;
+ private final BatchGraphFeatures graphFeatures = new BatchGraphFeatures();
+
+ private final VertexFeatures vertexFeatures = new VertexFeatures() {
+ @Override
+ public boolean supportsUserSuppliedIds() {
+ // batch loading supports user supplied ids
+ return true;
+ }
+
+ @Override
+ public VertexPropertyFeatures properties() {
+ return vertexPropertyFeatures;
+ }
+ };
+
+ private final EdgeFeatures edgeFeatures = new EdgeFeatures() {
+ @Override
+ public boolean supportsUserSuppliedIds() {
+ // batch loading supports user supplied identifiers
+ return true;
+ }
+
+ @Override
+ public EdgePropertyFeatures properties() {
+ return edgePropertyFeatures;
+ }
+ };
+
+ private final EdgePropertyFeatures edgePropertyFeatures = new BatchEdgePropertyFeatures();
+ private final VertexPropertyFeatures vertexPropertyFeatures = new BatchVertexPropertyFeatures();
+
+ public BatchFeatures(final Graph.Features baseFeatures) {
+ this.baseFeatures = baseFeatures;
+ }
+
+ @Override
+ public GraphFeatures graph() {
+ return graphFeatures;
+ }
+
+ @Override
+ public VertexFeatures vertex() {
+ return vertexFeatures;
+ }
+
+ @Override
+ public EdgeFeatures edge() {
+ return edgeFeatures;
+ }
+
+ class BatchVertexPropertyFeatures extends BatchDataTypeFeature implements VertexPropertyFeatures {
+ @Override
+ public boolean supportsProperties() {
+ return baseFeatures.vertex().properties().supportsProperties();
+ }
+ }
+
+ class BatchEdgePropertyFeatures extends BatchDataTypeFeature implements EdgePropertyFeatures {
+ @Override
+ public boolean supportsProperties() {
+ return baseFeatures.edge().properties().supportsProperties();
+ }
+ }
+
+ class BatchGraphFeatures implements GraphFeatures {
+
+ @Override
+ public boolean supportsComputer() {
+ return false;
+ }
+
+ @Override
+ public boolean supportsPersistence() {
+ return baseFeatures.graph().supportsPersistence();
+ }
+
+ @Override
+ public boolean supportsTransactions() {
+ // the transaction is true because as a wrapper the BatchGraph will check the features of the
+ // underlying graph and not let it fail if the underlying graph does not support tx.
+ return true;
+ }
+
+ @Override
+ public boolean supportsThreadedTransactions() {
+ return false;
+ }
+
+ @Override
+ public VariableFeatures variables() {
+ return new BatchVariableFeatures();
+ }
+ }
+
+ class BatchVariableFeatures extends BatchDataTypeFeature implements VariableFeatures {
+ @Override
+ public boolean supportsVariables() {
+ return baseFeatures.graph().variables().supportsVariables();
+ }
+ }
+
+ class BatchDataTypeFeature implements DataTypeFeatures {
+ @Override
+ public boolean supportsBooleanValues() {
+ return baseFeatures.graph().variables().supportsBooleanValues();
+ }
+
+ @Override
+ public boolean supportsDoubleValues() {
+ return baseFeatures.graph().variables().supportsDoubleValues();
+ }
+
+ @Override
+ public boolean supportsFloatValues() {
+ return baseFeatures.graph().variables().supportsFloatValues();
+ }
+
+ @Override
+ public boolean supportsIntegerValues() {
+ return baseFeatures.graph().variables().supportsIntegerValues();
+ }
+
+ @Override
+ public boolean supportsLongValues() {
+ return baseFeatures.graph().variables().supportsLongValues();
+ }
+
+ @Override
+ public boolean supportsMapValues() {
+ return baseFeatures.graph().variables().supportsMapValues();
+ }
+
+ @Override
+ public boolean supportsByteValues() {
+ return baseFeatures.graph().variables().supportsByteValues();
+ }
+
+ @Override
+ public boolean supportsMixedListValues() {
+ return baseFeatures.graph().variables().supportsMixedListValues();
+ }
+
+ @Override
+ public boolean supportsBooleanArrayValues() {
+ return baseFeatures.graph().variables().supportsBooleanArrayValues();
+ }
+
+ @Override
+ public boolean supportsByteArrayValues() {
+ return baseFeatures.graph().variables().supportsByteArrayValues();
+ }
+
+ @Override
+ public boolean supportsDoubleArrayValues() {
+ return baseFeatures.graph().variables().supportsDoubleArrayValues();
+ }
+
+ @Override
+ public boolean supportsFloatArrayValues() {
+ return baseFeatures.graph().variables().supportsFloatArrayValues();
+ }
+
+ @Override
+ public boolean supportsIntegerArrayValues() {
+ return baseFeatures.graph().variables().supportsIntegerArrayValues();
+ }
+
+ @Override
+ public boolean supportsLongArrayValues() {
+ return baseFeatures.graph().variables().supportsLongArrayValues();
+ }
+
+ @Override
+ public boolean supportsSerializableValues() {
+ return baseFeatures.graph().variables().supportsSerializableValues();
+ }
+
+ @Override
+ public boolean supportsStringValues() {
+ return baseFeatures.graph().variables().supportsStringValues();
+ }
+
+ @Override
+ public boolean supportsUniformListValues() {
+ return baseFeatures.graph().variables().supportsUniformListValues();
+ }
+ }
+}