Search in sources :

Example 1 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class BenchmarkTests method testPathTree.

/**
 * single-rule mimicking transitivity test rule defined by two-hop relations
 * Initial data arranged in N x N square grid.
 *
 * Rules:
 * (arc-from: $x, arc-to: $y) isa arc;},
 * ->
 * (path-from: $x, path-to: $y) isa path;};
 *
 * (path-from: $x, path-to: $z) isa path;
 * (path-from: $z, path-to: $y) isa path;},
 * ->
 * (path-from: $x, path-to: $y) isa path;};
 *
 * Initial data arranged as follows:
 *
 * N - tree heights
 * l - number of links per entity
 *
 *                     a0
 *               /     .   \
 *             arc          arc
 *             /       .       \
 *           a1,1     ...    a1,1^l
 *         /   .  \         /    .  \
 *       arc   .  arc     arc    .  arc
 *       /     .   \       /     .    \
 *     a2,1 ...  a2,l  a2,l+1  ...  a2,2^l
 *            .             .
 *            .             .
 *            .             .
 *   aN,1    ...  ...  ...  ...  ... ... aN,N^l
 */
@Test
public void testPathTree() {
    final int N = 5;
    final int linksPerEntity = 4;
    LOG.debug(new Object() {
    }.getClass().getEnclosingMethod().getName());
    int answers = 0;
    for (int i = 1; i <= N; i++) answers += Math.pow(linksPerEntity, i);
    SampleKBContext kb = PathTreeKB.context(N, linksPerEntity);
    GraknTx graph = kb.tx();
    String queryString = "match (path-from: $x, path-to: $y) isa path;" + "$x has index 'a0';" + "limit " + answers + ";" + "get $y;";
    assertEquals(executeQuery(queryString, graph, "tree").size(), answers);
}
Also used : GraknTx(ai.grakn.GraknTx) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Test(org.junit.Test)

Example 2 with GraknTx

use of ai.grakn.GraknTx 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 3 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class OntologicalQueryTest method allRolesGivenRelationRelates.

@Test
public void allRolesGivenRelationRelates() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    String queryString = "match reifying-relation relates $x; get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    assertEquals(answers.stream().map(ans -> ans.get("x")).collect(Collectors.toSet()), tx.getRelationshipType("reifying-relation").relates().collect(Collectors.toSet()));
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 4 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class ReasonerTest method testReasoningWithQueryContainingPlays.

@Test
public void testReasoningWithQueryContainingPlays() {
    GraknTx graph = nonMaterialisedGeoKB.tx();
    String queryString = "match " + "$x isa $type;$type plays geo-entity;" + "$y isa country;$y has name 'Poland';" + "($x, $y) isa is-located-in; get;";
    String explicitQuery = "match $y has name 'Poland';$x isa $type;$x has " + Schema.MetaSchema.ATTRIBUTE.getLabel().getValue() + " $name;" + "{" + "{$name val 'Europe';};" + "{$type label 'continent' or $type label 'geoObject';};" + "} or {" + "{$name val 'Warsaw-Polytechnics' or $name val 'University-of-Warsaw';};" + "{$type label 'university';};" + "} or {" + "{$name val 'Warsaw' or $name val 'Wroclaw';};" + "{$type label 'city' or $type label 'geoObject';};" + "} or {" + "{$name val 'Masovia' or $name val 'Silesia';};" + "{$type label 'region' or $type label 'geoObject';};" + "}; get $x, $y, $type;";
    GetQuery query = graph.graql().infer(true).parse(queryString);
    GetQuery query2 = graph.graql().infer(false).parse(explicitQuery);
    assertQueriesEqual(query, query2);
}
Also used : GraknTx(ai.grakn.GraknTx) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Example 5 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class ReasonerTest method testReasoningWithQueryContainingTypeVar2.

@Test
public void testReasoningWithQueryContainingTypeVar2() {
    String thing = Schema.MetaSchema.THING.getLabel().getValue();
    GraknTx graph = nonMaterialisedGeoKB.tx();
    String queryString = "match $x isa $type;" + "(geo-entity: $x, entity-location: $y) isa is-located-in; $y isa country;$y has name 'Poland'; get;";
    String explicitQuery = "match $y has name 'Poland';$x isa $type;$x has " + Schema.MetaSchema.ATTRIBUTE.getLabel().getValue() + " $name;" + "{" + "{$name val 'Warsaw-Polytechnics' or $name val 'University-of-Warsaw';};" + "{$type label 'university' or $type label 'entity' or $type label '" + thing + "';};" + "} or {" + "{$name val 'Warsaw' or $name val 'Wroclaw';};" + "{$type label 'city' or $type label 'geoObject' or $type label 'entity' or $type label '" + thing + "';};" + "} or {" + "{$name val 'Masovia' or $name val 'Silesia';};" + "{$type label 'region' or $type label 'geoObject' or $type label 'entity' or $type label '" + thing + "';};" + "}; get $x, $y, $type;";
    GetQuery query = graph.graql().infer(true).parse(queryString);
    GetQuery query2 = graph.graql().infer(false).parse(explicitQuery);
    assertQueriesEqual(query, query2);
}
Also used : GraknTx(ai.grakn.GraknTx) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Aggregations

GraknTx (ai.grakn.GraknTx)243 Test (org.junit.Test)189 EntityType (ai.grakn.concept.EntityType)54 GetQuery (ai.grakn.graql.GetQuery)52 Entity (ai.grakn.concept.Entity)51 QueryBuilder (ai.grakn.graql.QueryBuilder)49 Role (ai.grakn.concept.Role)48 RelationshipType (ai.grakn.concept.RelationshipType)46 Answer (ai.grakn.graql.admin.Answer)44 Set (java.util.Set)44 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)33 Label (ai.grakn.concept.Label)28 Attribute (ai.grakn.concept.Attribute)27 Concept (ai.grakn.concept.Concept)27 HashSet (java.util.HashSet)26 GraknSession (ai.grakn.GraknSession)22 AttributeType (ai.grakn.concept.AttributeType)22 ConceptId (ai.grakn.concept.ConceptId)22 List (java.util.List)19 GraknTxType (ai.grakn.GraknTxType)17