use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class InferenceRule method rewriteVariables.
private InferenceRule rewriteVariables(Atom parentAtom) {
if (parentAtom.isUserDefined() || parentAtom.requiresRoleExpansion()) {
ReasonerAtomicQuery rewrittenHead = ReasonerQueries.atomic(head.getAtom().rewriteToUserDefined(parentAtom));
List<Atom> bodyRewrites = new ArrayList<>();
body.getAtoms(Atom.class).map(at -> {
if (at.isRelation() && !at.isUserDefined() && Objects.equals(at.getSchemaConcept(), head.getAtom().getSchemaConcept())) {
return at.rewriteToUserDefined(parentAtom);
} else {
return at;
}
}).forEach(bodyRewrites::add);
ReasonerQueryImpl rewrittenBody = ReasonerQueries.create(bodyRewrites, tx);
return new InferenceRule(rewrittenHead, rewrittenBody, ruleId, tx);
}
return this;
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class AtomicQueryTest method testExactUnification_TernaryRelationWithTypes_RepeatingRelationPlayers_withMetaRoles.
@Test
public void testExactUnification_TernaryRelationWithTypes_RepeatingRelationPlayers_withMetaRoles() {
EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
String patternString = "{$x1 isa threeRoleEntity;$x3 isa threeRoleEntity3;($x1, $x2, $x3) isa ternary;}";
String patternString2 = "{$y3 isa threeRoleEntity3;$y1 isa threeRoleEntity;($y2, $y3, $y1) isa ternary;}";
String patternString3 = "{$y3 isa threeRoleEntity3;$y2 isa threeRoleEntity2;$y1 isa threeRoleEntity;(role2: $y2, role3: $y3, role1: $y1) isa ternary;}";
Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
Conjunction<VarPatternAdmin> pattern3 = conjunction(patternString3, graph);
ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(pattern3, graph);
Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
Unifier unifier2 = childQuery2.getMultiUnifier(parentQuery).getUnifier();
Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2"), var("y3"), var("x3")));
assertTrue(unifier.containsAll(correctUnifier));
assertTrue(unifier2.containsAll(correctUnifier));
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class AtomicQueryTest method testWhenConstructingNonAtomicQuery_ExceptionIsThrown.
@Test
public void testWhenConstructingNonAtomicQuery_ExceptionIsThrown() {
EmbeddedGraknTx<?> graph = geoKB.tx();
String patternString = "{$x isa university;$y isa country;($x, $y) isa is-located-in;($y, $z) isa is-located-in;}";
Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
exception.expect(GraqlQueryException.class);
ReasonerAtomicQuery atomicQuery = ReasonerQueries.atomic(pattern, graph);
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class AtomicQueryTest method testWhenUnifiyingAtomWithItself_UnifierIsIdentity.
@Test
public void testWhenUnifiyingAtomWithItself_UnifierIsIdentity() {
EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
String patternString = "{$x1 isa twoRoleEntity;$x2 isa twoRoleEntity2;($x1, $x2) isa binary;}";
Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern, graph);
Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
Unifier unifier2 = parentQuery.getMultiUnifier(parentQuery).getUnifier();
Unifier unifier3 = childQuery.getMultiUnifier(childQuery).getUnifier();
assertTrue(Sets.intersection(unifier.keySet(), Sets.newHashSet(var("x"), var("y"))).isEmpty());
assertTrue(unifier2.isEmpty());
assertTrue(unifier3.isEmpty());
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class AtomicQueryTest method testExactUnification_TernaryRelationWithTypes_SomeVarsHaveTypes_UnifierMatchesTypes.
@Test
public void testExactUnification_TernaryRelationWithTypes_SomeVarsHaveTypes_UnifierMatchesTypes() {
EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
String patternString = "{$x1 isa threeRoleEntity;$x3 isa threeRoleEntity3;($x1, $x2, $x3) isa ternary;}";
String patternString2 = "{$y3 isa threeRoleEntity3;$y1 isa threeRoleEntity;($y2, $y3, $y1) isa ternary;}";
String patternString3 = "{$y3 isa threeRoleEntity3;$y2 isa threeRoleEntity2;$y1 isa threeRoleEntity;(role2: $y2, role3: $y3, role1: $y1) isa ternary;}";
Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
Conjunction<VarPatternAdmin> pattern3 = conjunction(patternString3, graph);
ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(pattern3, graph);
Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
Unifier unifier2 = childQuery2.getMultiUnifier(parentQuery).getUnifier();
Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2"), var("y3"), var("x3")));
assertTrue(unifier.containsAll(correctUnifier));
assertTrue(unifier2.containsAll(correctUnifier));
}
Aggregations