Search in sources :

Example 1 with RelationshipType

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

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

the class OntologicalQueryTest method allInstancesOfTypesThatAreSubTypeOfGivenType_needInferenceToGetAllResults.

@Test
public void allInstancesOfTypesThatAreSubTypeOfGivenType_needInferenceToGetAllResults() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    String queryString = "match $x isa $type; $type sub relationship; get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    assertEquals(answers.size(), tx.getRelationshipType("relationship").subs().flatMap(RelationshipType::instances).count());
    assertCollectionsEqual(answers, qb.infer(false).<GetQuery>parse(queryString).execute());
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) RelationshipType(ai.grakn.concept.RelationshipType) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 3 with RelationshipType

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

the class SchemaMutationTest method whenChangingTheSuperTypeOfAnEntityTypeWhichHasAResource_EnsureTheResourceIsStillAccessibleViaTheRelationTypeInstances_ByPreventingChange.

@Test
public void whenChangingTheSuperTypeOfAnEntityTypeWhichHasAResource_EnsureTheResourceIsStillAccessibleViaTheRelationTypeInstances_ByPreventingChange() {
    AttributeType<String> name = tx.putAttributeType("name", AttributeType.DataType.STRING);
    // Create a person and allow person to have a name
    EntityType person = tx.putEntityType("person").attribute(name);
    // Create a man which is a person and is therefore allowed to have a name
    EntityType man = tx.putEntityType("man").sup(person);
    RelationshipType has_name = tx.getRelationshipType("@has-name");
    // Create a Man and name him Bob
    Attribute<String> nameBob = name.putAttribute("Bob");
    man.addEntity().attribute(nameBob);
    // Get The Relationship which says that our man is name bob
    Relationship expectedEdge = Iterables.getOnlyElement(has_name.instances().collect(toSet()));
    Role hasNameOwner = tx.getRole("@has-name-owner");
    assertThat(expectedEdge.type().instances().collect(toSet()), hasItem(expectedEdge));
    expectedException.expect(GraknTxOperationException.class);
    expectedException.expectMessage(GraknTxOperationException.changingSuperWillDisconnectRole(person, tx.admin().getMetaEntityType(), hasNameOwner).getMessage());
    // Man is no longer a person and therefore is not allowed to have a name
    man.sup(tx.admin().getMetaEntityType());
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 4 with RelationshipType

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

Example 5 with RelationshipType

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

the class RelationshipTest method whenRemovingRolePlayerFromRelationship_EnsureRolePlayerIsRemoved.

@Test
public void whenRemovingRolePlayerFromRelationship_EnsureRolePlayerIsRemoved() {
    Role role1 = tx.putRole("dark");
    Role role2 = tx.putRole("souls");
    RelationshipType relationshipType = tx.putRelationshipType("Dark Souls").relates(role1).relates(role2);
    EntityType entityType = tx.putEntityType("Dead Guys").plays(role1).plays(role2);
    Entity e1 = entityType.addEntity();
    Entity e2 = entityType.addEntity();
    Entity e3 = entityType.addEntity();
    Entity e4 = entityType.addEntity();
    Entity e5 = entityType.addEntity();
    Entity e6 = entityType.addEntity();
    Relationship relationship = relationshipType.addRelationship().addRolePlayer(role1, e1).addRolePlayer(role1, e2).addRolePlayer(role1, e3).addRolePlayer(role2, e4).addRolePlayer(role2, e5).addRolePlayer(role2, e6);
    assertThat(relationship.rolePlayers().collect(Collectors.toSet()), containsInAnyOrder(e1, e2, e3, e4, e5, e6));
    relationship.removeRolePlayer(role1, e2);
    relationship.removeRolePlayer(role2, e1);
    assertThat(relationship.rolePlayers().collect(Collectors.toSet()), containsInAnyOrder(e1, e3, e4, e5, e6));
    relationship.removeRolePlayer(role2, e6);
    assertThat(relationship.rolePlayers().collect(Collectors.toSet()), containsInAnyOrder(e1, e3, e4, e5));
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Aggregations

RelationshipType (ai.grakn.concept.RelationshipType)127 Role (ai.grakn.concept.Role)105 Test (org.junit.Test)91 EntityType (ai.grakn.concept.EntityType)80 Entity (ai.grakn.concept.Entity)52 GraknTx (ai.grakn.GraknTx)39 Relationship (ai.grakn.concept.Relationship)25 ConceptId (ai.grakn.concept.ConceptId)23 Label (ai.grakn.concept.Label)21 HashSet (java.util.HashSet)20 Set (java.util.Set)20 AttributeType (ai.grakn.concept.AttributeType)17 Thing (ai.grakn.concept.Thing)17 Attribute (ai.grakn.concept.Attribute)16 Schema (ai.grakn.util.Schema)12 Collectors (java.util.stream.Collectors)12 List (java.util.List)11 GraknSession (ai.grakn.GraknSession)10 GraknTxType (ai.grakn.GraknTxType)10 Concept (ai.grakn.concept.Concept)10