public void TestSimple_2()
        {
            var facts = new List<NullableFact>();
            facts.Add(new NullableFact() { ProductId = null, GeographyId = null, SalesComponentId = null, TimeId = 1, Value = 100 });

            var products = new Moq.Mock<IHierarchy>();
            var geographies = new Moq.Mock<IHierarchy>();
            var causals = new Moq.Mock<IHierarchy>();
            var periods = new Moq.Mock<IHierarchy>();

            Expression<Func<IHierarchy, IEnumerable<short>>> anyInt = p => p.GetParents(Moq.It.IsAny<short>());
            var result = new List<short> { 1 };
            products.Setup(anyInt).Returns(result);
            geographies.Setup(anyInt).Returns(result);
            causals.Setup(anyInt).Returns(result);
            periods.Setup(anyInt).Returns(result);

            Expression<Func<IHierarchy, bool>> allRelationships = p => p.RelationExists(Moq.It.IsAny<short>(), Moq.It.IsAny<short>());
            products.Setup(allRelationships).Returns(true);
            geographies.Setup(allRelationships).Returns(true);
            causals.Setup(allRelationships).Returns(true);
            periods.Setup(allRelationships).Returns(true);

            //, Hierarchy products, Hierarchy geographies, Hierarchy causals, Hierarchy periods

            var lookup = new FactLookup(facts, products.Object, geographies.Object, causals.Object, periods.Object);

            Assert.AreEqual(100, lookup.GetParent(0, 0, 0, 1));
            Assert.AreEqual(100, lookup.GetParent(1, 1, 1, 1));
            Assert.AreEqual(100, lookup.GetParent(2, 2, 2, 1));
        }
