org.apache.tinkerpop.gremlin.structure.PropertyType Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.structure.PropertyType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TraversalBuilder.java    From sparql-gremlin with Apache License 2.0 6 votes vote down vote up
public static GraphTraversal<?, ?> transform(final Triple triple) {
    final GraphTraversal<Vertex, ?> matchTraversal = __.as(triple.getSubject().getName());
    final Node predicate = triple.getPredicate();
    final String uri = predicate.getURI();
    final String uriValue = Prefixes.getURIValue(uri);
    final String prefix = Prefixes.getPrefix(uri);
    switch (prefix) {
        case "edge":
            return matchTraversal.out(uriValue).as(triple.getObject().getName());
        case "property":
            return matchProperty(matchTraversal, uriValue, PropertyType.PROPERTY, triple.getObject());
        case "value":
            return matchProperty(matchTraversal, uriValue, PropertyType.VALUE, triple.getObject());
        default:
            throw new IllegalStateException(String.format("Unexpected predicate: %s", predicate));
    }
}
 
Example #2
Source File: SqlgPropertiesStep.java    From sqlg with MIT License 6 votes vote down vote up
@Override
protected Iterator<E> flatMap(final Traverser.Admin<Element> traverser) {
    for (String appliesToLabel : appliesToLabels) {
        String label = SqlgUtil.originalLabel(appliesToLabel);
        Object o = traverser.path().get(label);
        if (o instanceof List) {
            List<SqlgElement> objects = (List) o;
            SqlgElement last = objects.get(objects.size() - 1);
            if (this.returnType.equals(PropertyType.VALUE)) {
                return last.values(this.propertyKeys);
            } else {
                return (Iterator)last.properties(this.propertyKeys);
            }
        } else {
            SqlgElement sqlgElement = traverser.path().get(label);
            if (this.returnType.equals(PropertyType.VALUE)) {
                return sqlgElement.values(this.propertyKeys);
            } else {
                return (Iterator) sqlgElement.properties(this.propertyKeys);
            }
        }
    }
    return EmptyIterator.INSTANCE;
}
 
Example #3
Source File: TraversalBuilder.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
static GraphTraversal<?, ?> transform(final Triple triple) {
    final GraphTraversal<Vertex, ?> matchTraversal = __.as(triple.getSubject().getName());
    
    final Node predicate = triple.getPredicate();
    final String uri = predicate.getURI();
    final String uriValue = Prefixes.getURIValue(uri);
    final String prefix = Prefixes.getPrefix(uri);

    switch (prefix) {
        case "edge":
            return matchTraversal.out(uriValue).as(triple.getObject().getName());
        case "property":
            return matchProperty(matchTraversal, uriValue, PropertyType.PROPERTY, triple.getObject());
        case "value":
            return matchProperty(matchTraversal, uriValue, PropertyType.VALUE, triple.getObject());
        default:
            throw new IllegalStateException(String.format("Unexpected predicate: %s", predicate));
    }
}
 
Example #4
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRemoveStepsCorrectly() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(new IdentityStep(traversal));
    traversal.asAdmin().addStep(new HasStep(traversal));
    traversal.asAdmin().addStep(new LambdaFilterStep(traversal, traverser -> true));

    traversal.asAdmin().addStep(new PropertiesStep(traversal, PropertyType.VALUE, "marko"));
    traversal.asAdmin().removeStep(3);
    validateToyTraversal(traversal);

    traversal.asAdmin().addStep(0, new PropertiesStep(traversal, PropertyType.PROPERTY, "marko"));
    traversal.asAdmin().removeStep(0);
    validateToyTraversal(traversal);

    traversal.asAdmin().removeStep(1);
    traversal.asAdmin().addStep(1, new HasStep(traversal));
    validateToyTraversal(traversal);
}
 
Example #5
Source File: ByModulatorOptimizationStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private void optimizeForStep(final TraversalParent step, final Traversal.Admin<?, ?> traversal, final Step singleStep) {
    if (singleStep instanceof PropertiesStep) {
        final PropertiesStep ps = (PropertiesStep) singleStep;
        if (ps.getReturnType().equals(PropertyType.VALUE) && ps.getPropertyKeys().length == 1) {
            step.replaceLocalChild(traversal, new ValueTraversal<>(ps.getPropertyKeys()[0]));
        }
    } else if (singleStep instanceof IdStep) {
        step.replaceLocalChild(traversal, new TokenTraversal<>(T.id));
    } else if (singleStep instanceof LabelStep) {
        step.replaceLocalChild(traversal, new TokenTraversal<>(T.label));
    } else if (singleStep instanceof PropertyKeyStep) {
        step.replaceLocalChild(traversal, new TokenTraversal<>(T.key));
    } else if (singleStep instanceof PropertyValueStep) {
        step.replaceLocalChild(traversal, new TokenTraversal<>(T.value));
    } else if (singleStep instanceof IdentityStep) {
        step.replaceLocalChild(traversal, new IdentityTraversal<>());
    }
}
 
