public boolean isValid(){ return getValue() != -1; }
@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; }
@Nullable public <T extends SchemaConcept> T getSchemaConcept(LabelId id) { if (!id.isValid()) return null; return this.<T>getConcept(Schema.VertexProperty.LABEL_ID, id.getValue()).orElse(null); }
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)); } }
@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)); } }
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)); }
private Stream<Relationship> relationEdges(){ //Unfortunately this is a slow process return roles(). flatMap(Role::players). flatMap(type ->{ //Traversal is used here to take advantage of vertex centric index return vertex().tx().getTinkerTraversal().V(). has(Schema.VertexProperty.ID.name(), type.id().getValue()). in(Schema.EdgeLabel.SHARD.getLabel()). in(Schema.EdgeLabel.ISA.getLabel()). outE(Schema.EdgeLabel.ATTRIBUTE.getLabel()). has(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID.name(), labelId().getValue()). toStream(). map(edge -> vertex().tx().factory().<Relationship>buildConcept(edge)); }); }
/** * If the edge does not exist then it adds a {@link Schema.EdgeLabel#ROLE_PLAYER} edge from * this {@link Relationship} to a target {@link Thing} which is playing some {@link Role}. * * If the edge does exist nothing is done. * * @param role The {@link Role} being played by the {@link Thing} in this {@link Relationship} * @param toThing The {@link Thing} playing a {@link Role} in this {@link Relationship} */ public void putRolePlayerEdge(Role role, Thing toThing) { //Checking if the edge exists GraphTraversal<Vertex, Edge> traversal = vertex().tx().getTinkerTraversal().V(). has(Schema.VertexProperty.ID.name(), this.id().getValue()). outE(Schema.EdgeLabel.ROLE_PLAYER.getLabel()). has(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID.name(), this.type().labelId().getValue()). has(Schema.EdgeProperty.ROLE_LABEL_ID.name(), role.labelId().getValue()). as("edge"). inV(). has(Schema.VertexProperty.ID.name(), toThing.id()). select("edge"); if(traversal.hasNext()){ return; } //Role player edge does not exist create a new one EdgeElement edge = this.addEdge(ConceptVertex.from(toThing), Schema.EdgeLabel.ROLE_PLAYER); edge.property(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID, this.type().labelId().getValue()); edge.property(Schema.EdgeProperty.ROLE_LABEL_ID, role.labelId().getValue()); Casting casting = Casting.create(edge, owner, role, toThing); vertex().tx().txCache().trackForValidation(casting); }