Search in sources :

Example 1 with Pair

use of com.vaticle.typedb.common.collection.Pair in project grakn by graknlabs.

the class ThingService method getPlayersByRoleType.

private void getPlayersByRoleType(Relation relation, UUID reqID) {
    // TODO: this should be optimised to actually iterate over role players by role type lazily
    Map<? extends RoleType, ? extends List<? extends Thing>> playersByRole = relation.getPlayersByRoleType();
    Stream.Builder<Pair<RoleType, Thing>> responses = Stream.builder();
    for (Map.Entry<? extends RoleType, ? extends List<? extends Thing>> players : playersByRole.entrySet()) {
        for (Thing player : players.getValue()) {
            responses.add(pair(players.getKey(), player));
        }
    }
    transactionSvc.stream(responses.build().iterator(), reqID, players -> getPlayersByRoleTypeResPart(reqID, players));
}
Also used : Stream(java.util.stream.Stream) Map(java.util.Map) Thing(com.vaticle.typedb.core.concept.thing.Thing) Pair(com.vaticle.typedb.common.collection.Pair)

Example 2 with Pair

use of com.vaticle.typedb.common.collection.Pair in project grakn by graknlabs.

the class ExplanationTest method test_all_transitive_explanations.

@Test
public void test_all_transitive_explanations() {
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            LogicManager logicMgr = txn.logic();
            txn.query().define(TypeQL.parseQuery("define " + "location sub entity, " + "  plays location-hierarchy:superior, " + "  plays location-hierarchy:subordinate; " + "location-hierarchy sub relation," + "  relates superior," + "  relates subordinate;").asDefine());
            logicMgr.putRule("transitive-location", TypeQL.parsePattern("{ (subordinate: $x, superior: $y) isa location-hierarchy;" + "(subordinate: $y, superior: $z) isa location-hierarchy; }").asConjunction(), TypeQL.parseVariable("(subordinate: $x, superior: $z) isa location-hierarchy").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 " + "(subordinate: $a, superior: $b) isa location-hierarchy; " + "(subordinate: $b, superior: $c) isa location-hierarchy; " + "(subordinate: $c, superior: $d) isa location-hierarchy; " + "(subordinate: $d, superior: $e) isa location-hierarchy; " + "$a isa location; $b isa location; $c isa location;" + "$d isa location; $e isa location;").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 $r isa location-hierarchy;").asMatch()).toList();
            assertEquals(10, ans.size());
            List<ConceptMap> explainableMaps = iterate(ans).filter(answer -> !answer.explainables().isEmpty()).toList();
            assertEquals(6, explainableMaps.size());
            Map<Pair<ConceptMap, ConceptMap.Explainable>, List<Explanation>> allExplanations = new HashMap<>();
            for (ConceptMap explainableMap : explainableMaps) {
                List<ConceptMap.Explainable> explainables = explainableMap.explainables().iterator().toList();
                assertEquals(1, explainables.size());
                List<Explanation> explanations = txn.query().explain(explainables.get(0).id()).toList();
                allExplanations.put(new Pair<>(explainableMap, explainables.get(0)), explanations);
            }
            int oneExplanation = 0;
            int twoExplanations = 0;
            int threeExplanations = 0;
            for (Map.Entry<Pair<ConceptMap, ConceptMap.Explainable>, List<Explanation>> entry : allExplanations.entrySet()) {
                List<Explanation> explanations = entry.getValue();
                if (explanations.size() == 1)
                    oneExplanation++;
                else if (explanations.size() == 2)
                    twoExplanations++;
                else if (explanations.size() == 3)
                    threeExplanations++;
                else
                    fail();
            }
            assertEquals(3, oneExplanation);
            assertEquals(2, twoExplanations);
            assertEquals(1, threeExplanations);
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) NOT_IDENTIFIED(com.vaticle.typedb.core.concept.answer.ConceptMap.Explainable.NOT_IDENTIFIED) Pair(com.vaticle.typedb.common.collection.Pair) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) HashMap(java.util.HashMap) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) Concept(com.vaticle.typedb.core.concept.Concept) LogicManager(com.vaticle.typedb.core.logic.LogicManager) After(org.junit.After) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) Path(java.nio.file.Path) Before(org.junit.Before) TypeQL(com.vaticle.typeql.lang.TypeQL) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Options(com.vaticle.typedb.core.common.parameters.Options) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test) IOException(java.io.IOException) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) CoreSession(com.vaticle.typedb.core.database.CoreSession) List(java.util.List) NamedThreadFactory(com.vaticle.typedb.common.concurrent.NamedThreadFactory) Paths(java.nio.file.Paths) Retrievable(com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable) Assert.assertFalse(org.junit.Assert.assertFalse) ActorExecutorGroup(com.vaticle.typedb.core.concurrent.actor.ActorExecutorGroup) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) Explanation(com.vaticle.typedb.core.reasoner.resolution.answer.Explanation) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) EntityType(com.vaticle.typedb.core.concept.type.EntityType) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Assert.assertEquals(org.junit.Assert.assertEquals) Util(com.vaticle.typedb.core.test.integration.util.Util) Options(com.vaticle.typedb.core.common.parameters.Options) HashMap(java.util.HashMap) Explanation(com.vaticle.typedb.core.reasoner.resolution.answer.Explanation) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) LogicManager(com.vaticle.typedb.core.logic.LogicManager) List(java.util.List) CoreSession(com.vaticle.typedb.core.database.CoreSession) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Pair(com.vaticle.typedb.common.collection.Pair) Test(org.junit.Test)

Example 3 with Pair

use of com.vaticle.typedb.common.collection.Pair in project grakn by graknlabs.

the class AlphaEquivalenceTest method test_isa_equivalent.

@Test
public void test_isa_equivalent() {
    ThingVariable a = parseVariable("a", "$a isa age").asThing();
    ThingVariable b = parseVariable("b", "$b isa age").asThing();
    AlphaEquivalence alphaEq = a.alphaEquals(b);
    assertEquals(map(new Pair<>("$a", "$b"), new Pair<>("$_age", "$_age")), alphaMapToStringMap(alphaEq.asValid()));
    testAlphaEquivalenceSymmetricReflexive(a, b, true);
}
Also used : ThingVariable(com.vaticle.typedb.core.pattern.variable.ThingVariable) Pair(com.vaticle.typedb.common.collection.Pair) Test(org.junit.Test)

Aggregations

Pair (com.vaticle.typedb.common.collection.Pair)3 Map (java.util.Map)2 Test (org.junit.Test)2 NamedThreadFactory (com.vaticle.typedb.common.concurrent.NamedThreadFactory)1 MB (com.vaticle.typedb.core.common.collection.Bytes.MB)1 Iterators.iterate (com.vaticle.typedb.core.common.iterator.Iterators.iterate)1 Arguments (com.vaticle.typedb.core.common.parameters.Arguments)1 Options (com.vaticle.typedb.core.common.parameters.Options)1 Concept (com.vaticle.typedb.core.concept.Concept)1 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)1 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)1 NOT_IDENTIFIED (com.vaticle.typedb.core.concept.answer.ConceptMap.Explainable.NOT_IDENTIFIED)1 Thing (com.vaticle.typedb.core.concept.thing.Thing)1 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)1 EntityType (com.vaticle.typedb.core.concept.type.EntityType)1 RelationType (com.vaticle.typedb.core.concept.type.RelationType)1 ActorExecutorGroup (com.vaticle.typedb.core.concurrent.actor.ActorExecutorGroup)1 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)1 CoreSession (com.vaticle.typedb.core.database.CoreSession)1 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)1