use of com.vaticle.typedb.core.logic.LogicManager in project grakn by graknlabs.
the class ReasonerTest method test_exception_kills_query.
@Test
public void test_exception_kills_query() {
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 milk = conceptMgr.putEntityType("milk");
AttributeType ageInDays = conceptMgr.putAttributeType("age-in-days", AttributeType.ValueType.LONG);
AttributeType isStillGood = conceptMgr.putAttributeType("is-still-good", AttributeType.ValueType.BOOLEAN);
milk.setOwns(ageInDays);
milk.setOwns(isStillGood);
logicMgr.putRule("old-milk-is-not-good", TypeQL.parsePattern("{ $x isa milk, has age-in-days >= 10; }").asConjunction(), TypeQL.parseVariable("$x has is-still-good false").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 $x isa milk, has age-in-days 5;").asInsert());
txn.query().insert(TypeQL.parseQuery("insert $x isa milk, has age-in-days 10;").asInsert());
txn.commit();
}
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ)) {
txn.reasoner().resolverRegistry().terminateResolvers(new RuntimeException());
try {
List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $x isa is-still-good;").asMatch()).toList();
} catch (TypeDBException e) {
assertEquals(e.code().get(), RESOLUTION_TERMINATED.code());
return;
}
fail();
}
}
}
use of com.vaticle.typedb.core.logic.LogicManager 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.logic.LogicManager 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);
}
}
}
use of com.vaticle.typedb.core.logic.LogicManager in project grakn by graknlabs.
the class BasicTest method write_and_retrieve_relation_rule.
@Test
public void write_and_retrieve_relation_rule() throws IOException {
Util.resetDirectory(dataDir);
try (TypeDB.DatabaseManager typedb = CoreDatabaseManager.open(options)) {
typedb.create(database);
try (TypeDB.Session session = typedb.session(database, Arguments.Session.Type.SCHEMA)) {
try (TypeDB.Transaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
LogicManager logicMgr = txn.logic();
EntityType person = conceptMgr.putEntityType("person");
RelationType friendship = conceptMgr.putRelationType("friendship");
friendship.setRelates("friend");
RelationType marriage = conceptMgr.putRelationType("marriage");
marriage.setRelates("spouse");
person.setPlays(friendship.getRelates("friend"));
person.setPlays(marriage.getRelates("spouse"));
logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{$x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship").asThing());
txn.commit();
}
try (TypeDB.Transaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
ConceptManager conceptMgr = txn.concepts();
LogicManager logicMgr = txn.logic();
EntityType person = conceptMgr.getEntityType("person");
RelationType friendship = conceptMgr.getRelationType("friendship");
RoleType friend = friendship.getRelates("friend");
RelationType marriage = conceptMgr.getRelationType("marriage");
RoleType spouse = marriage.getRelates("spouse");
Rule rule = logicMgr.getRule("marriage-is-friendship");
Pattern when = rule.getWhenPreNormalised();
ThingVariable<?> then = rule.getThenPreNormalised();
assertEquals(TypeQL.parsePattern("{$x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage; }"), when);
assertEquals(TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship"), then);
}
}
}
}
use of com.vaticle.typedb.core.logic.LogicManager in project grakn by graknlabs.
the class CoreTransaction method initialise.
void initialise(GraphManager graphMgr, TraversalCache traversalCache, LogicCache logicCache) {
traversalEng = new TraversalEngine(graphMgr, traversalCache);
conceptMgr = new ConceptManager(graphMgr);
logicMgr = new LogicManager(graphMgr, conceptMgr, traversalEng, logicCache);
reasoner = new Reasoner(conceptMgr, logicMgr, traversalEng, context);
queryMgr = new QueryManager(conceptMgr, logicMgr, reasoner, context);
isOpen = new AtomicBoolean(true);
}
Aggregations