Search in sources :

Example 11 with ConceptMap

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

the class ExplanationTest method test_relation_explainable_multiple_ways.

@Test
public void test_relation_explainable_multiple_ways() {
    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());
            logicMgr.putRule("everyone-is-friends", TypeQL.parsePattern("{ $x isa person; $y isa person; not { $x is $y; }; }").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, (new Options.Transaction().explain(true)))) {
            List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match (friend: $p1, friend: $p2) isa friendship; $p1 has name $na;").asMatch()).toList();
            assertEquals(2, ans.size());
            assertFalse(ans.get(0).explainables().isEmpty());
            assertFalse(ans.get(1).explainables().isEmpty());
            assertSingleExplainableExplanations(ans.get(0), 1, 1, 3, txn);
            assertSingleExplainableExplanations(ans.get(1), 1, 1, 3, txn);
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) Options(com.vaticle.typedb.core.common.parameters.Options) 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)

Example 12 with ConceptMap

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

the class ExplanationTest method applyMapping.

private ConceptMap applyMapping(Map<Retrievable, Set<Retrievable>> mapping, ConceptMap completeMap) {
    Map<Retrievable, Concept> concepts = new HashMap<>();
    mapping.forEach((from, tos) -> {
        assertTrue(completeMap.contains(from));
        Concept concept = completeMap.get(from);
        tos.forEach(mapped -> {
            assertTrue(!concepts.containsKey(mapped) || concepts.get(mapped).equals(concept));
            concepts.put(mapped, concept);
        });
    });
    return new ConceptMap(concepts);
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Retrievable(com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable) HashMap(java.util.HashMap) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap)

Example 13 with ConceptMap

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

the class QueryTest method test_query_undefine.

@Test
public void test_query_undefine() 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.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
                String queryString = "undefine analysis abstract, owns created, plays commit-analysis:analysis;";
                TypeQLUndefine query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine rule performance-tracker-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine performance-tracker relates tracker;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine email regex '.+\\@.+\\..+';";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine index sub attribute, value long;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                // undefine first 4 rules
                queryString = "undefine rule repo-fork-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine rule repo-dependency-transitive-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine rule repo-dependency-transitive-type-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine rule repo-collaborator-org-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                transaction.commit();
            }
            try (TypeDB.Transaction tx = session.transaction(Arguments.Transaction.Type.READ)) {
                EntityType analysis = tx.concepts().getEntityType("analysis");
                RelationType performanceTracker = tx.concepts().getRelationType("performance-tracker");
                RoleType commitAnalysisAnalysis = tx.concepts().getRelationType("commit-analysis").getRelates("analysis");
                AttributeType.DateTime created = tx.concepts().getAttributeType("created").asDateTime();
                AttributeType.String email = tx.concepts().getAttributeType("email").asString();
                assertNotNulls(analysis, performanceTracker, commitAnalysisAnalysis, created, email);
                assertFalse(analysis.isAbstract());
                assertTrue(analysis.getOwns().noneMatch(att -> att.equals(created)));
                assertTrue(analysis.getPlays().noneMatch(rol -> rol.equals(commitAnalysisAnalysis)));
                assertTrue(performanceTracker.getRelates().noneMatch(rol -> rol.getLabel().name().equals("tracker")));
                assertNull(email.getRegex());
                AttributeType index = tx.concepts().getAttributeType("index");
                assertNull(index);
                assertNull(tx.logic().getRule("repo-fork-rule"));
                assertNull(tx.logic().getRule("repo-dependency-transitive-rule"));
                assertNull(tx.logic().getRule("repo-dependency-transitive-type-rule"));
                assertNull(tx.logic().getRule("repo-collaborator-org-rule"));
                // check total count
                assertEquals(15 - 5, tx.logic().rules().toList().size());
                // a query that used to trigger a rule should not cause an error
                List<ConceptMap> answers = tx.query().match(TypeQL.parseQuery("match $x isa repo-fork;").asMatch()).toList();
            }
        }
    }
}
Also used : TypeQLDelete(com.vaticle.typeql.lang.query.TypeQLDelete) TypeQLInsert(com.vaticle.typeql.lang.query.TypeQLInsert) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) TypeQLUndefine(com.vaticle.typeql.lang.query.TypeQLUndefine) Util.assertNotNulls(com.vaticle.typedb.core.test.integration.util.Util.assertNotNulls) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) Path(java.nio.file.Path) TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) TypeQL(com.vaticle.typeql.lang.TypeQL) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test) IOException(java.io.IOException) RoleType(com.vaticle.typedb.core.concept.type.RoleType) TypeDB(com.vaticle.typedb.core.TypeDB) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Paths(java.nio.file.Paths) Assert.assertFalse(org.junit.Assert.assertFalse) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) Database(com.vaticle.typedb.core.common.parameters.Options.Database) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) EntityType(com.vaticle.typedb.core.concept.type.EntityType) TypeQLMatch(com.vaticle.typeql.lang.query.TypeQLMatch) Entity(com.vaticle.typedb.core.concept.thing.Entity) Assert.assertEquals(org.junit.Assert.assertEquals) Util(com.vaticle.typedb.core.test.integration.util.Util) TypeQLUndefine(com.vaticle.typeql.lang.query.TypeQLUndefine) RoleType(com.vaticle.typedb.core.concept.type.RoleType) TypeDB(com.vaticle.typedb.core.TypeDB) EntityType(com.vaticle.typedb.core.concept.type.EntityType) TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Example 14 with ConceptMap

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

