/// <summary>
        /// Returns a new table that is the union of the input tables.
        /// </summary>
        /// <param name="parameters">The model's parameters</param>
        /// <param name="first">First table</param>
        /// <param name="second">Second table</param>
        /// <param name="newState">Function to calculate state of merged value combinations</param>
        /// <returns>The new table</returns>
        public static ParameterInteraction Merge <T>(IList <ParameterBase> parameters, ParameterInteraction first, ParameterInteraction second, Func <ValueCombinationState, ValueCombinationState, ValueCombinationState> newState)
            where T : new()
        {
            List <int> parameterIndices = first.Parameters.Union(second.Parameters).ToList();

            parameterIndices.Sort();

            var mergedInteraction = new ParameterInteraction(parameterIndices);

            var valueTable = ParameterInteractionTable <T> .GenerateValueTable(parameters, mergedInteraction);

            foreach (var value in valueTable)
            {
                mergedInteraction.Combinations.Add(new ValueCombination(value, mergedInteraction));
            }

            foreach (var combination in mergedInteraction.Combinations)
            {
                var firstMatch  = first.Combinations.First((c) => ParameterInteractionTable <T> .MatchCombination(c, combination));
                var secondMatch = second.Combinations.First((c) => ParameterInteractionTable <T> .MatchCombination(c, combination));
                combination.State = newState(firstMatch.State, secondMatch.State);
            }

            return(mergedInteraction);
        }
