Search in sources :

Example 36 with RelationshipType

use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.

the class ValidatorTest method whenCommittingRelationWithoutSpecifyingSchema_ThrowOnCommit.

@Test
public void whenCommittingRelationWithoutSpecifyingSchema_ThrowOnCommit() {
    EntityType fakeType = tx.putEntityType("Fake Concept");
    RelationshipType relationshipType = tx.putRelationshipType("kicks");
    Role kicker = tx.putRole("kicker");
    Role kickee = tx.putRole("kickee");
    Thing kyle = fakeType.addEntity();
    Thing icke = fakeType.addEntity();
    relationshipType.addRelationship().addRolePlayer(kicker, kyle).addRolePlayer(kickee, icke);
    String error1 = ErrorMessage.VALIDATION_CASTING.getMessage(kyle.type().getLabel(), kyle.getId(), kicker.getLabel());
    String error2 = ErrorMessage.VALIDATION_CASTING.getMessage(icke.type().getLabel(), icke.getId(), kickee.getLabel());
    expectedException.expect(InvalidKBException.class);
    expectedException.expectMessage(allOf(containsString(error1), containsString(error2)));
    tx.commit();
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Matchers.containsString(org.hamcrest.Matchers.containsString) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 37 with RelationshipType

use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.

the class ValidatorTest method whenCommittingNonAbstractRelationTypeNotLinkedToAnyRoleType_Throw.

@Test
public void whenCommittingNonAbstractRelationTypeNotLinkedToAnyRoleType_Throw() {
    RelationshipType alone = tx.putRelationshipType("alone");
    expectedException.expect(InvalidKBException.class);
    expectedException.expectMessage(containsString(ErrorMessage.VALIDATION_RELATION_TYPE.getMessage(alone.getLabel())));
    tx.commit();
}
Also used : RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 38 with RelationshipType

use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.

the class RemoteConceptsTest method whenCallingSubs_GetTheExpectedResult.

@Test
public void whenCallingSubs_GetTheExpectedResult() {
    Type me = relationshipType;
    Type mySub = RemoteConcepts.createRelationshipType(tx, A);
    Type mySubsSub = RemoteConcepts.createRelationshipType(tx, B);
    mockConceptMethod(ConceptMethods.GET_SUB_CONCEPTS, Stream.of(me, mySub, mySubsSub));
    assertThat(relationshipType.subs().collect(toSet()), containsInAnyOrder(me, mySub, mySubsSub));
}
Also used : Type(ai.grakn.concept.Type) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTxType(ai.grakn.GraknTxType) DataType(ai.grakn.concept.AttributeType.DataType) AttributeType(ai.grakn.concept.AttributeType) Test(org.junit.Test)

Example 39 with RelationshipType

use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.

the class TypeImpl method has.

/**
 * Creates a relation type which allows this type and a {@link ai.grakn.concept.Attribute} type to be linked.
 * @param attributeType The {@link AttributeType} which instances of this type should be allowed to play.
 * @param has the implicit relation type to build
 * @param hasValue the implicit role type to build for the {@link AttributeType}
 * @param hasOwner the implicit role type to build for the type
 * @param required Indicates if the {@link ai.grakn.concept.Attribute} is required on the entity
 * @return The {@link Type} itself
 */
private T has(AttributeType attributeType, Schema.ImplicitType has, Schema.ImplicitType hasValue, Schema.ImplicitType hasOwner, boolean required) {
    // Check if this is a met type
    checkSchemaMutationAllowed();
    // Check if attribute type is the meta
    if (Schema.MetaSchema.ATTRIBUTE.getLabel().equals(attributeType.getLabel())) {
        throw GraknTxOperationException.metaTypeImmutable(attributeType.getLabel());
    }
    Label attributeLabel = attributeType.getLabel();
    Role ownerRole = vertex().tx().putRoleTypeImplicit(hasOwner.getLabel(attributeLabel));
    Role valueRole = vertex().tx().putRoleTypeImplicit(hasValue.getLabel(attributeLabel));
    RelationshipType relationshipType = vertex().tx().putRelationTypeImplicit(has.getLabel(attributeLabel)).relates(ownerRole).relates(valueRole);
    // Linking with ako structure if present
    AttributeType attributeTypeSuper = attributeType.sup();
    Label superLabel = attributeTypeSuper.getLabel();
    if (!Schema.MetaSchema.ATTRIBUTE.getLabel().equals(superLabel)) {
        // Check to make sure we dont add plays edges to meta types accidentally
        Role ownerRoleSuper = vertex().tx().putRoleTypeImplicit(hasOwner.getLabel(superLabel));
        Role valueRoleSuper = vertex().tx().putRoleTypeImplicit(hasValue.getLabel(superLabel));
        RelationshipType relationshipTypeSuper = vertex().tx().putRelationTypeImplicit(has.getLabel(superLabel)).relates(ownerRoleSuper).relates(valueRoleSuper);
        // Create the super type edges from sub role/relations to super roles/relation
        ownerRole.sup(ownerRoleSuper);
        valueRole.sup(valueRoleSuper);
        relationshipType.sup(relationshipTypeSuper);
        // Make sure the supertype attribute is linked with the role as well
        ((AttributeTypeImpl) attributeTypeSuper).plays(valueRoleSuper);
    }
    this.plays(ownerRole, required);
    // TODO: Use explicit cardinality of 0-1 rather than just false
    ((AttributeTypeImpl) attributeType).plays(valueRole, false);
    return getThis();
}
Also used : Role(ai.grakn.concept.Role) AttributeType(ai.grakn.concept.AttributeType) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType)