the class TypeQLSteps method answer_groups_are.

@Then("answer groups are")
public void answer_groups_are(List<Map<String, String>> answerIdentifierTable) {
    Set<AnswerIdentifierGroup> answerIdentifierGroups = answerIdentifierTable.stream().collect(Collectors.groupingBy(x -> x.get(AnswerIdentifierGroup.GROUP_COLUMN_NAME))).values().stream().map(AnswerIdentifierGroup::new).collect(Collectors.toSet());
    assertEquals(String.format("Expected [%d] answer groups, but found [%d].", answerIdentifierGroups.size(), answerGroups.size()), answerIdentifierGroups.size(), answerGroups.size());
    for (AnswerIdentifierGroup answerIdentifierGroup : answerIdentifierGroups) {
        String[] identifier = answerIdentifierGroup.ownerIdentifier.split(":", 2);
        UniquenessCheck checker;
        switch(identifier[0]) {
            case "label":
                checker = new LabelUniquenessCheck(identifier[1]);
                break;
            case "key":
                checker = new KeyUniquenessCheck(identifier[1]);
                break;
            case "value":
                checker = new ValueUniquenessCheck(identifier[1]);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + identifier[0]);
        }
        ConceptMapGroup answerGroup = answerGroups.stream().filter(ag -> checker.check(ag.owner())).findAny().orElse(null);
        assertNotNull(String.format("The group identifier [%s] does not match any of the answer group owners.", answerIdentifierGroup.ownerIdentifier), answerGroup);
        List<Map<String, String>> answersIdentifiers = answerIdentifierGroup.answersIdentifiers;
        for (ConceptMap answer : answerGroup.conceptMaps()) {
            List<Map<String, String>> matchingIdentifiers = new ArrayList<>();
            for (Map<String, String> answerIdentifiers : answersIdentifiers) {
                if (matchAnswerConcept(answerIdentifiers, answer)) {
                    matchingIdentifiers.add(answerIdentifiers);
                }
            }
            assertEquals(String.format("An identifier entry (row) should match 1-to-1 to an answer, but there were [%d] matching identifier entries for answer with variables %s.", matchingIdentifiers.size(), answer.concepts().keySet().toString()), 1, matchingIdentifiers.size());
        }
    }
}
Also used : Then(io.cucumber.java.en.Then) LocalDateTime(java.time.LocalDateTime) TypeQLDelete(com.vaticle.typeql.lang.query.TypeQLDelete) TypeQLInsert(com.vaticle.typeql.lang.query.TypeQLInsert) HashMap(java.util.HashMap) TypeQLUndefine(com.vaticle.typeql.lang.query.TypeQLUndefine) Reference(com.vaticle.typeql.lang.pattern.variable.Reference) ArrayList(java.util.ArrayList) Concept(com.vaticle.typedb.core.concept.Concept) Matcher(java.util.regex.Matcher) Given(io.cucumber.java.en.Given) Map(java.util.Map) TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) TypeQL(com.vaticle.typeql.lang.TypeQL) When(io.cucumber.java.en.When) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) NumericGroup(com.vaticle.typedb.core.concept.answer.NumericGroup) Collectors(java.util.stream.Collectors) Util.assertThrows(com.vaticle.typedb.core.common.test.Util.assertThrows) TypeQLException(com.vaticle.typeql.lang.common.exception.TypeQLException) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) TypeQLUpdate(com.vaticle.typeql.lang.query.TypeQLUpdate) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) LocalDate(java.time.LocalDate) Pattern(java.util.regex.Pattern) Util.assertThrowsWithMessage(com.vaticle.typedb.core.common.test.Util.assertThrowsWithMessage) ScenarioDefinitionException(com.vaticle.typedb.core.test.behaviour.exception.ScenarioDefinitionException) TypeQLMatch(com.vaticle.typeql.lang.query.TypeQLMatch) ConceptMapGroup(com.vaticle.typedb.core.concept.answer.ConceptMapGroup) Numeric(com.vaticle.typedb.core.concept.answer.Numeric) ConnectionSteps.tx(com.vaticle.typedb.core.test.behaviour.connection.ConnectionSteps.tx) Assert.assertEquals(org.junit.Assert.assertEquals) ConceptMapGroup(com.vaticle.typedb.core.concept.answer.ConceptMapGroup) ArrayList(java.util.ArrayList) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Then(io.cucumber.java.en.Then)

Example 15 with ConceptMap

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

the class MaterialiserTest method testDeduplicationOfInferredConcepts.

@Test
public void testDeduplicationOfInferredConcepts() {
    loadTransitivityExample(databaseMgr);
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
        Materialiser materialiser = Materialiser.materialise(session);
        TypeQLMatch inferredAnswersQuery = TypeQL.match(TypeQL.var("lh").isa("location-hierarchy"));
        List<ConceptMap> inferredAnswers = iterate(materialiser.query(inferredAnswersQuery).entrySet()).flatMap(Map.Entry::getValue).toList();
        assertEquals(6, inferredAnswers.size());
    }
}
Also used : Materialiser(com.vaticle.typedb.core.test.behaviour.reasoner.verification.Materialiser) CoreSession(com.vaticle.typedb.core.database.CoreSession) TypeQLMatch(com.vaticle.typeql.lang.query.TypeQLMatch) 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