Example #2
0
        private void GenerateHigherOrderDependentExclusions(Model <T> model, int order, List <int> parameterInteractionCounts, ParameterInteraction completeInteraction, IEnumerable <ValueCombination> allowedCombinations)
        {
            // generate the combinations for orders between order and completerInteraction.Parameters.Count
            foreach (var count in parameterInteractionCounts)
            {
                IList <int[]> parameterCombinations = GenerateCombinations(completeInteraction.Parameters.Count, count);
                foreach (var combinations in parameterCombinations)
                {
                    var interaction    = new ParameterInteraction(combinations.Select((i) => completeInteraction.Parameters[i]));
                    var possibleValues = ParameterInteractionTable <T> .GenerateValueTable(model.Parameters, interaction);

                    foreach (var value in possibleValues)
                    {
                        interaction.Combinations
                        .Add(new ValueCombination(value, interaction)
                        {
                            State = ValueCombinationState.Covered
                        });
                    }

                    // find combinations that should be excluded
                    var excludedCombinations = new List <ValueCombination>();
                    foreach (var combination in interaction.Combinations)
                    {
                        if (!combination.ParameterToValueMap.Any((p) => !completeInteraction.Parameters.Contains(p.Key)) &&
                            !allowedCombinations.Any((c) => MatchCombination(combination, c)))
                        {
                            excludedCombinations.Add(combination);
                        }
                    }

                    if (excludedCombinations.Count() > 0)
                    {
                        foreach (var combination in excludedCombinations)
                        {
                            combination.State = ValueCombinationState.Excluded;
                        }

                        MergeConstraintInteraction(order, interaction);
                    }
                }
            }
        }
        // helper to implement Constraint.GetExcludeCombinations
        internal static ParameterInteraction GetExcludedCombinations <T>(Model model, Constraint constraint, Parameter first, Parameter second, T value, Func <T, T, bool> comparison)
        {
            Debug.Assert(first != null && model != null && constraint != null && comparison != null);
            List <int> parameterIndices = new List <int>();

            parameterIndices.Add(model.Parameters.IndexOf(first));
            if (second != null)
            {
                parameterIndices.Add(model.Parameters.IndexOf(second));
            }
            parameterIndices.Sort();

            ParameterInteraction interaction = new ParameterInteraction(parameterIndices);
            List <int[]>         valueTable  = ParameterInteractionTable.GenerateValueTable(model.Parameters, interaction);

            foreach (var valueIndices in valueTable)
            {
                T value1, value2;

                value1 = (T)first[valueIndices[0]];
                if (second == null)
                {
                    value2 = value;
                }
                else
                {
                    value2 = (T)second[valueIndices[1]];
                }

                ValueCombinationState comboState = comparison(value1, value2) ? ValueCombinationState.Covered : ValueCombinationState.Excluded;
                interaction.Combinations.Add(new ValueCombination(valueIndices, interaction)
                {
                    State = comboState
                });
            }

            return(interaction);
        }
        // for the following system:
        // if A == 0 then B == 0
        // if B == 0 then C == 0
        // if C == 0 then A == 1
        // all combinations with A == 0 need to be excluded, but this is impossible to determine when looking at individual constraints
        // to do this:
        //      - create groups of constraints where all members have a parameter that overlaps with another constraint
        //      - create ParameterInteraction for each group of constraints
        //      - for the existing uncovered combinations, if they excluded in every matching combination in the group interaction, mark excluded
        //      - higher order combinations that match the order of a constraint's interaction can also need to be exculded
        private void GenerateDependentConstraintInteractions(Model model, int order, List <ParameterInteraction> constraintInteractions)
        {
            // group all the constraint parameter interactions that share parameters
            var dependentConstraintSets = new List <List <ParameterInteraction> >();

            while (constraintInteractions.Count > 0)
            {
                var dependentConstraintSet = new List <ParameterInteraction>();
                var interactionsToExplore  = new List <ParameterInteraction>();

                interactionsToExplore.Add(constraintInteractions[0]);
                constraintInteractions.RemoveAt(0);

                while (interactionsToExplore.Count > 0)
                {
                    ParameterInteraction current = interactionsToExplore[0];
                    interactionsToExplore.RemoveAt(0);
                    dependentConstraintSet.Add(current);

                    var dependentInteractions =
                        (from constraint in constraintInteractions
                         where constraint.Parameters.Any((i) => current.Parameters.Contains(i))
                         select constraint).ToList();

                    foreach (var dependentInteraction in dependentInteractions)
                    {
                        constraintInteractions.Remove(dependentInteraction);
                    }

                    interactionsToExplore.AddRange(dependentInteractions);
                }

                dependentConstraintSets.Add(dependentConstraintSet);
            }

            // walk over the groups of constraints
            foreach (var dependentConstraintSet in dependentConstraintSets)
            {
                // if there's only one constraint no more processing is necessary
                if (dependentConstraintSet.Count <= 1)
                {
                    continue;
                }

                // merge the interactions of all the constraints
                var uniqueParameters           = new Dictionary <int, bool>();
                var parameterInteractionCounts = new List <int>();
                for (int i = 0; i < dependentConstraintSet.Count; i++)
                {
                    foreach (var parameter in dependentConstraintSet[i].Parameters)
                    {
                        uniqueParameters[parameter] = true;
                    }

                    if (dependentConstraintSet[i].Parameters.Count > order)
                    {
                        parameterInteractionCounts.Add(dependentConstraintSet[i].Parameters.Count);
                    }
                }

                var sortedParameters = uniqueParameters.Keys.ToList();
                sortedParameters.Sort();

                ParameterInteraction completeInteraction = new ParameterInteraction(sortedParameters);
                var valueTable = ParameterInteractionTable.GenerateValueTable(model.Parameters, completeInteraction);
                foreach (var value in valueTable)
                {
                    completeInteraction.Combinations
                    .Add(new ValueCombination(value, completeInteraction)
                    {
                        State = ValueCombinationState.Covered
                    });
                }

                // calculate the excluded combinations in the new uber interaction
                var completeInteractionExcludedCombinations = new List <ValueCombination>();

                // find the combinations from the uber interaction that aren't excluded
                // if a combination is a subset of any of these it is not excluded
                var allowedCombinations = new List <ValueCombination>();

                foreach (var combination in completeInteraction.Combinations)
                {
                    bool exclude = false;
                    foreach (var constraint in completeConstraints)
                    {
                        if (constraint.SatisfiesContraint(model, combination) == ConstraintSatisfaction.Unsatisfied)
                        {
                            exclude = true;
                            break;
                        }
                    }

                    if (exclude)
                    {
                        combination.State = ValueCombinationState.Excluded;
                        completeInteractionExcludedCombinations.Add(combination);
                    }
                    else
                    {
                        allowedCombinations.Add(combination);
                    }
                }

                // find the existing combinations that are never allowed in the uber interaction
                var individualInteractionExcludedCombinations =
                    from interaction in Interactions
                    from combination in interaction.Combinations
                    where !combination.ParameterToValueMap.Any((p) => !completeInteraction.Parameters.Contains(p.Key)) &&
                    !allowedCombinations.Any((c) => MatchCombination(combination, c))
                    select combination;

                // mark the combinations in the table as excluded
                foreach (var combination in individualInteractionExcludedCombinations)
                {
                    combination.State = ValueCombinationState.Excluded;
                }

                GenerateHigherOrderDependentExclusions(model, order, parameterInteractionCounts, completeInteraction, allowedCombinations);
            }
        }