Example #6
Source File: TraversalBuilder.java    From sparql-gremlin with Apache License 2.0 5 votes vote down vote up
private static GraphTraversal<?, ?> matchProperty(final GraphTraversal<?, ?> traversal, final String propertyName,
                                                  final PropertyType type, final Node object) {
    switch (propertyName) {
        case "id":
            return object.isConcrete()
                    ? traversal.hasId(object.getLiteralValue())
                    : traversal.id().as(object.getName());
        case "label":
            return object.isConcrete()
                    ? traversal.hasLabel(object.getLiteralValue().toString())
                    : traversal.label().as(object.getName());
        case "key":
            return object.isConcrete()
                    ? traversal.hasKey(object.getLiteralValue().toString())
                    : traversal.key().as(object.getName());
        case "value":
            return object.isConcrete()
                    ? traversal.hasValue(object.getLiteralValue().toString())
                    : traversal.value().as(object.getName());
        default:
            if (type.equals(PropertyType.PROPERTY)) {
                return traversal.properties(propertyName).as(object.getName());
            } else {
                return object.isConcrete()
                        ? traversal.values(propertyName).is(object.getLiteralValue())
                        : traversal.values(propertyName).as(object.getName());
            }
    }
}
 
Example #7
Source File: TraversalBuilder.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static GraphTraversal<?, ?> matchProperty(final GraphTraversal<?, ?> traversal, final String propertyName,
                                                  final PropertyType type, final Node object) {
    switch (propertyName) {
        case "id":
            return object.isConcrete()
                    ? traversal.hasId(object.getLiteralValue())
                    : traversal.id().as(object.getName());
        case "label":
            return object.isConcrete()
                    ? traversal.hasLabel(object.getLiteralValue().toString())
                    : traversal.label().as(object.getName());
        case "key":
            return object.isConcrete()
                    ? traversal.hasKey(object.getLiteralValue().toString())
                    : traversal.key().as(object.getName());
        case "value":
            return object.isConcrete()
                    ? traversal.hasValue(object.getLiteralValue().toString())
                    : traversal.value().as(object.getName());
        default:
            if (type.equals(PropertyType.PROPERTY)) {
                return traversal.properties(propertyName).as(object.getName());
            } else {
                return object.isConcrete()
                        ? traversal.has(propertyName, object.getLiteralValue())
                        : traversal.values(propertyName).as(object.getName());
            }
    }
}
 
Example #8
Source File: PropertyMapStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public PropertyMapStep(final Traversal.Admin traversal, final PropertyType propertyType, final String... propertyKeys) {
    super(traversal);
    this.propertyKeys = propertyKeys;
    this.returnType = propertyType;
    this.propertyTraversal = null;
    this.traversalRing = new TraversalRing<>();
}
 
Example #9
Source File: ElementIdStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    TraversalHelper.getStepsOfAssignableClass(HasStep.class, traversal).stream()
            .filter(hasStep -> ((HasStep<?>) hasStep).getHasContainers().get(0).getKey().equals(T.id.getAccessor()))
            .forEach(hasStep -> ((HasStep<?>) hasStep).getHasContainers().get(0).setKey(this.idPropertyKey));

    if (traversal.getStartStep() instanceof GraphStep) {
        final GraphStep graphStep = (GraphStep) traversal.getStartStep();
        // only need to apply the custom id if ids were assigned - otherwise we want the full iterator.
        // note that it is then only necessary to replace the step if the id is a non-element.  other tests
        // in the suite validate that items in getIds() is uniform so it is ok to just test the first item
        // in the list.
        if (graphStep.getIds().length > 0 && !(graphStep.getIds()[0] instanceof Element)) {
            if (graphStep instanceof HasContainerHolder)
                ((HasContainerHolder) graphStep).addHasContainer(new HasContainer(this.idPropertyKey, P.within(Arrays.asList(graphStep.getIds()))));
            else
                TraversalHelper.insertAfterStep(new HasStep(traversal, new HasContainer(this.idPropertyKey, P.within(Arrays.asList(graphStep.getIds())))), graphStep, traversal);
            graphStep.clearIds();
        }
    }
    TraversalHelper.getStepsOfAssignableClass(IdStep.class, traversal).stream().forEach(step -> {
        TraversalHelper.replaceStep(step, new PropertiesStep(traversal, PropertyType.VALUE, idPropertyKey), traversal);
    });

    // in each case below, determine if the T.id is present and if so, replace T.id with the idPropertyKey or if
    // it is not present then shove it in there and generate an id
    traversal.getSteps().forEach(step -> {
        if (step instanceof AddVertexStep || step instanceof AddVertexStartStep || step instanceof AddEdgeStep) {
            final Parameterizing parameterizing = (Parameterizing) step;
            if (parameterizing.getParameters().contains(T.id))
                parameterizing.getParameters().rename(T.id, this.idPropertyKey);
            else if (!parameterizing.getParameters().contains(this.idPropertyKey))
                parameterizing.getParameters().set(null, this.idPropertyKey, idMaker.get());
        }
    });
}
 
