Search in sources :

Example 21 with ReasonerAtomicQuery

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

the class QueryCacheTest method getUpdateRetrieveAnswerStream.

@Test
public void getUpdateRetrieveAnswerStream() {
    QueryCache<ReasonerAtomicQuery> cache = new QueryCache<>();
    Answer answer = recordQuery.getQuery().stream().findFirst().orElse(null);
    Answer retrieveAnswer = answer.unify(recordToRetrieveUnifier);
    Answer retrieveSingleAnswer = singleAnswer.unify(recordToRetrieveUnifier);
    Stream<Answer> recordStream = cache.getAnswerStream(recordQuery);
    Stream<Answer> retrieveStream = cache.getAnswerStream(retrieveQuery);
    cache.recordAnswer(recordQuery, singleAnswer);
    QueryAnswers recordAnswers = new QueryAnswers(recordStream.collect(Collectors.toSet()));
    QueryAnswers retrieveAnswers = new QueryAnswers(retrieveStream.collect(Collectors.toSet()));
    // NB: not expecting the update in the stream
    assertTrue(recordAnswers.contains(answer));
    assertTrue(retrieveAnswers.contains(retrieveAnswer));
    assertFalse(recordAnswers.contains(singleAnswer));
    assertFalse(retrieveAnswers.contains(retrieveSingleAnswer));
    assertTrue(cache.getAnswers(recordQuery).contains(singleAnswer));
    assertTrue(cache.getAnswers(retrieveQuery).contains(retrieveSingleAnswer));
}
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) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) Test(org.junit.Test)

Example 22 with ReasonerAtomicQuery

use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery 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 23 with ReasonerAtomicQuery

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

the class QueryCacheTest method recordUpdateRetrieveAnswers.

@Test
public void recordUpdateRetrieveAnswers() {
    QueryCache<ReasonerAtomicQuery> cache = new QueryCache<>();
    cache.record(recordQuery, new QueryAnswers(recordQuery.getQuery().execute()));
    cache.recordAnswer(recordQuery, singleAnswer);
    assertTrue(cache.getAnswers(recordQuery).contains(singleAnswer));
    assertTrue(cache.getAnswers(retrieveQuery).contains(singleAnswer.unify(recordToRetrieveUnifier)));
}
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 24 with ReasonerAtomicQuery

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

the class AtomicTest method testAtomsAreCorrectlyIdentifiedAsRecursive.

@Test
public void testAtomsAreCorrectlyIdentifiedAsRecursive() {
    EmbeddedGraknTx<?> graph = ruleApplicabilitySet.tx();
    String recRelString = "{($x, $y) isa binary;}";
    String nrecRelString = "{($x, $y) isa ternary;}";
    ReasonerAtomicQuery recQuery = ReasonerQueries.atomic(conjunction(recRelString, graph), graph);
    ReasonerAtomicQuery nrecQuery = ReasonerQueries.atomic(conjunction(nrecRelString, graph), graph);
    assertTrue(recQuery.getAtom().isRecursive());
    assertTrue(!nrecQuery.getAtom().isRecursive());
}
Also used : ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 25 with ReasonerAtomicQuery

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

the class TypeInferenceQueryTest method typeInference.

private void typeInference(List<RelationshipType> possibleTypes, String pattern, String subbedPattern, EmbeddedGraknTx<?> graph) {
    ReasonerAtomicQuery query = ReasonerQueries.atomic(conjunction(pattern, graph), graph);
    ReasonerAtomicQuery subbedQuery = ReasonerQueries.atomic(conjunction(subbedPattern, graph), graph);
    RelationshipAtom atom = (RelationshipAtom) query.getAtom();
    RelationshipAtom subbedAtom = (RelationshipAtom) subbedQuery.getAtom();
    List<Type> relationshipTypes = atom.inferPossibleTypes(new QueryAnswer());
    List<Type> subbedRelationshipTypes = subbedAtom.inferPossibleTypes(new QueryAnswer());
    if (possibleTypes.size() == 1) {
        assertEquals(possibleTypes, relationshipTypes);
        assertEquals(relationshipTypes, subbedRelationshipTypes);
        assertEquals(atom.getSchemaConcept(), Iterables.getOnlyElement(possibleTypes));
        assertEquals(subbedAtom.getSchemaConcept(), Iterables.getOnlyElement(possibleTypes));
    } else {
        assertTrue(CollectionUtils.isEqualCollection(possibleTypes, relationshipTypes));
        assertTrue(CollectionUtils.isEqualCollection(relationshipTypes, subbedRelationshipTypes));
        assertEquals(atom.getSchemaConcept(), null);
        assertEquals(subbedAtom.getSchemaConcept(), null);
    }
    typeInferenceQueries(possibleTypes, pattern, graph);
    typeInferenceQueries(possibleTypes, subbedPattern, graph);
}
Also used : Type(ai.grakn.concept.Type) RelationshipType(ai.grakn.concept.RelationshipType) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)

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