Search in sources :

Example 31 with ReasonerAtomicQuery

use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.

the class QueryCacheTest method recordRetrieveAnswers.

@Test
public void recordRetrieveAnswers() {
    QueryCache<ReasonerAtomicQuery> cache = new QueryCache<>();
    QueryAnswers record = cache.record(recordQuery, new QueryAnswers(recordQuery.getQuery().execute()));
    assertEquals(record, cache.getAnswers(retrieveQuery).unify(retrieveToRecordUnifier));
    assertEquals(record, cache.getAnswers(recordQuery));
}
Also used : QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) Test(org.junit.Test)

Example 32 with ReasonerAtomicQuery

use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.

the class QueryCacheTest method lazilyGetUpdateRetrieveAnswers.

@Test
public void lazilyGetUpdateRetrieveAnswers() {
    LazyQueryCache<ReasonerAtomicQuery> cache = new LazyQueryCache<>();
    Answer retrieveSingleAnswer = singleAnswer.unify(recordToRetrieveUnifier);
    cache.record(recordQuery, recordQuery.getQuery().stream());
    LazyAnswerIterator retrieveIterator = cache.getAnswers(retrieveQuery);
    LazyAnswerIterator recordIterator = cache.getAnswers(recordQuery);
    cache.record(recordQuery, Stream.of(singleAnswer));
    Set<Answer> record = recordIterator.stream().collect(toSet());
    Set<Answer> retrieve = retrieveIterator.stream().map(ans -> ans.unify(retrieveToRecordUnifier)).collect(toSet());
    assertTrue(!retrieve.isEmpty());
    assertTrue(!retrieve.contains(singleAnswer));
    assertEquals(record, retrieve);
    assertTrue(cache.getAnswers(recordQuery).stream().anyMatch(ans -> ans.equals(singleAnswer)));
    assertTrue(cache.getAnswers(retrieveQuery).stream().anyMatch(ans -> ans.equals(retrieveSingleAnswer)));
}
Also used : GraknTestUtil(ai.grakn.util.GraknTestUtil) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) Entity(ai.grakn.concept.Entity) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) GraknTx(ai.grakn.GraknTx) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ClassRule(org.junit.ClassRule) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) Graql.var(ai.grakn.graql.Graql.var) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Stream(java.util.stream.Stream) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) Assert.assertFalse(org.junit.Assert.assertFalse) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Unifier(ai.grakn.graql.admin.Unifier) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 33 with ReasonerAtomicQuery

use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.

the class AtomicState method consumeAnswer.

@Override
Answer consumeAnswer(AnswerState state) {
    Answer answer;
    ReasonerAtomicQuery query = getQuery();
    Answer baseAnswer = state.getSubstitution();
    InferenceRule rule = state.getRule();
    Unifier unifier = state.getUnifier();
    if (rule == null)
        answer = state.getSubstitution();
    else {
        answer = rule.requiresMaterialisation(query.getAtom()) ? materialisedAnswer(baseAnswer, rule, unifier) : ruleAnswer(baseAnswer, rule, unifier);
    }
    return getCache().recordAnswerWithUnifier(query, answer, getCacheUnifier());
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Unifier(ai.grakn.graql.admin.Unifier)

Example 34 with ReasonerAtomicQuery

use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.

the class AtomicQueryTest method testExactUnification_TernaryRelation_ParentRepeatsMetaRoles_ParentRepeatsRPs.

@Test
public void testExactUnification_TernaryRelation_ParentRepeatsMetaRoles_ParentRepeatsRPs() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String parentString = "{(role: $x, role: $x, role2: $y) isa ternary;}";
    String childString = "{(role1: $u, role2: $v, role3: $q) isa ternary;}";
    String childString2 = "{(role1: $u, role2: $v, role2: $q) isa ternary;}";
    String childString3 = "{(role1: $u, role1: $v, role2: $q) isa ternary;}";
    String childString4 = "{(role1: $u, role1: $u, role2: $q) isa ternary;}";
    Conjunction<VarPatternAdmin> parentPattern = conjunction(parentString, graph);
    Conjunction<VarPatternAdmin> childPattern = conjunction(childString, graph);
    Conjunction<VarPatternAdmin> childPattern2 = conjunction(childString2, graph);
    Conjunction<VarPatternAdmin> childPattern3 = conjunction(childString3, graph);
    Conjunction<VarPatternAdmin> childPattern4 = conjunction(childString4, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(parentPattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(childPattern, graph);
    ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(childPattern2, graph);
    ReasonerAtomicQuery childQuery3 = ReasonerQueries.atomic(childPattern3, graph);
    ReasonerAtomicQuery childQuery4 = ReasonerQueries.atomic(childPattern4, graph);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("q"), var("x"), var("u"), var("x"), var("v"), var("y")));
    assertTrue(unifier.containsAll(correctUnifier));
    MultiUnifier unifier2 = childQuery2.getMultiUnifier(parentQuery);
    MultiUnifier correctUnifier2 = new MultiUnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("q"), var("x"), var("v"), var("y")), ImmutableMultimap.of(var("u"), var("x"), var("v"), var("x"), var("q"), var("y")));
    assertTrue(unifier2.containsAll(correctUnifier2));
    assertEquals(unifier2.size(), 2);
    Unifier unifier3 = childQuery3.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier3 = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("x"), var("q"), var("y")));
    assertTrue(unifier3.containsAll(correctUnifier3));
    Unifier unifier4 = childQuery4.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier4 = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("q"), var("y")));
    assertTrue(unifier4.containsAll(correctUnifier4));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 35 with ReasonerAtomicQuery

