Search in sources :

Example 66 with Entity

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

the class KCoreTest method addSchemaAndEntities.

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

Example 67 with Entity

use of ai.grakn.concept.Entity 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 68 with Entity

use of ai.grakn.concept.Entity 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 69 with Entity

use of ai.grakn.concept.Entity 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)

Example 70 with Entity

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

the class JsonMigratorTest method whenMigratorExecutedOverDataWithAllDataTypes_AllDataIsPersistedInGraph.

@Test
public void whenMigratorExecutedOverDataWithAllDataTypes_AllDataIsPersistedInGraph() throws FileNotFoundException {
    load(factory, getFile("json", "all-types/schema.gql"));
    String template = "" + "insert $x isa thingy\n" + "  has a-boolean <a-boolean>\n" + "  has a-number  <a-number>\n" + "  for (int in <array-of-ints> ) do {\n" + "  has a-int <int>\n" + "  }\n" + "  has a-string <a-string>\n" + "  if (<a-null> != null) do {has a-null <a-null>};";
    declareAndLoad(template, "all-types/data.json");
    try (GraknTx graph = factory.open(GraknTxType.READ)) {
        EntityType rootType = graph.getEntityType("thingy");
        Set<Entity> things = rootType.instances().collect(toSet());
        assertEquals(1, things.size());
        Entity thing = things.iterator().next();
        Collection<Object> integers = getResources(graph, thing, Label.of("a-int")).map(Attribute::getValue).collect(toSet());
        assertEquals(Sets.newHashSet(1L, 2L, 3L), integers);
        Attribute aBoolean = getResource(graph, thing, Label.of("a-boolean"));
        assertEquals(true, aBoolean.getValue());
        Attribute aNumber = getResource(graph, thing, Label.of("a-number"));
        assertEquals(42.1, aNumber.getValue());
        Attribute aString = getResource(graph, thing, Label.of("a-string"));
        assertEquals("hi", aString.getValue());
        assertEquals(0, graph.getAttributeType("a-null").instances().count());
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) 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