Search in sources :

Example 26 with ConceptMap

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

the class UnifyHasConcludableTest method has_attribute_variable_unifies_rule_has_variable.

@Test
public void has_attribute_variable_unifies_rule_has_variable() {
    String conjunction = "{ $y has $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 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());
    // test requirements
    assertEquals(0, unifier.requirements().types().size());
    assertEquals(2, unifier.requirements().isaExplicit().size());
    assertEquals(0, unifier.requirements().predicates().size());
}
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 27 with ConceptMap

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

the class UnifyHasConcludableTest method has_attribute_variable_unifies_rule_has_exact.

@Test
public void has_attribute_variable_unifies_rule_has_exact() {
    String conjunction = "{ $y has $a; }";
    Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
    Concludable.Has queryConcludable = concludables.iterator().next().asHas();
    Rule rule = createRule("has-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("$y", set("$x"));
            put("$a", set("$_0"));
        }
    };
    assertEquals(expected, result);
    // test unifier allows a valid answer
    Map<Identifier.Variable, Concept> concepts = map(pair(Identifier.Variable.name("x"), instanceOf("person")), pair(Identifier.Variable.anon(0), instanceOf("first-name", "john")));
    FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
    assertTrue(unified.hasNext());
    assertEquals(2, unified.next().concepts().size());
    // test requirements
    assertEquals(0, unifier.requirements().types().size());
    assertEquals(2, unifier.requirements().isaExplicit().size());
    assertEquals(0, unifier.requirements().predicates().size());
}
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 28 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_exact.

// TODO: create more tests when type inference is working to test unifier pruning
@Test
public void has_attribute_exact_unifies_rule_has_exact() {
    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; }", "$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("$y", set("$x"));
            put("$_0", set("$_0"));
        }
    };
    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 forward unification can reject an invalid partial answer
    ConceptMap partialAnswer = new ConceptMap(map(pair(Identifier.Variable.anon(0), instanceOf("age"))));
    assertFalse(unifier.unify(partialAnswer).isPresent());
    // test filter allows a valid answer
    Map<Identifier.Variable, Concept> concepts = map(pair(Identifier.Variable.name("x"), instanceOf("person")), pair(Identifier.Variable.anon(0), 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.anon(0), 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.anon(0), 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 29 with ConceptMap

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

the class UnifyRelationConcludableTest method relation_distinct_roles_unifies_rule_relation_duplicate_roles.

// [reflexive child]
@Test
public void relation_distinct_roles_unifies_rule_relation_duplicate_roles() {
    String conjunction = "{ (employee: $p, employee: $q) isa employment; }";
    Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
    Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
    Rule rule = createRule("a-person-is-employed-twice", "{ $x isa person; $employment type employment; $employee type employment:employee; }", "($employee: $x, $employee: $x) isa $employment", logicMgr);
    List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
    Set<Map<String, Set<String>>> result = iterate(unifiers).map(u -> getStringMapping(u.mapping())).toSet();
    Set<Map<String, Set<String>>> expected = set(new HashMap<String, Set<String>>() {

        {
            put("$p", set("$x"));
            put("$q", set("$x"));
            put("$_0", set("$_0"));
        }
    });
    assertEquals(expected, result);
    Unifier unifier = unifiers.get(0);
    // test requirements
    assertEquals(2, unifier.requirements().types().size());
    assertEquals(roleHierarchy("employee", "employment"), unifier.requirements().types().get(Variable.label("employment:employee")));
    assertEquals(typeHierarchy("employment"), unifier.requirements().isaExplicit().get(Variable.anon(0)));
    assertEquals(3, 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);
    addRolePlayer(employment, "employee", person);
    Map<Variable, Concept> concepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.name("employment"), employment.getType()), pair(Variable.name("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, unifiedAnswer.concepts().size());
    assertEquals(person, unifiedAnswer.get("p"));
    assertEquals(person, unifiedAnswer.get("q"));
    assertEquals(employment, unifiedAnswer.get(Variable.anon(0)));
}
Also used : Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Util.getStringMapping(com.vaticle.typedb.core.logic.resolvable.Util.getStringMapping) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) LogicManager(com.vaticle.typedb.core.logic.LogicManager) ThingType(com.vaticle.typedb.core.concept.type.ThingType) After(org.junit.After) Map(java.util.Map) Collections.list(com.vaticle.typedb.common.collection.Collections.list) Thing(com.vaticle.typedb.core.concept.thing.Thing) Path(java.nio.file.Path) TypeQL(com.vaticle.typeql.lang.TypeQL) AfterClass(org.junit.AfterClass) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Options(com.vaticle.typedb.core.common.parameters.Options) Set(java.util.Set) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Collections.map(com.vaticle.typedb.common.collection.Collections.map) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) ILLEGAL_STATE(com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.ILLEGAL_STATE) Util.resolvedConjunction(com.vaticle.typedb.core.logic.resolvable.Util.resolvedConjunction) BeforeClass(org.junit.BeforeClass) Collections.set(com.vaticle.typedb.common.collection.Collections.set) HashMap(java.util.HashMap) Concept(com.vaticle.typedb.core.concept.Concept) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) Rule(com.vaticle.typedb.core.logic.Rule) Collections.pair(com.vaticle.typedb.common.collection.Collections.pair) Util.createRule(com.vaticle.typedb.core.logic.resolvable.Util.createRule) Label(com.vaticle.typedb.core.common.parameters.Label) Before(org.junit.Before) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) Iterator(java.util.Iterator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) RoleType(com.vaticle.typedb.core.concept.type.RoleType) CoreSession(com.vaticle.typedb.core.database.CoreSession) Relation(com.vaticle.typedb.core.concept.thing.Relation) Paths(java.nio.file.Paths) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Type(com.vaticle.typedb.core.concept.type.Type) Util(com.vaticle.typedb.core.test.integration.util.Util) Concept(com.vaticle.typedb.core.concept.Concept) Set(java.util.Set) HashSet(java.util.HashSet) 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) Test(org.junit.Test)

Example 30 with ConceptMap

use of com.vaticle.typedb.core.concept.answer.ConceptMap 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());
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) LogicManager(com.vaticle.typedb.core.logic.LogicManager) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) CoreSession(com.vaticle.typedb.core.database.CoreSession) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) 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