use of com.vaticle.typedb.core.reasoner.resolution.answer.Explanation in project grakn by graknlabs.
the class ExplanationProducer method requestAnswered.
// note: root resolver calls this single-threaded, so is threads safe
private void requestAnswered(Explain.Finished explainedAnswer) {
if (options.traceInference())
ResolutionTracer.get().finish();
Explanation explanation = explainedAnswer.explanation();
explainablesManager.setAndRecordExplainables(explanation.conditionAnswer());
queue.put(explanation);
if (required.decrementAndGet() > 0)
requestExplanation();
else
processing.decrementAndGet();
}
use of com.vaticle.typedb.core.reasoner.resolution.answer.Explanation in project grakn by graknlabs.
the class ExplanationTest method assertSingleExplainableExplanations.
private List<Explanation> assertSingleExplainableExplanations(ConceptMap ans, int anonymousConcepts, int explainablesCount, int explanationsCount, CoreTransaction txn) {
List<ConceptMap.Explainable> explainables = ans.explainables().iterator().toList();
assertEquals(anonymousConcepts, iterate(ans.concepts().keySet()).filter(Identifier::isAnonymous).count());
assertEquals(explainablesCount, explainables.size());
ConceptMap.Explainable explainable = explainables.iterator().next();
assertNotEquals(NOT_IDENTIFIED, explainable.id());
List<Explanation> explanations = txn.query().explain(explainable.id()).toList();
assertEquals(explanationsCount, explanations.size());
explanations.forEach(explanation -> {
Map<Retrievable, Set<Retrievable>> mapping = explanation.variableMapping();
ConceptMap projected = applyMapping(mapping, ans);
projected.concepts().forEach((var, concept) -> {
assertTrue(explanation.conclusionAnswer().contains(var));
assertEquals(explanation.conclusionAnswer().get(var), concept);
});
});
return explanations;
}
use of com.vaticle.typedb.core.reasoner.resolution.answer.Explanation in project grakn by graknlabs.
the class ExplanationTest method test_all_transitive_explanations.
@Test
public void test_all_transitive_explanations() {
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
LogicManager logicMgr = txn.logic();
txn.query().define(TypeQL.parseQuery("define " + "location sub entity, " + " plays location-hierarchy:superior, " + " plays location-hierarchy:subordinate; " + "location-hierarchy sub relation," + " relates superior," + " relates subordinate;").asDefine());
logicMgr.putRule("transitive-location", TypeQL.parsePattern("{ (subordinate: $x, superior: $y) isa location-hierarchy;" + "(subordinate: $y, superior: $z) isa location-hierarchy; }").asConjunction(), TypeQL.parseVariable("(subordinate: $x, superior: $z) isa location-hierarchy").asThing());
txn.commit();
}
}
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
txn.query().insert(TypeQL.parseQuery("insert " + "(subordinate: $a, superior: $b) isa location-hierarchy; " + "(subordinate: $b, superior: $c) isa location-hierarchy; " + "(subordinate: $c, superior: $d) isa location-hierarchy; " + "(subordinate: $d, superior: $e) isa location-hierarchy; " + "$a isa location; $b isa location; $c isa location;" + "$d isa location; $e isa location;").asInsert());
txn.commit();
}
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ, (new Options.Transaction().explain(true)))) {
List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $r isa location-hierarchy;").asMatch()).toList();
assertEquals(10, ans.size());
List<ConceptMap> explainableMaps = iterate(ans).filter(answer -> !answer.explainables().isEmpty()).toList();
assertEquals(6, explainableMaps.size());
Map<Pair<ConceptMap, ConceptMap.Explainable>, List<Explanation>> allExplanations = new HashMap<>();
for (ConceptMap explainableMap : explainableMaps) {
List<ConceptMap.Explainable> explainables = explainableMap.explainables().iterator().toList();
assertEquals(1, explainables.size());
List<Explanation> explanations = txn.query().explain(explainables.get(0).id()).toList();
allExplanations.put(new Pair<>(explainableMap, explainables.get(0)), explanations);
}
int oneExplanation = 0;
int twoExplanations = 0;
int threeExplanations = 0;
for (Map.Entry<Pair<ConceptMap, ConceptMap.Explainable>, List<Explanation>> entry : allExplanations.entrySet()) {
List<Explanation> explanations = entry.getValue();
if (explanations.size() == 1)
oneExplanation++;
else if (explanations.size() == 2)
twoExplanations++;
else if (explanations.size() == 3)
threeExplanations++;
else
fail();
}
assertEquals(3, oneExplanation);
assertEquals(2, twoExplanations);
assertEquals(1, threeExplanations);
}
}
}
use of com.vaticle.typedb.core.reasoner.resolution.answer.Explanation in project grakn by graknlabs.
the class ExplanationTest method test_nested_explanations.
@Test
public void test_nested_explanations() {
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
LogicManager logicMgr = txn.logic();
EntityType person = conceptMgr.putEntityType("person");
AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
AttributeType gender = conceptMgr.putAttributeType("gender", AttributeType.ValueType.STRING);
EntityType city = conceptMgr.putEntityType("city");
person.setOwns(name);
person.setOwns(gender);
RelationType friendship = conceptMgr.putRelationType("friendship");
friendship.setRelates("friend");
RelationType marriage = conceptMgr.putRelationType("marriage");
marriage.setRelates("husband");
marriage.setRelates("wife");
RelationType wedding = conceptMgr.putRelationType("wedding");
wedding.setRelates("male");
wedding.setRelates("female");
wedding.setRelates("location");
person.setPlays(friendship.getRelates("friend"));
person.setPlays(marriage.getRelates("husband"));
person.setPlays(marriage.getRelates("wife"));
person.setPlays(wedding.getRelates("male"));
person.setPlays(wedding.getRelates("female"));
city.setPlays(wedding.getRelates("location"));
logicMgr.putRule("wedding-implies-marriage", TypeQL.parsePattern("{ $x isa person, has gender \"male\"; $y isa person, has gender \"female\"; " + "$l isa city; (male: $x, female: $y, location: $l) isa wedding; }").asConjunction(), TypeQL.parseVariable("(husband: $x, wife: $y) isa marriage").asThing());
logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $a isa person; $b isa person; (husband: $a, wife: $b) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $a, friend: $b) isa friendship").asThing());
txn.commit();
}
}
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
txn.query().insert(TypeQL.parseQuery("insert " + "(male: $x, female: $y, location: $l) isa wedding;" + "$x isa person, has gender \"male\";" + "$y isa person, has gender \"female\";" + "$l isa city;").asInsert());
txn.commit();
}
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ, (new Options.Transaction().explain(true)))) {
List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match ($x) isa friendship;").asMatch()).toList();
assertEquals(2, ans.size());
assertFalse(ans.get(0).explainables().isEmpty());
List<Explanation> explanations = assertSingleExplainableExplanations(ans.get(0), 1, 1, 1, txn);
Explanation explanation = explanations.get(0);
assertEquals(txn.logic().getRule("marriage-is-friendship"), explanation.rule());
assertEquals(2, explanation.variableMapping().size());
assertEquals(3, explanation.conclusionAnswer().concepts().size());
ConceptMap marriageIsFriendshipAnswer = explanation.conditionAnswer();
assertEquals(1, marriageIsFriendshipAnswer.explainables().iterator().count());
assertSingleExplainableExplanations(marriageIsFriendshipAnswer, 1, 1, 1, txn);
}
}
}
Aggregations