Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal#hasLabel()

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal#hasLabel() . 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: ElementGraph.java    From gremlin-ogm with Apache License 2.0 6 votes vote down vote up
/**
 * Find the given element using it's id, if it has one, or all of it's properties.
 */
protected <E extends Element> GraphTraversal find(E element) {
  GraphTraversal traversal = g.V();
  if (element.id() != null) {
    traversal = traversal.hasId(element.id());
  } else {
    traversal = traversal.hasLabel(element.label());
    Object[] properties = Properties.id(element);
    if (properties == null || properties.length == 0) {
      properties = Properties.all(element);
    }
    for (Property property : list(properties)) {
      traversal = traversal.has(property.key(), property.value());
    }
  }
  return traversal;
}
 
Example 2
Source File: TraversalBuilder.java    From sql-gremlin with Apache License 2.0 6 votes vote down vote up
public static GraphTraversal toTraversal(List<RelNode> relList) {
    final GraphTraversal traversal = __.identity();
    TableDef tableDef = null;
    for(RelNode rel : relList) {
        if(rel instanceof GremlinTableScan) {
            GremlinTableScan tableScan = (GremlinTableScan) rel;
            tableDef = tableScan.getGremlinTable().getTableDef();
            traversal.hasLabel(tableDef.label);
        } if(rel instanceof GremlinFilter) {
            GremlinFilter filter = (GremlinFilter) rel;
            RexNode condition = filter.getCondition();
            FilterTranslator translator = new FilterTranslator(tableDef, filter.getRowType().getFieldNames());
            GraphTraversal predicates = translator.translateMatch(condition);
            for(Step step : (List<Step>)predicates.asAdmin().getSteps()) {
                traversal.asAdmin().addStep(step);
            }
        }
    }
    return traversal;
}
 
Example 3
Source File: SpecifiedLabels.java    From amazon-neptune-tools with Apache License 2.0 5 votes vote down vote up
@Override
public GraphTraversal<? extends Element, ?> apply(GraphTraversal<? extends Element, ?> traversal) {
    String firstLabel = labels.stream().findFirst().orElse(null);
    String[] remainingLabels = labels.stream().skip(1).collect(Collectors.toList()).toArray(new String[]{});

    return traversal.hasLabel(firstLabel, remainingLabels);
}
 
Example 4
Source File: KillrVideoTraversalSourceDsl.java    From graph-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Gets movies by their title.
 */
public GraphTraversal<Vertex, Vertex> movies(String title, String... additionalTitles) {
    List<String> titles = Stream.concat(Stream.of(title), Stream.of(additionalTitles)).collect(Collectors.toList());
    GraphTraversal traversal = this.clone().V();
    traversal = traversal.hasLabel(VERTEX_MOVIE);
    if (titles.size() == 1)
        traversal = traversal.has(KEY_TITLE, titles.get(0));
    else if (titles.size() > 1)
        traversal = traversal.has(KEY_TITLE, P.within(titles));

    return traversal;
}
 
Example 5
Source File: KillrVideoTraversalSourceDsl.java    From graph-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Gets users by their identifier.
 */
public GraphTraversal<Vertex, Vertex> users(String userId, String... additionalUserIds) {
    List<String> userIds = Stream.concat(Stream.of(userId), Stream.of(additionalUserIds)).collect(Collectors.toList());
    GraphTraversal traversal = this.clone().V();
    traversal = traversal.hasLabel(VERTEX_USER);
    if (userIds.size() == 1)
        traversal = traversal.has(KEY_USER_ID, userIds.get(0));
    else if (userIds.size() > 1)
        traversal = traversal.has(KEY_USER_ID, P.within(userIds));

    return traversal;
}
 
Example 6
Source File: HasKeys.java    From gremlin-ogm with Apache License 2.0 5 votes vote down vote up
@Override
@SneakyThrows
public GraphTraversal<Element, Element> apply(GraphTraversal<Element, Element> traversal) {
  traversal.hasLabel(element.label());
  List<Field> fields;
  if (keyType != null) {
    if (is(keyType, PrimaryKey.class)) {
      fields = primaryKeyFields(element);
    } else if (is(keyType, OrderingKey.class)) {
      fields = orderingKeyFields(element);
    } else {
      throw org.apache.tinkerpop.gremlin.object.structure.Element.Exceptions
          .invalidAnnotationType(keyType);
    }
  } else {
    fields = keyFields(element);
  }
  for (Field field : fields) {
    String key = propertyKey(field);
    Object value = propertyValue(field, element);
    if (isMissing(value)) {
      throw org.apache.tinkerpop.gremlin.object.structure.Element.Exceptions.requiredKeysMissing(
          element.getClass(), key);
    }
    traversal.has(key, value);
  }
  return traversal;
}
 
