Search in sources :

Example 16 with Entity

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

the class TxCacheTest method whenClosingTransaction_EnsureTransactionCacheIsEmpty.

@Test
public void whenClosingTransaction_EnsureTransactionCacheIsEmpty() {
    TxCache cache = tx.txCache();
    // Load some sample data
    AttributeType<String> attributeType = tx.putAttributeType("Attribute Type", AttributeType.DataType.STRING);
    Role role1 = tx.putRole("role 1");
    Role role2 = tx.putRole("role 2");
    EntityType entityType = tx.putEntityType("My Type").plays(role1).plays(role2).attribute(attributeType);
    RelationshipType relationshipType = tx.putRelationshipType("My Relationship Type").relates(role1).relates(role2);
    Entity e1 = entityType.addEntity();
    Entity e2 = entityType.addEntity();
    Attribute<String> r1 = attributeType.putAttribute("test");
    e1.attribute(r1);
    relationshipType.addRelationship().addRolePlayer(role1, e1).addRolePlayer(role2, e2);
    // Check the caches are not empty
    assertThat(cache.getConceptCache().keySet(), not(empty()));
    assertThat(cache.getSchemaConceptCache().keySet(), not(empty()));
    assertThat(cache.getLabelCache().keySet(), not(empty()));
    assertThat(cache.getShardingCount().keySet(), not(empty()));
    assertThat(cache.getModifiedCastings(), not(empty()));
    // Close the transaction
    tx.commit();
    // Check the caches are empty
    assertThat(cache.getConceptCache().keySet(), empty());
    assertThat(cache.getSchemaConceptCache().keySet(), empty());
    assertThat(cache.getLabelCache().keySet(), empty());
    assertThat(cache.getShardingCount().keySet(), empty());
    assertThat(cache.getModifiedThings(), empty());
    assertThat(cache.getModifiedRoles(), empty());
    assertThat(cache.getModifiedRelationshipTypes(), empty());
    assertThat(cache.getModifiedRules(), empty());
    assertThat(cache.getModifiedCastings(), empty());
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 17 with Entity

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

the class TxCacheTest method whenDeletingAnInstanceWithNoRelations_EnsureLogIsEmpty.

@Test
public void whenDeletingAnInstanceWithNoRelations_EnsureLogIsEmpty() {
    EntityType t1 = tx.putEntityType("1");
    Entity i1 = t1.addEntity();
    tx.commit();
    tx = EmbeddedGraknSession.create(tx.keyspace(), Grakn.IN_MEMORY).open(GraknTxType.WRITE);
    assertThat(tx.txCache().getModifiedThings(), is(empty()));
    i1.delete();
    assertThat(tx.txCache().getModifiedThings(), is(empty()));
}
Also used : EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Test(org.junit.Test)

Example 18 with Entity

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

the class EntityTest method whenCreatingAnInferredEntity_EnsureMarkedAsInferred.

@Test
public void whenCreatingAnInferredEntity_EnsureMarkedAsInferred() {
    EntityTypeImpl et = EntityTypeImpl.from(tx.putEntityType("et"));
    Entity entity = et.addEntity();
    Entity entityInferred = et.addEntityInferred();
    assertFalse(entity.isInferred());
    assertTrue(entityInferred.isInferred());
}
Also used : Entity(ai.grakn.concept.Entity) Test(org.junit.Test)

Example 19 with Entity

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

the class StatisticsTest method addSchemaAndEntities.

private void addSchemaAndEntities() throws InvalidKBException {
    try (GraknTx tx = session.open(GraknTxType.WRITE)) {
        EntityType entityType1 = tx.putEntityType(thing);
        EntityType entityType2 = tx.putEntityType(anotherThing);
        Entity entity1 = entityType1.addEntity();
        Entity entity2 = entityType1.addEntity();
        Entity entity3 = entityType1.addEntity();
        Entity entity4 = entityType2.addEntity();
        entityId1 = entity1.getId();
        entityId2 = entity2.getId();
        entityId3 = entity3.getId();
        entityId4 = entity4.getId();
        Role relation1 = tx.putRole("relation1");
        Role relation2 = tx.putRole("relation2");
        entityType1.plays(relation1).plays(relation2);
        entityType2.plays(relation1).plays(relation2);
        RelationshipType related = tx.putRelationshipType("related").relates(relation1).relates(relation2);
        related.addRelationship().addRolePlayer(relation1, entity1).addRolePlayer(relation2, entity2);
        related.addRelationship().addRolePlayer(relation1, entity2).addRolePlayer(relation2, entity3);
        related.addRelationship().addRolePlayer(relation1, entity2).addRolePlayer(relation2, entity4);
        AttributeType<Double> attribute1 = tx.putAttributeType(resourceType1, AttributeType.DataType.DOUBLE);
        AttributeType<Long> attribute2 = tx.putAttributeType(resourceType2, AttributeType.DataType.LONG);
        AttributeType<Long> attribute3 = tx.putAttributeType(resourceType3, AttributeType.DataType.LONG);
        AttributeType<String> attribute4 = tx.putAttributeType(resourceType4, AttributeType.DataType.STRING);
        AttributeType<Long> attribute5 = tx.putAttributeType(resourceType5, AttributeType.DataType.LONG);
        AttributeType<Double> attribute6 = tx.putAttributeType(resourceType6, AttributeType.DataType.DOUBLE);
        AttributeType<Double> attribute7 = tx.putAttributeType(resourceType7, AttributeType.DataType.DOUBLE);
        entityType1.attribute(attribute1);
        entityType1.attribute(attribute2);
        entityType1.attribute(attribute3);
        entityType1.attribute(attribute4);
        entityType1.attribute(attribute5);
        entityType1.attribute(attribute6);
        entityType1.attribute(attribute7);
        entityType2.attribute(attribute1);
        entityType2.attribute(attribute2);
        entityType2.attribute(attribute3);
        entityType2.attribute(attribute4);
        entityType2.attribute(attribute5);
        entityType2.attribute(attribute6);
        entityType2.attribute(attribute7);
        tx.commit();
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType)

Example 20 with Entity

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

the class StatisticsTest method testStd.

@Test
public void testStd() throws Exception {
    Optional<Double> result;
    // resource-type has no instance
    addSchemaAndEntities();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        result = Graql.compute().std().of(resourceType1).in(Collections.emptyList()).withTx(graph).execute();
        assertFalse(result.isPresent());
        result = Graql.compute().std().of(resourceType1).withTx(graph).execute();
        assertFalse(result.isPresent());
        result = Graql.compute().withTx(graph).std().of(resourceType1).execute();
        assertFalse(result.isPresent());
        result = Graql.compute().std().withTx(graph).of(resourceType1).execute();
        assertFalse(result.isPresent());
        result = graph.graql().compute().std().of(resourceType2).execute();
        assertFalse(result.isPresent());
        result = graph.graql().compute().std().of(resourceType2, resourceType5).execute();
        assertFalse(result.isPresent());
        result = graph.graql().compute().std().of(resourceType2).withTx(graph).execute();
        assertFalse(result.isPresent());
        result = graph.graql().compute().withTx(graph).std().of(resourceType2).execute();
        assertFalse(result.isPresent());
    }
    // add resources, but resources are not connected to any entities
    addResourcesInstances();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        result = Graql.compute().std().of(resourceType1).withTx(graph).execute();
        assertFalse(result.isPresent());
        result = Graql.compute().std().of(resourceType1).in().withTx(graph).execute();
        assertFalse(result.isPresent());
        result = graph.graql().compute().std().of(resourceType2).in(thing, anotherThing).execute();
        assertFalse(result.isPresent());
        result = Graql.compute().std().of(resourceType2).withTx(graph).in(anotherThing).execute();
        assertFalse(result.isPresent());
    }
    // connect entity and resources
    addResourceRelations();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        result = Graql.compute().std().of(resourceType1).withTx(graph).execute();
        assertEquals(Math.sqrt(0.18 / 3), result.get(), delta);
        result = Graql.compute().std().of(resourceType2).withTx(graph).in(anotherThing).execute();
        assertEquals(Math.sqrt(0D), result.get(), delta);
        result = graph.graql().compute().std().of(resourceType1, resourceType6).execute();
        assertEquals(Math.sqrt(54.18 / 6), result.get(), delta);
        result = graph.graql().compute().std().of(resourceType2, resourceType5).in(thing, anotherThing).execute();
        assertEquals(Math.sqrt(110.0 / 6), result.get(), delta);
        result = graph.graql().compute().std().of(resourceType2).in(thing).execute();
        assertEquals(2.5, result.get(), delta);
    }
    List<Long> list = new ArrayList<>();
    long workerNumber = 3L;
    if (GraknTestUtil.usingTinker())
        workerNumber = 1;
    for (long i = 0L; i < workerNumber; i++) {
        list.add(i);
    }
    List<Double> numberList = list.parallelStream().map(i -> {
        try (GraknTx graph = session.open(GraknTxType.READ)) {
            return graph.graql().compute().std().of(resourceType2).in(thing).execute().get();
        }
    }).collect(Collectors.toList());
    numberList.forEach(value -> assertEquals(2.5D, value.doubleValue(), delta));
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) GraknTestUtil(ai.grakn.util.GraknTestUtil) Role(ai.grakn.concept.Role) Entity(ai.grakn.concept.Entity) Graql(ai.grakn.graql.Graql) Supplier(java.util.function.Supplier) EntityType(ai.grakn.concept.EntityType) ArrayList(java.util.ArrayList) Attribute(ai.grakn.concept.Attribute) SessionContext(ai.grakn.test.rule.SessionContext) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) GraqlQueryException(ai.grakn.exception.GraqlQueryException) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) ArrayList(java.util.ArrayList) 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