public void RemovePackage(PackageId packageId) { var packageInfo = ProductPackages.FirstOrDefault(x => x.Configuration.Id.IsSamePackage(packageId)); if (packageInfo != null) { ProductPackages.Remove(packageInfo); } var allPackages = Packages.GetNodes(new PackageId[] {}); var builder = new PackageGraphBuilder(); foreach (var packageNode in allPackages) { if (packageNode.Package.IsSamePackage(packageId)) { continue; } builder.Append(packageNode.Package, packageNode.Dependencies.Where(x => !x.Package.IsSamePackage(packageId)).Select(p => p.Package)); } Packages = builder.Build(); }
private void TryParseConfiguration() { if (!File.Exists(_configPath)) { return; } using (var stream = File.OpenText(_configPath)) { var reader = new JsonTextReader(stream); _jsonObject = JObject.Load(reader); JToken token; if (_jsonObject.TryGetValue(_packagesTag, out token)) { var builder = new PackageGraphBuilder(); foreach (var package in token.Children()) { var packageProp = (JProperty)package; var packageObject = (JObject)packageProp.Value; var packageId = new PackageId(packageProp.Name); var info = new ProductPackageInfo(); if (packageObject.TryGetValue(_packageTag, out token)) { var packageConfigObject = (JObject)token; info.Configuration = new PackageConfiguration(packageId, packageConfigObject); if (packageObject.TryGetValue(_filesTag, out token) && token.HasValues) { var array = (JArray)token; info.Files.AddRange(array.Select(x => x.ToString())); } List <PackageId> dependencies = new List <PackageId>(); if (packageObject.TryGetValue(_dependenciesTag, out token) && token.HasValues) { var array = (JArray)token; dependencies.AddRange(array.Select(x => new PackageId(x.ToString()))); } builder.Append(packageId, dependencies); ProductPackages.Add(info); } } Packages = builder.Build(); IsProductConfigured = true; } } }
public IEnumerable <string> GetInstalledFiles(PackageId packageId, bool nonSharedFilesOnly) { var packageInfo = ProductPackages.FirstOrDefault(x => x.Configuration.Id.IsSamePackage(packageId)); if (packageInfo == null) { return new string[] {} } ; if (!nonSharedFilesOnly) { return(packageInfo.Files); } return(packageInfo.Files.Where( file => !ProductPackages.Any(x => x != packageInfo && x.Files.Contains(file))).ToList()); }
public void Reconfigure(IEnumerable <IPackageConfiguration> packages, IPackageGraph packageGraph, IDictionary <PackageId, IEnumerable <string> > files) { Packages = packageGraph; ProductPackages.Clear(); foreach (var package in packages) { var info = new ProductPackageInfo { Configuration = package }; var packageFiles = files.FirstOrDefault(f => f.Key.IsSamePackage(package.Id)).Value; if (packageFiles != null) { info.Files.AddRange(packageFiles); } ProductPackages.Add(info); } }
/// <summary> /// Returns package's installed files /// </summary> /// <param name="packageId">Package ID</param> /// <param name="nonSharedFilesOnly">When true, instructs function to return only the files which are unique for the package</param> /// <returns>Files list</returns> public IReadOnlyCollection <IPackageFile> GetInstalledFiles(PackageId packageId, bool nonSharedFilesOnly) { var packageInfo = ProductPackages.FirstOrDefault(x => x.Configuration.Id.IsSamePackage(packageId)); if (packageInfo == null) { return(new List <PackageFileInfo>(new PackageFileInfo[] {})); } var files = !nonSharedFilesOnly ? packageInfo.Files : packageInfo.Files.Where(file => !ProductPackages.Any(x => x != packageInfo && x.Files.Any(file.IsSameFile))) .ToList(); if (string.IsNullOrWhiteSpace(_productPath)) { return(files); } return(files.Select( x => Path.IsPathRooted(x.FileName) ? x : new PackageFileInfo(Path.Combine(_productPath, x.FileName))) .ToList()); }
public void Test_CK_einProduktZweiMal_InsPaket() { var sut = new Solution(); var products = new Products { { "Apfel", 10 } }; var success = false; var productPackages = new ProductPackages { new ProductPackage(){ Price = 15, Products = { "Apfel", "Apfel" }}, }; sut.Setup(products, productPackages); var price = 0; sut.SendPrice += p => { price = p; Debug.WriteLine("{0},", price); }; sut.Order("Apfel"); price.Should().Be(10); sut.Order("Apfel"); price.Should().Be(15); }
public void Test_switch_to_another_package() { var sut = new Solution(); var products = new Products {{"A", 10}, {"B", 20}, {"C", 30}}; var productPackages = new ProductPackages { new ProductPackage {Price = 29, Products = new List<string> {"A", "B"}}, new ProductPackage {Price = 48, Products = new List<string> {"B", "C"}}, }; sut.Setup(products, productPackages); var price = 0; sut.SendPrice += p => price = p; sut.Order("A"); sut.Order("B"); price.Should().Be(29, "there is a package for A and B with price 29."); sut.Order("C"); price.Should().Be(10 + 48, "it is cheaper to take package B & C"); }
public void Test_quantity_discount() { var sut = new Solution(); var products = new Products {{"A", 10}}; var productPackages = new ProductPackages { new ProductPackage {Price = 19, Products = new List<string> {"A", "A"}}, new ProductPackage {Price = 27, Products = new List<string> {"A", "A", "A"}}, }; sut.Setup(products, productPackages); var price = 0; sut.SendPrice += p => price = p; sut.Order("A"); price.Should().Be(10); sut.Order("A"); price.Should().Be(19); sut.Order("A"); price.Should().Be(27); sut.Order("A"); price.Should().Be(27 + 10); sut.Order("A"); price.Should().Be(27 + 19); sut.Order("A"); price.Should().Be(2 * 27); }
public void Test_performance() { var sut = new Solution(); var products = new Products {{"A", 10}, {"B", 20}, {"C", 30}}; var productPackages = new ProductPackages { new ProductPackage {Price = 29, Products = new List<string> {"A", "B"}}, new ProductPackage {Price = 30, Products = new List<string> {"A", "A", "A", "A"}}, new ProductPackage {Price = 48, Products = new List<string> {"B", "C"}}, new ProductPackage {Price = 57, Products = new List<string> {"A", "B", "C"}} }; sut.Setup(products, productPackages); sut.SendPrice += price => Console.WriteLine("{0,3}. Order, Price: {1,5}", count++, price); //sut.OrderMultiple(50, "A"); //sut.OrderMultiple(5, "B"); //sut.OrderMultiple(3, "C"); sut.OrderMultiple(40, "A"); sut.OrderMultiple(40, "B"); sut.OrderMultiple(40, "C"); }
public void Test_order_where_it_is_not_better_to_take_highest_discount() { var sut = new Solution(); var products = new Products { { "A", 10 } }; var productPackages = new ProductPackages { new ProductPackage {Price = 47, Products = new List<string> {"A", "A", "A", "A", "A"}}, // discount 3/5 pp new ProductPackage {Price = 38, Products = new List<string> {"A", "A", "A", "A"}} // discount 1/2 pp }; var price = 0; sut.Setup(products, productPackages); sut.SendPrice += p => price = p; sut.OrderMultiple(8, "A"); price.Should().Be(2 * 38); }
public void Test_if_pitfall_package_is_ignored() { var sut = new Solution(); var products = new Products {{"A", 10}}; var productPackages = new ProductPackages { new ProductPackage {Price = 21, Products = new List<string> {"A", "A"}} }; sut.Setup(products, productPackages); var price = 0; sut.SendPrice += p => price = p; sut.Order("A"); price.Should().Be(10); sut.Order("A"); price.Should().Be(2 * 10, "package is a pitfall"); }
public void Test_Forum() { var sut = new Solution(); var products = new Products { { "VegMac", 4 }, { "BurgerMac", 1 }, { "WoodMac", 3 }, { "MacMac", 5 }, { "PommesPommes", 2 }, { "Pommes", 1 }, { "PommesGr", 1 }, { "Cola", 3 } }; var productPackages = new ProductPackages { new ProductPackage(){ Price = 1, Products = { "MacMac", "Cola" }}, new ProductPackage(){ Price = 10, Products = { "BurgerMac", "WoodMac", "Pommes", "Cola" }}, new ProductPackage(){ Price = 7, Products = { "VegMac", "Cola", "Pommes" }}, new ProductPackage(){ Price = 7, Products = { "VegMac", "Cola", "PommesGr" }}, new ProductPackage(){ Price = 2, Products = { "Cola", "PommesGr" }}, new ProductPackage(){ Price = 1, Products = { "VegMac", "Cola", "Pommes", "Pommes" }} }; sut.Setup(products, productPackages); var price = 0; sut.SendPrice += p => { price = p; Debug.WriteLine("{0},",price); }; sut.Order("VegMac"); price.Should().Be(4); sut.Order("VegMac"); price.Should().Be(8); sut.Order("VegMac"); price.Should().Be(12); sut.Order("Pommes"); price.Should().Be(13); sut.Order("Cola"); price.Should().Be(15); // P3 + 2xVegMac sut.Order("Pommes"); price.Should().Be(9); // P3 + P6 + 1xVegMac sut.Order("PommesGr"); price.Should().Be(10); // P4 + P6 + 1xPommes sut.Order("MacMac"); price.Should().Be(15); // P4 + P6 + 1xPommes + 1xMacMac sut.Order("WoodMac"); price.Should().Be(18); // P4 + P6 + 1xPommes + 1xMacMac + 1xWoodMac sut.Order("BurgerMac"); price.Should().Be(19); // P4 + P6 + 1xPommes + 1xMacMac + 1xWoodMac + 1xBurgeMac sut.Order("MacMac"); price.Should().Be(24); // P4 + P6 + 1xPommes + 2xMacMac + 1xWoodMac + 1xBurgeMac sut.Order("PommesPommes"); price.Should().Be(26); // P4 + P6 + 1xPommes + 2xMacMac + 1xWoodMac + 1xBurgeMac + PommesPommes sut.Order("Pommes"); price.Should().Be(27); // P4 + P6 + 2xPommes + 2xMacMac + 1xWoodMac + 1xBurgeMac + PommesPommes sut.Order("Cola"); price.Should().Be(23); // P1 + P4 + P6 + 2xPommes + 1xMacMac + 1xWoodMac + 1xBurgeMac + PommesPommes }
public IPackageConfiguration GetPackageConfiguration(PackageId packageId) { return(ProductPackages.FirstOrDefault(x => x.Configuration.Id.IsSamePackage(packageId))?.Configuration); }
public void Setup(Products products, ProductPackages productpackages) { this.allproducts = products; this.allproductpackages = productpackages; productsAndPackagesCount = products.Count + productpackages.Count; var product2PackageArray = new double[productsAndPackagesCount, products.Count]; var pricesArray = new double[productsAndPackagesCount]; var packagedOrderArray = new double[productsAndPackagesCount]; // Produktmatrix und Preisvectoraufbauen var col = 0; var row = 0; var grundpreis = 0.0; var productSet = new HashSet<Tuple<double, double, object>>(); // Schritt 1 - Preis Index der Pakete ermitteln foreach (var package in productpackages) { grundpreis = 0.0; foreach (var price in package.Products) { var preis = products[price]; grundpreis += preis; } var preisIndex = (package.Price / grundpreis); productSet.Add(new Tuple<double, double, object>(preisIndex, package.Price, package)); } // Schritt 2 - Preis Index der Produkte ermitteln foreach (var product in products) { grundpreis = product.Value; var preisIndex = (product.Value / grundpreis); productSet.Add(new Tuple<double, double, object>(preisIndex, product.Value, product)); } // Schritt 3 - das ProduktSet sortieren und in eine Matrix überführen var query = from p in productSet orderby p.Item1 ascending select p; row = 0; foreach (var p in query) { col = 0; foreach (var product in products) { if (p.Item3 is ProductPackage) { foreach (var item in ((ProductPackage)p.Item3).Products) { if (item == product.Key) { product2PackageArray[row, col]++; } } } else { if (product.Key == ((KeyValuePair<string, int>)p.Item3).Key) { product2PackageArray[row, col]++; } } col++; } pricesArray[row] = p.Item2; row++; } productMatrix = Matrix<double>.Build.DenseOfArray(product2PackageArray); packagedOrder = Vector<double>.Build.DenseOfArray(packagedOrderArray); prices = Vector<double>.Build.DenseOfArray(pricesArray); orderedProducts = Vector<double>.Build.Dense(products.Count); }