Exemplo n.º 1
0
        /// <summary>
        /// Replaces the names for parent, children, etc. with their actual objects of the variability model
        /// </summary>
        internal void init()
        {
            this.parent = vm.getBinaryOption(parentName);
            foreach (var name in this.children_names)
            {
                ConfigurationOption c = vm.getBinaryOption(name);
                if (c == null)
                {
                    c = vm.getNumericOption(name);
                }
                if (c == null)
                {
                    continue;
                }
                this.children.Add(c);
            }

            foreach (var imply_names in this.implied_Options_names)
            {
                List <ConfigurationOption> optionImplies = new List <ConfigurationOption>();
                foreach (var optName in imply_names)
                {
                    ConfigurationOption c = vm.getBinaryOption(optName);
                    if (c == null)
                    {
                        c = vm.getNumericOption(optName);
                    }
                    if (c == null)
                    {
                        continue;
                    }
                    optionImplies.Add(c);
                }
                this.implied_Options.Add(optionImplies);
            }

            foreach (var exclud_names in this.excluded_Options_names)
            {
                List <ConfigurationOption> excImplies = new List <ConfigurationOption>();
                foreach (var optName in exclud_names)
                {
                    ConfigurationOption c = vm.getBinaryOption(optName);
                    if (c == null)
                    {
                        c = vm.getNumericOption(optName);
                    }
                    if (c == null)
                    {
                        continue;
                    }
                    excImplies.Add(c);
                }
                this.excluded_Options.Add(excImplies);
            }
        }
        /// <summary>
        /// Parses the binary options, represented as string, of a configuration.
        /// </summary>
        /// <param name="binaryString">The string representation of the selected binary configuration options.</param>
        /// <param name="binaryOptions">The data strcutre containing the parsed binary options and their values.</param>
        /// <param name="varModel">The variability model the configuration is defined for.</param>
        /// <returns>True if the configuration is valid.</returns>
        private static bool parseBinaryOptionString(string binaryString, out Dictionary <BinaryOption, BinaryOption.BinaryValue> binaryOptions, VariabilityModel varModel)
        {
            bool valid = true;

            Dictionary <BinaryOption, BinaryOption.BinaryValue> _binaryOptions = new Dictionary <BinaryOption, BinaryOption.BinaryValue>();

            string[] binaryOptionNames = binaryString.Split(',');
            foreach (string binaryOptionName in binaryOptionNames)
            {
                string currOption = binaryOptionName.Trim();
                if (currOption.Length > 0)
                {
                    BinaryOption bOpt = null;

                    bOpt = varModel.getBinaryOption(currOption);

                    if (bOpt == null)
                    {
                        GlobalState.logError.logLine("No Binary option found with name: " + currOption);
                        valid = false;
                        break;
                    }
                    _binaryOptions.Add(bOpt, BinaryOption.BinaryValue.Selected);
                }
            }

            binaryOptions = _binaryOptions;

            return(valid);
        }
