Search in sources :

Example 91 with EntityType

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

the class CountTest method testHasResourceEdges.

@Test
public void testHasResourceEdges() {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType person = graph.putEntityType("person");
        AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
        person.attribute(name);
        Entity aPerson = person.addEntity();
        aPerson.attribute(name.putAttribute("jason"));
        graph.commit();
    }
    long count;
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        count = graph.graql().compute().count().execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().includeAttribute().execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name", "thing").execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("@has-name", "name").execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().in("relationship").execute();
        assertEquals(0L, count);
        count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
        assertEquals(1L, count);
    }
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        // manually construct the relation type and instance
        EntityType person = graph.getEntityType("person");
        Entity aPerson = person.addEntity();
        AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
        Attribute jason = name.putAttribute("jason");
        Role resourceOwner = graph.putRole(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of("name")));
        person.plays(resourceOwner);
        Role resourceValue = graph.putRole(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of("name")));
        name.plays(resourceValue);
        RelationshipType relationshipType = graph.putRelationshipType(Schema.ImplicitType.HAS.getLabel(Label.of("name"))).relates(resourceOwner).relates(resourceValue);
        relationshipType.addRelationship().addRolePlayer(resourceOwner, aPerson).addRolePlayer(resourceValue, jason);
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        count = graph.graql().compute().count().execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().includeAttribute().execute();
        assertEquals(5L, count);
        count = graph.graql().compute().count().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().includeAttribute().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name").execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().in("@has-name", "thing").execute();
        assertEquals(5L, count);
        count = graph.graql().compute().count().in("@has-name", "name").execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("relationship").execute();
        assertEquals(0L, count);
        count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
        assertEquals(2L, count);
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 92 with EntityType

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

the class CountTest method testConcurrentCount.

@Test
public void testConcurrentCount() throws Exception {
    assumeFalse(GraknTestUtil.usingTinker());
    String nameThing = "thingy";
    String nameAnotherThing = "another";
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType thingy = graph.putEntityType(nameThing);
        thingy.addEntity();
        thingy.addEntity();
        EntityType anotherThing = graph.putEntityType(nameAnotherThing);
        anotherThing.addEntity();
        graph.commit();
    }
    List<Long> list = new ArrayList<>(4);
    long workerNumber = 6L;
    for (long i = 0L; i < workerNumber; i++) {
        list.add(i);
    }
    // running 4 jobs at the same time
    // collecting the result in the end so engine won't stop before the test finishes
    Set<Long> result;
    result = list.parallelStream().map(i -> executeCount(session)).collect(Collectors.toSet());
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(3L, result.iterator().next().longValue());
    result = list.parallelStream().map(i -> executeCount(session)).collect(Collectors.toSet());
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(3L, result.iterator().next().longValue());
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 93 with EntityType

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

the class CountTest method testCountAfterCommit.

@Test
public void testCountAfterCommit() throws Exception {
    String nameThing = "thingy";
    String nameAnotherThing = "another";
    // assert the graph is empty
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        Assert.assertEquals(0L, Graql.compute().count().withTx(graph).execute().longValue());
        Assert.assertEquals(0L, graph.graql().compute().count().includeAttribute().execute().longValue());
    }
    // add 2 instances
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType thingy = graph.putEntityType(nameThing);
        thingy.addEntity();
        thingy.addEntity();
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        Assert.assertEquals(2L, Graql.compute().withTx(graph).count().in(nameThing).execute().longValue());
    }
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType anotherThing = graph.putEntityType(nameAnotherThing);
        anotherThing.addEntity();
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        // assert computer returns the correct count of instances
        Assert.assertEquals(2L, Graql.compute().withTx(graph).count().in(nameThing).includeAttribute().execute().longValue());
        Assert.assertEquals(3L, graph.graql().compute().count().execute().longValue());
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Test(org.junit.Test)

Example 94 with EntityType

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

the class CountTest method testHasResourceVerticesAndEdges.

@Test
public void testHasResourceVerticesAndEdges() {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        // manually construct the relation type and instance
        EntityType person = graph.putEntityType("person");
        Entity aPerson = person.addEntity();
        AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
        Attribute jason = name.putAttribute("jason");
        person.attribute(name);
        Role resourceOwner = graph.putRole(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of("name")));
        person.plays(resourceOwner);
        Role resourceValue = graph.putRole(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of("name")));
        name.plays(resourceValue);
        RelationshipType relationshipType = graph.putRelationshipType(Schema.ImplicitType.HAS.getLabel(Label.of("name"))).relates(resourceOwner).relates(resourceValue);
        // here relationship type is still implicit
        relationshipType.addRelationship().addRolePlayer(resourceOwner, aPerson).addRolePlayer(resourceValue, jason);
        graph.commit();
    }
    long count;
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        count = graph.graql().compute().count().execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().includeAttribute().execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name", "name").execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().in("relationship").execute();
        assertEquals(0L, count);
        count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
        assertEquals(1L, count);
    }
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType person = graph.getEntityType("person");
        AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
        Entity aPerson = person.addEntity();
        aPerson.attribute(name.getAttribute("jason"));
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        count = graph.graql().compute().count().includeAttribute().execute();
        assertEquals(5L, count);
        count = graph.graql().compute().count().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name").execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().in("@has-name", "name").execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("relationship").execute();
        assertEquals(0L, count);
        count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
        assertEquals(2L, count);
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 95 with EntityType

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

the class GraqlTest method addSchemaAndEntities.

private void addSchemaAndEntities() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType1 = graph.putEntityType(thingy);
        EntityType entityType2 = graph.putEntityType(anotherThing);
        Entity entity1 = entityType1.addEntity();
        Entity entity2 = entityType1.addEntity();
        Entity entity3 = entityType1.addEntity();
        Entity entity4 = entityType2.addEntity();
        entityId1 = entity1.getId().getValue();
        entityId2 = entity2.getId().getValue();
        entityId3 = entity3.getId().getValue();
        entityId4 = entity4.getId().getValue();
        Role role1 = graph.putRole("role1");
        Role role2 = graph.putRole("role2");
        entityType1.plays(role1).plays(role2);
        entityType2.plays(role1).plays(role2);
        RelationshipType relationshipType = graph.putRelationshipType(related).relates(role1).relates(role2);
        relationId12 = relationshipType.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2).getId().getValue();
        relationId24 = relationshipType.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity4).getId().getValue();
        graph.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)

Aggregations

EntityType (ai.grakn.concept.EntityType)168 Test (org.junit.Test)141 Role (ai.grakn.concept.Role)83 RelationshipType (ai.grakn.concept.RelationshipType)82 Entity (ai.grakn.concept.Entity)74 GraknTx (ai.grakn.GraknTx)45 Relationship (ai.grakn.concept.Relationship)21 Attribute (ai.grakn.concept.Attribute)19 AttributeType (ai.grakn.concept.AttributeType)17 ConceptId (ai.grakn.concept.ConceptId)17 Label (ai.grakn.concept.Label)17 Set (java.util.Set)16 HashSet (java.util.HashSet)15 Thing (ai.grakn.concept.Thing)14 GraknTxType (ai.grakn.GraknTxType)8 ArrayList (java.util.ArrayList)8 GraknSession (ai.grakn.GraknSession)7 Concept (ai.grakn.concept.Concept)7 HashMap (java.util.HashMap)7 Before (org.junit.Before)7