Search in sources :

Example 1 with ReasonerAtomicQuery

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;
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) SchemaConcept(ai.grakn.concept.SchemaConcept) Answer(ai.grakn.graql.admin.Answer) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) Rule(ai.grakn.concept.Rule) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) Conjunction(ai.grakn.graql.admin.Conjunction) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) List(java.util.List) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) RuleState(ai.grakn.graql.internal.reasoner.state.RuleState) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Unifier(ai.grakn.graql.admin.Unifier) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) ArrayList(java.util.ArrayList) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)

Example 2 with ReasonerAtomicQuery

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

Example 3 with ReasonerAtomicQuery

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);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 4 with ReasonerAtomicQuery

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

Example 5 with ReasonerAtomicQuery

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