Search in sources :

Example 56 with Multimap

use of com.google.common.collect.Multimap in project apex-malhar by apache.

the class PojoOuterJoinTest method PojoLeftOuterJoinTest.

@Test
public void PojoLeftOuterJoinTest() {
    String[] leftKeys = { "uId" };
    String[] rightKeys = { "uId" };
    PojoLeftOuterJoin<TestPojo1, TestPojo3> pij = new PojoLeftOuterJoin<>(TestOutClass.class, leftKeys, rightKeys);
    List<Multimap<List<Object>, Object>> accu = pij.defaultAccumulatedValue();
    Assert.assertEquals(2, accu.size());
    accu = pij.accumulate(accu, new TestPojo1(1, "Josh"));
    accu = pij.accumulate(accu, new TestPojo1(2, "Bob"));
    accu = pij.accumulate2(accu, new TestPojo3(1, "NickJosh", 12));
    accu = pij.accumulate2(accu, new TestPojo3(3, "NickBob", 13));
    List result = pij.getOutput(accu);
    Assert.assertEquals(2, result.size());
    Object o = result.get(0);
    Assert.assertTrue(o instanceof TestOutClass);
    TestOutClass testOutClass = (TestOutClass) o;
    int uId = testOutClass.getUId();
    if (uId == 1) {
        checkNameAge("Josh", 12, testOutClass);
        o = result.get(1);
        Assert.assertTrue(o instanceof TestOutClass);
        testOutClass = (TestOutClass) o;
        uId = testOutClass.getUId();
        Assert.assertEquals(2, testOutClass.getUId());
        checkNameAge("Bob", 0, testOutClass);
    } else if (uId == 2) {
        checkNameAge("Bob", 0, testOutClass);
        o = result.get(1);
        Assert.assertTrue(o instanceof TestOutClass);
        testOutClass = (TestOutClass) o;
        uId = testOutClass.getUId();
        Assert.assertEquals(1, testOutClass.getUId());
        checkNameAge("Josh", 12, testOutClass);
    }
}
Also used : Multimap(com.google.common.collect.Multimap) List(java.util.List) Test(org.junit.Test)

Example 57 with Multimap

use of com.google.common.collect.Multimap in project grakn by graknlabs.

the class QueryOperationExecutor method sortProperties.

/**
 * Produce a valid ordering of the properties by using the given dependency information.
 *
 * <p>
 *     This method uses a topological sort (Kahn's algorithm) in order to find a valid ordering.
 * </p>
 */
private ImmutableList<VarAndProperty> sortProperties() {
    ImmutableList.Builder<VarAndProperty> sorted = ImmutableList.builder();
    // invertedDependencies is intended to just be a 'view' on dependencies, so when dependencies is modified
    // we should always also modify invertedDependencies (and vice-versa).
    Multimap<VarAndProperty, VarAndProperty> dependencies = HashMultimap.create(this.dependencies);
    Multimap<VarAndProperty, VarAndProperty> invertedDependencies = HashMultimap.create();
    Multimaps.invertFrom(dependencies, invertedDependencies);
    Queue<VarAndProperty> propertiesWithoutDependencies = new ArrayDeque<>(Sets.filter(properties, property -> dependencies.get(property).isEmpty()));
    VarAndProperty property;
    // Retrieve the next property without any dependencies
    while ((property = propertiesWithoutDependencies.poll()) != null) {
        sorted.add(property);
        // We copy this into a new list because the underlying collection gets modified during iteration
        Collection<VarAndProperty> dependents = Lists.newArrayList(invertedDependencies.get(property));
        for (VarAndProperty dependent : dependents) {
            // Because the property has been removed, the dependent no longer needs to depend on it
            dependencies.remove(dependent, property);
            invertedDependencies.remove(property, dependent);
            boolean hasNoDependencies = dependencies.get(dependent).isEmpty();
            if (hasNoDependencies) {
                propertiesWithoutDependencies.add(dependent);
            }
        }
    }
    if (!dependencies.isEmpty()) {
        // This means there must have been a loop. Pick an arbitrary remaining var to display
        Var var = dependencies.keys().iterator().next().var();
        throw GraqlQueryException.insertRecursive(printableRepresentation(var));
    }
    return sorted.build();
}
Also used : PropertyExecutor(ai.grakn.graql.internal.pattern.property.PropertyExecutor) Concept(ai.grakn.concept.Concept) InsertQuery(ai.grakn.graql.InsertQuery) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Multimaps(com.google.common.collect.Multimaps) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Partition(ai.grakn.graql.internal.util.Partition) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Nullable(javax.annotation.Nullable) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) Patterns(ai.grakn.graql.internal.pattern.Patterns) VarPropertyInternal(ai.grakn.graql.internal.pattern.property.VarPropertyInternal) GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) Maps(com.google.common.collect.Maps) Query(ai.grakn.graql.Query) Sets(com.google.common.collect.Sets) VarProperty(ai.grakn.graql.admin.VarProperty) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) DefineQuery(ai.grakn.graql.DefineQuery) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) ImmutableList(com.google.common.collect.ImmutableList) Var(ai.grakn.graql.Var) ArrayDeque(java.util.ArrayDeque)