Example 40 with RelationshipType

use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.

the class GraknTxTest method checkThatMainCentralCacheIsNotAffectedByTransactionModifications.

@Test
public void checkThatMainCentralCacheIsNotAffectedByTransactionModifications() throws InvalidKBException, ExecutionException, InterruptedException {
    // Check Central cache is empty
    assertCacheOnlyContainsMetaTypes();
    Role r1 = tx.putRole("r1");
    Role r2 = tx.putRole("r2");
    EntityType e1 = tx.putEntityType("e1").plays(r1).plays(r2);
    RelationshipType rel1 = tx.putRelationshipType("rel1").relates(r1).relates(r2);
    // Purge the above concepts into the main cache
    tx.commit();
    tx = EmbeddedGraknSession.create(tx.keyspace(), Grakn.IN_MEMORY).open(GraknTxType.WRITE);
    // Check cache is in good order
    Collection<SchemaConcept> cachedValues = tx.getGlobalCache().getCachedTypes().values();
    assertTrue("Type [" + r1 + "] was not cached", cachedValues.contains(r1));
    assertTrue("Type [" + r2 + "] was not cached", cachedValues.contains(r2));
    assertTrue("Type [" + e1 + "] was not cached", cachedValues.contains(e1));
    assertTrue("Type [" + rel1 + "] was not cached", cachedValues.contains(rel1));
    assertThat(e1.plays().collect(toSet()), containsInAnyOrder(r1, r2));
    ExecutorService pool = Executors.newSingleThreadExecutor();
    // Mutate Schema in a separate thread
    pool.submit(() -> {
        GraknTx innerGraph = Grakn.session(Grakn.IN_MEMORY, tx.keyspace()).open(GraknTxType.WRITE);
        EntityType entityType = innerGraph.getEntityType("e1");
        Role role = innerGraph.getRole("r1");
        entityType.deletePlays(role);
    }).get();
    // Check the above mutation did not affect central repo
    SchemaConcept foundE1 = tx.getGlobalCache().getCachedTypes().get(e1.getLabel());
    assertTrue("Main cache was affected by transaction", foundE1.asType().plays().anyMatch(role -> role.equals(r1)));
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) CoreMatchers.is(org.hamcrest.CoreMatchers.is) InvalidKBException(ai.grakn.exception.InvalidKBException) EntityTypeImpl(ai.grakn.kb.internal.concept.EntityTypeImpl) Keyspace(ai.grakn.Keyspace) Role(ai.grakn.concept.Role) SchemaConcept(ai.grakn.concept.SchemaConcept) Entity(ai.grakn.concept.Entity) Type(ai.grakn.concept.Type) EntityType(ai.grakn.concept.EntityType) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) Future(java.util.concurrent.Future) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ExecutorService(java.util.concurrent.ExecutorService) Collectors.toSet(java.util.stream.Collectors.toSet) Grakn(ai.grakn.Grakn) IsInstanceOf(org.hamcrest.core.IsInstanceOf) GraknTxType(ai.grakn.GraknTxType) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) ErrorMessage(ai.grakn.util.ErrorMessage) Matchers.empty(org.hamcrest.Matchers.empty) VerificationException(org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.VerificationException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Shard(ai.grakn.kb.internal.structure.Shard) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) Stream(java.util.stream.Stream) Assert.assertNull(org.junit.Assert.assertNull) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) EmbeddedGraknSession(ai.grakn.factory.EmbeddedGraknSession) Schema(ai.grakn.util.Schema) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) RelationshipType(ai.grakn.concept.RelationshipType) ExecutorService(java.util.concurrent.ExecutorService) SchemaConcept(ai.grakn.concept.SchemaConcept) Test(org.junit.Test)

Aggregations

RelationshipType (ai.grakn.concept.RelationshipType)127 Role (ai.grakn.concept.Role)105 Test (org.junit.Test)91 EntityType (ai.grakn.concept.EntityType)80 Entity (ai.grakn.concept.Entity)52 GraknTx (ai.grakn.GraknTx)39 Relationship (ai.grakn.concept.Relationship)25 ConceptId (ai.grakn.concept.ConceptId)23 Label (ai.grakn.concept.Label)21 HashSet (java.util.HashSet)20 Set (java.util.Set)20 AttributeType (ai.grakn.concept.AttributeType)17 Thing (ai.grakn.concept.Thing)17 Attribute (ai.grakn.concept.Attribute)16 Schema (ai.grakn.util.Schema)12 Collectors (java.util.stream.Collectors)12 List (java.util.List)11 GraknSession (ai.grakn.GraknSession)10 GraknTxType (ai.grakn.GraknTxType)10 Concept (ai.grakn.concept.Concept)10