Search in sources :

Example 1 with InferenceRule

use of ai.grakn.graql.internal.reasoner.rule.InferenceRule in project grakn by graknlabs.

the class ReasonerTest method testTwoRulesOnlyDifferingByVarNamesAreEquivalent.

@Test
public void testTwoRulesOnlyDifferingByVarNamesAreEquivalent() {
    EmbeddedGraknTx<?> tx = testGeoKB.tx();
    Rule rule1 = tx.getRule("Geo Rule");
    Pattern body2 = Graql.and(tx.graql().parser().parsePatterns("(geo-entity: $l1, entity-location: $l2) isa is-located-in;" + "(geo-entity: $l2, entity-location: $l3) isa is-located-in;"));
    Pattern head2 = Graql.and(tx.graql().parser().parsePatterns("(geo-entity: $l1, entity-location: $l3) isa is-located-in;"));
    Rule rule2 = tx.putRule("Rule 2", body2, head2);
    InferenceRule R1 = new InferenceRule(rule1, tx);
    InferenceRule R2 = new InferenceRule(rule2, tx);
    assertEquals(R1, R2);
}
Also used : Pattern(ai.grakn.graql.Pattern) Rule(ai.grakn.concept.Rule) ClassRule(org.junit.ClassRule) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) Test(org.junit.Test)

Example 2 with InferenceRule

use of ai.grakn.graql.internal.reasoner.rule.InferenceRule in project grakn by graknlabs.

the class AtomicTest method testRewriteAndUnification.

@Test
public void testRewriteAndUnification() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    String parentString = "{$r (subRole1: $x) isa binary;}";
    Atom parentAtom = ReasonerQueries.atomic(conjunction(parentString, graph), graph).getAtom();
    Var parentVarName = parentAtom.getVarName();
    String childPatternString = "(subRole1: $x, subRole2: $y) isa binary";
    InferenceRule testRule = new InferenceRule(graph.putRule("Checking Rewrite & Unification", graph.graql().parser().parsePattern(childPatternString), graph.graql().parser().parsePattern(childPatternString)), graph).rewrite(parentAtom);
    RelationshipAtom headAtom = (RelationshipAtom) testRule.getHead().getAtom();
    Var headVarName = headAtom.getVarName();
    Unifier unifier = Iterables.getOnlyElement(testRule.getMultiUnifier(parentAtom));
    Unifier correctUnifier = new UnifierImpl(ImmutableMap.of(var("x"), var("x"), headVarName, parentVarName));
    assertTrue(unifier.containsAll(correctUnifier));
    Multimap<Role, Var> roleMap = roleSetMap(headAtom.getRoleVarMap());
    Collection<Var> wifeEntry = roleMap.get(graph.getRole("subRole1"));
    assertEquals(wifeEntry.size(), 1);
    assertEquals(wifeEntry.iterator().next(), var("x"));
}
Also used : Role(ai.grakn.concept.Role) Var(ai.grakn.graql.Var) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 3 with InferenceRule

use of ai.grakn.graql.internal.reasoner.rule.InferenceRule in project grakn by graknlabs.

the class AtomicTest method testRuleApplicability_DerivedTypes.

@Test
public void testRuleApplicability_DerivedTypes() {
    EmbeddedGraknTx<?> graph = ruleApplicabilitySet.tx();
    String typeString = "{$x isa reifying-relation;}";
    String typeString2 = "{$x isa typed-relation;}";
    String typeString3 = "{$x isa description;}";
    String typeString4 = "{$x isa attribute;}";
    String typeString5 = "{$x isa relationship;}";
    Atom type = ReasonerQueries.atomic(conjunction(typeString, graph), graph).getAtom();
    Atom type2 = ReasonerQueries.atomic(conjunction(typeString2, graph), graph).getAtom();
    Atom type3 = ReasonerQueries.atomic(conjunction(typeString3, graph), graph).getAtom();
    Atom type4 = ReasonerQueries.atomic(conjunction(typeString4, graph), graph).getAtom();
    Atom type5 = ReasonerQueries.atomic(conjunction(typeString5, graph), graph).getAtom();
    List<InferenceRule> rules = RuleUtils.getRules(graph).map(r -> new InferenceRule(r, graph)).collect(Collectors.toList());
    assertEquals(2, type.getApplicableRules().count());
    assertEquals(1, type2.getApplicableRules().count());
    assertEquals(3, type3.getApplicableRules().count());
    assertEquals(rules.stream().filter(r -> r.getHead().getAtom().isResource()).count(), type4.getApplicableRules().count());
    assertEquals(rules.stream().filter(r -> r.getHead().getAtom().isRelation()).count(), type5.getApplicableRules().count());
}
Also used : Iterables(com.google.common.collect.Iterables) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) GraqlTestUtil.assertCollectionsEqual(ai.grakn.util.GraqlTestUtil.assertCollectionsEqual) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Assert.assertThat(org.junit.Assert.assertThat) HashMultimap(com.google.common.collect.HashMultimap) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) ClassRule(org.junit.ClassRule) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) Patterns(ai.grakn.graql.internal.pattern.Patterns) Graql.var(ai.grakn.graql.Graql.var) Matchers.empty(org.hamcrest.Matchers.empty) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Collection(java.util.Collection) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) Assert.assertEquals(org.junit.Assert.assertEquals) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) Test(org.junit.Test)

