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