Search in sources :

Example 81 with Answer

use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.

the class QueryCacheTest method recordRetrieveSingleAnswer.

@Test
public void recordRetrieveSingleAnswer() {
    QueryCache<ReasonerAtomicQuery> cache = new QueryCache<>();
    Answer answer = recordQuery.getQuery().stream().findFirst().orElse(null);
    Answer retrieveAnswer = answer.unify(recordToRetrieveUnifier);
    cache.recordAnswer(recordQuery, answer);
    assertEquals(cache.getAnswer(recordQuery, new QueryAnswer()), new QueryAnswer());
    assertEquals(cache.getAnswer(recordQuery, answer), answer);
    assertEquals(cache.getAnswer(recordQuery, retrieveAnswer), answer);
    assertEquals(cache.getAnswer(retrieveQuery, new QueryAnswer()), new QueryAnswer());
    assertEquals(cache.getAnswer(retrieveQuery, retrieveAnswer), retrieveAnswer);
    assertEquals(cache.getAnswer(retrieveQuery, answer), retrieveAnswer);
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 82 with Answer

use of ai.grakn.graql.admin.Answer 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 83 with Answer

use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.

the class GenealogyTest method testGrandParentship.

@Test
public void testGrandParentship() {
    String queryString = "match " + "(grandchild: $x); (granddaughter: $x);$x has gender $g; get;";
    GetQuery query = iqb.parse(queryString);
    List<Answer> answers = query.execute();
    assertEquals(answers.size(), 18);
    assertTrue(checkResource(answers, "g", "female"));
    assertCollectionsEqual(answers, qb.<GetQuery>parse(queryString).execute());
}
Also used : Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Example 84 with Answer

use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.

the class GenealogyTest method matchAllRelationsWithDocumentPlayingARole.

/*
    test for first rule file:
    match $x isa person has first-name $name;
    match (child: $x, parent: $y) isa parentship;
    match (spouse: $x, spouse: $y) isa marriage;
    */
@Test
public void matchAllRelationsWithDocumentPlayingARole() {
    String queryString = "match $x isa document; ($x, $y); get;";
    GetQuery query = iqb.parse(queryString);
    List<Answer> answers = query.execute();
    assertTrue(answers.isEmpty());
}
Also used : Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Example 85 with Answer

use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.

the class GenealogyTest method testParentship.

@Test
public void testParentship() {
    String queryString = "match (child: $c, parent: $p) isa parentship; get;";
    GetQuery query = iqb.parse(queryString);
    List<Answer> answers = query.execute();
    assertEquals(answers.size(), 76);
    assertTrue(!hasDuplicates(answers));
    answers.forEach(answer -> assertEquals(answer.size(), 2));
}
Also used : Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Aggregations

Answer (ai.grakn.graql.admin.Answer)193 Test (org.junit.Test)164 QueryBuilder (ai.grakn.graql.QueryBuilder)104 GetQuery (ai.grakn.graql.GetQuery)69 GraknTx (ai.grakn.GraknTx)47 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)37 Concept (ai.grakn.concept.Concept)32 Var (ai.grakn.graql.Var)21 Set (java.util.Set)21 Unifier (ai.grakn.graql.admin.Unifier)19 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)18 Collectors (java.util.stream.Collectors)18 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 Sets (com.google.common.collect.Sets)16 List (java.util.List)16 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)15 Stream (java.util.stream.Stream)15 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)14 SampleKBContext (ai.grakn.test.rule.SampleKBContext)14 Pair (ai.grakn.graql.internal.reasoner.utils.Pair)12