Search in sources :

Example 1 with ReasonerQueryImpl

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

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

the class QueryTest method testAlphaEquivalence_reifiedRelation.

// Bug #11150 Relations with resources as single VarPatternAdmin
// tests whether directly and indirectly reified relations are equivalent
@Test
public void testAlphaEquivalence_reifiedRelation() {
    EmbeddedGraknTx<?> graph = genealogySchema.tx();
    String patternString = "{$rel (happening: $b, protagonist: $p) isa event-protagonist has event-role 'parent';}";
    String patternString2 = "{$rel (happening: $c, protagonist: $r) isa event-protagonist; $rel has event-role 'parent';}";
    ReasonerQueryImpl query = ReasonerQueries.create(conjunction(patternString, graph), graph);
    ReasonerQueryImpl query2 = ReasonerQueries.create(conjunction(patternString2, graph), graph);
    queryEquivalence(query, query2, true);
}
Also used : ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Test(org.junit.Test)

Example 3 with ReasonerQueryImpl

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

the class QueryTest method testAlphaEquivalence_chainTreeAndLoopStructure.

// TODO
@Ignore
@Test
public void testAlphaEquivalence_chainTreeAndLoopStructure() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String chainString = "{" + "($x, $y) isa is-located-in;" + "($y, $z) isa is-located-in;" + "($z, $u) isa is-located-in;" + "}";
    String treeString = "{" + "($x, $y) isa is-located-in;" + "($y, $z) isa is-located-in;" + "($y, $u) isa is-located-in;" + "}";
    String loopString = "{" + "($x, $y) isa is-located-in;" + "($y, $z) isa is-located-in;" + "($z, $x) isa is-located-in;" + "}";
    ReasonerQueryImpl chainQuery = ReasonerQueries.create(conjunction(chainString, graph), graph);
    ReasonerQueryImpl treeQuery = ReasonerQueries.create(conjunction(treeString, graph), graph);
    ReasonerQueryImpl loopQuery = ReasonerQueries.create(conjunction(loopString, graph), graph);
    queryEquivalence(chainQuery, treeQuery, false);
    queryEquivalence(chainQuery, loopQuery, false);
    queryEquivalence(treeQuery, loopQuery, false);
}
Also used : ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with ReasonerQueryImpl

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

the class QueryTest method testQueryReiterationCondition_CyclicalRuleGraph.

@Test
public void testQueryReiterationCondition_CyclicalRuleGraph() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String patternString = "{($x, $y) isa is-located-in;}";
    ReasonerQueryImpl query = ReasonerQueries.create(conjunction(patternString, graph), graph);
    assertTrue(query.requiresReiteration());
}
Also used : ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Test(org.junit.Test)

Example 5 with ReasonerQueryImpl

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

the class QueryTest method testAlphaEquivalence_QueryCopyIsAlphaEquivalent.

// simple equality tests between original and a copy of a query
@Test
public void testAlphaEquivalence_QueryCopyIsAlphaEquivalent() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String patternString = "{$x isa city;$y isa country;($x, $y) isa is-located-in;}";
    ReasonerQueryImpl query = ReasonerQueries.create(conjunction(patternString, graph), graph);
    queryEquivalence(query, (ReasonerQueryImpl) query.copy(), true);
}
Also used : ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Test(org.junit.Test)

Aggregations

ReasonerQueryImpl (ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)28 Test (org.junit.Test)20 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)11 ResolutionPlan (ai.grakn.graql.internal.reasoner.plan.ResolutionPlan)7 Var (ai.grakn.graql.Var)5 HashSet (java.util.HashSet)5 ConceptId (ai.grakn.concept.ConceptId)4 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)4 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 Stream (java.util.stream.Stream)4 GraknTx (ai.grakn.GraknTx)3 Rule (ai.grakn.concept.Rule)3 SchemaConcept (ai.grakn.concept.SchemaConcept)3 Answer (ai.grakn.graql.admin.Answer)3 Atomic (ai.grakn.graql.admin.Atomic)3 Unifier (ai.grakn.graql.admin.Unifier)3 UnifierType (ai.grakn.graql.internal.reasoner.UnifierType)3 ValuePredicate (ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate)3 Sets (com.google.common.collect.Sets)3