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