private bool print_csv(List <Configuration> configurations, List <NFProperty> nfpPropertiesToPrint) { StringBuilder csvContent = new StringBuilder(); for (int i = 0; i < this.order.Count; i++) { ConfigurationOption c = this.order[i]; if (i != 0) { csvContent.Append(CSV_ELEMENT_DELIMITER); } csvContent.Append(c.Name); } if (!GlobalState.currentNFP.Equals(NFProperty.DefaultProperty)) { foreach (NFProperty nfpProperty in nfpPropertiesToPrint) { csvContent.Append(CSV_ELEMENT_DELIMITER); csvContent.Append(nfpProperty.Name); } } csvContent.Append(CSV_ROW_DELIMITER); foreach (Configuration c in configurations) { csvContent.Append(c.toCsv(this.order, nfpPropertiesToPrint)); } File.WriteAllText(file, csvContent.ToString()); return(true); }
/// <summary> /// Adds a configuration option to the variability model. /// The method checks whether an option with the same name already exists and whether invalid characters are within the name /// </summary> /// <param name="option">The option to be added to the variability model.</param> /// <returns>True if the option was added to the model, false otherwise</returns> public bool addConfigurationOption(ConfigurationOption option) { if (option.Name.Contains('-') || option.Name.Contains('+')) { return(false); } // the vitrual root configuration option does not have to be added to the variability model. if (option.Name.Equals("root")) { return(true); } foreach (var opt in binaryOptions) { if (opt.Name.Equals(option.Name)) { return(false); } } foreach (var opt in numericOptions) { if (opt.Name.Equals(option.Name)) { return(false); } } if (this.hasOption(option.ParentName)) { option.Parent = this.getOption(option.ParentName); } //Every option must have a parent if (option.Parent == null) { option.Parent = this.root; } if (option is BinaryOption) { this.binaryOptions.Add((BinaryOption)option); } else { this.numericOptions.Add((NumericOption)option); } // create Index optionToIndex.Add(optionToIndex.Count, option); indexToOption.Add(option, indexToOption.Count); return(true); }
/// <summary> /// Removes a configuration from the variability model. /// </summary> /// <param name="toDelete"></param> public void deleteOption(ConfigurationOption toDelete) { // Removing all children List <ConfigurationOption> children = new List <ConfigurationOption>(); foreach (ConfigurationOption opt in this.BinaryOptions) { if (opt.Parent != null && opt.Parent.Equals(toDelete)) { children.Add(opt); } } foreach (ConfigurationOption child in children) { deleteOption(child); } // Removing option from other options foreach (ConfigurationOption opt in getOptions()) { for (int i = opt.Excluded_Options.Count - 1; i >= 0; i--) { if (opt.Excluded_Options[i].Contains(toDelete)) { opt.Excluded_Options.RemoveAt(i); } } for (int i = opt.Implied_Options.Count - 1; i >= 0; i--) { if (opt.Implied_Options[i].Contains(toDelete)) { opt.Implied_Options.RemoveAt(i); } } } // Removing option from constraints binaryConstraints.RemoveAll(x => x.Contains(toDelete.ToString())); nonBooleanConstraints.RemoveAll(x => x.ToString().Contains(toDelete.ToString())); if (toDelete is BinaryOption) { binaryOptions.Remove((BinaryOption)toDelete); } else if (toDelete is NumericOption) { numericOptions.Remove((NumericOption)toDelete); } else { throw new Exception("An illegal option was found while deleting."); } }
/// <summary> /// Searches for a numeric option with the given name. /// </summary> /// <param name="name">Name of the option</param> /// <returns>Either the numeric option with the given name or NULL if not found</returns> public NumericOption getNumericOption(String name) { name = ConfigurationOption.removeInvalidCharsFromName(name); foreach (var numO in numericOptions) { if (numO.Name.Equals(name)) { return(numO); } } return(null); }
/// <summary> /// Searches for a binary option with the given name. /// </summary> /// <param name="name">Name of the option</param> /// <returns>Either the binary option with the given name or NULL if not found</returns> public BinaryOption getBinaryOption(String name) { name = ConfigurationOption.removeInvalidCharsFromName(name); foreach (var binO in binaryOptions) { if (binO.Name.Equals(name)) { return(binO); } } return(null); }
/// <summary> /// Returns whether the influence function coints a specific configuration option. /// </summary> /// <param name="option"></param> /// <returns></returns> public bool containsOption(ConfigurationOption option) { if (this.participatingBoolOptions.Contains(option)) { return(true); } if (this.participatingNumOptions.Contains(option)) { return(true); } return(false); }
/// <summary> /// This method retuns the configuration with the given name. /// </summary> /// <param name="name">Name of the option under consideration.</param> /// <returns>The option with the given name or NULL of no option with the name is defined.</returns> public ConfigurationOption getOption(String name) { name = ConfigurationOption.removeInvalidCharsFromName(name); ConfigurationOption opt = getNumericOption(name); if (opt != null) { return(opt); } opt = getBinaryOption(name); return(opt); }
/// <summary> /// Returns the csv-representation of the configuration. /// </summary> /// <returns>The csv-representation of the configuration.</returns> /// <param name="order">The order of the configuration options.</param> public string toCsv(List <ConfigurationOption> order, List <NFProperty> nfpProperties) { StringBuilder result = new StringBuilder(); for (int i = 0; i < order.Count; i++) { ConfigurationOption c = order[i]; if (i != 0) { result.Append(ConfigurationPrinter.CSV_ELEMENT_DELIMITER); } if (c.GetType().Equals(typeof(BinaryOption))) { if (this.BinaryOptions.ContainsKey((BinaryOption)c) && this.BinaryOptions[(BinaryOption)c] == BinaryOption.BinaryValue.Selected) { result.Append(1); } else { result.Append(0); } } else { NumericOption n = (NumericOption)c; if (this.numericOptions.ContainsKey(n)) { result.Append(this.NumericOptions[n]); } } } if (nfpProperties != null && nfpProperties.Count != 0 && !GlobalState.currentNFP.Equals(NFProperty.DefaultProperty)) { result.Append(ConfigurationPrinter.CSV_ELEMENT_DELIMITER); foreach (NFProperty nfp in this.nfpValues.Keys) { if (nfpProperties.Contains(nfp)) { result.Append(this.nfpValues[nfp]); result.Append(ConfigurationPrinter.CSV_ELEMENT_DELIMITER); } } result.Remove(result.Length - ConfigurationPrinter.CSV_ELEMENT_DELIMITER.Length, ConfigurationPrinter.CSV_ELEMENT_DELIMITER.Length); } result.Append(ConfigurationPrinter.CSV_ROW_DELIMITER); return(result.ToString()); }
/// <summary> /// Returns the csv-representation of the configuration. /// </summary> /// <returns>The csv-representation of the configuration.</returns> /// <param name="order">The order of the configuration options.</param> public string toCsv(List <ConfigurationOption> order) { StringBuilder result = new StringBuilder(); for (int i = 0; i < order.Count; i++) { ConfigurationOption c = order[i]; if (i != 0) { result.Append(ConfigurationPrinter.CSV_ELEMENT_DELIMITER); } if (c.GetType().Equals(typeof(BinaryOption))) { if (this.BinaryOptions.ContainsKey((BinaryOption)c)) { result.Append(1); } else { result.Append(0); } } else { NumericOption n = (NumericOption)c; if (this.numericOptions.ContainsKey(n)) { result.Append(this.NumericOptions[n]); } } } if (!GlobalState.currentNFP.Equals(NFProperty.DefaultProperty)) { result.Append(ConfigurationPrinter.CSV_ELEMENT_DELIMITER); if (this.nfpValues.ContainsKey(GlobalState.currentNFP)) { result.Append(this.nfpValues[GlobalState.currentNFP]); } else { result.Append(Double.NaN); } } result.Append(ConfigurationPrinter.CSV_ROW_DELIMITER); return(result.ToString()); }
/// <summary> /// This method reads all configurations specified in the .csv file. In this mehtod, we assume that the file has a header. /// </summary> /// <param name="file"></param> /// <param name="model">The variabiliy model for the configurations.</param> /// <returns>A list of all configurations </returns> public static List <Configuration> readConfigurations_Header_CSV(String file, VariabilityModel model) { List <Configuration> configurations = new List <Configuration>(); StreamReader sr = new StreamReader(file); String[] optionOrder = new String[model.getOptions().Count]; String[] nfpOrder = null; bool isHeader = true; while (!sr.EndOfStream) { String[] tokens = sr.ReadLine().Split(';'); if (isHeader) { nfpOrder = new String[tokens.Length - optionOrder.Length]; for (int i = 0; i < tokens.Length; i++) { String token = tokens[i]; if (i < optionOrder.Length) { optionOrder[i] = token; } else { nfpOrder[i - optionOrder.Length] = token; if (!GlobalState.nfProperties.ContainsKey(token)) { GlobalState.nfProperties.Add(token, new NFProperty(token)); } } } isHeader = false; } else { Dictionary <BinaryOption, BinaryOption.BinaryValue> binOptions = new Dictionary <BinaryOption, BinaryOption.BinaryValue>(); Dictionary <NumericOption, double> numOptions = new Dictionary <NumericOption, double>(); Dictionary <NFProperty, double> properties = new Dictionary <NFProperty, double>(); for (int i = 0; i < tokens.Length; i++) { String token = tokens[i]; if (i < optionOrder.Length) { ConfigurationOption option = model.getOption(optionOrder[i]); if (option.GetType() == typeof(BinaryOption)) { if (token.Equals("true") || token.Equals("1")) { binOptions.Add((BinaryOption)option, BinaryOption.BinaryValue.Selected); } } else { double value = Convert.ToDouble(token); numOptions.Add((NumericOption)option, value); } } else { NFProperty nfp = GlobalState.nfProperties[nfpOrder[i - optionOrder.Length]]; double value = Convert.ToDouble(token); properties.Add(nfp, value); double currentMaxMeasuredValue; if (GlobalState.allMeasurements.maxMeasuredValue.TryGetValue(nfp, out currentMaxMeasuredValue)) { if (Math.Abs(value) > Math.Abs(currentMaxMeasuredValue)) { GlobalState.allMeasurements.maxMeasuredValue[nfp] = value; } } else { GlobalState.allMeasurements.maxMeasuredValue.Add(nfp, value); } } } Configuration config = new Configuration(binOptions, numOptions, properties); configurations.Add(config); } } sr.Close(); return(configurations); }