Example 58 with Multimap

use of com.google.common.collect.Multimap in project grakn by graknlabs.

the class RelationshipAtom method inferPossibleRelationConfigurations.

/**
 * @return a map of relationships and corresponding roles that could be played by this atom
 */
private Multimap<RelationshipType, Role> inferPossibleRelationConfigurations(Answer sub) {
    Set<Role> roles = getExplicitRoles().filter(r -> !Schema.MetaSchema.isMetaLabel(r.getLabel())).collect(toSet());
    Map<Var, Type> varTypeMap = getParentQuery().getVarTypeMap(sub);
    Set<Type> types = getRolePlayers().stream().filter(varTypeMap::containsKey).map(varTypeMap::get).collect(toSet());
    if (roles.isEmpty() && types.isEmpty()) {
        RelationshipType metaRelationType = tx().admin().getMetaRelationType();
        Multimap<RelationshipType, Role> compatibleTypes = HashMultimap.create();
        metaRelationType.subs().filter(rt -> !rt.equals(metaRelationType)).forEach(rt -> compatibleTypes.putAll(rt, rt.relates().collect(toSet())));
        return compatibleTypes;
    }
    // intersect relation types from roles and types
    Multimap<RelationshipType, Role> compatibleTypes;
    Multimap<RelationshipType, Role> compatibleTypesFromRoles = compatibleRelationTypesWithRoles(roles, new RoleConverter());
    Multimap<RelationshipType, Role> compatibleTypesFromTypes = compatibleRelationTypesWithRoles(types, new TypeConverter());
    if (roles.isEmpty()) {
        compatibleTypes = compatibleTypesFromTypes;
    } else // no types from roles -> roles correspond to mutually exclusive relations
    if (compatibleTypesFromRoles.isEmpty() || types.isEmpty()) {
        compatibleTypes = compatibleTypesFromRoles;
    } else {
        compatibleTypes = multimapIntersection(compatibleTypesFromTypes, compatibleTypesFromRoles);
    }
    return compatibleTypes;
}
Also used : Role(ai.grakn.concept.Role) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) VarPattern(ai.grakn.graql.VarPattern) RelationshipProperty(ai.grakn.graql.internal.pattern.property.RelationshipProperty) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Graql(ai.grakn.graql.Graql) ReasonerUtils.top(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.top) Type(ai.grakn.concept.Type) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) EntityType(ai.grakn.concept.EntityType) HashMultimap(com.google.common.collect.HashMultimap) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) ReasonerUtils.supers(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.supers) RoleConverter(ai.grakn.graql.internal.reasoner.utils.conversion.RoleConverter) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Stream(java.util.stream.Stream) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) ReasonerUtils.compatibleRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRoles) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Iterables(com.google.common.collect.Iterables) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) ReasonerUtils.compatibleRelationTypesWithRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRelationTypesWithRoles) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Rule(ai.grakn.concept.Rule) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ReasonerUtils.multimapIntersection(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.multimapIntersection) ImmutableList(com.google.common.collect.ImmutableList) CommonUtil(ai.grakn.util.CommonUtil) Predicate(ai.grakn.graql.internal.reasoner.atom.predicate.Predicate) Relationship(ai.grakn.concept.Relationship) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) TypeConverter(ai.grakn.graql.internal.reasoner.utils.conversion.TypeConverter) Nullable(javax.annotation.Nullable) ErrorMessage(ai.grakn.util.ErrorMessage) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Iterator(java.util.Iterator) Memoized(com.google.auto.value.extension.memoized.Memoized) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) VarProperty(ai.grakn.graql.admin.VarProperty) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) TypeConverter(ai.grakn.graql.internal.reasoner.utils.conversion.TypeConverter) Type(ai.grakn.concept.Type) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Var(ai.grakn.graql.Var) RoleConverter(ai.grakn.graql.internal.reasoner.utils.conversion.RoleConverter) RelationshipType(ai.grakn.concept.RelationshipType)