Example #10
Source File: SqlgPropertiesStep.java    From sqlg with MIT License 4 votes vote down vote up
public PropertyType getReturnType() {
    return this.returnType;
}
 
Example #11
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
public static void extractAggregateFunc(Step<?, ?> newStep,
                                        Traversal.Admin<?, ?> traversal) {
    PropertiesStep<?> propertiesStep = null;
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof PropertiesStep) {
            @SuppressWarnings("resource")
            PropertiesStep<?> propStep = (PropertiesStep<?>) step;
            if (propStep.getReturnType() == PropertyType.VALUE &&
                propStep.getPropertyKeys().length == 1) {
                propertiesStep = propStep;
            }
        } else if (propertiesStep != null &&
                   step instanceof ReducingBarrierStep) {
            AggregateFunc aggregateFunc;
            if (step instanceof CountGlobalStep) {
                aggregateFunc = AggregateFunc.COUNT;
            } else if (step instanceof MaxGlobalStep) {
                aggregateFunc = AggregateFunc.MAX;
            } else if (step instanceof MinGlobalStep) {
                aggregateFunc = AggregateFunc.MIN;
            } else if (step instanceof MeanGlobalStep) {
                aggregateFunc = AggregateFunc.AVG;
            } else if (step instanceof SumGlobalStep) {
                aggregateFunc = AggregateFunc.SUM;
            } else {
                aggregateFunc = null;
            }

            if (aggregateFunc != null) {
                QueryHolder holder = (QueryHolder) newStep;
                holder.setAggregate(aggregateFunc,
                                    propertiesStep.getPropertyKeys()[0]);
                traversal.removeStep(step);
                traversal.removeStep(propertiesStep);
            }
        }
    } while (step instanceof FilterStep ||
             step instanceof PropertiesStep ||
             step instanceof IdentityStep ||
             step instanceof NoOpBarrierStep);
}
 
Example #12
Source File: SqlgPropertiesStep.java    From sqlg with MIT License 4 votes vote down vote up
public SqlgPropertiesStep(final Traversal.Admin traversal, final PropertyType propertyType, final String... propertyKeys) {
    super(traversal);
    this.returnType = propertyType;
    this.propertyKeys = propertyKeys;
}
 
Example #13
Source File: PropertiesStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public PropertyType getReturnType() {
    return this.returnType;
}
 
Example #14
Source File: PropertiesStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected Iterator<E> flatMap(final Traverser.Admin<Element> traverser) {
    return this.returnType.equals(PropertyType.VALUE) ?
            traverser.get().values(this.propertyKeys) :
            (Iterator) traverser.get().properties(this.propertyKeys);
}
 
Example #15
Source File: PropertiesStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public PropertiesStep(final Traversal.Admin traversal, final PropertyType propertyType, final String... propertyKeys) {
    super(traversal);
    this.returnType = propertyType;
    this.propertyKeys = propertyKeys;
}
 
Example #16
Source File: PropertyMapStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public PropertyType getReturnType() {
    return this.returnType;
}
 
Example #17
Source File: PropertyMapStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public PropertyMapStep(final Traversal.Admin traversal, final int options, final PropertyType propertyType, final String... propertyKeys) {
    this(traversal, propertyType, propertyKeys);
    this.configure(WithOptions.tokens, options);
}
 
Example #18
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 3 votes vote down vote up
/**
 * Filters vertices, edges and vertex properties based on the value of the specified property key.
 *
 * @param propertyKey       the key of the property to filter on
 * @param propertyTraversal the traversal to filter the property value by
 * @return the traversal with an appended {@link HasStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#has-step" target="_blank">Reference Documentation - Has Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> has(final String propertyKey, final Traversal<?, ?> propertyTraversal) {
    this.asAdmin().getBytecode().addStep(Symbols.has, propertyKey, propertyTraversal);
    return this.asAdmin().addStep(
            new TraversalFilterStep<>(this.asAdmin(), propertyTraversal.asAdmin().addStep(0,
                    new PropertiesStep(propertyTraversal.asAdmin(), PropertyType.VALUE, propertyKey))));
}
 
Example #19
Source File: AdjacentToIncidentStrategy.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Checks whether a given step is optimizable or not.
 *
 * @param step the step to check
 * @return <code>true</code> if the step is optimizable, otherwise <code>false</code>
 */
