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);
}
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);
}
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));
}
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());
}
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));
}
Aggregations