Example 4 with InferenceRule

use of ai.grakn.graql.internal.reasoner.rule.InferenceRule in project grakn by graknlabs.

the class AtomicState method consumeAnswer.

@Override
Answer consumeAnswer(AnswerState state) {
    Answer answer;
    ReasonerAtomicQuery query = getQuery();
    Answer baseAnswer = state.getSubstitution();
    InferenceRule rule = state.getRule();
    Unifier unifier = state.getUnifier();
    if (rule == null)
        answer = state.getSubstitution();
    else {
        answer = rule.requiresMaterialisation(query.getAtom()) ? materialisedAnswer(baseAnswer, rule, unifier) : ruleAnswer(baseAnswer, rule, unifier);
    }
    return getCache().recordAnswerWithUnifier(query, answer, getCacheUnifier());
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Unifier(ai.grakn.graql.admin.Unifier)

Example 5 with InferenceRule

use of ai.grakn.graql.internal.reasoner.rule.InferenceRule in project grakn by graknlabs.

the class ReasonerAtomicQuery method answerStream.

/**
 * resolves the query by performing lookups and rule resolution and returns a stream of new answers
 * @param subGoals visited subGoals (recursive queries)
 * @param cache global query cache
 * @param dCache differential query cache
 * @return stream of differential answers
 */
Stream<Answer> answerStream(Set<ReasonerAtomicQuery> subGoals, Cache<ReasonerAtomicQuery, ?> cache, Cache<ReasonerAtomicQuery, ?> dCache, boolean differentialJoin) {
    boolean queryAdmissible = !subGoals.contains(this);
    LOG.trace("AQ: " + this);
    Stream<Answer> answerStream = cache.contains(this) ? Stream.empty() : dCache.record(this, cache.getAnswerStream(this));
    if (queryAdmissible) {
        Iterator<Pair<InferenceRule, Unifier>> ruleIterator = getRuleStream().iterator();
        while (ruleIterator.hasNext()) {
            Pair<InferenceRule, Unifier> ruleContext = ruleIterator.next();
            Unifier unifier = ruleContext.getValue();
            Unifier unifierInverse = unifier.inverse();
            Answer sub = this.getSubstitution().unify(unifierInverse);
            InferenceRule rule = ruleContext.getKey().propagateConstraints(getAtom(), unifierInverse).withSubstitution(sub);
            Stream<Answer> localStream = resolveViaRule(rule, unifier, subGoals, cache, dCache, differentialJoin);
            answerStream = Stream.concat(answerStream, localStream);
        }
    }
    return dCache.record(this, answerStream);
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) ReasonerUtils.typeUnifier(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.typeUnifier) Unifier(ai.grakn.graql.admin.Unifier) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Aggregations

InferenceRule (ai.grakn.graql.internal.reasoner.rule.InferenceRule)5 Unifier (ai.grakn.graql.admin.Unifier)4 Answer (ai.grakn.graql.admin.Answer)3 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)3 Test (org.junit.Test)3 Role (ai.grakn.concept.Role)2 Var (ai.grakn.graql.Var)2 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)2 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)2 RelationshipAtom (ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom)2 ResourceAtom (ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom)2 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)2 Concept (ai.grakn.concept.Concept)1 Rule (ai.grakn.concept.Rule)1 GetQuery (ai.grakn.graql.GetQuery)1 Graql.var (ai.grakn.graql.Graql.var)1 Pattern (ai.grakn.graql.Pattern)1 Atomic (ai.grakn.graql.admin.Atomic)1 Conjunction (ai.grakn.graql.admin.Conjunction)1 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)1