Exemplo n.º 3
0
        private bool tokenIsAFeatureOrNumber(string token, VariabilityModel varModel)
        {
            token = token.Trim();

            double value = 0.0;

            if (Double.TryParse(token, out value))
            {
                return(true);
            }

            if (varModel != null)
            {
                NumericOption numOption = varModel.getNumericOption(token);
                if (numOption != null)
                {
                    if (!participatingNumOptions.Contains(numOption))
                    {
                        this.participatingNumOptions.Add(numOption);
                    }
                    numberOfParticipatingFeatures++;
                    return(true);
                }

                BinaryOption binOption = varModel.getBinaryOption(token);

                if (token.StartsWith("Enabled") || token.StartsWith("Disabled"))
                {
                    binOption = getAbstractOption(token, varModel);
                }

                if (binOption != null)
                {
                    if (!participatingBoolOptions.Contains(binOption))
                    {
                        this.participatingBoolOptions.Add(binOption);
                    }
                    numberOfParticipatingFeatures++;
                    return(true);
                }
            }
            else
            {
                if (token.Equals(this.numOption.Name))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 4
0
        private static double getValueOfToken(Configuration config, string token, VariabilityModel fm)
        {
            token = token.Trim();

            double value = 0.0;

            if (Double.TryParse(token, out value))
            {
                return(value);
            }

            NumericOption numOpt = fm.getNumericOption(token);

            if (numOpt != null)
            {
                return(config.NumericOptions[numOpt]);
            }
            BinaryOption binOpt = fm.getBinaryOption(token);

            if (token.StartsWith("Enabled") || token.StartsWith("Disabled"))
            {
                binOpt = getAbstractOption(token, fm);
            }

            if (binOpt != null)
            {
                if (token.Equals("base") || token.Equals("root"))
                {
                    return(1.0);
                }

                if (config.BinaryOptions.Keys.Contains(binOpt) && config.BinaryOptions[binOpt] == BinaryOption.BinaryValue.Selected)
                {
                    return(1.0);
                }
                else
                {
                    foreach (BinaryOption option in config.BinaryOptions.Keys)
                    {
                        // option has to be selected in the configuration
                        if (option.Name == binOpt.Name && config.BinaryOptions[option] == BinaryOption.BinaryValue.Selected)
                        {
                            return(1.0);
                        }
                    }
                }
            }
            return(0.0);
        }
Exemplo n.º 5
0
        private static double getValueOfToken(Configuration config, string token, VariabilityModel fm)
        {
            token = token.Trim();

            double value = 0.0;

            if (Double.TryParse(token, out value))
            {
                return(value);
            }

            NumericOption numOpt = fm.getNumericOption(token);

            if (numOpt != null)
            {
                return(config.NumericOptions[numOpt]);
            }
            BinaryOption binOpt = fm.getBinaryOption(token);

            if (binOpt != null)
            {
                if (token.Equals("base"))
                {
                    return(1.0);
                }

                if (config.BinaryOptions.Keys.Contains(binOpt) && config.BinaryOptions[binOpt] == BinaryOption.BinaryValue.Selected)
                {
                    return(1.0);
                }
                else
                {
                    foreach (BinaryOption option in config.BinaryOptions.Keys)
                    {
                        if (option.Name == binOpt.Name)
                        {
                            return(1.0);
                        }
                    }
                }
            }
            return(0.0);
        }
        private static List <Configuration> readCSVBinaryOptFormat(string file, VariabilityModel model)
        {
            List <Configuration> result = new List <Configuration>();
            StreamReader         sr     = new StreamReader(file);

            while (!sr.EndOfStream)
            {
                var line = sr.ReadLine().Split(';');
                List <BinaryOption> temp = new List <BinaryOption>();
                for (int i = 0; i < line.Length - 1; i++)
                {
                    BinaryOption b = model.getBinaryOption(line[i]);
                    temp.Add(b);
                }
                double value = Double.Parse(line[line.Length - 1].Replace(',', '.'), System.Globalization.CultureInfo.InvariantCulture);
                var    c     = new Configuration(temp);
                c.setMeasuredValue(GlobalState.currentNFP, value);
                result.Add(c);
            }
            sr.Close();
            return(result);
        }
        /// <summary>
        /// Creates a configuration based on a hash representation of that configuration.
        /// </summary>
        /// <param name="hashString">The String which from which we can infer the configuration</param>
        /// <param name="vm">The variability model that is required for identifying options in the hash string to instantiate actual configuration options.</param>
        /// <returns>A configuration that maps to the given hash string.</returns>
        internal static Configuration createFromHashString(string hashString, VariabilityModel vm)
        {
            Dictionary <NumericOption, double> numOptions = new Dictionary <NumericOption, double>();
            List <BinaryOption> binaryFeatures            = new List <BinaryOption>();
            Configuration       c;

            String[] optionList = hashString.Split(new String[] { "%;%" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (String option in optionList)
            {
                if (Char.IsDigit(option[option.Length - 1]))//If last char is a digit, then it must be a numeric option
                {
                    //Now remove the digit from the name
                    int  index = option.Length - 1;
                    Char last  = option[index];

                    while (Char.IsDigit(last) || last == ',' || last == '.' || last == '-')
                    {
                        index--;
                        last = option[index];
                    }
                    Double        optionsValue = Math.Round(Double.Parse(option.Substring(index + 1).Replace(',', '.')), 1);
                    NumericOption no           = vm.getNumericOption(option.Substring(0, index + 1));
                    if (no == null)
                    {
                        continue;
                    }
                    numOptions.Add(no, optionsValue);
                }
                else
                {
                    BinaryOption binOpt = vm.getBinaryOption(option);
                    binaryFeatures.Add(binOpt);
                }
            }
            c = new Configuration(binaryFeatures, numOptions);
            return(c);
        }
        private static List<Configuration> readCSVBinaryOptFormat(string file, VariabilityModel model)
        {
            List<Configuration> result = new List<Configuration>();
            StreamReader sr = new StreamReader(file);
            while (!sr.EndOfStream)
            {
                var line = sr.ReadLine().Split(';');
                List<BinaryOption> temp = new List<BinaryOption>();
                for (int i = 0; i < line.Length - 1; i++)
                {
                    BinaryOption b = model.getBinaryOption(line[i]);
                    temp.Add(b);
                }
                double value = Double.Parse(line[line.Length - 1].Replace(',', '.'));
                var c = new Configuration(temp);
                c.setMeasuredValue(GlobalState.currentNFP, value);
                result.Add(c);

            }
            sr.Close();
            return result;
        }
        /// <summary>
        /// This method returns a list of all configurations stored in a given file. All options of the configurations have to be defined in the variability model. 
        /// </summary>
        /// <param name="dat">Object representing the configuration file.</param>
        /// <param name="model">Variability model of the configurations.</param>
        /// <returns>Returns a list of configurations that were defined in the XML document. Can be an empty list.</returns>
        public static List<Configuration> readConfigurations(XmlDocument dat, VariabilityModel model)
        {
            XmlElement currentElemt = dat.DocumentElement;

            // Retrieve the decimal delimiter and the separator sign if included
            if (currentElemt.HasAttribute(decimalDelimiterTag) && currentElemt.HasAttribute(separatorTag))
            {
                // I assume that the decimal delimiter as well as the separator are only one symbol
                ConfigurationReader.decimalDelimiter = currentElemt.GetAttribute(decimalDelimiterTag)[0];
                ConfigurationReader.separator = currentElemt.GetAttribute(separatorTag)[0];

                if (currentElemt.GetAttribute(decimalDelimiterTag).Length > 1 || currentElemt.GetAttribute(separatorTag).Length > 1)
                {
                    GlobalState.logError.log("The decimal delimiter and the separator must consist of only one symbol.");
                }
                if (ConfigurationReader.decimalDelimiter == ConfigurationReader.separator)
                {
                    GlobalState.logError.log("The decimal delimiter symbol and the separator symbol must be different.");
                }
            }
            else if (currentElemt.HasAttribute(decimalDelimiterTag))
            {
                ConfigurationReader.decimalDelimiter = currentElemt.GetAttribute(decimalDelimiterTag)[0];
                if (currentElemt.GetAttribute(decimalDelimiterTag).Length > 1)
                {
                    GlobalState.logError.log("The decimal delimiter must consist of only one symbol.");
                }
            }
            else if (currentElemt.HasAttribute(separatorTag))
            {
                ConfigurationReader.separator = currentElemt.GetAttribute(separatorTag)[0];
                if (currentElemt.GetAttribute(separatorTag).Length > 1)
                {
                    GlobalState.logError.log("The separator symbol must be different.");
                }
            }

            HashSet<Configuration> configurations = new HashSet<Configuration>();

            int numberOfConfigs = currentElemt.ChildNodes.Count;
            int configsWithTooLargeDeviation = 0;
            foreach (XmlNode node in currentElemt.ChildNodes)
            {
                bool readMultipleMeasurements = false;
                if (node.Attributes.Count > 0 && node.Attributes[0].Value.ToLower() == "true")
                {
                    readMultipleMeasurements = true;
                }
                Dictionary<NFProperty, double> propertiesForConfig = new Dictionary<NFProperty, double>(); ;
                bool alternativeFormat = false;
                string binaryString = "";
                string numericString = "";
                string configID = "";
                Dictionary<NFProperty, double> measuredProperty = new Dictionary<NFProperty, double>();
                Configuration c = null;
                bool hasSetConfig = false;
                foreach (XmlNode childNode in node.ChildNodes)
                {
                    if (c == null && hasSetConfig)
                        continue;
                    switch (childNode.Attributes[0].Value)
                    {
                        // TODO we use this to support result files having the old structure
                        case "Configuration":
                            binaryString = childNode.InnerText;
                            break;
                        case "Variable Features":
                            numericString = childNode.InnerText;
                            break;

                        case "BinaryOptions":
                            binaryString = childNode.InnerText;
                            break;
                        case "NumericOptions":
                            numericString = childNode.InnerText;
                            break;
                        case "ConfigID":
                            if (readMultipleMeasurements)
                            {
                                configID = childNode.InnerText.Replace("_", "%;%");
                            }
                            else
                                configID = childNode.InnerText;
                            if (configID.Contains("%;%seek") && configID.Contains("%;%seek0") == false)
                            {
                                hasSetConfig = true;
                                c = null;
                                break;
                            }
                            alternativeFormat = true;
                            c = Configuration.createFromHashString(configID, GlobalState.varModel);
                            hasSetConfig = true;
                            break;
                        case "CompilerOptions":
                            //todo
                            break;
                        case "ConfigFileOptions":
                            //todo
                            break;
                        case "ParameterOptions":
                            //todo
                            break;
                        case "ProgramName":
                            //todo
                            break;
                        case "StartupBegin":
                            //todo
                            break;
                        case "StartupEnd":
                            //todo
                            break;
                        default:
                            NFProperty property = GlobalState.getOrCreateProperty(childNode.Attributes[0].Value);
                            double measuredValue = 0;
                            //-1 means that measurement failed... 3rd values strongly devigates in C.'s measurements, hence we use it only in case we have no other measurements
                            if (readMultipleMeasurements)
                            {
                                //if (property.Name != "run-real")
                                //    continue;
                                String[] m = childNode.InnerText.ToString().Split(separator);
                                double val1 = 0;
                                if (!Double.TryParse(m[0], out val1))
                                    break;
                                if (m.Length > 1)
                                {
                                    List<double> values = new List<double>();
                                    double avg = 0;
                                    foreach (var i in m)
                                    {
                                        double d = Convert.ToDouble(i.Replace(decimalDelimiter, '.'));
                                        if (d != -1)
                                        {
                                            values.Add(d);
                                            avg += d;
                                        }
                                    }
                                    if (values.Count == 0)
                                    {
                                        configsWithTooLargeDeviation++;
                                        c = null;
                                        break;
                                    }
                                    avg = avg / values.Count;
                                    /* foreach (var d in values)
                                        {
                                            if ((d / avg) * 100 > 10)
                                            {
                                                configsWithTooLargeDeviation++;
                                                c = null;
                                                break;
                                            }
                                        }*/
                                    measuredValue = avg;
                                    /*double val2 = Convert.ToDouble(m[1]);
                                        if (val1 == -1)
                                            measuredValue = val2;
                                        else if (val1 == -1 && val2 == -1)
                                            measuredValue = Convert.ToDouble(m[2]);
                                        else if (val2 == -1)
                                            measuredValue = val1;
                                        else
                                            measuredValue = (val1 + val2) / 2;*/
                                }
                                else
                                    measuredValue = val1;
                            }
                            else
                                measuredValue = Convert.ToDouble(childNode.InnerText.ToString().Replace(decimalDelimiter, '.'));

                            // Save the largest measured value.
                            double currentMaxMeasuredValue;
                            if (GlobalState.allMeasurements.maxMeasuredValue.TryGetValue(property, out currentMaxMeasuredValue))
                            {
                                if (Math.Abs(measuredValue) > Math.Abs(currentMaxMeasuredValue))
                                {
                                    GlobalState.allMeasurements.maxMeasuredValue[property] = measuredValue;
                                }
                            }
                            else
                            {
                                GlobalState.allMeasurements.maxMeasuredValue.Add(property, measuredValue);
                            }

                            // Add measured value to the configuration.
                            if (alternativeFormat && c != null)
                            {
                                c.setMeasuredValue(property, measuredValue);
                            }
                            else
                            {
                                measuredProperty.Add(property, measuredValue);
                            }
                            break;
                    }
                }

                if (alternativeFormat && c != null)
                {
                    if (configurations.Contains(c))
                    {
                        GlobalState.logError.logLine("Mutiple definition of one configuration in the configurations file:  " + c.ToString());
                    }
                    else
                    {
                        // if (GlobalState.currentNFP != null && c.nfpValues.Keys.Contains(GlobalState.currentNFP) && c.nfpValues[GlobalState.currentNFP] != -1)
                        configurations.Add(c);
                    }
                cont: { }
                    continue;
                }

                Dictionary<BinaryOption, BinaryOption.BinaryValue> binaryOptions = new Dictionary<BinaryOption, BinaryOption.BinaryValue>();

                string[] binaryOptionNames = binaryString.Split(',');
                foreach (string binaryOptionName in binaryOptionNames)
                {
                    string currOption = binaryOptionName.Trim();
                    if (currOption.Length > 0)
                    {
                        BinaryOption bOpt = null;

                        bOpt = model.getBinaryOption(currOption);

                        if (bOpt == null)
                            GlobalState.logError.logLine("No Binary option found with name: " + currOption);
                        binaryOptions.Add(bOpt, BinaryOption.BinaryValue.Selected);
                    }
                }

                // Add "root" binary option to the configuration
                if (!binaryOptions.ContainsKey(model.Root))
                    binaryOptions.Add(model.Root, BinaryOption.BinaryValue.Selected);

                Dictionary<NumericOption, double> numericOptions = new Dictionary<NumericOption, double>();
                if (!string.IsNullOrEmpty(numericString))
                {
                    string[] numOptionArray = numericString.Trim().Split(',');
                    foreach (string numOption in numOptionArray)
                    {
                        string[] numOptionsKeyValue;
                        if (numOption.Contains(";"))
                            numOptionsKeyValue = numOption.Split(';');
                        else
                            numOptionsKeyValue = numOption.Split(' ');// added for rc-lookahead 40
                        numOptionsKeyValue[0] = numOptionsKeyValue[0].Trim();
                        if (numOptionsKeyValue[0].Length == 0)
                            continue;
                        NumericOption varFeat = model.getNumericOption(numOptionsKeyValue[0]);
                        if (varFeat == null)
                            GlobalState.logError.logLine("No numeric option found with name: " + numOptionsKeyValue[0]);
                        double varFeatValue = Convert.ToDouble(numOptionsKeyValue[1]);

                        numericOptions.Add(varFeat, varFeatValue);
                    }
                }

                Configuration config = new Configuration(binaryOptions, numericOptions, measuredProperty);

                //if(configurations.Contains(config))
                //{
                //    GlobalState.logError.log("Mutiple definition of one configuration in the configurations file:  " + config.ToString());
                //}else
                //{
                configurations.Add(config);
                //}
            }
            GlobalState.logInfo.logLine("Configs with too large deviation: " + configsWithTooLargeDeviation);
            return configurations.ToList();
        }
Exemplo n.º 10
0
        /// <summary>
        /// Generates a constraint system based on a variability model. The constraint system can be used to check for satisfiability of configurations as well as optimization.
        /// </summary>
        /// <param name="variables">Empty input, outputs a list of CSP terms that correspond to the configuration options of the variability model</param>
        /// <param name="optionToTerm">A map to get for a given configuration option the corresponding CSP term of the constraint system</param>
        /// <param name="termToOption">A map that gives for a given CSP term the corresponding configuration option of the variability model</param>
        /// <param name="vm">The variability model for which we generate a constraint system</param>
        /// <returns>The generated constraint system consisting of logical terms representing configuration options as well as their boolean constraints.</returns>
        internal static ConstraintSystem getConstraintSystem(out List<CspTerm> variables, out Dictionary<BinaryOption, CspTerm> optionToTerm, out Dictionary<CspTerm, BinaryOption> termToOption, VariabilityModel vm)
        {
            //Reusing seems to not work correctely. The problem: configurations are realized as additional constraints for the system.
            //however, when checking for the next config, the old config's constraints remain in the solver such that we have a wrong result.
            /*
            if (csystem != null && variables_global != null && optionToTerm_global != null && termToOption_global != null && vm != null)
            {//For optimization purpose
                if (vm.BinaryOptions.Count == vm_global.BinaryOptions.Count && vm.Name.Equals(vm_global.Name))
                {
                    variables = variables_global;
                    optionToTerm = optionToTerm_global;
                    termToOption = termToOption_global;
                    return csystem;
                }
            }*/

            ConstraintSystem S = ConstraintSystem.CreateSolver();

            optionToTerm = new Dictionary<BinaryOption, CspTerm>();
            termToOption = new Dictionary<CspTerm, BinaryOption>();
            variables = new List<CspTerm>();
            foreach (BinaryOption binOpt in vm.BinaryOptions)
            {
                CspDomain domain = S.DefaultBoolean;
                CspTerm temp = S.CreateVariable(domain, binOpt);
                optionToTerm.Add(binOpt, temp);
                termToOption.Add(temp, binOpt);
                variables.Add(temp);
            }

            List<List<ConfigurationOption>> alreadyHandledAlternativeOptions = new List<List<ConfigurationOption>>();

            //Constraints of a single configuration option
            foreach (BinaryOption current in vm.BinaryOptions)
            {
                CspTerm cT = optionToTerm[current];
                if (current.Parent == null || current.Parent == vm.Root)
                {
                    if (current.Optional == false && current.Excluded_Options.Count == 0)
                        S.AddConstraints(S.Implies(S.True, cT));
                    else
                        S.AddConstraints(S.Implies(cT, optionToTerm[vm.Root]));
                }

                if (current.Parent != null && current.Parent != vm.Root)
                {
                    CspTerm parent = optionToTerm[(BinaryOption)current.Parent];
                    S.AddConstraints(S.Implies(cT, parent));
                    if (current.Optional == false && current.Excluded_Options.Count == 0)
                        S.AddConstraints(S.Implies(parent, cT));//mandatory child relationship
                }

                //Alternative or other exclusion constraints
                if (current.Excluded_Options.Count > 0)
                {
                    List<ConfigurationOption> alternativeOptions = current.collectAlternativeOptions();
                    if (alternativeOptions.Count > 0)
                    {
                        //Check whether we handled this group of alternatives already
                        foreach (var alternativeGroup in alreadyHandledAlternativeOptions)
                            foreach (var alternative in alternativeGroup)
                                if (current == alternative)
                                    goto handledAlternative;

                        //It is not allowed that an alternative group has no parent element
                        CspTerm parent = null;
                        if (current.Parent == null)
                            parent = S.True;
                        else
                            parent = optionToTerm[(BinaryOption)current.Parent];

                        CspTerm[] terms = new CspTerm[alternativeOptions.Count + 1];
                        terms[0] = cT;
                        int i = 1;
                        foreach (BinaryOption altEle in alternativeOptions)
                        {
                            CspTerm temp = optionToTerm[altEle];
                            terms[i] = temp;
                            i++;
                        }
                        S.AddConstraints(S.Implies(parent, S.ExactlyMofN(1, terms)));
                        alreadyHandledAlternativeOptions.Add(alternativeOptions);
                        handledAlternative: { }
                    }

                    //Excluded option(s) as cross-tree constraint(s)
                    List<List<ConfigurationOption>> nonAlternative = current.getNonAlternativeExlcudedOptions();
                    if(nonAlternative.Count > 0) {
                        foreach(var excludedOption in nonAlternative){
                            CspTerm[] orTerm = new CspTerm[excludedOption.Count];
                            int i = 0;
                            foreach (var opt in excludedOption)
                            {
                                CspTerm target = optionToTerm[(BinaryOption)opt];
                                orTerm[i] = target;
                                i++;
                            }
                            S.AddConstraints(S.Implies(cT, S.Not(S.Or(orTerm))));
                        }
                    }
                }
                //Handle implies
                if (current.Implied_Options.Count > 0)
                {
                    foreach (List<ConfigurationOption> impliedOr in current.Implied_Options)
                    {
                        CspTerm[] orTerms = new CspTerm[impliedOr.Count];
                        //Possible error: if a binary option impies a numeric option
                        for (int i = 0; i < impliedOr.Count; i++)
                            orTerms[i] = optionToTerm[(BinaryOption)impliedOr.ElementAt(i)];
                        S.AddConstraints(S.Implies(optionToTerm[current], S.Or(orTerms)));
                    }
                }
            }

            //Handle global cross-tree constraints involving multiple options at a time
            // the constraints should be in conjunctive normal form
            foreach (string constraint in vm.BooleanConstraints)
            {
                bool and = false;
                string[] terms;
                if (constraint.Contains("&"))
                {
                    and = true;
                    terms = constraint.Split('&');
                }
                else
                    terms = constraint.Split('|');

                CspTerm[] cspTerms = new CspTerm[terms.Count()];
                int i = 0;
                foreach (string t in terms)
                {
                    string optName = t.Trim();
                    if (optName.StartsWith("-") || optName.StartsWith("!"))
                    {
                        optName = optName.Substring(1);
                        BinaryOption binOpt = vm.getBinaryOption(optName);
                        CspTerm cspElem = optionToTerm[binOpt];
                        CspTerm notCspElem = S.Not(cspElem);
                        cspTerms[i] = notCspElem;
                    }
                    else
                    {
                        BinaryOption binOpt = vm.getBinaryOption(optName);
                        CspTerm cspElem = optionToTerm[binOpt];
                        cspTerms[i] = cspElem;
                    }
                    i++;
                }
                if (and)
                    S.AddConstraints(S.And(cspTerms));
                else
                    S.AddConstraints(S.Or(cspTerms));
            }
            csystem = S;
            optionToTerm_global = optionToTerm;
            vm_global = vm;
            termToOption_global = termToOption;
            variables_global = variables;
            return S;
        }
Exemplo n.º 11
0
        /// <summary>
        /// Produce a reduced version of the variability model, containing only binary options
        /// and at least the considered options. Is a considered option, all parent option will be inlcuded.
        /// Constraints between options(alternative groups, implication etc), will be included if enough options
        /// are present to (e.g. both options for implications or at least 2 options in alternative groups).
        /// </summary>
        /// <param name="consideredOptions">The options that will be in the reduced model.</param>
        /// <returns>A reduced version of the model containing the considered options.</returns>
        public VariabilityModel reduce(List <BinaryOption> consideredOptions)
        {
            VariabilityModel reduced = new VariabilityModel(this.name);

            foreach (BinaryOption binOpt in consideredOptions)
            {
                BinaryOption child = new BinaryOption(reduced, binOpt.Name);
                replicateOption(binOpt, child);
                reduced.addConfigurationOption(child);
                BinaryOption parent       = (BinaryOption)binOpt.Parent;
                bool         parentExists = false;
                while ((parent != null && parent != this.root) && !parentExists)
                {
                    BinaryOption newParent = reduced.getBinaryOption(parent.Name);
                    if (newParent == null)
                    {
                        newParent = new BinaryOption(reduced, parent.Name);
                        replicateOption(parent, newParent);
                        reduced.addConfigurationOption(newParent);
                    }
                    else
                    {
                        parentExists = true;
                    }
                    child.Parent = newParent;
                    child        = newParent;
                    parent       = (BinaryOption)parent.Parent;
                }

                if (child.Parent == null)
                {
                    child.Parent = reduced.root;
                }
            }

            foreach (BinaryOption opt in consideredOptions)
            {
                List <List <ConfigurationOption> > impliedOptionsRepl = new List <List <ConfigurationOption> >();
                foreach (List <ConfigurationOption> implied in opt.Implied_Options)
                {
                    List <ConfigurationOption> implRepl = new List <ConfigurationOption>();

                    foreach (ConfigurationOption impliedOption in implied)
                    {
                        if (reduced.getOption(impliedOption.Name) != null)
                        {
                            implRepl.Add(reduced.getOption(impliedOption.Name));
                        }
                    }
                    impliedOptionsRepl.Add(implRepl);
                }

                reduced.getBinaryOption(opt.Name).Implied_Options = impliedOptionsRepl;

                List <List <ConfigurationOption> > excludedOptionsRepl = new List <List <ConfigurationOption> >();
                foreach (List <ConfigurationOption> excluded in opt.Excluded_Options)
                {
                    List <ConfigurationOption> exclRepl = new List <ConfigurationOption>();

                    foreach (ConfigurationOption excludedOption in excluded)
                    {
                        if (reduced.getOption(excludedOption.Name) != null)
                        {
                            exclRepl.Add(reduced.getOption(excludedOption.Name));
                        }
                    }
                    excludedOptionsRepl.Add(exclRepl);
                }

                reduced.getBinaryOption(opt.Name).Excluded_Options = excludedOptionsRepl;
            }

            return(reduced);
        }
Exemplo n.º 12
0
        /// <summary>
        /// This method returns a list of all configurations stored in a given file. All options of the configurations have to be defined in the variability model.
        /// </summary>
        /// <param name="dat">Object representing the configuration file.</param>
        /// <param name="model">Variability model of the configurations.</param>
        /// <returns>Returns a list of configurations that were defined in the XML document. Can be an empty list.</returns>
        public static List <Configuration> readConfigurations(XmlDocument dat, VariabilityModel model)
        {
            XmlElement currentElemt = dat.DocumentElement;

            HashSet <Configuration> configurations = new HashSet <Configuration>();

            int numberOfConfigs = currentElemt.ChildNodes.Count;
            int configsWithTooLargeDeviation = 0;

            foreach (XmlNode node in currentElemt.ChildNodes)
            {
                bool readMultipleMeasurements = false;
                if (node.Attributes.Count > 0 && node.Attributes[0].Value.ToLower() == "true")
                {
                    readMultipleMeasurements = true;
                }
                Dictionary <NFProperty, double> propertiesForConfig = new Dictionary <NFProperty, double>();;
                bool   alternativeFormat = false;
                string binaryString      = "";
                string numericString     = "";
                string configID          = "";
                Dictionary <NFProperty, double> measuredProperty = new Dictionary <NFProperty, double>();
                Configuration c            = null;
                bool          hasSetConfig = false;
                foreach (XmlNode childNode in node.ChildNodes)
                {
                    if (c == null && hasSetConfig)
                    {
                        continue;
                    }
                    switch (childNode.Attributes[0].Value)
                    {
                    // TODO we use this to support result files having the old structure
                    case "Configuration":
                        binaryString = childNode.InnerText;
                        break;

                    case "Variable Features":
                        numericString = childNode.InnerText;
                        break;

                    case "BinaryOptions":
                        binaryString = childNode.InnerText;
                        break;

                    case "NumericOptions":
                        numericString = childNode.InnerText;
                        break;

                    case "ConfigID":
                        if (readMultipleMeasurements)
                        {
                            configID = childNode.InnerText.Replace("_", "%;%");
                        }
                        else
                        {
                            configID = childNode.InnerText;
                        }
                        if (configID.Contains("%;%seek") && configID.Contains("%;%seek0") == false)
                        {
                            hasSetConfig = true;
                            c            = null;
                            break;
                        }
                        alternativeFormat = true;
                        c            = Configuration.createFromHashString(configID, GlobalState.varModel);
                        hasSetConfig = true;
                        break;

                    case "CompilerOptions":
                        //todo
                        break;

                    case "ConfigFileOptions":
                        //todo
                        break;

                    case "ParameterOptions":
                        //todo
                        break;

                    case "ProgramName":
                        //todo
                        break;

                    case "StartupBegin":
                        //todo
                        break;

                    case "StartupEnd":
                        //todo
                        break;

                    default:
                        NFProperty property      = GlobalState.getOrCreateProperty(childNode.Attributes[0].Value);
                        double     measuredValue = 0;
                        //-1 means that measurement failed... 3rd values strongly devigates in C.'s measurements, hence we use it only in case we have no other measurements
                        if (readMultipleMeasurements)
                        {
                            if (property.Name != "run-real")
                            {
                                continue;
                            }
                            String[] m    = childNode.InnerText.ToString().Split(',');
                            double   val1 = 0;
                            if (!Double.TryParse(m[0], out val1))
                            {
                                break;
                            }
                            if (m.Length > 1)
                            {
                                List <double> values = new List <double>();
                                double        avg    = 0;
                                foreach (var i in m)
                                {
                                    double d = Convert.ToDouble(i);
                                    if (d != -1)
                                    {
                                        values.Add(d);
                                        avg += d;
                                    }
                                }
                                if (values.Count == 0)
                                {
                                    configsWithTooLargeDeviation++;
                                    c = null;
                                    break;
                                }
                                avg = avg / values.Count;

                                /* foreach (var d in values)
                                 * {
                                 *   if ((d / avg) * 100 > 10)
                                 *   {
                                 *       configsWithTooLargeDeviation++;
                                 *       c = null;
                                 *       break;
                                 *   }
                                 * }*/
                                measuredValue = avg;

                                /*double val2 = Convert.ToDouble(m[1]);
                                 * if (val1 == -1)
                                 *  measuredValue = val2;
                                 * else if (val1 == -1 && val2 == -1)
                                 *  measuredValue = Convert.ToDouble(m[2]);
                                 * else if (val2 == -1)
                                 *  measuredValue = val1;
                                 * else
                                 *  measuredValue = (val1 + val2) / 2;*/
                            }
                            else
                            {
                                measuredValue = val1;
                            }
                        }
                        else
                        {
                            measuredValue = Convert.ToDouble(childNode.InnerText.ToString().Replace(',', '.'));
                        }
                        if (alternativeFormat && c != null)
                        {
                            c.setMeasuredValue(property, measuredValue);
                        }
                        else
                        {
                            measuredProperty.Add(property, measuredValue);
                        }
                        break;
                    }
                }

                if (alternativeFormat && c != null)
                {
                    if (configurations.Contains(c))
                    {
                        GlobalState.logError.log("Mutiple definition of one configuration in the configurations file:  " + c.ToString());
                    }
                    else
                    {
                        // if (GlobalState.currentNFP != null && c.nfpValues.Keys.Contains(GlobalState.currentNFP) && c.nfpValues[GlobalState.currentNFP] != -1)
                        configurations.Add(c);
                    }
                    cont : { }
                    continue;
                }

                Dictionary <BinaryOption, BinaryOption.BinaryValue> binaryOptions = new Dictionary <BinaryOption, BinaryOption.BinaryValue>();

                string[] binaryOptionNames = binaryString.Split(',');
                foreach (string binaryOptionName in binaryOptionNames)
                {
                    string currOption = binaryOptionName.Trim();
                    if (currOption.Length > 0)
                    {
                        BinaryOption bOpt = null;

                        bOpt = model.getBinaryOption(currOption);

                        if (bOpt == null)
                        {
                            GlobalState.logError.log("No Binary option found with name: " + currOption);
                        }
                        binaryOptions.Add(bOpt, BinaryOption.BinaryValue.Selected);
                    }
                }

                // Add "root" binary option to the configuration
                if (!binaryOptions.ContainsKey(model.Root))
                {
                    binaryOptions.Add(model.Root, BinaryOption.BinaryValue.Selected);
                }


                Dictionary <NumericOption, double> numericOptions = new Dictionary <NumericOption, double>();
                if (!string.IsNullOrEmpty(numericString))
                {
                    string[] numOptionArray = numericString.Trim().Split(',');
                    foreach (string numOption in numOptionArray)
                    {
                        string[] numOptionsKeyValue;
                        if (numOption.Contains(";"))
                        {
                            numOptionsKeyValue = numOption.Split(';');
                        }
                        else
                        {
                            numOptionsKeyValue = numOption.Split(' ');// added for rc-lookahead 40
                        }
                        numOptionsKeyValue[0] = numOptionsKeyValue[0].Trim();
                        if (numOptionsKeyValue[0].Length == 0)
                        {
                            continue;
                        }
                        NumericOption varFeat = model.getNumericOption(numOptionsKeyValue[0]);
                        if (varFeat == null)
                        {
                            GlobalState.logError.log("No numeric option found with name: " + numOptionsKeyValue[0]);
                        }
                        double varFeatValue = Convert.ToDouble(numOptionsKeyValue[1]);

                        numericOptions.Add(varFeat, varFeatValue);
                    }
                }

                Configuration config = new Configuration(binaryOptions, numericOptions, measuredProperty);

                //if(configurations.Contains(config))
                //{
                //    GlobalState.logError.log("Mutiple definition of one configuration in the configurations file:  " + config.ToString());
                //}else
                //{
                configurations.Add(config);
                //}
            }
            GlobalState.logInfo.log("Configs with too large deviation: " + configsWithTooLargeDeviation);
            return(configurations.ToList());
        }
        private bool tokenIsAFeatureOrNumber(string token, VariabilityModel varModel)
        {
            token = token.Trim();

            double value = 0.0;

            if (Double.TryParse(token, out value))
            {
                return true;
            }

            if (varModel != null)
            {
                NumericOption numOption = varModel.getNumericOption(token);
                if (numOption != null)
                {
                    if (!participatingNumOptions.Contains(numOption))
                        this.participatingNumOptions.Add(numOption);
                    numberOfParticipatingFeatures++;
                    return true;
                }

                BinaryOption binOption = varModel.getBinaryOption(token);
                if (binOption != null)
                {
                    if (!participatingBoolOptions.Contains(binOption))
                        this.participatingBoolOptions.Add(binOption);
                    numberOfParticipatingFeatures++;
                    return true;
                }
            }
            else
            {
                if(token.Equals(this.numOption.Name))
                    return true;
            }
            return false;
        }
        private static double getValueOfToken(Configuration config, string token, VariabilityModel fm)
        {
            token = token.Trim();

            double value = 0.0;

            if (Double.TryParse(token, out value))
            {
                return value;
            }

            NumericOption numOpt = fm.getNumericOption(token);
            if (numOpt != null)
            {
                return config.NumericOptions[numOpt];
            }
            BinaryOption binOpt = fm.getBinaryOption(token);
            if (binOpt != null)
            {
                if(token.Equals("base"))
                    return 1.0;

                if (config.BinaryOptions.Keys.Contains(binOpt) && config.BinaryOptions[binOpt] == BinaryOption.BinaryValue.Selected)
                    return 1.0;
                else
                {
                    foreach (BinaryOption option in config.BinaryOptions.Keys)
                    {
                        if (option.Name == binOpt.Name )
                        {
                            return 1.0;
                        }
                    }
                }

            }
            return 0.0;
        }