private static boolean isOptimizable(final Step step) {
    return ((step instanceof VertexStep && ((VertexStep) step).returnsVertex()) ||
            (step instanceof PropertiesStep && PropertyType.VALUE.equals(((PropertiesStep) step).getReturnType()))) && (step.getTraversal().getEndStep().getLabels().isEmpty() || step.getNextStep() instanceof CountGlobalStep);
}
 
Example #20
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Map the {@link Element} to a {@code Map} of the property values key'd according to their {@link Property#key}.
 * If no property keys are provided, then all property values are retrieved.
 *
 * @param includeTokens whether to include {@link T} tokens in the emitted map.
 * @param propertyKeys  the properties to retrieve
 * @param <E2>          the value type of the returned properties
 * @return the traversal with an appended {@link PropertyMapStep}.
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#valuemap-step" target="_blank">Reference Documentation - ValueMap Step</a>
 * @since 3.0.0-incubating
 * @deprecated As of release 3.4.0, deprecated in favor of {@link GraphTraversal#valueMap(String...)} in conjunction with
 *             {@link GraphTraversal#with(String, Object)} or simple prefer {@link #elementMap(String...)}.
 */
@Deprecated
public default <E2> GraphTraversal<S, Map<Object, E2>> valueMap(final boolean includeTokens, final String... propertyKeys) {
    this.asAdmin().getBytecode().addStep(Symbols.valueMap, includeTokens, propertyKeys);
    return this.asAdmin().addStep(new PropertyMapStep<>(this.asAdmin(), WithOptions.all, PropertyType.VALUE, propertyKeys));
}
 
Example #21
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Map the {@link Element} to a {@code Map} of the property values key'd according to their {@link Property#key}.
 * If no property keys are provided, then all property values are retrieved.
 *
 * @param propertyKeys the properties to retrieve
 * @param <E2>         the value type of the returned properties
 * @return the traversal with an appended {@link PropertyMapStep}.
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#valuemap-step" target="_blank">Reference Documentation - ValueMap Step</a>
 * @since 3.0.0-incubating
 */
public default <E2> GraphTraversal<S, Map<Object, E2>> valueMap(final String... propertyKeys) {
    this.asAdmin().getBytecode().addStep(Symbols.valueMap, propertyKeys);
    return this.asAdmin().addStep(new PropertyMapStep<>(this.asAdmin(), WithOptions.none, PropertyType.VALUE, propertyKeys));
}
 
Example #22
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Map the {@link Element} to a {@link Map} of the properties key'd according to their {@link Property#key}.
 * If no property keys are provided, then all properties are retrieved.
 *
 * @param propertyKeys the properties to retrieve
 * @param <E2>         the value type of the returned properties
 * @return the traversal with an appended {@link PropertyMapStep}.
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#propertymap-step" target="_blank">Reference Documentation - PropertyMap Step</a>
 * @since 3.0.0-incubating
 */
public default <E2> GraphTraversal<S, Map<String, E2>> propertyMap(final String... propertyKeys) {
    this.asAdmin().getBytecode().addStep(Symbols.propertyMap, propertyKeys);
    return this.asAdmin().addStep(new PropertyMapStep<>(this.asAdmin(), WithOptions.none, PropertyType.PROPERTY, propertyKeys));
}
 
Example #23
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Map the {@link Element} to the values of the associated properties given the provide property keys.
 * If no property keys are provided, then all property values are emitted.
 *
 * @param propertyKeys the properties to retrieve their value from
 * @param <E2>         the value type of the properties
 * @return the traversal with an appended {@link PropertiesStep}.
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#values-step" target="_blank">Reference Documentation - Values Step</a>
 * @since 3.0.0-incubating
 */
public default <E2> GraphTraversal<S, E2> values(final String... propertyKeys) {
    this.asAdmin().getBytecode().addStep(Symbols.values, propertyKeys);
    return this.asAdmin().addStep(new PropertiesStep<>(this.asAdmin(), PropertyType.VALUE, propertyKeys));
}
 
Example #24
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Map the {@link Element} to its associated properties given the provide property keys.
 * If no property keys are provided, then all properties are emitted.
 *
 * @param propertyKeys the properties to retrieve
 * @param <E2>         the value type of the returned properties
 * @return the traversal with an appended {@link PropertiesStep}.
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#properties-step" target="_blank">Reference Documentation - Properties Step</a>
 * @since 3.0.0-incubating
 */
public default <E2> GraphTraversal<S, ? extends Property<E2>> properties(final String... propertyKeys) {
    this.asAdmin().getBytecode().addStep(Symbols.properties, propertyKeys);
    return this.asAdmin().addStep(new PropertiesStep<>(this.asAdmin(), PropertyType.PROPERTY, propertyKeys));
}