Example 59 with Multimap

use of com.google.common.collect.Multimap in project grakn by graknlabs.

the class RelationshipAtom method getRoleRelationPlayerMap.

private Multimap<Role, RelationPlayer> getRoleRelationPlayerMap() {
    Multimap<Role, RelationPlayer> roleRelationPlayerMap = ArrayListMultimap.create();
    Multimap<Role, Var> roleVarMap = getRoleVarMap();
    List<RelationPlayer> relationPlayers = getRelationPlayers();
    roleVarMap.asMap().forEach((role, value) -> {
        Label roleLabel = role.getLabel();
        relationPlayers.stream().filter(rp -> rp.getRole().isPresent()).forEach(rp -> {
            VarPatternAdmin roleTypeVar = rp.getRole().orElse(null);
            Label rl = roleTypeVar != null ? roleTypeVar.getTypeLabel().orElse(null) : null;
            if (roleLabel != null && roleLabel.equals(rl)) {
                roleRelationPlayerMap.put(role, rp);
            }
        });
    });
    return roleRelationPlayerMap;
}
Also used : Role(ai.grakn.concept.Role) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) VarPattern(ai.grakn.graql.VarPattern) RelationshipProperty(ai.grakn.graql.internal.pattern.property.RelationshipProperty) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Graql(ai.grakn.graql.Graql) ReasonerUtils.top(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.top) Type(ai.grakn.concept.Type) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) EntityType(ai.grakn.concept.EntityType) HashMultimap(com.google.common.collect.HashMultimap) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) ReasonerUtils.supers(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.supers) RoleConverter(ai.grakn.graql.internal.reasoner.utils.conversion.RoleConverter) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Stream(java.util.stream.Stream) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) ReasonerUtils.compatibleRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRoles) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Iterables(com.google.common.collect.Iterables) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) ReasonerUtils.compatibleRelationTypesWithRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRelationTypesWithRoles) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Rule(ai.grakn.concept.Rule) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ReasonerUtils.multimapIntersection(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.multimapIntersection) ImmutableList(com.google.common.collect.ImmutableList) CommonUtil(ai.grakn.util.CommonUtil) Predicate(ai.grakn.graql.internal.reasoner.atom.predicate.Predicate) Relationship(ai.grakn.concept.Relationship) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) TypeConverter(ai.grakn.graql.internal.reasoner.utils.conversion.TypeConverter) Nullable(javax.annotation.Nullable) ErrorMessage(ai.grakn.util.ErrorMessage) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Iterator(java.util.Iterator) Memoized(com.google.auto.value.extension.memoized.Memoized) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) VarProperty(ai.grakn.graql.admin.VarProperty) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) Label(ai.grakn.concept.Label) RelationPlayer(ai.grakn.graql.admin.RelationPlayer)

Example 60 with Multimap

use of com.google.common.collect.Multimap in project grakn by graknlabs.

