Search in sources :

Example 11 with Thing

use of com.vaticle.typedb.core.concept.thing.Thing in project grakn by graknlabs.

the class UnifyRelationConcludableTest method relation_and_player_unifies_rule_relation_exact.

@Test
public void relation_and_player_unifies_rule_relation_exact() {
    Unifier unifier = uniqueUnifier("{ $r (employee: $y) isa employment; }", rule(" (employee: $x) isa employment", "{ $x isa person; }"));
    Map<String, Set<String>> result = getStringMapping(unifier.mapping());
    Map<String, Set<String>> expected = new HashMap<String, Set<String>>() {

        {
            put("$y", set("$x"));
            put("$r", set("$_0"));
        }
    };
    assertEquals(expected, result);
    // test requirements
    assertEquals(typeHierarchy("employment"), unifier.requirements().isaExplicit().get(Variable.name("r")));
    assertEquals(2, unifier.requirements().isaExplicit().size());
    assertEquals(roleHierarchy("employee", "employment"), unifier.requirements().types().get(Variable.label("employment:employee")));
    assertEquals(2, unifier.requirements().types().size());
    assertEquals(0, unifier.requirements().predicates().size());
    // test filter allows a valid answer
    // code below tests unifier applied to an answer that is 1) satisfiable, 2) non-satisfiable
    Relation employment = instanceOf("employment").asRelation();
    Thing person = instanceOf("person");
    addRolePlayer(employment, "employee", person);
    Map<Variable, Concept> concepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.label("employment"), employment.getType()), pair(Variable.label("employment:employee"), employment.getType().getRelates("employee")));
    FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
    assertTrue(unified.hasNext());
    ConceptMap unifiedAnswer = unified.first().get();
    assertEquals(2, unifiedAnswer.concepts().size());
    assertEquals(employment, unifiedAnswer.get("r"));
    assertEquals(person, unifiedAnswer.get("y"));
    // filter out invalid types
    Relation friendship = instanceOf("friendship").asRelation();
    person = instanceOf("person");
    addRolePlayer(friendship, "friend", person);
    concepts = map(pair(Variable.anon(0), friendship), pair(Variable.name("x"), person), pair(Variable.label("employment"), friendship.getType()), pair(Variable.label("employment:employee"), friendship.getType().getRelates("friend")));
    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) HashSet(java.util.HashSet) Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) HashMap(java.util.HashMap) Relation(com.vaticle.typedb.core.concept.thing.Relation) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Thing(com.vaticle.typedb.core.concept.thing.Thing) Test(org.junit.Test)

Aggregations

Thing (com.vaticle.typedb.core.concept.thing.Thing)11 Relation (com.vaticle.typedb.core.concept.thing.Relation)9 Concept (com.vaticle.typedb.core.concept.Concept)7 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)7 Variable (com.vaticle.typedb.core.traversal.common.Identifier.Variable)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 Test (org.junit.Test)7 Map (java.util.Map)6 RoleType (com.vaticle.typedb.core.concept.type.RoleType)4 Rule (com.vaticle.typedb.core.logic.Rule)4 Util.createRule (com.vaticle.typedb.core.logic.resolvable.Util.createRule)4 RelationType (com.vaticle.typedb.core.concept.type.RelationType)3 Set (java.util.Set)3 UUID (java.util.UUID)3 Lists (com.google.common.collect.Lists)2 Collections.list (com.vaticle.typedb.common.collection.Collections.list)2 Collections.map (com.vaticle.typedb.common.collection.Collections.map)2 Collections.pair (com.vaticle.typedb.common.collection.Collections.pair)2 Collections.set (com.vaticle.typedb.common.collection.Collections.set)2