Search in sources :

Example 36 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 37 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 38 with ConceptMap

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

the class ReasonerTest method test_offset_limit.

@Test
public void test_offset_limit() {
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            ConceptManager conceptMgr = txn.concepts();
            EntityType milk = conceptMgr.putEntityType("milk");
            AttributeType ageInDays = conceptMgr.putAttributeType("age-in-days", AttributeType.ValueType.LONG);
            milk.setOwns(ageInDays);
            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 milk, has age-in-days 5;").asInsert());
            txn.query().insert(TypeQL.parseQuery("insert $x isa milk, has age-in-days 10;").asInsert());
            txn.commit();
        }
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ)) {
            List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $x has age-in-days $a;").asMatch()).toList();
            ans.iterator().forEachRemaining(a -> {
                assertEquals("age-in-days", a.get("a").asThing().getType().getLabel().scopedName());
                assertEquals("milk", a.get("x").asThing().getType().getLabel().scopedName());
            });
            assertEquals(2, ans.size());
            List<ConceptMap> ansLimited = txn.query().match(TypeQL.parseQuery("match $x has age-in-days $a; limit 1;").asMatch()).toList();
            assertEquals(1, ansLimited.size());
            List<ConceptMap> ansLimitedOffsetted = txn.query().match(TypeQL.parseQuery("match $x has age-in-days $a; offset 1; limit 1;").asMatch()).toList();
            assertEquals(1, ansLimitedOffsetted.size());
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) 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)

Example 39 with ConceptMap

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

the class ReasonerTest method test_has_explicit_rule.

@Test
public void test_has_explicit_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 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);
            logicMgr.putRule("old-milk-is-not-good", TypeQL.parsePattern("{ $x isa milk, has age-in-days >= 10; }").asConjunction(), TypeQL.parseVariable("$x has is-still-good false").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 milk, has age-in-days 5;").asInsert());
            txn.query().insert(TypeQL.parseQuery("insert $x isa milk, has age-in-days 10;").asInsert());
            txn.query().insert(TypeQL.parseQuery("insert $x isa milk, has age-in-days 15;").asInsert());
            txn.commit();
        }
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ)) {
            List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $x has is-still-good $a;").asMatch()).toList();
            ans.iterator().forEachRemaining(a -> {
                assertFalse(a.get("a").asAttribute().asBoolean().getValue());
                assertEquals("is-still-good", a.get("a").asThing().getType().getLabel().scopedName());
                assertEquals("milk", a.get("x").asThing().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) 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)

Example 40 with ConceptMap

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

the class ReasonerTest method test_exception_kills_query.

@Test
public void test_exception_kills_query() {
    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 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);
            logicMgr.putRule("old-milk-is-not-good", TypeQL.parsePattern("{ $x isa milk, has age-in-days >= 10; }").asConjunction(), TypeQL.parseVariable("$x has is-still-good false").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 milk, has age-in-days 5;").asInsert());
            txn.query().insert(TypeQL.parseQuery("insert $x isa milk, has age-in-days 10;").asInsert());
            txn.commit();
        }
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ)) {
            txn.reasoner().resolverRegistry().terminateResolvers(new RuntimeException());
            try {
                List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $x isa is-still-good;").asMatch()).toList();
            } catch (TypeDBException e) {
                assertEquals(e.code().get(), RESOLUTION_TERMINATED.code());
                return;
            }
            fail();
        }
    }
}
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) CoreSession(com.vaticle.typedb.core.database.CoreSession) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) 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