Search in sources :

Example 26 with QueryAnswer

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

the class AtomicQueryTest method testWhenMaterialisingResources_MaterialisedInformationIsCorrectlyFlaggedAsInferred.

@Test
public void testWhenMaterialisingResources_MaterialisedInformationIsCorrectlyFlaggedAsInferred() {
    EmbeddedGraknTx<?> graph = materialisationTestSet.tx();
    QueryBuilder qb = graph.graql().infer(false);
    Concept firstEntity = Iterables.getOnlyElement(qb.<GetQuery>parse("match $x isa entity1; get;").execute()).get("x");
    Concept secondEntity = Iterables.getOnlyElement(qb.<GetQuery>parse("match $x isa entity2; get;").execute()).get("x");
    Concept resource = Iterables.getOnlyElement(qb.<GetQuery>parse("match $x isa resource; get;").execute()).get("x");
    ReasonerAtomicQuery resourceQuery = ReasonerQueries.atomic(conjunction("{$x has resource $r;$r val 'inferred';$x id " + firstEntity.getId().getValue() + ";}", graph), graph);
    String reuseResourcePatternString = "{" + "$x has resource $r;" + "$x id " + secondEntity.getId().getValue() + ";" + "$r id " + resource.getId().getValue() + ";" + "}";
    ReasonerAtomicQuery reuseResourceQuery = ReasonerQueries.atomic(conjunction(reuseResourcePatternString, graph), graph);
    assertEquals(resourceQuery.materialise(new QueryAnswer()).findFirst().orElse(null).get("r").asAttribute().isInferred(), true);
    reuseResourceQuery.materialise(new QueryAnswer()).collect(Collectors.toList());
    assertEquals(Iterables.getOnlyElement(qb.<GetQuery>parse("match" + "$x has resource $r via $rel;" + "$x id " + secondEntity.getId().getValue() + ";" + "$r id " + resource.getId().getValue() + ";" + "get;").execute()).get("rel").asRelationship().isInferred(), true);
    assertEquals(Iterables.getOnlyElement(qb.<GetQuery>parse("match" + "$x has resource $r via $rel;" + "$x id " + firstEntity.getId().getValue() + ";" + "$r id " + resource.getId().getValue() + ";" + "get;").execute()).get("rel").asRelationship().isInferred(), false);
}
Also used : Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 27 with QueryAnswer

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

the class AtomicQueryTest method testWhenMaterialising_MaterialisedInformationIsPresentInGraph.

@Test
public void testWhenMaterialising_MaterialisedInformationIsPresentInGraph() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    QueryBuilder qb = graph.graql().infer(false);
    String explicitQuery = "match (geo-entity: $x, entity-location: $y) isa is-located-in;$x has name 'Warsaw';$y has name 'Poland'; get;";
    assertTrue(!qb.<GetQuery>parse(explicitQuery).iterator().hasNext());
    String patternString = "{(geo-entity: $x, entity-location: $y) isa is-located-in;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    List<Answer> answers = new ArrayList<>();
    answers.add(new QueryAnswer(ImmutableMap.of(var("x"), getConceptByResourceValue(graph, "Warsaw"), var("y"), getConceptByResourceValue(graph, "Poland"))));
    ReasonerAtomicQuery atomicQuery = ReasonerQueries.atomic(pattern, graph);
    assertNotExists(qb.parse(explicitQuery));
    answers.forEach(atomicQuery::materialise);
    assertExists(qb.parse(explicitQuery));
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) GetQuery(ai.grakn.graql.GetQuery) ArrayList(java.util.ArrayList) QueryBuilder(ai.grakn.graql.QueryBuilder) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 28 with QueryAnswer

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

the class AtomicQueryTest method testWhenMaterialisingEntity_MaterialisedInformationIsCorrectlyFlaggedAsInferred.

@Test
public void testWhenMaterialisingEntity_MaterialisedInformationIsCorrectlyFlaggedAsInferred() {
    EmbeddedGraknTx<?> graph = materialisationTestSet.tx();
    ReasonerAtomicQuery entityQuery = ReasonerQueries.atomic(conjunction("$x isa newEntity", graph), graph);
    assertEquals(entityQuery.materialise(new QueryAnswer()).findFirst().orElse(null).get("x").asEntity().isInferred(), true);
}
Also used : QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 29 with QueryAnswer

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

the class GetQueryPropertyTest method joinAnswer.

private Optional<Answer> joinAnswer(Answer answerA, Answer answerB) {
    Map<Var, Concept> answer = Maps.newHashMap(answerA.map());
    answer.putAll(answerB.map());
    for (Var var : Sets.intersection(answerA.vars(), answerB.vars())) {
        if (!answerA.get(var).equals(answerB.get(var))) {
            return Optional.empty();
        }
    }
    return Optional.of(new QueryAnswer(answer));
}
Also used : Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Var(ai.grakn.graql.Var)

Aggregations

QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)29 Answer (ai.grakn.graql.admin.Answer)15 Concept (ai.grakn.concept.Concept)13 Test (org.junit.Test)13 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)9 Var (ai.grakn.graql.Var)8 GetQuery (ai.grakn.graql.GetQuery)6 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5 ConceptId (ai.grakn.concept.ConceptId)4 Type (ai.grakn.concept.Type)4 Unifier (ai.grakn.graql.admin.Unifier)4 MultiUnifierImpl (ai.grakn.graql.internal.reasoner.MultiUnifierImpl)4 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)4 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)4 GraqlQueryException (ai.grakn.exception.GraqlQueryException)3 RelationshipAtom (ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 List (java.util.List)3 Set (java.util.Set)3