use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.

the class AtomicQueryTest method testExactUnification_TernaryRelation_ParentRepeatsMetaRoles.

@Test
public void testExactUnification_TernaryRelation_ParentRepeatsMetaRoles() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String parentString = "{(role: $x, role: $y, role2: $z) isa ternary;}";
    String childString = "{(role1: $u, role2: $v, role3: $q) isa ternary;}";
    String childString2 = "{(role1: $u, role2: $v, role2: $q) isa ternary;}";
    String childString3 = "{(role1: $u, role1: $v, role2: $q) isa ternary;}";
    String childString4 = "{(role1: $u, role1: $u, role2: $q) isa ternary;}";
    Conjunction<VarPatternAdmin> parentPattern = conjunction(parentString, graph);
    Conjunction<VarPatternAdmin> childPattern = conjunction(childString, graph);
    Conjunction<VarPatternAdmin> childPattern2 = conjunction(childString2, graph);
    Conjunction<VarPatternAdmin> childPattern3 = conjunction(childString3, graph);
    Conjunction<VarPatternAdmin> childPattern4 = conjunction(childString4, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(parentPattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(childPattern, graph);
    ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(childPattern2, graph);
    ReasonerAtomicQuery childQuery3 = ReasonerQueries.atomic(childPattern3, graph);
    ReasonerAtomicQuery childQuery4 = ReasonerQueries.atomic(childPattern4, graph);
    MultiUnifier unifier = childQuery.getMultiUnifier(parentQuery);
    MultiUnifier correctUnifier = new MultiUnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("z"), var("q"), var("y")), ImmutableMultimap.of(var("u"), var("y"), var("v"), var("z"), var("q"), var("x")));
    assertTrue(unifier.containsAll(correctUnifier));
    assertEquals(unifier.size(), 2);
    MultiUnifier unifier2 = childQuery2.getMultiUnifier(parentQuery);
    MultiUnifier correctUnifier2 = new MultiUnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("y"), var("q"), var("z")), ImmutableMultimap.of(var("u"), var("x"), var("v"), var("z"), var("q"), var("y")), ImmutableMultimap.of(var("u"), var("y"), var("v"), var("z"), var("q"), var("x")), ImmutableMultimap.of(var("u"), var("y"), var("v"), var("x"), var("q"), var("z")));
    assertTrue(unifier2.containsAll(correctUnifier2));
    assertEquals(unifier2.size(), 4);
    MultiUnifier unifier3 = childQuery3.getMultiUnifier(parentQuery);
    MultiUnifier correctUnifier3 = new MultiUnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("y"), var("q"), var("z")), ImmutableMultimap.of(var("u"), var("y"), var("v"), var("x"), var("q"), var("z")));
    assertTrue(unifier3.containsAll(correctUnifier3));
    assertEquals(unifier3.size(), 2);
    Unifier unifier4 = childQuery4.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier4 = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("u"), var("y"), var("q"), var("z")));
    assertTrue(unifier4.containsAll(correctUnifier4));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Aggregations

ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)43 Test (org.junit.Test)35 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)24 Unifier (ai.grakn.graql.admin.Unifier)19 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)18 Answer (ai.grakn.graql.admin.Answer)17 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)17 QueryCache (ai.grakn.graql.internal.reasoner.cache.QueryCache)8 Concept (ai.grakn.concept.Concept)7 Conjunction (ai.grakn.graql.admin.Conjunction)7 Patterns (ai.grakn.graql.internal.pattern.Patterns)7 LazyQueryCache (ai.grakn.graql.internal.reasoner.cache.LazyQueryCache)7 ReasonerQueries (ai.grakn.graql.internal.reasoner.query.ReasonerQueries)7 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)7 Set (java.util.Set)7 Collectors.toSet (java.util.stream.Collectors.toSet)7 GetQuery (ai.grakn.graql.GetQuery)6 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)6 QueryAnswers (ai.grakn.graql.internal.reasoner.query.QueryAnswers)6 SampleKBContext (ai.grakn.test.rule.SampleKBContext)6