@Override public void safeExecute(Vertex vertex, Messenger<VertexMessage> messenger, final Memory memory) { String vertexId = this.<String>get(vertex, Schema.VertexProperty.ID.name()).get();
errors.add(ErrorMessage.VALIDATION_RULE_MISSING_ELEMENTS.getMessage(side, rule.label(), typeLabel)); } else { if(Schema.VertexProperty.RULE_WHEN.equals(side)){ if (schemaConcept.isType()){ RuleImpl.from(rule).addHypothesis(schemaConcept.asType()); } else if (Schema.VertexProperty.RULE_THEN.equals(side)){ if (schemaConcept.isType()) { RuleImpl.from(rule).addConclusion(schemaConcept.asType());
private static void makePropertyKeys(JanusGraphManagement management){ stream(Schema.VertexProperty.values()).forEach(property -> makePropertyKey(management, property.name(), property.getDataType())); stream(Schema.EdgeProperty.values()).forEach(property -> makePropertyKey(management, property.name(), property.getDataType())); }
has(Schema.VertexProperty.ID.name(), id().getValue()). union(shortcutTraversal, attributeEdgeTraversal).toStream(). map(vertex -> vertex().tx().<X>buildConcept(vertex));
/** * 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); }
static void updateEntityAndAttribute(Vertex vertex, Messenger<String> messenger, Memory memory, boolean persistMessageCount) { if (vertex.property(K_CORE_LABEL).isPresent()) { String id = vertex.value(Schema.VertexProperty.ID.name()); long messageCount = (long) getMessageCountExcludeSelf(messenger, id); if (vertex.property(IMPLICIT_MESSAGE_COUNT).isPresent()) { messageCount += vertex.<Long>value(IMPLICIT_MESSAGE_COUNT); // need to remove implicit count as the vertex may not receive msg via implicit edge vertex.property(IMPLICIT_MESSAGE_COUNT).remove(); } if (messageCount >= memory.<Long>get(K)) { LOGGER.trace("Sending msg from " + id); sendMessage(messenger, id); memory.add(K_CORE_EXIST, true); if (persistMessageCount) { // message count may help eliminate unqualified vertex in earlier iterations vertex.property(MESSAGE_COUNT, messageCount); } } else { LOGGER.trace("Removing label of " + id); vertex.property(K_CORE_LABEL).remove(); memory.add(K_CORE_STABLE, false); } } }
/** * 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)); }
/** * 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); } }
private void applyFilters(Set<LabelId> types, boolean includesRolePlayerEdge) { if (types == null || types.isEmpty()) return; Set<Integer> labelIds = types.stream().map(LabelId::getValue).collect(Collectors.toSet()); Traversal<Vertex, Vertex> vertexFilter = __.has(Schema.VertexProperty.THING_TYPE_LABEL_ID.name(), P.within(labelIds)); Traversal<Vertex, Edge> edgeFilter; if (filterAllEdges) { edgeFilter = __.<Vertex>bothE().limit(0); } else { edgeFilter = includesRolePlayerEdge ? __.union( __.<Vertex>bothE(Schema.EdgeLabel.ROLE_PLAYER.getLabel()), __.<Vertex>bothE(Schema.EdgeLabel.ATTRIBUTE.getLabel()) .has(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID.name(), P.within(labelIds))) : __.<Vertex>bothE(Schema.EdgeLabel.ATTRIBUTE.getLabel()) .has(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID.name(), P.within(labelIds)); } graphComputer.vertices(vertexFilter).edges(edgeFilter); } }
@Override public final <S, E> GraphTraversal<S, E> applyPredicate(GraphTraversal<S, E> traversal) { var.ifPresent(theVar -> { // Compare to another variable String thisVar = UUID.randomUUID().toString(); Var otherVar = theVar.var(); String otherValue = UUID.randomUUID().toString(); Traversal[] traversals = Stream.of(VALUE_PROPERTIES) .map(prop -> __.values(prop).as(otherValue).select(thisVar).values(prop).where(gremlinPredicate(otherValue))) .toArray(Traversal[]::new); traversal.as(thisVar).select(otherVar.name()).or(traversals).select(thisVar); }); persistedValue().ifPresent(theValue -> { // Compare to a given value DataType<?> dataType = DataType.SUPPORTED_TYPES.get(value().get().getClass().getTypeName()); Schema.VertexProperty property = dataType.getVertexProperty(); traversal.has(property.name(), gremlinPredicate(theValue)); }); return traversal; }
@Override public void setup(final Memory memory) { LOGGER.debug("MedianVertexProgram Started !!!!!!!!"); memory.set(COUNT, 0L); memory.set(LABEL_SELECTED, memory.getIteration()); memory.set(NEGATIVE_COUNT, 0L); memory.set(POSITIVE_COUNT, 0L); memory.set(FOUND, false); if (persistentProperties.get(RESOURCE_DATA_TYPE).equals(Schema.VertexProperty.VALUE_LONG.name())) { memory.set(MEDIAN, 0L); memory.set(PIVOT, 0L); memory.set(PIVOT_NEGATIVE, 0L); memory.set(PIVOT_POSITIVE, 0L); } else { memory.set(MEDIAN, 0D); memory.set(PIVOT, 0D); memory.set(PIVOT_NEGATIVE, 0D); memory.set(PIVOT_POSITIVE, 0D); } }
@Override public void safeMap(final Vertex vertex, final MapEmitter<Serializable, Number> emitter) { if (usingLong()) { if (resourceIsValid(vertex)) { emitter.emit(NullObject.instance(), ((Long) vertex.value(Schema.VertexProperty.VALUE_LONG.name())) * ((Long) vertex.value(degreePropertyKey))); return; } emitter.emit(NullObject.instance(), 0L); } else { if (resourceIsValid(vertex)) { emitter.emit(NullObject.instance(), ((Double) vertex.value(Schema.VertexProperty.VALUE_DOUBLE.name())) * ((Long) vertex.value(degreePropertyKey))); return; } emitter.emit(NullObject.instance(), 0D); } }
@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); } }
private GraphTraversal<Vertex, Edge> toEdgeInstances() { Var type = var(); Var labelId = var(); // There is no fast way to retrieve all edge instances, because edges cannot be globally indexed. // This is a best-effort, that uses the schema to limit the search space... // First retrieve the type ID GraphTraversal<Vertex, Vertex> traversal = __.<Vertex>as(type.name()).values(LABEL_ID.name()).as(labelId.name()).select(type.name()); // Next, navigate the schema to all possible types whose instances can be in this relation traversal = Fragments.inSubs(traversal.out(RELATES.getLabel()).in(PLAYS.getLabel())); // Navigate to all (vertex) instances of those types // (we do not need to navigate to edge instances, because edge instances cannot be role-players) traversal = toVertexInstances(traversal); // Finally, navigate to all relation edges with the correct type attached to these instances return traversal.outE(ATTRIBUTE.getLabel()) .has(RELATIONSHIP_TYPE_LABEL_ID.name(), __.where(P.eq(labelId.name()))); }
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); } }
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)); }); }
/** * @param key The concept property tp search by. * @param value The value of the concept * @return A concept with the matching key and value */ //----------------------------------------------General Functionality----------------------------------------------- public <T extends Concept> Optional<T> getConcept(Schema.VertexProperty key, Object value) { Iterator<Vertex> vertices = getTinkerTraversal().V().has(key.name(), value); if (vertices.hasNext()) { Vertex vertex = vertices.next(); return Optional.of(factory().buildConcept(vertex)); } else { return Optional.empty(); } }
/** * @param to the target {@link VertexElement} * @param type the type of the edge to create */ public EdgeElement putEdge(VertexElement to, Schema.EdgeLabel type){ GraphTraversal<Vertex, Edge> traversal = tx().getTinkerTraversal().V(). has(Schema.VertexProperty.ID.name(), id().getValue()). outE(type.getLabel()).as("edge").otherV(). has(Schema.VertexProperty.ID.name(), to.id().getValue()).select("edge"); if(!traversal.hasNext()) { return addEdge(to, type); } else { return tx().factory().buildEdgeElement(traversal.next()); } }
/** * The state of the vertex in the database. This may detect ghost nodes and allow them to be excluded from * computations. If the vertex is alive it is likely to be a valid Grakn concept. * * @return if the vertex is alive */ static boolean isAlive(Vertex vertex) { if (vertex == null) return false; try { return vertex.property(Schema.VertexProperty.ID.name()).isPresent(); } catch (IllegalStateException e) { return false; } }
@Override public boolean isTinkerPopGraphClosed() { return !rootGraph.traversal().V().has(Schema.VertexProperty.SCHEMA_LABEL.name(), Schema.MetaSchema.ENTITY.getLabel().getValue()).hasNext(); }