Search in sources :

Example 31 with ConceptMap

use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.

the class UnifyHasConcludableTest method has_one_to_many_unifier.

@Test
public void has_one_to_many_unifier() {
    String conjunction = "{ $b has attribute $b; }";
    Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
    Concludable.Has queryConcludable = concludables.iterator().next().asHas();
    Rule rule = createRule("has-rule", "{ $x isa self-owning-attribute; }", "$x has self-owning-attribute 5", 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("$b", set("$x", "$_0"));
        }
    };
    assertEquals(expected, result);
    // test requirements of one-to-many using valid answer
    Map<Identifier.Variable, Concept> concepts = map(pair(Identifier.Variable.name("x"), instanceOf("self-owning-attribute")), pair(Identifier.Variable.anon(0), instanceOf("self-owning-attribute")));
    FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
    assertTrue(unified.hasNext());
    assertEquals(1, unified.next().concepts().size());
    // test requirements of one-to-many using invalid answer
    concepts = map(pair(Identifier.Variable.name("x"), instanceOf("person")), pair(Identifier.Variable.anon(0), instanceOf("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 32 with ConceptMap

use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.

the class UnifyHasConcludableTest method has_attribute_exact_unifies_rule_has_variable.

@Test
public void has_attribute_exact_unifies_rule_has_variable() {
    String conjunction = "{ $y has name 'john'; }";
    Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
    Concludable.Has queryConcludable = concludables.iterator().next().asHas();
    Rule rule = createRule("has-rule", "{ $x isa person; $a isa first-name; }", "$x has $a", 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());
    HashMap<String, Set<String>> expected = new HashMap<String, Set<String>>() {

        {
            put("$y", set("$x"));
            put("$_0", set("$a"));
        }
    };
    assertEquals(expected, result);
    // test requirements
    assertEquals(0, unifier.requirements().types().size());
    assertEquals(2, unifier.requirements().isaExplicit().size());
    assertEquals(set(Label.of("first-name"), Label.of("last-name")), unifier.requirements().isaExplicit().get(Identifier.Variable.anon(0)));
    assertEquals(1, unifier.requirements().predicates().size());
    // test filter allows a valid answer
    Map<Identifier.Variable, Concept> concepts = map(pair(Identifier.Variable.name("x"), instanceOf("person")), pair(Identifier.Variable.name("a"), instanceOf("last-name", "john")));
    FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
    assertTrue(unified.hasNext());
    assertEquals(2, unified.next().concepts().size());
    // filter out invalid type
    concepts = map(pair(Identifier.Variable.name("x"), instanceOf("person")), pair(Identifier.Variable.name("a"), instanceOf("age")));
    unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
    assertFalse(unified.hasNext());
    // filter out invalid value
    concepts = map(pair(Identifier.Variable.name("x"), instanceOf("person")), pair(Identifier.Variable.name("a"), instanceOf("first-name", "bob")));
    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 33 with ConceptMap

use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.

the class UnifyHasConcludableTest method has_attribute_typed_variable_unifies_rule_has_variable.

@Test
public void has_attribute_typed_variable_unifies_rule_has_variable() {
    String conjunction = "{ $y has name $b; }";
    Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
    Concludable.Has queryConcludable = concludables.iterator().next().asHas();
    Rule rule = createRule("has-rule", "{ $x isa person; $a isa first-name; }", "$x has $a", 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());
    HashMap<String, Set<String>> expected = new HashMap<String, Set<String>>() {

        {
            put("$y", set("$x"));
            put("$b", set("$a"));
        }
    };
    assertEquals(expected, result);
    // test requirements
    assertEquals(0, unifier.requirements().types().size());
    assertEquals(2, unifier.requirements().isaExplicit().size());
    assertEquals(set(Label.of("first-name"), Label.of("last-name")), unifier.requirements().isaExplicit().get(Identifier.Variable.name("b")));
    assertEquals(0, unifier.requirements().predicates().size());
    // test filter allows a valid answer
    Map<Identifier.Variable, Concept> concepts = map(pair(Identifier.Variable.name("x"), instanceOf("person")), pair(Identifier.Variable.name("a"), instanceOf("first-name", "john")));
    FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
    assertTrue(unified.hasNext());
    assertEquals(2, unified.next().concepts().size());
    // filter out invalid type
    concepts = map(pair(Identifier.Variable.name("x"), instanceOf("person")), pair(Identifier.Variable.name("a"), instanceOf("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 34 with ConceptMap

use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.

the class UnifyRelationConcludableTest method relation_role_unifies_rule_relation_exact.

@Test
public void relation_role_unifies_rule_relation_exact() {
    Unifier unifier = uniqueUnifier("{ ($role: $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("$role", set("$_employment:employee"));
            put("$_0", set("$_0"));
        }
    };
    assertEquals(expected, result);
    // test requirement
    assertEquals(typeHierarchy("employment"), unifier.requirements().isaExplicit().get(Variable.anon(0)));
    assertEquals(2, unifier.requirements().isaExplicit().size());
    assertEquals(0, unifier.requirements().predicates().size());
    // test filter allows a valid answer
    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(3, unified.next().concepts().size());
    assertEquals(employment.getType().getRelates("employee"), unifiedAnswer.get("role"));
    assertEquals(person, unifiedAnswer.get("y"));
    assertEquals(employment, unifiedAnswer.get(Variable.anon(0)));
    // 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)

Example 35 with ConceptMap

use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.

the class UnifyRelationConcludableTest method unUnify_produces_cartesian_named_types.

@Test
public void unUnify_produces_cartesian_named_types() {
    String conjunction = "{$r ($role: $x) isa $rel;}";
    Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
    Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
    Rule rule = createRule("people-are-self-friends", "{ $x isa person; }", " (friend: $x) isa friendship ", logicMgr);
    List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
    assertEquals(1, unifiers.size());
    Unifier unifier = unifiers.get(0);
    // test filter allows a valid answer
    Relation friendship = instanceOf("friendship").asRelation();
    Thing person = instanceOf("person");
    addRolePlayer(friendship, "friend", person);
    Map<Variable, Concept> concepts = map(pair(Variable.anon(0), friendship), pair(Variable.name("x"), person), pair(Variable.label("friendship"), friendship.getType()), pair(Variable.label("friendship:friend"), friendship.getType().getRelates("friend")));
    List<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map())).toList();
    assertEquals(6, unified.size());
    Set<Map<String, String>> expected = set(new HashMap<String, String>() {

        {
            put("$rel", "friendship");
            put("$role", "friendship:friend");
        }
    }, new HashMap<String, String>() {

        {
            put("$rel", "friendship");
            put("$role", "relation:role");
        }
    }, new HashMap<String, String>() {

        {
            put("$rel", "relation");
            put("$role", "friendship:friend");
        }
    }, new HashMap<String, String>() {

        {
            put("$rel", "relation");
            put("$role", "relation:role");
        }
    }, new HashMap<String, String>() {

        {
            put("$rel", "thing");
            put("$role", "friendship:friend");
        }
    }, new HashMap<String, String>() {

        {
            put("$rel", "thing");
            put("$role", "relation:role");
        }
    });
    Set<Map<String, String>> actual = new HashSet<>();
    iterate(unified).forEachRemaining(answer -> {
        actual.add(new HashMap<String, String>() {

            {
                put("$rel", answer.get("rel").asType().getLabel().name());
                put("$role", answer.get("role").asType().getLabel().scopedName());
            }
        });
    });
    assertEquals(expected, actual);
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) Relation(com.vaticle.typedb.core.concept.thing.Relation) 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) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap) Thing(com.vaticle.typedb.core.concept.thing.Thing) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)68 Test (org.junit.Test)42 Concept (com.vaticle.typedb.core.concept.Concept)33 HashMap (java.util.HashMap)33 Set (java.util.Set)23 CoreSession (com.vaticle.typedb.core.database.CoreSession)20 Map (java.util.Map)20 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)19 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)18 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)17 Rule (com.vaticle.typedb.core.logic.Rule)17 Util.createRule (com.vaticle.typedb.core.logic.resolvable.Util.createRule)17 EntityType (com.vaticle.typedb.core.concept.type.EntityType)16 LogicManager (com.vaticle.typedb.core.logic.LogicManager)13 RelationType (com.vaticle.typedb.core.concept.type.RelationType)12 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)11 Variable (com.vaticle.typedb.core.traversal.common.Identifier.Variable)11 HashSet (java.util.HashSet)11 Options (com.vaticle.typedb.core.common.parameters.Options)10 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)9