Search in sources :

Example 36 with ReasonerAtomicQuery

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

the class AtomicQueryTest method queryEquivalence.

private void queryEquivalence(String patternA, String patternB, boolean queryExpectation, boolean atomExpectation, boolean structuralExpectation, EmbeddedGraknTx<?> graph) {
    ReasonerAtomicQuery a = ReasonerQueries.atomic(conjunction(patternA, graph), graph);
    ReasonerAtomicQuery b = ReasonerQueries.atomic(conjunction(patternB, graph), graph);
    queryEquivalence(a, b, queryExpectation, ReasonerQueryEquivalence.AlphaEquivalence);
    queryEquivalence(a, b, structuralExpectation, ReasonerQueryEquivalence.StructuralEquivalence);
    atomicEquivalence(a.getAtom(), b.getAtom(), atomExpectation);
}
Also used : ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)

Example 37 with ReasonerAtomicQuery

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

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

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

the class AtomicQueryTest method testWhenCopying_TheCopyIsAlphaEquivalent.

@Test
public void testWhenCopying_TheCopyIsAlphaEquivalent() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String patternString = "{($x, $y) isa is-located-in;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    ReasonerAtomicQuery atomicQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery copy = ReasonerQueries.atomic(atomicQuery);
    assertEquals(atomicQuery, copy);
    assertEquals(atomicQuery.hashCode(), copy.hashCode());
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 40 with ReasonerAtomicQuery

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

the class AtomicQueryTest method testExactUnification_BinaryRelationWithTypes_SomeVarsHaveTypes_UnifierMatchesTypes.

// only a single unifier exists
@Test
public void testExactUnification_BinaryRelationWithTypes_SomeVarsHaveTypes_UnifierMatchesTypes() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String patternString = "{$x1 isa twoRoleEntity;($x1, $x2) isa binary;}";
    String patternString2 = "{$y1 isa twoRoleEntity;($y1, $y2) isa binary;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2")));
    assertTrue(unifier.containsAll(correctUnifier));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) 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