Search in sources :

Example 36 with Rule

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());
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Set(java.util.Set) HashMap(java.util.HashMap) Rule(com.vaticle.typedb.core.logic.Rule) Util.createRule(com.vaticle.typedb.core.logic.resolvable.Util.createRule) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Example 37 with Rule

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());
}
Also used : Rule(com.vaticle.typedb.core.logic.Rule) Util.createRule(com.vaticle.typedb.core.logic.resolvable.Util.createRule) Test(org.junit.Test)

Example 38 with Rule

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);
            }
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) Pattern(com.vaticle.typeql.lang.pattern.Pattern) LogicManager(com.vaticle.typedb.core.logic.LogicManager) RoleType(com.vaticle.typedb.core.concept.type.RoleType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Rule(com.vaticle.typedb.core.logic.Rule) TypeDB(com.vaticle.typedb.core.TypeDB) Test(org.junit.Test)

Aggregations

Rule (com.vaticle.typedb.core.logic.Rule)38 Test (org.junit.Test)37 Util.createRule (com.vaticle.typedb.core.logic.resolvable.Util.createRule)35 HashMap (java.util.HashMap)32 Set (java.util.Set)30 Concept (com.vaticle.typedb.core.concept.Concept)27 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)27 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)15 Relation (com.vaticle.typedb.core.concept.thing.Relation)15 Thing (com.vaticle.typedb.core.concept.thing.Thing)15 RelationType (com.vaticle.typedb.core.concept.type.RelationType)15 LogicManager (com.vaticle.typedb.core.logic.LogicManager)15 Variable (com.vaticle.typedb.core.traversal.common.Identifier.Variable)15 HashSet (java.util.HashSet)15 Map (java.util.Map)15 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)14 RoleType (com.vaticle.typedb.core.concept.type.RoleType)14 Lists (com.google.common.collect.Lists)13 Collections.list (com.vaticle.typedb.common.collection.Collections.list)13 Collections.map (com.vaticle.typedb.common.collection.Collections.map)13