the class BenchmarkIT method loadRandomisedRelationInstances.

private void loadRandomisedRelationInstances(String entityLabel, String fromRoleLabel, String toRoleLabel, String relationLabel, int N, GraknSession session, GraknClient graknClient, Keyspace keyspace) {
    try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).build()) {
        GraknTx tx = session.open(GraknTxType.READ);
        Var entityVar = var().asUserDefined();
        ConceptId[] instances = tx.graql().match(entityVar.isa(entityLabel)).get().execute().stream().map(ans -> ans.get(entityVar).getId()).toArray(ConceptId[]::new);
        assertEquals(instances.length, N);
        Role fromRole = tx.getRole(fromRoleLabel);
        Role toRole = tx.getRole(toRoleLabel);
        RelationshipType relationType = tx.getRelationshipType(relationLabel);
        Random rand = new Random();
        Multimap<Integer, Integer> assignmentMap = HashMultimap.create();
        for (int i = 0; i < N; i++) {
            int from = rand.nextInt(N - 1);
            int to = rand.nextInt(N - 1);
            while (to == from && assignmentMap.get(from).contains(to)) to = rand.nextInt(N - 1);
            Var fromRolePlayer = Graql.var();
            Var toRolePlayer = Graql.var();
            Pattern relationInsert = Graql.var().rel(Graql.label(fromRole.getLabel()), fromRolePlayer).rel(Graql.label(toRole.getLabel()), toRolePlayer).isa(Graql.label(relationType.getLabel())).and(fromRolePlayer.asUserDefined().id(instances[from])).and(toRolePlayer.asUserDefined().id(instances[to]));
            loader.add(Graql.insert(relationInsert.admin().varPatterns()), keyspace).subscribe();
        }
        tx.close();
    }
}
Also used : Iterables(com.google.common.collect.Iterables) VarPattern(ai.grakn.graql.VarPattern) Keyspace(ai.grakn.Keyspace) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) InsertQuery(ai.grakn.graql.InsertQuery) LoggerFactory(org.slf4j.LoggerFactory) Graql(ai.grakn.graql.Graql) Random(java.util.Random) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) Charset(java.nio.charset.Charset) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) GraknClient(ai.grakn.client.GraknClient) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Grakn(ai.grakn.Grakn) GraknSystemProperty(ai.grakn.GraknSystemProperty) EngineContext(ai.grakn.test.rule.EngineContext) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Graql.var(ai.grakn.graql.Graql.var) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) Logger(org.slf4j.Logger) GraknSession(ai.grakn.GraknSession) Test(org.junit.Test) GetQuery(ai.grakn.graql.GetQuery) File(java.io.File) GraknTestUtil.usingTinker(ai.grakn.util.GraknTestUtil.usingTinker) List(java.util.List) SampleKBLoader.randomKeyspace(ai.grakn.util.SampleKBLoader.randomKeyspace) Var(ai.grakn.graql.Var) Pattern(ai.grakn.graql.Pattern) Assert.assertEquals(org.junit.Assert.assertEquals) VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) Var(ai.grakn.graql.Var) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Random(java.util.Random) BatchExecutorClient(ai.grakn.client.BatchExecutorClient)

Aggregations

Multimap (com.google.common.collect.Multimap)61 HashMultimap (com.google.common.collect.HashMultimap)25 List (java.util.List)25 Test (org.junit.Test)20 Map (java.util.Map)17 ImmutableList (com.google.common.collect.ImmutableList)15 Collection (java.util.Collection)14 Set (java.util.Set)14 ImmutableMap (com.google.common.collect.ImmutableMap)13 HashMap (java.util.HashMap)13 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)12 ImmutableSet (com.google.common.collect.ImmutableSet)11 ArrayList (java.util.ArrayList)11 Collectors (java.util.stream.Collectors)11 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)10 Nullable (javax.annotation.Nullable)10 IOException (java.io.IOException)9 Stream (java.util.stream.Stream)9 InetAddress (java.net.InetAddress)8 Objects (java.util.Objects)8