Example usage for com.google.common.collect Sets cartesianProduct

List of usage examples for com.google.common.collect Sets cartesianProduct

Introduction

In this page you can find the example usage for com.google.common.collect Sets cartesianProduct.

Prototype

public static <B> Set<List<B>> cartesianProduct(Set<? extends B>... sets) 

Source Link

Document

Returns every possible list that can be formed by choosing one element from each of the given sets in order; the "n-ary <a href="http://en.wikipedia.org/wiki/Cartesian_product">Cartesian product</a>" of the sets.

Usage

From source file:org.jamocha.dn.compiler.pathblocks.PathBlocks.java

protected static void findMatchingAndIncompatibleFilters(
        final Map<Filter, List<FilterInstance>> nFilterToInstances,
        final Set<Either<Rule, ExistentialProxy>> bRules, final List<Filter> nFilters,
        final List<Filter> bFilters,
        final List<Map<Either<Rule, ExistentialProxy>, Set<FilterInstancesSideBySide>>> matchingFilters,
        final List<Filter> incompatibleFilters,
        final Map<Either<Rule, ExistentialProxy>, Map<Filter, FilterInstancesSideBySide>> bRuleToFilterToBlockInstances) {
    // iterate over every single-/multi-cell filter and check that its instances have the same
    // conflicts in every rule
    for (final Filter nFilter : nFilters) {
        boolean matchingConstellationFound = false;

        // iterate over the possible mappings: (filter,rule) -> filter INSTANCE
        final List<Set<FilterInstance>> nListOfRelevantFilterInstancesGroupedByRule = new ArrayList<>(
                nFilterToInstances.get(nFilter).stream()
                        .collect(groupingBy(FilterInstance::getRuleOrProxy, toSet())).values());
        // create the cartesian product
        final Set<List<FilterInstance>> nRelevantFilterInstanceCombinations = Sets
                .cartesianProduct(nListOfRelevantFilterInstancesGroupedByRule);
        // iterate over the possible filter INSTANCE combinations
        cartesianProductLoop: for (final List<FilterInstance> nCurrentOutsideFilterInstances : nRelevantFilterInstanceCombinations) {
            // list of conflicts for this filter INSTANCE combination
            final List<Conflict> conflicts = new ArrayList<>();
            // create a map for faster lookup: rule -> filter INSTANCE (outside)
            final Map<Either<Rule, ExistentialProxy>, FilterInstance> nRuleToCurrentOutsideFilterInstance = nCurrentOutsideFilterInstances
                    .stream().collect(toMap(FilterInstance::getRuleOrProxy, Function.identity()));
            if (bRules.size() > 1) {
                // iterate over the Rule-BlockFilterInstance-mappings
                for (final Entry<Either<Rule, ExistentialProxy>, Map<Filter, FilterInstancesSideBySide>> entry : bRuleToFilterToBlockInstances
                        .entrySet()) {/*  w  w  w.  j av a2  s  . c o m*/
                    int i = 0;
                    final Either<Rule, ExistentialProxy> rule = entry.getKey();
                    final Map<Filter, FilterInstancesSideBySide> bFilterToBlockInstances = entry.getValue();
                    // for every filter of the block check that the conflicts are the same as
                    // those of the first rule
                    for (final Filter bFilter : bFilters) {
                        // get the mapping from rule to filter INSTANCE for the current filter
                        final FilterInstancesSideBySide bSideBySide = bFilterToBlockInstances.get(bFilter);
                        // get the corresponding filter INSTANCE(s) that may be added
                        final FilterInstance nSource = nRuleToCurrentOutsideFilterInstance.get(rule);
                        // iterate over the filter instances
                        for (final FilterInstance bTarget : bSideBySide.getInstances()) {
                            // determine conflict between inside INSTANCE and outside INSTANCE
                            final Conflict conflict = nSource.getOrDetermineConflicts(bTarget);
                            // if this is the first loop iteration, just add the conflict to be
                            // compared later on
                            if (i >= conflicts.size()) {
                                conflicts.add(conflict);
                            } else if (!hasEqualConflicts(conflicts.get(i), conflict)) {
                                // if the conflicts don't match, continue with next filter
                                continue cartesianProductLoop;
                            }
                            ++i;
                        }
                    }
                }
            }
            // conflict identical for all rules
            matchingFilters.add(bRules.stream().collect(toMap(Function.identity(), rule -> Collections
                    .singleton(new FilterInstancesSideBySide(nRuleToCurrentOutsideFilterInstance.get(rule))))));
            matchingConstellationFound = true;
        }
        if (!matchingConstellationFound) {
            incompatibleFilters.add(nFilter);
        }
    }
}