use of ai.grakn.kb.internal.structure.EdgeElement in project grakn by graknlabs.
the class RelationshipReified method putRolePlayerEdge.
/**
* 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.getId().getValue()).outE(Schema.EdgeLabel.ROLE_PLAYER.getLabel()).has(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID.name(), this.type().getLabelId().getValue()).has(Schema.EdgeProperty.ROLE_LABEL_ID.name(), role.getLabelId().getValue()).as("edge").inV().has(Schema.VertexProperty.ID.name(), toThing.getId()).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().getLabelId().getValue());
edge.property(Schema.EdgeProperty.ROLE_LABEL_ID, role.getLabelId().getValue());
Casting casting = Casting.create(edge, owner, role, toThing);
vertex().tx().txCache().trackForValidation(casting);
}
use of ai.grakn.kb.internal.structure.EdgeElement in project grakn by graknlabs.
the class SchemaConceptTest method whenSpecifyingTheResourceTypeOfAnEntityType_EnsureTheImplicitStructureIsCreated.
@Test
public void whenSpecifyingTheResourceTypeOfAnEntityType_EnsureTheImplicitStructureIsCreated() {
Label resourceLabel = Label.of("Attribute Type");
EntityType entityType = tx.putEntityType("Entity1");
AttributeType attributeType = tx.putAttributeType("Attribute Type", AttributeType.DataType.STRING);
// Implicit Names
Label hasResourceOwnerLabel = Schema.ImplicitType.HAS_OWNER.getLabel(resourceLabel);
Label hasResourceValueLabel = Schema.ImplicitType.HAS_VALUE.getLabel(resourceLabel);
Label hasResourceLabel = Schema.ImplicitType.HAS.getLabel(resourceLabel);
entityType.attribute(attributeType);
RelationshipType relationshipType = tx.getRelationshipType(hasResourceLabel.getValue());
Assert.assertEquals(hasResourceLabel, relationshipType.getLabel());
Set<Label> roleLabels = relationshipType.relates().map(SchemaConcept::getLabel).collect(toSet());
assertThat(roleLabels, containsInAnyOrder(hasResourceOwnerLabel, hasResourceValueLabel));
assertThat(entityType.plays().collect(toSet()), containsInAnyOrder(tx.getRole(hasResourceOwnerLabel.getValue())));
assertThat(attributeType.plays().collect(toSet()), containsInAnyOrder(tx.getRole(hasResourceValueLabel.getValue())));
// Check everything is implicit
assertTrue(relationshipType.isImplicit());
relationshipType.relates().forEach(role -> assertTrue(role.isImplicit()));
// Check that resource is not required
EdgeElement entityPlays = ((EntityTypeImpl) entityType).vertex().getEdgesOfType(Direction.OUT, Schema.EdgeLabel.PLAYS).iterator().next();
assertFalse(entityPlays.propertyBoolean(Schema.EdgeProperty.REQUIRED));
EdgeElement resourcePlays = ((AttributeTypeImpl<?>) attributeType).vertex().getEdgesOfType(Direction.OUT, Schema.EdgeLabel.PLAYS).iterator().next();
assertFalse(resourcePlays.propertyBoolean(Schema.EdgeProperty.REQUIRED));
}
use of ai.grakn.kb.internal.structure.EdgeElement in project grakn by graknlabs.
the class ConceptTest method whenGettingEdgesFromAConcept_EdgesFilteredByLabelAreReturned.
@Test
public void whenGettingEdgesFromAConcept_EdgesFilteredByLabelAreReturned() {
EntityType entityType1 = tx.putEntityType("entity type");
EntityTypeImpl entityType2 = (EntityTypeImpl) tx.putEntityType("entity type 1").sup(entityType1);
EntityType entityType3 = tx.putEntityType("entity type 2").sup(entityType2);
Set<EdgeElement> superType = entityType2.vertex().getEdgesOfType(Direction.OUT, Schema.EdgeLabel.SUB).collect(Collectors.toSet());
Set<EdgeElement> subs = entityType2.vertex().getEdgesOfType(Direction.IN, Schema.EdgeLabel.SUB).collect(Collectors.toSet());
assertThat(superType, is(not(empty())));
assertThat(subs, is(not(empty())));
superType.forEach(edge -> assertEquals(entityType1, tx.factory().buildConcept(edge.target())));
subs.forEach(edge -> assertEquals(entityType3, tx.factory().buildConcept(edge.source())));
}
use of ai.grakn.kb.internal.structure.EdgeElement in project grakn by graknlabs.
the class ThingImpl method attributeRelationship.
private Relationship attributeRelationship(Attribute attribute, boolean isInferred) {
Schema.ImplicitType has = Schema.ImplicitType.HAS;
Schema.ImplicitType hasValue = Schema.ImplicitType.HAS_VALUE;
Schema.ImplicitType hasOwner = Schema.ImplicitType.HAS_OWNER;
// Is this attribute a key to me?
if (type().keys().anyMatch(rt -> rt.equals(attribute.type()))) {
has = Schema.ImplicitType.KEY;
hasValue = Schema.ImplicitType.KEY_VALUE;
hasOwner = Schema.ImplicitType.KEY_OWNER;
}
Label label = attribute.type().getLabel();
RelationshipType hasAttribute = vertex().tx().getSchemaConcept(has.getLabel(label));
Role hasAttributeOwner = vertex().tx().getSchemaConcept(hasOwner.getLabel(label));
Role hasAttributeValue = vertex().tx().getSchemaConcept(hasValue.getLabel(label));
if (hasAttribute == null || hasAttributeOwner == null || hasAttributeValue == null || type().plays().noneMatch(play -> play.equals(hasAttributeOwner))) {
throw GraknTxOperationException.hasNotAllowed(this, attribute);
}
EdgeElement attributeEdge = addEdge(AttributeImpl.from(attribute), Schema.EdgeLabel.ATTRIBUTE);
if (isInferred)
attributeEdge.property(Schema.EdgeProperty.IS_INFERRED, true);
return vertex().tx().factory().buildRelation(attributeEdge, hasAttribute, hasAttributeOwner, hasAttributeValue);
}
use of ai.grakn.kb.internal.structure.EdgeElement in project grakn by graknlabs.
the class TypeImpl method plays.
public T plays(Role role, boolean required) {
checkSchemaMutationAllowed();
// Update the internal cache of role types played
cachedDirectPlays.ifPresent(map -> map.put(role, required));
// Update the cache of types played by the role
((RoleImpl) role).addCachedDirectPlaysByType(this);
EdgeElement edge = putEdge(ConceptVertex.from(role), Schema.EdgeLabel.PLAYS);
if (required) {
edge.property(Schema.EdgeProperty.REQUIRED, true);
}
return getThis();
}
Aggregations