Example #2
0
        public List<ConcreteFact> AdjustSales(Request roiRequest)
        {
            var marginLookup = new FactLookup(roiRequest.Margins, products, geographies, causals, periods);
            var spendPeriods = roiRequest.Spend.Select(s => s.TimeId).ToList();

            if (!spendPeriods.Any())
                return new List<ConcreteFact>();

            // we can genericize this when more adjustments come online
            List<NullableFact> adjustment = null;

            var adjustmentLookup = new FactLookup(adjustment, products, geographies, causals, periods);

            var timeIsNull = spendPeriods.All(s => s == null);
            var dueTos = roiRequest.Sales.Where(d => timeIsNull || spendPeriods.Contains(d.TimeId));

            dueTos.AsParallel().ForAll(item =>
                {
                    var margin = marginLookup.GetParent(item, false);
                    var adjustmentFactor = adjustmentLookup.GetParent(item, false, 1.0f, false);

                    item.Value *= margin;
                    item.Value *= adjustmentFactor;
                });

            return roiRequest.Sales;
        }
        public void FactHierarchy_CrossMultipleFactsTest()
        {
            var p2g2 = 50;
            var p2g3 = 250;
            var p3g2 = 100;
            var p3g3 = 200;

            // geog 2
            facts.Add(new NullableFact() { ProductId = 3, GeographyId = 2, SalesComponentId = null, TimeId = 1, Value = p3g2 });
            facts.Add(new NullableFact() { ProductId = 2, GeographyId = 2, SalesComponentId = null, TimeId = 1, Value = p2g2 });

            // geog 3
            facts.Add(new NullableFact() { ProductId = 3, GeographyId = 3, SalesComponentId = null, TimeId = 1, Value = p3g3 });
            facts.Add(new NullableFact() { ProductId = 2, GeographyId = 3, SalesComponentId = null, TimeId = 1, Value = p2g3 });

            var lookup = new FactLookup(this.facts, this.prod, this.geog, this.causal, this.period);

            // tests from above but constrained to the 'geog 2' branch:
            // i.e. geog 2, or children 4 / 5

                // items in the '3' branch for products
            Assert.AreEqual(p3g2, lookup.GetParent(3, 2, 1, 1));
            Assert.AreEqual(p3g2, lookup.GetParent(7, 4, 7, 7));
            Assert.AreEqual(p3g2, lookup.GetParent(6, 5, 1, 1));
            Assert.AreEqual(p3g2, lookup.GetParent(7, 2, 2, 4));

                // in the '2' branch
            Assert.AreEqual(p2g2, lookup.GetParent(2, 5, 1, 1));
            Assert.AreEqual(p2g2, lookup.GetParent(2, 4, 7, 7));

            Assert.AreEqual(p2g2, lookup.GetParent(4, 2, 1, 1));
            Assert.AreEqual(p2g2, lookup.GetParent(4, 5, 7, 7));
            Assert.AreEqual(p2g2, lookup.GetParent(5, 4, 3, 3));
            Assert.AreEqual(p2g2, lookup.GetParent(5, 2, 3, 1));

            // repeated tests from above but now constrained to the 'geog 3' branch:
            // i.e. geog 3, or children 6 / 7

            // items in the '3' branch for products
            Assert.AreEqual(p3g3, lookup.GetParent(3, 3, 1, 1));
            Assert.AreEqual(p3g3, lookup.GetParent(7, 6, 7, 7));
            Assert.AreEqual(p3g3, lookup.GetParent(6, 7, 1, 1));
            Assert.AreEqual(p3g3, lookup.GetParent(7, 3, 2, 4));

            // in the '2' branch
            Assert.AreEqual(p2g3, lookup.GetParent(2, 6, 1, 1));
            Assert.AreEqual(p2g3, lookup.GetParent(2, 7, 7, 7));

            Assert.AreEqual(p2g3, lookup.GetParent(4, 3, 1, 1));
            Assert.AreEqual(p2g3, lookup.GetParent(4, 6, 7, 7));
            Assert.AreEqual(p2g3, lookup.GetParent(5, 7, 3, 3));
            Assert.AreEqual(p2g3, lookup.GetParent(5, 3, 3, 1));
        }
        public void Test_NullFacts()
        {
            var products = new Moq.Mock<IHierarchy>();
            var geographies = new Moq.Mock<IHierarchy>();
            var causals = new Moq.Mock<IHierarchy>();
            var periods = new Moq.Mock<IHierarchy>();

            Expression<Func<IHierarchy, IEnumerable<short>>> anyInt = p => p.GetParents(Moq.It.IsAny<short>());
            var result = new List<short> { 1 };
            products.Setup(anyInt).Returns(result);
            geographies.Setup(anyInt).Returns(result);
            causals.Setup(anyInt).Returns(result);
            periods.Setup(anyInt).Returns(result);

            var factLookup = new FactLookup(null, products.Object, geographies.Object, causals.Object, periods.Object);

            Assert.AreEqual(1, factLookup.GetParent(2, 2, 2, 2, false, 1));
        }
        public void TestSimple_Unmatched1()
        {
            var facts = new List<NullableFact>();
            facts.Add(new NullableFact() { ProductId = 1, GeographyId = 1, SalesComponentId = 1, TimeId = 1, Value = 100 });

            var products = new Moq.Mock<IHierarchy>();
            var geographies = new Moq.Mock<IHierarchy>();
            var causals = new Moq.Mock<IHierarchy>();
            var periods = new Moq.Mock<IHierarchy>();

            Expression<Func<IHierarchy, IEnumerable<short>>> anyInt = p => p.GetParents(2);
            var result = new List<short> { 1 };
            products.Setup(anyInt).Returns(result);
            geographies.Setup(anyInt).Returns(result);
            causals.Setup(anyInt).Returns(result);
            periods.Setup(anyInt).Returns(result);

            //, Hierarchy products, Hierarchy geographies, Hierarchy causals, Hierarchy periods
            var lookup = new FactLookup(facts, products.Object, geographies.Object, causals.Object, periods.Object);

            Assert.AreEqual(100, lookup.GetParent(3, 2, 2, 2));
        }
        public void FactHierarchy_TopParentTest_ExactTime()
        {
            facts.Add(new NullableFact() { ProductId = 1, GeographyId = 1, SalesComponentId = null, TimeId = 1, Value = 100 });
            var lookup = new FactLookup(this.facts, this.prod, this.geog, this.causal, this.period);

            Assert.AreEqual(100, lookup.GetParent(1, 1, 1, 1));
            Assert.AreEqual(100, lookup.GetParent(7, 7, 7, 1));
            Assert.AreEqual(100, lookup.GetParent(3, 3, 3, 1));
            Assert.AreEqual(100, lookup.GetParent(1, 4, 6, 1));
            Assert.AreEqual(100, lookup.GetParent(2, 3, 3, 1));
            Assert.AreEqual(100, lookup.GetParent(6, 2, 4, 1));
        }
        public void FactHierarchy_MultipleFactsTest_ExactTime()
        {
            facts.Add(new NullableFact() { ProductId = 3, GeographyId = 1, SalesComponentId = null, TimeId = 1, Value = 100 });
            facts.Add(new NullableFact() { ProductId = 2, GeographyId = 1, SalesComponentId = null, TimeId = 1, Value = 50 });
            var lookup = new FactLookup(this.facts, this.prod, this.geog, this.causal, this.period);

            // items in the '3' branch for products
            Assert.AreEqual(100, lookup.GetParent(3, 1, 1, 1));
            Assert.AreEqual(100, lookup.GetParent(7, 7, 7, 1));
            Assert.AreEqual(100, lookup.GetParent(6, 1, 1, 1));
            Assert.AreEqual(100, lookup.GetParent(7, 3, 2, 1));

            // in the '2' branch
            Assert.AreEqual(50, lookup.GetParent(2, 1, 1, 1));
            Assert.AreEqual(50, lookup.GetParent(2, 7, 7, 1));

            Assert.AreEqual(50, lookup.GetParent(4, 1, 1, 1));
            Assert.AreEqual(50, lookup.GetParent(4, 7, 7, 1));
            Assert.AreEqual(50, lookup.GetParent(5, 3, 3, 1));
            Assert.AreEqual(50, lookup.GetParent(5, 6, 3, 1));
        }
        public void Test_FactVsIds_Relation()
        {
            var products = new Moq.Mock<IHierarchy>();
            var geographies = new Moq.Mock<IHierarchy>();
            var causals = new Moq.Mock<IHierarchy>();
            var periods = new Moq.Mock<IHierarchy>();

            Expression<Func<IHierarchy, bool>> anyInt = p => p.RelationExists(Moq.It.IsAny<short>(), Moq.It.IsAny<short>());
            products.Setup(anyInt).Returns(true);
            geographies.Setup(anyInt).Returns(true);
            causals.Setup(anyInt).Returns(true);
            periods.Setup(anyInt).Returns(true);

            var factLookup = new FactLookup(null, products.Object, geographies.Object, causals.Object, periods.Object);

            Assert.AreEqual(1, factLookup.GetParent(2, 2, 2, 2, false, 1));
            Assert.AreEqual(1, factLookup.GetParent(new ConcreteFact() { ProductId = 2, GeographyId = 2, SalesComponentId = 2, TimeId = 2 }, false, 1));
        }