Search in sources :

Example 76 with Entity

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

the class ConceptTest method whenCastingToInCorrectType_Throw.

@Test
public void whenCastingToInCorrectType_Throw() {
    EntityType thingType = tx.putEntityType("thing type");
    Entity thing = thingType.addEntity();
    expectedException.expect(GraknTxOperationException.class);
    expectedException.expectMessage(GraknTxOperationException.invalidCasting(thing, Type.class).getMessage());
    // noinspection ResultOfMethodCallIgnored
    thing.asType();
}
Also used : EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Test(org.junit.Test)

Example 77 with Entity

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

the class AddBenchmark method addRelation.

@Benchmark
public void addRelation() {
    Entity entity1 = entityType.addEntity();
    Entity entity2 = entityType.addEntity();
    relationshipType.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2);
}
Also used : Entity(ai.grakn.concept.Entity) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 78 with Entity

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

the class EntityTypeTest method addResourceTypeAsKeyToOneEntityTypeAndAsResourceToAnotherEntityType.

@Test
public void addResourceTypeAsKeyToOneEntityTypeAndAsResourceToAnotherEntityType() {
    AttributeType<String> attributeType1 = tx.putAttributeType("Shared Attribute 1", AttributeType.DataType.STRING);
    AttributeType<String> attributeType2 = tx.putAttributeType("Shared Attribute 2", AttributeType.DataType.STRING);
    EntityType entityType1 = tx.putEntityType("EntityType 1");
    EntityType entityType2 = tx.putEntityType("EntityType 2");
    assertThat(entityType1.keys().collect(toSet()), is(empty()));
    assertThat(entityType1.attributes().collect(toSet()), is(empty()));
    assertThat(entityType2.keys().collect(toSet()), is(empty()));
    assertThat(entityType2.attributes().collect(toSet()), is(empty()));
    // Link the resources
    entityType1.attribute(attributeType1);
    entityType1.key(attributeType2);
    entityType2.key(attributeType1);
    entityType2.key(attributeType2);
    assertThat(entityType1.attributes().collect(toSet()), containsInAnyOrder(attributeType1, attributeType2));
    assertThat(entityType2.attributes().collect(toSet()), containsInAnyOrder(attributeType1, attributeType2));
    assertThat(entityType1.keys().collect(toSet()), containsInAnyOrder(attributeType2));
    assertThat(entityType2.keys().collect(toSet()), containsInAnyOrder(attributeType1, attributeType2));
    // Add resource which is a key for one entity and a resource for another
    Entity entity1 = entityType1.addEntity();
    Entity entity2 = entityType2.addEntity();
    Attribute<String> attribute1 = attributeType1.putAttribute("Test 1");
    Attribute<String> attribute2 = attributeType2.putAttribute("Test 2");
    Attribute<String> attribute3 = attributeType2.putAttribute("Test 3");
    // Attribute 1 is a key to one and a resource to another
    entity1.attribute(attribute1);
    entity2.attribute(attribute1);
    entity1.attribute(attribute2);
    entity2.attribute(attribute3);
    tx.commit();
}
Also used : EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Test(org.junit.Test)

Example 79 with Entity

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

the class EntityTypeTest method whenAddingInstanceToType_EnsureIsaEdgeIsPlacedOnShard.

@Test
public void whenAddingInstanceToType_EnsureIsaEdgeIsPlacedOnShard() {
    EntityTypeImpl entityType = (EntityTypeImpl) tx.putEntityType("EntityType");
    Shard shard = entityType.currentShard();
    Entity e1 = entityType.addEntity();
    assertFalse("The isa edge was places on the type rather than the shard", entityType.neighbours(Direction.IN, Schema.EdgeLabel.ISA).iterator().hasNext());
    assertEquals(e1, shard.links().findAny().get());
}
Also used : Entity(ai.grakn.concept.Entity) Shard(ai.grakn.kb.internal.structure.Shard) Test(org.junit.Test)

Example 80 with Entity

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

the class RelationshipTypeTest method whenCallingInstancesOnImplicitRelationType_RelationEdgesAreReturned.

@Test
public void whenCallingInstancesOnImplicitRelationType_RelationEdgesAreReturned() {
    AttributeType<String> attributeType = tx.putAttributeType("My Special Attribute Type", AttributeType.DataType.STRING);
    Attribute<String> attribute = attributeType.putAttribute("Ad thing");
    EntityType entityType = tx.putEntityType("My Special Entity Type").attribute(attributeType);
    Entity entity = entityType.addEntity();
    RelationshipType implicitRelationshipType = tx.getRelationshipType(Schema.ImplicitType.HAS.getLabel(attributeType.getLabel()).getValue());
    assertNotNull(implicitRelationshipType);
    assertThat(implicitRelationshipType.instances().collect(toSet()), empty());
    entity.attribute(attribute);
    assertEquals(1, implicitRelationshipType.instances().count());
}
Also used : EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Aggregations

Entity (ai.grakn.concept.Entity)99 Test (org.junit.Test)81 EntityType (ai.grakn.concept.EntityType)74 Role (ai.grakn.concept.Role)53 RelationshipType (ai.grakn.concept.RelationshipType)50 GraknTx (ai.grakn.GraknTx)44 Attribute (ai.grakn.concept.Attribute)18 Set (java.util.Set)16 Relationship (ai.grakn.concept.Relationship)14 Label (ai.grakn.concept.Label)11 ConceptId (ai.grakn.concept.ConceptId)10 HashSet (java.util.HashSet)10 ArrayList (java.util.ArrayList)8 AttributeType (ai.grakn.concept.AttributeType)7 List (java.util.List)7 GraknSession (ai.grakn.GraknSession)6 Before (org.junit.Before)6 GraknTxType (ai.grakn.GraknTxType)5 Concept (ai.grakn.concept.Concept)5 GraqlQueryException (ai.grakn.exception.GraqlQueryException)5