Example 7
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 8
Source File: SocialTraversalSourceDsl.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Starts a traversal that finds all vertices with a "person" label and optionally allows filtering of those
 * vertices on the "name" property.
 *
 * @param names list of person names to filter on
 */
public GraphTraversal<Vertex, Vertex> persons(String... names) {
    GraphTraversalSource clone = this.clone();

    // Manually add a "start" step for the traversal in this case the equivalent of V(). GraphStep is marked
    // as a "start" step by passing "true" in the constructor.
    clone.getBytecode().addStep(GraphTraversal.Symbols.V);
    GraphTraversal<Vertex, Vertex> traversal = new DefaultGraphTraversal<>(clone);
    traversal.asAdmin().addStep(new GraphStep<>(traversal.asAdmin(), Vertex.class, true));

    traversal = traversal.hasLabel("person");
    if (names.length > 0) traversal = traversal.has("name", P.within(names));

    return traversal;
}
 
Example 9
Source File: SocialPackageTraversalSourceDsl.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public GraphTraversal<Vertex, Vertex> persons(String... names) {
    GraphTraversalSource clone = this.clone();

    clone.getBytecode().addStep(GraphTraversal.Symbols.V);
    GraphTraversal<Vertex, Vertex> traversal = new DefaultGraphTraversal<>(clone);
    traversal.asAdmin().addStep(new GraphStep<>(traversal.asAdmin(), Vertex.class, true));

    traversal = traversal.hasLabel("person");
    if (names.length > 0) traversal = traversal.has("name", P.within(names));

    return traversal;
}
 
Example 10
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 11
Source File: VertexAPI.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@GET
@Timed
@Compress
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=vertex_read"})
public String list(@Context GraphManager manager,
                   @PathParam("graph") String graph,
                   @QueryParam("label") String label,
                   @QueryParam("properties") String properties,
                   @QueryParam("keep_start_p")
                   @DefaultValue("false") boolean keepStartP,
                   @QueryParam("offset") @DefaultValue("0") long offset,
                   @QueryParam("page") String page,
                   @QueryParam("limit") @DefaultValue("100") long limit) {
    LOG.debug("Graph [{}] query vertices by label: {}, properties: {}, " +
              "offset: {}, page: {}, limit: {}",
              graph, label, properties, offset, page, limit);

    Map<String, Object> props = parseProperties(properties);
    if (page != null) {
        E.checkArgument(offset == 0,
                        "Not support querying vertices based on paging " +
                        "and offset together");
    }

    HugeGraph g = graph(manager, graph);

    GraphTraversal<Vertex, Vertex> traversal = g.traversal().V();
    if (label != null) {
        traversal = traversal.hasLabel(label);
    }

    // Convert relational operator like P.gt()/P.lt()
    for (Map.Entry<String, Object> prop : props.entrySet()) {
        Object value = prop.getValue();
        if (!keepStartP && value instanceof String &&
            ((String) value).startsWith(TraversalUtil.P_CALL)) {
            prop.setValue(TraversalUtil.parsePredicate((String) value));
        }
    }

    for (Map.Entry<String, Object> entry : props.entrySet()) {
        traversal = traversal.has(entry.getKey(), entry.getValue());
    }

    if (page == null) {
        traversal = traversal.range(offset, offset + limit);
    } else {
        traversal = traversal.has(QueryHolder.SYSPROP_PAGE, page)
                             .limit(limit);
    }

    try {
        return manager.serializer(g).writeVertices(traversal,
                                                   page != null);
    } finally {
        if (g.tx().isOpen()) {
            g.tx().close();
        }
    }
}
 
Example 12
Source File: HasLabel.java    From gremlin-ogm with Apache License 2.0 4 votes vote down vote up
@Override
public GraphTraversal<Element, Element> apply(GraphTraversal<Element, Element> traversal) {
  return traversal.hasLabel(label);
}