Search in sources :

Example 1 with SampleKBContext

use of ai.grakn.test.rule.SampleKBContext 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 SampleKBContext

use of ai.grakn.test.rule.SampleKBContext in project grakn by graknlabs.

the class BenchmarkTests method testDiagonal.

/**
 * single-rule mimicking transitivity test rule defined by two-hop relations
 * Initial data arranged in N x N square grid.
 *
 * Rule:
 * (rel-from:$x, rel-to:$y) isa horizontal;
 * (rel-from:$y, rel-to:$z) isa horizontal;
 * (rel-from:$z, rel-to:$u) isa vertical;
 * (rel-from:$u, rel-to:$v) isa vertical;
 * ->
 * (rel-from:$x, rel-to:$v) isa diagonal;
 *
 * Initial data arranged as follows:
 *
 *  a_{i  , j} -  horizontal  - a_{i, j + 1}
 *       |                    |
 *    vertical             vertical
 *       |                    |
 *  a_{i+1, j} -  horizontal  - a_{i+1, j+1}
 *
 *  i e [0, N)
 *  j e [0, N)
 */
@Test
public void testDiagonal() {
    // 9604
    final int N = 10;
    final int limit = 10;
    LOG.debug(new Object() {
    }.getClass().getEnclosingMethod().getName());
    // results @N = 40  1444  3.5s
    // results @N = 50  2304    8s    / 1s
    // results @N = 100 9604  loading takes ages
    SampleKBContext kb = DiagonalKB.context(N, N);
    QueryBuilder iqb = kb.tx().graql().infer(true).materialise(false);
    String queryString = "match (rel-from: $x, rel-to: $y) isa diagonal; get;";
    GetQuery query = iqb.parse(queryString);
    executeQuery(query, "full");
    executeQuery(query.match().limit(limit).get(), "limit " + limit);
}
Also used : SampleKBContext(ai.grakn.test.rule.SampleKBContext) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 3 with SampleKBContext

use of ai.grakn.test.rule.SampleKBContext in project grakn by graknlabs.

the class RecursiveInferenceTest method testPathSymmetric.

@Test
public void testPathSymmetric() {
    final int N = 2;
    SampleKBContext kb = PathTreeSymmetricKB.context(N, 3);
    QueryBuilder qb = kb.tx().graql().infer(false);
    QueryBuilder iqb = kb.tx().graql().infer(true);
    String queryString = "match ($x, $y) isa path;$x has index 'a0'; get $y;";
    String explicitQuery = "match {$y isa vertex;} or {$y isa start-vertex;}; get;";
    assertQueriesEqual(iqb.materialise(false).parse(queryString), qb.parse(explicitQuery));
    assertQueriesEqual(iqb.materialise(true).parse(queryString), qb.parse(explicitQuery));
}
Also used : SampleKBContext(ai.grakn.test.rule.SampleKBContext) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 4 with SampleKBContext

use of ai.grakn.test.rule.SampleKBContext in project grakn by graknlabs.

the class RecursiveInferenceTest method testNguyen.

/**
 *test3 from Nguyen (similar to test 6.5 from Cao)
 * N(x, y) :- R(x, y)
 * N(x, y) :- P(x, z), N(z, w), Q(w, y)
 *
 *   c -- P -- d -- R -- e -- Q -- a0
 *     \                        /
 *         P               Q
 *      \    \          /
 *                b0   --  Q  --   a1
 *        \                     /
 *          P              Q
 *             \        /
 *                b1   --  Q  --   a2
 *                            .
 *                         .
 *                      .
 *                bN   --  Q --    aN+1
 */
@Test
public void testNguyen() {
    final int N = 9;
    SampleKBContext kb = NguyenKB.context(N);
    QueryBuilder qb = kb.tx().graql().infer(false);
    QueryBuilder iqb = kb.tx().graql().infer(true);
    String queryString = "match (N-rA: $x, N-rB: $y) isa N; $x has index 'c'; get $y;";
    String explicitQuery = "match $y isa a-entity; get;";
    List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
    List<Answer> explicitAnswers = qb.<GetQuery>parse(explicitQuery).execute();
    assertCollectionsEqual(answers, explicitAnswers);
    List<Answer> answers2 = iqb.materialise(true).<GetQuery>parse(queryString).execute();
    assertCollectionsEqual(answers, answers2);
}
Also used : Answer(ai.grakn.graql.admin.Answer) SampleKBContext(ai.grakn.test.rule.SampleKBContext) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 5 with SampleKBContext

use of ai.grakn.test.rule.SampleKBContext in project grakn by graknlabs.

the class RecursiveInferenceTest method testDualLinearTransitivityMatrix.

/**
 * test 6.1 from Cao p 71
 */
@Test
public void testDualLinearTransitivityMatrix() {
    final int N = 5;
    SampleKBContext kb = DualLinearTransitivityMatrixKB.context(N, N);
    QueryBuilder qb = kb.tx().graql().infer(false);
    QueryBuilder iqb = kb.tx().graql().infer(true);
    String queryString = "match (Q1-from: $x, Q1-to: $y) isa Q1; $x has index 'a0'; get $y;";
    String explicitQuery = "match $y isa a-entity or $y isa end; get;";
    assertQueriesEqual(iqb.materialise(false).parse(queryString), qb.parse(explicitQuery));
    assertQueriesEqual(iqb.materialise(true).parse(queryString), qb.parse(explicitQuery));
}
Also used : SampleKBContext(ai.grakn.test.rule.SampleKBContext) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Aggregations

SampleKBContext (ai.grakn.test.rule.SampleKBContext)17 Test (org.junit.Test)17 QueryBuilder (ai.grakn.graql.QueryBuilder)15 GetQuery (ai.grakn.graql.GetQuery)4 GraknTx (ai.grakn.GraknTx)2 Concept (ai.grakn.concept.Concept)2 Answer (ai.grakn.graql.admin.Answer)2