use of com.vaticle.typedb.core.database.CoreSession in project grakn by graknlabs.
the class ReasonerTest method test_relation_rule.
@Test
public void test_relation_rule() {
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);
person.setOwns(name);
RelationType friendship = conceptMgr.putRelationType("friendship");
friendship.setRelates("friend");
RelationType marriage = conceptMgr.putRelationType("marriage");
marriage.setRelates("husband");
marriage.setRelates("wife");
person.setPlays(friendship.getRelates("friend"));
person.setPlays(marriage.getRelates("husband"));
person.setPlays(marriage.getRelates("wife"));
logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $x isa person; $y isa person; (husband: $x, wife: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) 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 $x isa person, has name 'Zack'; $y isa person, has name 'Yasmin'; (husband: $x, wife: $y) isa marriage;").asInsert());
txn.commit();
}
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ)) {
List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $f (friend: $p1, friend: $p2) isa friendship; $p1 has name $na;").asMatch()).toList();
ans.iterator().forEachRemaining(a -> {
assertEquals("friendship", a.get("f").asThing().getType().getLabel().scopedName());
assertEquals("person", a.get("p1").asThing().getType().getLabel().scopedName());
assertEquals("person", a.get("p2").asThing().getType().getLabel().scopedName());
assertEquals("name", a.get("na").asAttribute().getType().getLabel().scopedName());
});
assertEquals(2, ans.size());
}
}
}
use of com.vaticle.typedb.core.database.CoreSession in project grakn by graknlabs.
the class ReiterationTest method test_first_iteration_exhausts_and_second_iteration_recurses_infinitely.
@Test
public void test_first_iteration_exhausts_and_second_iteration_recurses_infinitely() throws InterruptedException {
try (CoreSession session = schemaSession()) {
try (CoreTransaction transaction = singleThreadElgTransaction(session)) {
transaction.query().define(TypeQL.parseQuery("define " + "X sub relation, relates item, plays Y:item;" + "Y sub relation, relates item, plays X:item;" + "object sub entity, plays X:item, plays Y:item;" + "rule rule-b: when {" + "$r(item:$x) isa X;" + "} then {" + "(item:$r) isa Y;" + "};" + "rule rule-a: when {" + "$r(item:$x) isa Y;" + "} then {" + "(item:$r) isa X;" + "};" + "rule rule-c: when {" + "$o isa object;" + "} then {" + "(item:$o) isa X;" + "};"));
transaction.commit();
}
}
try (CoreSession session = dataSession()) {
try (CoreTransaction transaction = singleThreadElgTransaction(session)) {
transaction.query().insert(TypeQL.parseQuery("insert $o isa object;"));
transaction.commit();
}
}
try (CoreSession session = dataSession()) {
try (CoreTransaction transaction = singleThreadElgTransaction(session)) {
Conjunction conjunction = resolvedConjunction("{ $y isa Y; }", transaction.logic());
Set<Identifier.Variable.Retrievable> filter = new HashSet<>();
iterate(conjunction.variables()).map(Variable::id).filter(Identifier::isName).map(Identifier.Variable::asName).forEachRemaining(filter::add);
ResolverRegistry registry = transaction.reasoner().resolverRegistry();
LinkedBlockingQueue<Match.Finished> responses = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Integer> failed = new LinkedBlockingQueue<>();
int[] iteration = { 0 };
int[] doneInIteration = { 0 };
boolean[] receivedInferredAnswer = { false };
ResolutionTracer.get().start();
Actor.Driver<RootResolver.Conjunction> root = registry.root(conjunction, answer -> {
if (iterate(answer.conceptMap().concepts().entrySet()).map(e -> e.getValue().asThing().isInferred()).first().isPresent()) {
receivedInferredAnswer[0] = true;
}
responses.add(answer);
}, iterDone -> {
assert iteration[0] == iterDone;
doneInIteration[0]++;
failed.add(iterDone);
}, throwable -> fail());
Set<Match.Finished> answers = new HashSet<>();
// iteration 0
sendRootRequest(root, filter, iteration[0]);
answers.add(responses.take());
ResolutionTracer.get().finish();
ResolutionTracer.get().start();
sendRootRequest(root, filter, iteration[0]);
// Block and wait for an failed message
failed.take();
ResolutionTracer.get().finish();
assertTrue(receivedInferredAnswer[0]);
assertEquals(1, doneInIteration[0]);
// iteration 1 onwards
for (int j = 0; j <= 100; j++) {
ResolutionTracer.get().start();
sendRootRequest(root, filter, iteration[0]);
Match.Finished re = responses.poll(100, MILLISECONDS);
if (re == null) {
Integer ex = failed.poll(100, MILLISECONDS);
if (ex == null) {
ResolutionTracer.get().finish();
fail();
}
// Reset the iteration
iteration[0]++;
receivedInferredAnswer[0] = false;
doneInIteration[0] = 0;
}
ResolutionTracer.get().finish();
}
} catch (TypeDBException e) {
e.printStackTrace();
fail();
}
}
}
use of com.vaticle.typedb.core.database.CoreSession in project grakn by graknlabs.
the class RuleTest method rule_contains_indexes_prevent_undefining_contained_types.
@Test
public void rule_contains_indexes_prevent_undefining_contained_types() throws IOException {
Util.resetDirectory(dataDir);
try (CoreDatabaseManager databaseMgr = CoreDatabaseManager.open(options)) {
databaseMgr.create(database);
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
try (CoreTransaction 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");
AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
marriage.setRelates("spouse");
person.setPlays(friendship.getRelates("friend"));
person.setPlays(marriage.getRelates("spouse"));
person.setOwns(name);
Rule marriageFriendsRule = 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());
Conjunction marriageFriendsThen = marriageFriendsRule.then();
Variable marriageFriendsRelation = getVariable(marriageFriendsThen.variables(), Identifier.Variable.anon(0));
assertEquals(set(Label.of("friendship")), marriageFriendsRelation.inferredTypes());
Rule marriageSameName = logicMgr.putRule("marriage-same-name", TypeQL.parsePattern("{ $x isa person, has name $a; $y isa person; (spouse:$x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("$y has $a").asThing());
Conjunction sameName = marriageSameName.then();
Variable nameAttr = getVariable(sameName.variables(), Identifier.Variable.name("a"));
assertEquals(set(Label.of("name")), nameAttr.inferredTypes());
txn.commit();
}
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
EntityType person = conceptMgr.getEntityType("person");
assertThrowsTypeDBException(person::delete, ErrorMessage.TypeWrite.TYPE_REFERENCED_IN_RULES.code());
}
}
}
}
use of com.vaticle.typedb.core.database.CoreSession in project grakn by graknlabs.
the class RuleTest method rule_has_explicit_materialises_when_missing.
@Test
public void rule_has_explicit_materialises_when_missing() throws IOException {
Util.resetDirectory(dataDir);
try (CoreDatabaseManager databaseMgr = CoreDatabaseManager.open(options)) {
databaseMgr.create(database);
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
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);
txn.logic().putRule("old-milk-is-not-good", TypeQL.parsePattern("{ $x isa milk, has age-in-days $a; $a >= 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 = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
EntityType milk = conceptMgr.getEntityType("milk");
AttributeType ageInDays = conceptMgr.getAttributeType("age-in-days");
Entity milkInst = milk.create();
milkInst.setHas(ageInDays.asLong().put(20L));
Rule rule = txn.logic().getRule("old-milk-is-not-good");
ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), milkInst)));
List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
assertEquals(1, materialisations.size());
assertEquals(3, materialisations.get(0).size());
AttributeType isStillGood = conceptMgr.getAttributeType("is-still-good");
List<? extends Attribute> isStillGoodOwned = milkInst.getHas(isStillGood).toList();
assertEquals(1, isStillGoodOwned.size());
assertEquals(isStillGood.asBoolean().getInstances().first().get(), isStillGoodOwned.get(0));
}
}
}
}
use of com.vaticle.typedb.core.database.CoreSession in project grakn by graknlabs.
the class RuleTest method rule_with_negated_cycle_throws_an_error.
@Test
public void rule_with_negated_cycle_throws_an_error() throws IOException {
Util.resetDirectory(dataDir);
try (CoreDatabaseManager databaseMgr = CoreDatabaseManager.open(options)) {
databaseMgr.create(database);
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
txn.query().define(TypeQL.parseQuery("define " + "person sub entity, owns is-starting-school, owns grade;" + "is-starting-school sub attribute, value boolean;" + "grade sub attribute, value long;" + "rule person-starting-school: when {" + " $x isa person;" + " not { $x has is-starting-school true; };" + "} then {" + " $x has grade 1;" + "};" + "" + "rule person-with-grade-is-in-school: when {" + " $x isa person, has grade 1;" + "} then {" + " $x has is-starting-school true;" + "};").asDefine());
assertThrowsTypeDBException(txn::commit, CONTRADICTORY_RULE_CYCLE.code());
}
}
}
}
Aggregations