Search in sources :

Example 76 with Answer

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

the class InsertQueryTest method whenInsertingMultipleRolePlayers_BothRolePlayersAreAdded.

@Test
public void whenInsertingMultipleRolePlayers_BothRolePlayersAreAdded() {
    List<Answer> results = qb.match(var("g").has("title", "Godfather"), var("m").has("title", "The Muppets")).insert(var("c").isa("cluster").has("name", "2"), var("r").rel("cluster-of-production", "c").rel("production-with-cluster", "g").rel("production-with-cluster", "m").isa("has-cluster")).execute();
    Thing cluster = results.get(0).get("c").asThing();
    Thing godfather = results.get(0).get("g").asThing();
    Thing muppets = results.get(0).get("m").asThing();
    Relationship relationship = results.get(0).get("r").asRelationship();
    Role clusterOfProduction = movieKB.tx().getRole("cluster-of-production");
    Role productionWithCluster = movieKB.tx().getRole("production-with-cluster");
    assertEquals(relationship.rolePlayers().collect(toSet()), ImmutableSet.of(cluster, godfather, muppets));
    assertEquals(relationship.rolePlayers(clusterOfProduction).collect(toSet()), ImmutableSet.of(cluster));
    assertEquals(relationship.rolePlayers(productionWithCluster).collect(toSet()), ImmutableSet.of(godfather, muppets));
}
Also used : Role(ai.grakn.concept.Role) Answer(ai.grakn.graql.admin.Answer) Relationship(ai.grakn.concept.Relationship) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 77 with Answer

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

the class GraqlPrinterTest method testEmptyResult.

@Test
public void testEmptyResult() {
    Printer printer = Printers.graql(true);
    Answer emptyResult = new QueryAnswer();
    assertEquals("{}", printer.graqlString(emptyResult));
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Printer(ai.grakn.graql.Printer) Test(org.junit.Test)

Example 78 with Answer

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

the class ExplanationTest method testExplanationConsistency.

@Test
public void testExplanationConsistency() {
    GraknTx genealogyGraph = genealogyKB.tx();
    final long limit = 3;
    QueryBuilder iqb = genealogyGraph.graql().infer(true);
    String queryString = "match " + "($x, $y) isa cousins;" + "limit " + limit + ";" + "get;";
    List<Answer> answers = iqb.<GetQuery>parse(queryString).execute();
    assertEquals(answers.size(), limit);
    answers.forEach(answer -> {
        testExplanation(answer);
        String specificQuery = "match " + "$x id '" + answer.get(var("x")).getId().getValue() + "';" + "$y id '" + answer.get(var("y")).getId().getValue() + "';" + "(cousin: $x, cousin: $y) isa cousins;" + "limit 1; get;";
        Answer specificAnswer = Iterables.getOnlyElement(iqb.<GetQuery>parse(specificQuery).execute());
        assertEquals(answer, specificAnswer);
        testExplanation(specificAnswer);
    });
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 79 with Answer

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

the class ExplanationTest method testExplainingConjunctiveQueryWithTwoIdPredicates.

@Test
public void testExplainingConjunctiveQueryWithTwoIdPredicates() {
    String queryString = "match " + "(geo-entity: $x, entity-location: $y) isa is-located-in;" + "(geo-entity: $y, entity-location: $z) isa is-located-in;" + "$x id '" + polibuda.getId() + "';" + "$z id '" + masovia.getId() + "';" + "get $y;";
    GetQuery query = iqb.parse(queryString);
    List<Answer> answers = query.execute();
    assertEquals(answers.size(), 1);
    testExplanation(answers);
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Example 80 with Answer

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

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