List of usage examples for com.google.common.collect Multimap replaceValues
Collection<V> replaceValues(@Nullable K key, Iterable<? extends V> values);
From source file:org.summer.dsl.xbase.typesystem.conformance.TypeConformanceComputer.java
protected LightweightTypeReference getTypeParametersForSupertype( final Multimap<JvmType, LightweightTypeReference> all, JvmType rawType, ITypeReferenceOwner owner, List<LightweightTypeReference> initiallyRequested) { if (rawType instanceof JvmTypeParameterDeclarator) { List<JvmTypeParameter> typeParameters = ((JvmTypeParameterDeclarator) rawType).getTypeParameters(); // if we do not declare any parameters it is safe to return the first candidate if (typeParameters.isEmpty()) { return getFirstForRawType(all, rawType); }// w w w . j a va2 s.c om List<LightweightTypeReference> parameterSuperTypes = Lists .newArrayListWithCapacity(typeParameters.size()); for (int i = 0; i < typeParameters.size(); i++) { List<LightweightTypeReference> parameterReferences = Lists .newArrayListWithCapacity(typeParameters.size()); for (LightweightTypeReference reference : all.get(rawType)) { if (reference instanceof ParameterizedTypeReference) { ParameterizedTypeReference parameterized = (ParameterizedTypeReference) reference; if (parameterized.getTypeArguments().isEmpty()) { // raw type candidate - best result return parameterized; } LightweightTypeReference parameterReference = parameterized.getTypeArguments().get(i); if (parameterized instanceof FunctionTypeReference && !(parameterReference instanceof WildcardTypeReference)) { FunctionTypeReference functionType = (FunctionTypeReference) parameterized; if (i == typeParameters.size() - 1 && parameterReference.equals(functionType.getReturnType())) { WildcardTypeReference wildcard = new WildcardTypeReference(owner); wildcard.addUpperBound(parameterReference); parameterReferences.add(wildcard); } else if (functionType.getParameterTypes().contains(parameterReference)) { WildcardTypeReference wildcard = createObjectWildcardReference(owner); wildcard.setLowerBound(parameterReference); parameterReferences.add(wildcard); } else { parameterReferences.add(parameterReference); } } else { parameterReferences.add(parameterReference); } } else { return null; } } LightweightTypeReference parameterSuperType = getCommonParameterSuperType(parameterReferences, initiallyRequested, owner); if (parameterSuperType == null) { return null; } else { parameterSuperTypes.add(parameterSuperType); } } ParameterizedTypeReference result = new ParameterizedTypeReference(owner, rawType); for (LightweightTypeReference parameterSuperType : parameterSuperTypes) { result.addTypeArgument(parameterSuperType.copyInto(owner)); } FunctionTypeReference resultAsFunctionType = result.getAsFunctionTypeReference(); if (resultAsFunctionType != null) return resultAsFunctionType; return result; } else if (rawType.eClass() == TypesPackage.Literals.JVM_ARRAY_TYPE) { JvmComponentType componentType = ((JvmArrayType) rawType).getComponentType(); Multimap<JvmType, LightweightTypeReference> copiedMultimap = LinkedHashMultimap.create(all); Collection<LightweightTypeReference> originalReferences = all.get(rawType); List<LightweightTypeReference> componentReferences = Lists .newArrayListWithCapacity(originalReferences.size()); for (LightweightTypeReference originalReference : originalReferences) { addComponentType(originalReference, componentReferences); } copiedMultimap.replaceValues(componentType, componentReferences); List<LightweightTypeReference> componentRequests = Lists .newArrayListWithCapacity(initiallyRequested.size()); for (LightweightTypeReference initialRequest : initiallyRequested) { addComponentType(initialRequest, componentRequests); } LightweightTypeReference componentTypeReference = getTypeParametersForSupertype(copiedMultimap, componentType, owner, componentRequests); if (componentTypeReference != null) { return new ArrayTypeReference(owner, componentTypeReference); } } return null; }
From source file:grakn.core.graql.executor.WriteExecutor.java
static WriteExecutor create(TransactionOLTP transaction, ImmutableSet<Writer> writers) { transaction.checkMutationAllowed();/* w w w.j a v a2 s .co m*/ /* We build several many-to-many relations, indicated by a `Multimap<X, Y>`. These are used to represent the dependencies between properties and variables. `propertyToItsRequiredVars.containsEntry(prop, var)` indicates that the property `prop` cannot be inserted until the concept represented by the variable `var` is created. For example, the property `$x isa $y` depends on the existence of the concept represented by `$y`. */ Multimap<Writer, Variable> executorToRequiredVars = HashMultimap.create(); for (Writer writer : writers) { for (Variable requiredVar : writer.requiredVars()) { executorToRequiredVars.put(writer, requiredVar); } } /* `varToItsProducerProperties.containsEntry(var, prop)` indicates that the concept represented by the variable `var` cannot be created until the property `prop` is inserted. For example, the concept represented by `$x` will not exist before the property `$x isa $y` is inserted. */ Multimap<Variable, Writer> varToProducingWriter = HashMultimap.create(); for (Writer executor : writers) { for (Variable producedVar : executor.producedVars()) { varToProducingWriter.put(producedVar, executor); } } /* Equivalent vars are variables that must represent the same concept as another var. $X label movie, sub entity; $Y label movie; $z isa $Y; In this example, `$z isa $Y` must not be inserted before `$Y` is. However, `$Y` does not have enough information to insert on its own. It also needs a super type! We know `$Y` must represent the same concept as `$X`, because they both share the same label property. Therefore, we can share their dependencies, such that: varToItsProducerProperties.containsEntry($X, prop) <=> varToItsProducerProperties.containsEntry($Y, prop) Therefore: varToItsProducerProperties.containsEntry($X, `$X sub entity`) => varToItsProducerProperties.containsEntry($Y, `$X sub entity`) Now we know that `$Y` depends on `$X sub entity` as well as `$X label movie`, which is enough information to insert the type! */ Partition<Variable> equivalentVars = Partition.singletons(Collections.emptyList()); propertyToEquivalentVars(writers).asMap().values().forEach(vars -> { // These vars must refer to the same concept, so share their dependencies Collection<Writer> producingWriters = vars.stream() .flatMap(var -> varToProducingWriter.get(var).stream()).collect(toList()); Variable first = vars.iterator().next(); vars.forEach(var -> { varToProducingWriter.replaceValues(var, producingWriters); equivalentVars.merge(first, var); }); }); /* Together, `propertyToItsRequiredVars` and `varToItsProducerProperties` can be composed into a single many-to-many relation: propertyDependencies = propertyToItsRequiredVars varToItsProducerProperties By doing so, we map directly between properties, skipping the vars. For example, if we previously had: propertyToItsRequiredVars.containsEntry(`$x isa $y`, `$y`); // `$x isa $y` depends on `$y` varToItsProducerProperties.containsEntry(`$y`, `$y label movie`); // `$y` depends on `$y label movie` Then it follows that: propertyDependencies.containsEntry(`$x isa $y`, `$y label movie`); // `$x isa $y` depends on `$y label movie` The `propertyDependencies` relation contains all the information to decide what order to execute the properties. */ Multimap<Writer, Writer> writerDependencies = writerDependencies(executorToRequiredVars, varToProducingWriter); return new WriteExecutor(transaction, writers, equivalentVars, writerDependencies); }
From source file:ai.grakn.graql.internal.query.QueryOperationExecutor.java
private static QueryOperationExecutor create(Collection<VarPatternAdmin> patterns, GraknTx graph, ExecutionType executionType) {/*from www .j a v a 2 s. co m*/ ImmutableSet<VarAndProperty> properties = patterns.stream().flatMap(VarAndProperty::fromPattern) .collect(toImmutableSet()); /* We build several many-to-many relations, indicated by a `Multimap<X, Y>`. These are used to represent the dependencies between properties and variables. `propDependencies.containsEntry(prop, var)` indicates that the property `prop` cannot be inserted until the concept represented by the variable `var` is created. For example, the property `$x isa $y` depends on the existence of the concept represented by `$y`. */ Multimap<VarAndProperty, Var> propDependencies = HashMultimap.create(); for (VarAndProperty property : properties) { for (Var requiredVar : property.executor(executionType).requiredVars()) { propDependencies.put(property, requiredVar); } } /* `varDependencies.containsEntry(var, prop)` indicates that the concept represented by the variable `var` cannot be created until the property `prop` is inserted. For example, the concept represented by `$x` will not exist before the property `$x isa $y` is inserted. */ Multimap<Var, VarAndProperty> varDependencies = HashMultimap.create(); for (VarAndProperty property : properties) { for (Var producedVar : property.executor(executionType).producedVars()) { varDependencies.put(producedVar, property); } } /* Equivalent vars are variables that must represent the same concept as another var. $X label movie, sub entity; $Y label movie; $z isa $Y; In this example, `$z isa $Y` must not be inserted before `$Y` is. However, `$Y` does not have enough information to insert on its own. It also needs a super type! We know `$Y` must represent the same concept as `$X`, because they both share the same label property. Therefore, we can share their dependencies, such that: varDependencies.containsEntry($X, prop) <=> varDependencies.containsEntry($Y, prop) Therefore: varDependencies.containsEntry($X, `$X sub entity`) => varDependencies.containsEntry($Y, `$X sub entity`) Now we know that `$Y` depends on `$X sub entity` as well as `$X label movie`, which is enough information to insert the type! */ Partition<Var> equivalentVars = Partition.singletons(Collections.emptyList()); equivalentProperties(properties).asMap().values().forEach(vars -> { // These vars must refer to the same concept, so share their dependencies Collection<VarAndProperty> producers = vars.stream().flatMap(var -> varDependencies.get(var).stream()) .collect(toList()); Var first = vars.iterator().next(); vars.forEach(var -> { varDependencies.replaceValues(var, producers); equivalentVars.merge(first, var); }); }); /* Together, `propDependencies` and `varDependencies` can be composed into a single many-to-many relation: dependencies = propDependencies varDependencies By doing so, we map _directly_ between properties, skipping the vars. For example, if we previously had: propDependencies.containsEntry(`$x isa $y`, `$y`); // `$x isa $y` depends on `$y` varDependencies.containsEntry(`$y`, `$y label movie`); // `$y` depends on `$y label movie` Then it follows that: dependencies.containsEntry(`$x isa $y`, `$y label movie`); // `$x isa $y` depends on `$y label movie` The `dependencies` relation contains all the information to decide what order to execute the properties. */ Multimap<VarAndProperty, VarAndProperty> dependencies = composeMultimaps(propDependencies, varDependencies); return new QueryOperationExecutor(graph, properties, equivalentVars, ImmutableMultimap.copyOf(dependencies), executionType); }