Search in sources :

Example 51 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)

Example 52 with ConceptMap

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

the class UnifyRelationConcludableTest method relation_type_and_player_unifies_rule_relation_exact.

@Test
public void relation_type_and_player_unifies_rule_relation_exact() {
    Unifier unifier = uniqueUnifier("{ (employee: $y) isa $rel; }", 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("$rel", set("$_employment"));
            put("$_0", set("$_0"));
        }
    };
    assertEquals(expected, result);
    // test requirements
    assertEquals(roleHierarchy("employee", "employment"), unifier.requirements().types().get(Variable.label("relation:employee")));
    assertEquals(2, unifier.requirements().types().size());
    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, unifiedAnswer.concepts().size());
    assertEquals(employment.getType(), unifiedAnswer.get("rel"));
    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 53 with ConceptMap

use of com.vaticle.typedb.core.concept.answer.ConceptMap 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)

Example 54 with ConceptMap

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

the class QueryTest method test_query_delete.

@Test
public void test_query_delete() 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 transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
                TypeQLDefine query = TypeQL.parseQuery(new String(Files.readAllBytes(Paths.get("test/integration/schema.tql")), UTF_8));
                transaction.query().define(query);
                transaction.commit();
            }
        }
        try (TypeDB.Session session = typedb.session(database, Arguments.Session.Type.DATA)) {
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
                String insertString = "insert " + "$o isa organisation, has name 'vaticle'; " + "$t isa team, has name 'engineers', has symbol 'vaticle/engineers'; " + "$u isa user, has name 'butler', has email 'butler@vaticle.com'; " + "($o, $t) isa org-team; " + "($o, $u) isa org-member; " + "($t, $u) isa team-member;";
                TypeQLInsert insertQuery = TypeQL.parseQuery(insertString);
                transaction.query().insert(insertQuery);
                transaction.commit();
            }
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
                String deleteString = "match $x isa thing; delete $x isa thing;";
                TypeQLDelete deleteQuery = TypeQL.parseQuery(deleteString);
                transaction.query().delete(deleteQuery);
                transaction.commit();
            }
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.READ)) {
                String matchString = "match $x isa thing;";
                TypeQLMatch matchQuery = TypeQL.parseQuery(matchString);
                FunctionalIterator<ConceptMap> answers = transaction.query().match(matchQuery);
                assertFalse(answers.hasNext());
            }
        }
    }
}
Also used : TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) TypeQLDelete(com.vaticle.typeql.lang.query.TypeQLDelete) TypeDB(com.vaticle.typedb.core.TypeDB) TypeQLInsert(com.vaticle.typeql.lang.query.TypeQLInsert) TypeQLMatch(com.vaticle.typeql.lang.query.TypeQLMatch) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Example 55 with ConceptMap

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

the class RuleTest method rule_has_variable_materialises_when_missing.

@Test
public void rule_has_variable_materialises_when_missing() throws IOException {
    Util.resetDirectory(dataDir);
    try (CoreDatabaseManager databaseMgr = CoreDatabaseManager.open(options)) {
        databaseMgr.create(database);
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                EntityType milk = conceptMgr.putEntityType("milk");
                AttributeType ageInDays = conceptMgr.putAttributeType("age-in-days", AttributeType.ValueType.LONG);
                AttributeType isStillGood = conceptMgr.putAttributeType("is-still-good", AttributeType.ValueType.BOOLEAN);
                milk.setOwns(ageInDays);
                milk.setOwns(isStillGood);
                txn.logic().putRule("old-milk-is-not-good", TypeQL.parsePattern("{ $x isa milk; $a 10 isa age-in-days; }").asConjunction(), TypeQL.parseVariable("$x has $a").asThing());
                txn.commit();
            }
        }
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                EntityType milk = conceptMgr.getEntityType("milk");
                AttributeType ageInDays = conceptMgr.getAttributeType("age-in-days");
                Entity milkInst = milk.create();
                Attribute.Long ageInDays10 = ageInDays.asLong().put(10L);
                Rule rule = txn.logic().getRule("old-milk-is-not-good");
                ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), milkInst), pair(Identifier.Variable.name("a"), ageInDays10)));
                List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
                assertEquals(1, materialisations.size());
                assertEquals(2, materialisations.get(0).size());
                List<? extends Attribute> ageInDaysOwned = milkInst.getHas(ageInDays).toList();
                assertEquals(1, ageInDaysOwned.size());
                assertEquals(ageInDays10, ageInDaysOwned.get(0));
            }
        }
    }
}
Also used : Entity(com.vaticle.typedb.core.concept.thing.Entity) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) CoreSession(com.vaticle.typedb.core.database.CoreSession) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) 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