Search in sources :

Example 1 with Entity

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

the class BenchmarkTests method nonRecursiveChainOfRules.

/**
 * Executes a scalability test defined in terms of the number of rules in the system. Creates a simple rule chain:
 *
 * R_i(x, y) := R_{i-1}(x, y);     i e [1, N]
 *
 * with a single initial relation instance R_0(a ,b)
 */
@Test
public void nonRecursiveChainOfRules() {
    final int N = 200;
    LOG.debug(new Object() {
    }.getClass().getEnclosingMethod().getName());
    GraknSession graknSession = sessionContext.newSession();
    // NB: loading data here as defining it as KB and using graql api leads to circular dependencies
    try (GraknTx tx = graknSession.open(GraknTxType.WRITE)) {
        Role fromRole = tx.putRole("fromRole");
        Role toRole = tx.putRole("toRole");
        RelationshipType relation0 = tx.putRelationshipType("relation0").relates(fromRole).relates(toRole);
        for (int i = 1; i <= N; i++) {
            tx.putRelationshipType("relation" + i).relates(fromRole).relates(toRole);
        }
        EntityType genericEntity = tx.putEntityType("genericEntity").plays(fromRole).plays(toRole);
        Entity fromEntity = genericEntity.addEntity();
        Entity toEntity = genericEntity.addEntity();
        relation0.addRelationship().addRolePlayer(fromRole, fromEntity).addRolePlayer(toRole, toEntity);
        for (int i = 1; i <= N; i++) {
            Var fromVar = Graql.var().asUserDefined();
            Var toVar = Graql.var().asUserDefined();
            VarPattern rulePattern = Graql.label("rule" + i).when(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa("relation" + (i - 1)))).then(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa("relation" + i)));
            tx.graql().define(rulePattern).execute();
        }
        tx.commit();
    }
    try (GraknTx tx = graknSession.open(GraknTxType.READ)) {
        final long limit = 1;
        String queryPattern = "(fromRole: $x, toRole: $y) isa relation" + N + ";";
        String queryString = "match " + queryPattern + " get;";
        String limitedQueryString = "match " + queryPattern + "limit " + limit + ";" + "get;";
        assertEquals(executeQuery(queryString, tx, "full").size(), limit);
        assertEquals(executeQuery(limitedQueryString, tx, "limit").size(), limit);
    }
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Var(ai.grakn.graql.Var) GraknSession(ai.grakn.GraknSession) RelationshipType(ai.grakn.concept.RelationshipType) VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 2 with Entity

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

the class CastingTest method whenCreatingRelation_EnsureRolePlayerContainsInstanceRoleTypeRelationTypeAndRelation.

@Test
public void whenCreatingRelation_EnsureRolePlayerContainsInstanceRoleTypeRelationTypeAndRelation() {
    Entity e1 = entityType.addEntity();
    RelationshipImpl relation = (RelationshipImpl) relationshipType.addRelationship().addRolePlayer(role1, e1);
    Set<Casting> castings = relation.reified().get().castingsRelation().collect(Collectors.toSet());
    castings.forEach(rolePlayer -> {
        assertEquals(e1, rolePlayer.getRolePlayer());
        assertEquals(role1, rolePlayer.getRole());
        assertEquals(relationshipType, rolePlayer.getRelationshipType());
        assertEquals(relation, rolePlayer.getRelationship());
    });
}
Also used : Entity(ai.grakn.concept.Entity) RelationshipImpl(ai.grakn.kb.internal.concept.RelationshipImpl) Test(org.junit.Test)

Example 3 with Entity

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

the class CastingTest method whenUpdatingRelation_EnsureRolePlayersAreUpdated.

@Test
public void whenUpdatingRelation_EnsureRolePlayersAreUpdated() {
    Entity e1 = entityType.addEntity();
    Entity e3 = entityType.addEntity();
    RelationshipImpl relation = (RelationshipImpl) relationshipType.addRelationship().addRolePlayer(role1, e1);
    Set<Thing> things = relation.reified().get().castingsRelation().map(Casting::getRolePlayer).collect(Collectors.toSet());
    Set<Role> roles = relation.reified().get().castingsRelation().map(Casting::getRole).collect(Collectors.toSet());
    assertThat(things, containsInAnyOrder(e1));
    assertThat(roles, containsInAnyOrder(role1));
    // Now Update
    relation.addRolePlayer(role2, e1).addRolePlayer(role3, e3);
    things = relation.reified().get().castingsRelation().map(Casting::getRolePlayer).collect(Collectors.toSet());
    roles = relation.reified().get().castingsRelation().map(Casting::getRole).collect(Collectors.toSet());
    assertThat(things, containsInAnyOrder(e1, e3));
    assertThat(roles, containsInAnyOrder(role1, role2, role3));
}
Also used : Role(ai.grakn.concept.Role) Entity(ai.grakn.concept.Entity) RelationshipImpl(ai.grakn.kb.internal.concept.RelationshipImpl) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 4 with Entity

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

the class MigratorTestUtils method assertPetGraphCorrect.

/**
 * Check that the pet graph has been loaded correctly
 */
public static void assertPetGraphCorrect(GraknSession session) {
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        Collection<Entity> pets = graph.getEntityType("pet").instances().collect(Collectors.toSet());
        assertEquals(9, pets.size());
        Collection<Entity> cats = graph.getEntityType("cat").instances().collect(Collectors.toSet());
        assertEquals(2, cats.size());
        Collection<Entity> hamsters = graph.getEntityType("hamster").instances().collect(Collectors.toSet());
        assertEquals(1, hamsters.size());
        AttributeType<String> name = graph.getAttributeType("name");
        AttributeType<String> death = graph.getAttributeType("death");
        Entity puffball = name.getAttribute("Puffball").ownerInstances().iterator().next().asEntity();
        assertEquals(0, puffball.attributes(death).count());
        Entity bowser = name.getAttribute("Bowser").ownerInstances().iterator().next().asEntity();
        assertEquals(1, bowser.attributes(death).count());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity)

Example 5 with Entity

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

the class MigratorTestUtils method assertPokemonGraphCorrect.

/**
 * Check that the pokemon graph has been loaded correctly
 */
public static void assertPokemonGraphCorrect(GraknSession session) {
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        Collection<Entity> pokemon = graph.getEntityType("pokemon").instances().collect(Collectors.toSet());
        assertEquals(9, pokemon.size());
        AttributeType<String> typeid = graph.getAttributeType("type-id");
        AttributeType<String> pokedexno = graph.getAttributeType("pokedex-no");
        Entity grass = typeid.getAttribute("12").ownerInstances().iterator().next().asEntity();
        Entity poison = typeid.getAttribute("4").ownerInstances().iterator().next().asEntity();
        Entity bulbasaur = pokedexno.getAttribute("1").ownerInstances().iterator().next().asEntity();
        RelationshipType relation = graph.getRelationshipType("has-type");
        assertNotNull(grass);
        assertNotNull(poison);
        assertNotNull(bulbasaur);
        assertRelationBetweenInstancesExists(graph, bulbasaur, grass, relation.getLabel());
        assertRelationBetweenInstancesExists(graph, bulbasaur, poison, relation.getLabel());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType)

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