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));
}
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);
}
}
}
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);
}
Aggregations