use of com.vaticle.typedb.core.logic.Rule in project grakn by graknlabs.
the class UnifyIsaConcludableTest method isa_exact_unifies_rule_has_exact.
@Test
public void isa_exact_unifies_rule_has_exact() {
String conjunction = "{ $a isa name; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Isa queryConcludable = concludables.iterator().next().asIsa();
Rule rule = createRule("isa-rule", "{ $x isa person; }", "$x has first-name \"john\"", logicMgr);
List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
assertEquals(1, unifiers.size());
Unifier unifier = unifiers.get(0);
Map<String, Set<String>> result = getStringMapping(unifier.mapping());
Map<String, Set<String>> expected = new HashMap<String, Set<String>>() {
{
put("$a", set("$_0"));
}
};
assertEquals(expected, result);
// test requirements
assertEquals(1, unifier.requirements().types().size());
assertEquals(1, unifier.requirements().isaExplicit().size());
assertEquals(set(Label.of("first-name"), Label.of("last-name")), unifier.requirements().isaExplicit().values().iterator().next());
assertEquals(0, unifier.requirements().predicates().size());
// test filter allows a valid answer
Map<Identifier.Variable, Concept> concepts = map(pair(Identifier.Variable.anon(0), instanceOf("first-name", "john")), pair(Identifier.Variable.label("first-name"), conceptMgr.getThingType("first-name")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
assertEquals(1, unified.next().concepts().size());
// filter out invalid type
concepts = map(pair(Identifier.Variable.anon(0), instanceOf("age")), pair(Identifier.Variable.label("first-name"), conceptMgr.getThingType("age")));
unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertFalse(unified.hasNext());
}
use of com.vaticle.typedb.core.logic.Rule in project grakn by graknlabs.
the class UnifyIsaConcludableTest method isa_concrete_prunes_irrelevant_rules.
@Test
public void isa_concrete_prunes_irrelevant_rules() {
String conjunction = "{ $a isa age; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Isa queryConcludable = concludables.iterator().next().asIsa();
Rule rule = createRule("isa-rule", "{ $x isa person; }", "$x has first-name \"john\"", logicMgr);
List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
assertEquals(0, unifiers.size());
Rule rule2 = createRule("isa-rule-2", "{ $x isa person; }", "(employee: $x) isa employment", logicMgr);
unifiers = queryConcludable.unify(rule2.conclusion(), conceptMgr).toList();
assertEquals(0, unifiers.size());
Rule rule3 = createRule("isa-rule-3", "{ $x isa person; $role-type type employment:employee; $rel-type relates $role-type; }", "($role-type: $x) isa $rel-type", logicMgr);
unifiers = queryConcludable.unify(rule3.conclusion(), conceptMgr).toList();
assertEquals(0, unifiers.size());
}
use of com.vaticle.typedb.core.logic.Rule 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);
}
}
}
}
Aggregations