public boolean isValid(){ return getValue() != -1; }
MetaSchema(String s, int i) { label = Label.of(s); id = LabelId.of(i); }
private Set<Integer> implicitLabelsToIds(Set<Label> labels, Set<Schema.ImplicitType> implicitTypes){ return labels.stream() .flatMap(label -> implicitTypes.stream().map(it -> it.getLabel(label))) .map(label -> vertex().tx().convertToId(label)) .filter(id -> !id.equals(LabelId.invalid())) .map(LabelId::getValue) .collect(toSet()); }
/** * The Grakn type property on a given Tinkerpop vertex. * If the vertex is a {@link SchemaConcept}, return invalid {@link Label}. * * @param vertex the Tinkerpop vertex * @return the type */ static LabelId getVertexTypeId(Vertex vertex) { if (vertex.property(Schema.VertexProperty.THING_TYPE_LABEL_ID.name()).isPresent()) { return LabelId.of(vertex.value(Schema.VertexProperty.THING_TYPE_LABEL_ID.name())); } return LabelId.invalid(); }
static void degreeStatisticsStepResourceOwner(Vertex vertex, Messenger<Long> messenger, Set<LabelId> ofLabelIds) { LabelId labelId = Utility.getVertexTypeId(vertex); if (labelId.isValid() && !ofLabelIds.contains(labelId)) { messenger.sendMessage(messageScopeShortcutIn, 1L); messenger.sendMessage(messageScopeResourceOut, 1L); } }
private Stream<Relationship> edgeRelations(Role... roles){ Set<Role> roleSet = new HashSet<>(Arrays.asList(roles)); Stream<EdgeElement> stream = vertex().getEdgesOfType(Direction.BOTH, Schema.EdgeLabel.ATTRIBUTE); if(!roleSet.isEmpty()){ stream = stream.filter(edge -> { Role roleOwner = vertex().tx().getSchemaConcept(LabelId.of(edge.property(Schema.EdgeProperty.RELATIONSHIP_ROLE_OWNER_LABEL_ID))); return roleSet.contains(roleOwner); }); } return stream.map(edge -> vertex().tx().factory().buildRelation(edge)); }
/** * Converts a Type Label into a type Id for this specific graph. Mapping labels to ids will differ between graphs * so be sure to use the correct graph when performing the mapping. * * @param label The label to be converted to the id * @return The matching type id */ public LabelId convertToId(Label label) { if (txCache().isLabelCached(label)) { return txCache().convertLabelToId(label); } return LabelId.invalid(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof LabelId) { LabelId that = (LabelId) o; return (this.value.equals(that.getValue())); } return false; }
/** * Run Graql compute count query * * @return a Answer object containing the count value */ private Stream<Value> runComputeCount() { if (!scopeContainsInstance()) { LOG.debug("Count = 0"); return Stream.of(new Value(0)); } Set<LabelId> typeLabelIds = convertLabelsToIds(scopeTypeLabels()); Map<Integer, Long> count; Set<LabelId> rolePlayerLabelIds = getRolePlayerLabelIds(); rolePlayerLabelIds.addAll(typeLabelIds); ComputerResult result = compute( new CountVertexProgram(), new CountMapReduceWithAttribute(), rolePlayerLabelIds, false); count = result.memory().get(CountMapReduceWithAttribute.class.getName()); long finalCount = count.keySet().stream() .filter(id -> typeLabelIds.contains(LabelId.of(id))) .mapToLong(count::get).sum(); if (count.containsKey(GraknMapReduce.RESERVED_TYPE_LABEL_KEY)) { finalCount += count.get(GraknMapReduce.RESERVED_TYPE_LABEL_KEY); } LOG.debug("Count = " + finalCount); return Stream.of(new Value(finalCount)); }
static void applyLabelsToTraversal( GraphTraversal<?, Edge> traversal, Schema.EdgeProperty property, @Nullable Set<Label> typeLabels, EmbeddedGraknTx<?> tx) { if (typeLabels != null) { Set<Integer> typeIds = typeLabels.stream().map(label -> tx.convertToId(label).getValue()).collect(toSet()); traversal.has(property.name(), P.within(typeIds)); } }
static boolean vertexHasSelectedTypeId(Vertex vertex, Set<LabelId> selectedTypeIds) { return vertex.property(Schema.VertexProperty.THING_TYPE_LABEL_ID.name()).isPresent() && selectedTypeIds.contains(LabelId.of(vertex.value(Schema.VertexProperty.THING_TYPE_LABEL_ID.name()))); }
@Override public GraphTraversal<Vertex, ? extends Element> applyTraversalInner( GraphTraversal<Vertex, ? extends Element> traversal, EmbeddedGraknTx<?> tx, Collection<Var> vars) { Set<Integer> labelIds = labels().stream().map(label -> tx.convertToId(label).getValue()).collect(toSet()); if (labelIds.size() == 1) { int labelId = Iterables.getOnlyElement(labelIds); return traversal.has(LABEL_ID.name(), labelId); } else { return traversal.has(LABEL_ID.name(), P.within(labelIds)); } }
/** * Gets and increments the current available type id. * * @return the current available Grakn id which can be used for types */ private LabelId getNextId() { TypeImpl<?, ?> metaConcept = (TypeImpl<?, ?>) getMetaConcept(); Integer currentValue = metaConcept.vertex().property(Schema.VertexProperty.CURRENT_LABEL_ID); if (currentValue == null) { currentValue = Schema.MetaSchema.values().length + 1; } else { currentValue = currentValue + 1; } //Vertex is used directly here to bypass meta type mutation check metaConcept.property(Schema.VertexProperty.CURRENT_LABEL_ID, currentValue); return LabelId.of(currentValue); }
private RelationshipEdge(RelationshipType relationshipType, Role ownerRole, Role valueRole, EdgeElement edgeElement) { this(edgeElement); edgeElement.propertyImmutable(Schema.EdgeProperty.RELATIONSHIP_ROLE_OWNER_LABEL_ID, ownerRole, null, o -> o.labelId().getValue()); edgeElement.propertyImmutable(Schema.EdgeProperty.RELATIONSHIP_ROLE_VALUE_LABEL_ID, valueRole, null, v -> v.labelId().getValue()); edgeElement.propertyImmutable(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID, relationshipType, null, t -> t.labelId().getValue()); this.relationType.set(relationshipType); this.ownerRole.set(ownerRole); this.valueRole.set(valueRole); }
/** * Adds a new type vertex which occupies a grakn id. This result in the grakn id count on the meta concept to be * incremented. * * @param label The label of the new type vertex * @param baseType The base type of the new type * @return The new type vertex */ private VertexElement addTypeVertex(LabelId id, Label label, Schema.BaseType baseType) { VertexElement vertexElement = addVertexElement(baseType); vertexElement.property(Schema.VertexProperty.SCHEMA_LABEL, label.getValue()); vertexElement.property(Schema.VertexProperty.LABEL_ID, id.getValue()); return vertexElement; }
/** * * @param type The type of this concept */ private void setInternalType(Type type){ cachedInternalType.set(type.label()); vertex().property(Schema.VertexProperty.THING_TYPE_LABEL_ID, type.labelId().getValue()); }
/** * Castings are retrieved from the perspective of the {@link Relationship} * * @param roles The {@link Role} which the {@link Thing}s are playing * @return The {@link Casting} which unify a {@link Role} and {@link Thing} with this {@link Relationship} */ public Stream<Casting> castingsRelation(Role... roles){ Set<Role> roleSet = new HashSet<>(Arrays.asList(roles)); if(roleSet.isEmpty()){ return vertex().getEdgesOfType(Direction.OUT, Schema.EdgeLabel.ROLE_PLAYER). map(edge -> Casting.withRelationship(edge, owner)); } //Traversal is used so we can potentially optimise on the index Set<Integer> roleTypesIds = roleSet.stream().map(r -> r.labelId().getValue()).collect(Collectors.toSet()); return vertex().tx().getTinkerTraversal().V(). has(Schema.VertexProperty.ID.name(), id().getValue()). outE(Schema.EdgeLabel.ROLE_PLAYER.getLabel()). has(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID.name(), type().labelId().getValue()). has(Schema.EdgeProperty.ROLE_LABEL_ID.name(), P.within(roleTypesIds)). toStream(). map(edge -> vertex().tx().factory().buildEdgeElement(edge)). map(edge -> Casting.withRelationship(edge, owner)); }
private Stream<Relationship> reifiedRelations(Role... roles){ GraphTraversal<Vertex, Vertex> traversal = vertex().tx().getTinkerTraversal().V(). has(Schema.VertexProperty.ID.name(), id().getValue()); if(roles.length == 0){ traversal.in(Schema.EdgeLabel.ROLE_PLAYER.getLabel()); } else { Set<Integer> roleTypesIds = Arrays.stream(roles).map(r -> r.labelId().getValue()).collect(toSet()); traversal.inE(Schema.EdgeLabel.ROLE_PLAYER.getLabel()). has(Schema.EdgeProperty.ROLE_LABEL_ID.name(), P.within(roleTypesIds)).outV(); } return traversal.toStream().map(vertex -> vertex().tx().<Relationship>buildConcept(vertex)); }