/** * This is a helper method to get the base type of a vertex. * It first tried to get the base type via the label. * If this is not possible it then tries to get the base type via the Shard Edge. * * @param vertex The vertex to build a concept from * @return The base type of the vertex, if it is a valid concept. */ private Schema.BaseType getBaseType(VertexElement vertex){ try { return Schema.BaseType.valueOf(vertex.label()); } catch (IllegalArgumentException e){ //Base type appears to be invalid. Let's try getting the type via the shard edge Optional<VertexElement> type = vertex.getEdgesOfType(Direction.OUT, Schema.EdgeLabel.SHARD). map(EdgeElement::target).findAny(); if(type.isPresent()){ String label = type.get().label(); if(label.equals(Schema.BaseType.ENTITY_TYPE.name())) return Schema.BaseType.ENTITY; if(label.equals(RELATIONSHIP_TYPE.name())) return Schema.BaseType.RELATIONSHIP; if(label.equals(Schema.BaseType.ATTRIBUTE_TYPE.name())) return Schema.BaseType.ATTRIBUTE; } } throw new IllegalStateException("Could not determine the base type of vertex [" + vertex + "]"); }
private <T extends Concept> T validateSchemaConcept(Concept concept, Schema.BaseType baseType, Supplier<T> invalidHandler) { if (concept != null && baseType.getClassType().isInstance(concept)) { //noinspection unchecked return (T) concept; } else { return invalidHandler.get(); } }
break; default: throw GraknTxOperationException.unknownConcept(type.name());
} else if (!baseType.equals(schemaConcept.baseType())) { throw labelTaken(schemaConcept);
@Override public void safeExecute(final Vertex vertex, Messenger<String> messenger, final Memory memory) { switch (memory.getIteration()) { case 0: sendMessage(messenger, EMPTY_MESSAGE); break; case 1: // get degree first, as degree must >= k filterByDegree(vertex, messenger, memory, true); break; default: if (memory.<Boolean>get(CONNECTED_COMPONENT_STARTED)) { if (messenger.receiveMessages().hasNext()) { if (vertex.property(K_CORE_LABEL).isPresent()) { updateClusterLabel(vertex, messenger, memory); } else if (vertex.label().equals(Schema.BaseType.RELATIONSHIP.name())) { relayClusterLabel(messenger, memory); } } } else { // relay message through relationship vertices in even iterations // send message from regular entities in odd iterations if (atRelationships(memory)) { relayOrSaveMessages(vertex, messenger); } else { updateEntityAndAttribute(vertex, messenger, memory, false); } } break; } }
/** * Creates a new {@link VertexElement} with a {@link ConceptId} which can optionally be set. * * @param baseType The {@link Schema.BaseType} * @param conceptIds the optional {@link ConceptId} to set as the new {@link ConceptId} * @return a new {@link VertexElement} */ public VertexElement addVertexElement(Schema.BaseType baseType, ConceptId ... conceptIds) { Vertex vertex = tx.getTinkerPopGraph().addVertex(baseType.name()); String newConceptId = Schema.PREFIX_VERTEX + vertex.id().toString(); if(conceptIds.length > 1){ throw new IllegalArgumentException("Cannot provide more than one concept id when creating a new concept"); } else if (conceptIds.length == 1){ newConceptId = conceptIds[0].getValue(); } vertex.property(Schema.VertexProperty.ID.name(), newConceptId); tx.txCache().writeOccurred(); return new VertexElement(tx, vertex); } }
@Override public GraphTraversal<Vertex, ? extends Element> applyTraversalInner( GraphTraversal<Vertex, ? extends Element> traversal, EmbeddedGraknTx<?> graph, Collection<Var> vars) { GraphTraversal<Vertex, Vertex> vertexTraversal = Fragments.isVertex(traversal); if (mayHaveEdgeInstances()) { GraphTraversal<Vertex, Vertex> isImplicitRelationType = __.<Vertex>hasLabel(RELATIONSHIP_TYPE.name()).has(IS_IMPLICIT.name(), true); GraphTraversal<Vertex, Element> toVertexAndEdgeInstances = Fragments.union(ImmutableSet.of( toVertexInstances(__.identity()), toEdgeInstances() )); return choose(vertexTraversal, isImplicitRelationType, toVertexAndEdgeInstances, toVertexInstances(__.identity()) ); } else { return toVertexInstances(vertexTraversal); } }
static void filterByDegree(Vertex vertex, Messenger<String> messenger, Memory memory, boolean persistId) { if ((vertex.label().equals(Schema.BaseType.ENTITY.name()) || vertex.label().equals(Schema.BaseType.ATTRIBUTE.name())) && Iterators.size(messenger.receiveMessages()) >= memory.<Long>get(K)) { String id = vertex.value(Schema.VertexProperty.ID.name()); // coreness query doesn't require id if (persistId) { vertex.property(K_CORE_LABEL, id); } else { vertex.property(K_CORE_LABEL, true); } memory.add(K_CORE_EXIST, true); // send ids from now on, as we want to count connected entities, not relationships sendMessage(messenger, id); } }
static void relayOrSaveMessages(Vertex vertex, Messenger<String> messenger) { if (messenger.receiveMessages().hasNext()) { if (vertex.label().equals(Schema.BaseType.RELATIONSHIP.name())) { // relay the messages messenger.receiveMessages().forEachRemaining(msg -> sendMessage(messenger, msg)); } else if ((vertex.label().equals(Schema.BaseType.ENTITY.name()) || vertex.label().equals(Schema.BaseType.ATTRIBUTE.name())) && vertex.property(K_CORE_LABEL).isPresent()) { // messages received via implicit edge, save the count for next iteration vertex.property(IMPLICIT_MESSAGE_COUNT, (long) newHashSet(messenger.receiveMessages()).size()); } } }
/** * * @param label The {@link AttributeType} label * @param value The value of the {@link Attribute} * @return A unique id for the {@link Attribute} */ @CheckReturnValue public static String generateAttributeIndex(Label label, String value){ return Schema.BaseType.ATTRIBUTE.name() + "-" + label + "-" + value; } }
static void degreeStatisticsStepResourceRelation(Vertex vertex, Messenger<Long> messenger, Set<LabelId> ofLabelIds) { if (messenger.receiveMessages().hasNext()) { if (vertex.label().equals(Schema.BaseType.RELATIONSHIP.name())) { messenger.sendMessage(messageScopeOut, 1L); } else if (ofLabelIds.contains(Utility.getVertexTypeId(vertex))) { vertex.property(DEGREE, getMessageCount(messenger)); } } }
@Override public GraphTraversal<Vertex, ? extends Element> applyTraversalInner( GraphTraversal<Vertex, ? extends Element> traversal, EmbeddedGraknTx<?> graph, Collection<Var> vars) { return traversal.not(__.hasLabel(Schema.BaseType.SHARD.name())); }
static <T> GraphTraversal<T, Vertex> outSubs(GraphTraversal<T, Vertex> traversal) { // These traversals make sure to only navigate types by checking they do not have a `THING_TYPE_LABEL_ID` property return union(traversal, ImmutableSet.of( __.<Vertex>not(__.has(THING_TYPE_LABEL_ID.name())).not(__.hasLabel(Schema.BaseType.SHARD.name())), __.repeat(__.out(SUB.getLabel())).emit() )).unfold(); }
/** * * @return The base type of this concept which helps us identify the concept */ public Schema.BaseType baseType(){ return Schema.BaseType.valueOf(vertex().label()); }
static <T> GraphTraversal<T, Vertex> inSubs(GraphTraversal<T, Vertex> traversal) { // These traversals make sure to only navigate types by checking they do not have a `THING_TYPE_LABEL_ID` property return union(traversal, ImmutableSet.of( __.<Vertex>not(__.has(THING_TYPE_LABEL_ID.name())).not(__.hasLabel(Schema.BaseType.SHARD.name())), __.repeat(__.in(SUB.getLabel())).emit() )).unfold(); }