Exemplo n.º 1
0
        private static BottomUpOptimizer <StrTree, StrKind, StrWildcards> GetStrOptimizer()
        {
            var burs = new BottomUpOptimizer <StrTree, StrKind, StrWildcards>(new TreeEqualityComparer <StrKind>(), EqualityComparer <StrKind> .Default)
            {
                Leaves =
                {
                    { (ConcatStrTree c) => ConstStrTree.Empty, c => c.Children.Count == 0, 1 },
                },

                Rules =
                {
                    { () => new UnaryStrTree(StrKind.ToLower, ConstStrTree.Empty),              () => ConstStrTree.Empty, 1 },
                    { () => new UnaryStrTree(StrKind.ToUpper, ConstStrTree.Empty),              () => ConstStrTree.Empty, 1 },
                    { s => new UnaryStrTree(StrKind.ToUpper,  new UnaryStrTree(StrKind.ToUpper, s)),                      s => new UnaryStrTree(StrKind.ToUpper, s), 1},
                    { s => new UnaryStrTree(StrKind.ToUpper,  new UnaryStrTree(StrKind.ToLower, s)),                      s => new UnaryStrTree(StrKind.ToUpper, s), 1},
                    { s => new UnaryStrTree(StrKind.ToLower,  new UnaryStrTree(StrKind.ToUpper, s)),                      s => new UnaryStrTree(StrKind.ToLower, s), 1},
                    { s => new UnaryStrTree(StrKind.ToLower,  new UnaryStrTree(StrKind.ToLower, s)),                      s => new UnaryStrTree(StrKind.ToLower, s), 1},
                },

                Fallbacks =
                {
                    { s => new ConcatStrTree(s.Children.Cast <StrTree>().Where(c => c != ConstStrTree.Empty).ToArray()), s => s is ConcatStrTree && s.Children.Contains(ConstStrTree.Empty), 1 },
                },
            };

            return(burs);
        }
Exemplo n.º 2
0
        public void BottomUpOptimizer_ArgumentChecking()
        {
            AssertEx.ThrowsException <ArgumentNullException>(() => new BottomUpOptimizer <ITree <Bar>, Bar, BarWildcards>(sourceTreeComparer: null, EqualityComparer <Bar> .Default), ex => Assert.AreEqual("sourceTreeComparer", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => new BottomUpOptimizer <ITree <Bar>, Bar, BarWildcards>(EqualityComparer <ITree <Bar> > .Default, sourceNodeComparer: null), ex => Assert.AreEqual("sourceNodeComparer", ex.ParamName));

            var burs = new BottomUpOptimizer <ITree <Bar>, Bar, BarWildcards>();

            AssertEx.ThrowsException <ArgumentNullException>(() => burs.Optimize(tree: null), ex => Assert.AreEqual("tree", ex.ParamName));
        }
Exemplo n.º 3
0
        public void BottomUpOptimizer_Logic1()
        {
            var logger = new StringWriter();

            var burw = new BottomUpOptimizer <LogicExpr, LogicNodeType, LogicWildcardFactory>
            {
                // Leaf nodes
                Leaves =
                {
                    { (BoolConst b) => b, 1 },
                },

                Rules =
                {
                    // Tree patterns
                    { () => !BoolConst.True,    () => BoolConst.False, 1 },
                    { () => !BoolConst.False,   () => BoolConst.True,  1 },
                    { p => !!p,                 p => p,                2 },

                    { p => p & BoolConst.True,  p => p,                2 },
                    { p => p & BoolConst.False, p => BoolConst.False,  2 },
                    { p => BoolConst.True & p,  p => p,                2 },
                    { p => BoolConst.False & p, p => BoolConst.False,  2 },

                    /*
                     * With those rules commented out, we got a partial tree rewrite using Update
                     * method calls on the tree nodes.
                     *
                     * { p => p | BoolConst.True, p => BoolConst.True, 2 },
                     * { p => p | BoolConst.False, p => p, 2 },
                     * { p => BoolConst.True | p, p => BoolConst.True, 2 },
                     * { p => BoolConst.False | p, p => p, 2 },
                     *
                     */

                    { (p,                       q) => !(!p & !q),      (p, q) => p | q, 1},
                    { (p,                       q) => !(!p | !q),      (p, q) => p & q, 1},
                },

                Log = logger
            };

            // Internal tables
            var debugView = burw.DebugView;

            Assert.IsTrue(!string.IsNullOrEmpty(debugView));

            var e = !(!BoolConst.True & !BoolConst.False);

            var res = burw.Optimize(e);

            Assert.AreEqual("Or(True, False)", res.ToString());
            Assert.AreEqual(e.Eval(), res.Eval());
        }
Exemplo n.º 4
0
        public void BottomUpOptimizer_Logic2()
        {
            var logger = new StringWriter();

            var burw = new BottomUpOptimizer <LogicExpr, LogicNodeType, LogicWildcardFactory>
            {
                // Leaf nodes
                Leaves =
                {
                    { (BoolConst b) => b, 1 },
                },

                Rules =
                {
                    // Tree patterns
                    { () => !BoolConst.True,    () => BoolConst.False, 1 },
                    { () => !BoolConst.False,   () => BoolConst.True,  1 },
                    { p => !!p,                 p => p,                2 },

                    { p => p & BoolConst.True,  p => p,                2 },
                    { p => p & BoolConst.False, p => BoolConst.False,  2 },
                    { p => BoolConst.True & p,  p => p,                2 },
                    { p => BoolConst.False & p, p => BoolConst.False,  2 },

                    { p => p | BoolConst.True,  p => BoolConst.True,   2 },
                    { p => p | BoolConst.False, p => p,                2 },
                    { p => BoolConst.True | p,  p => BoolConst.True,   2 },
                    { p => BoolConst.False | p, p => p,                2 },

                    { (p,                       q) => !(!p & !q),      (p, q) => p | q, 1},
                    { (p,                       q) => !(!p | !q),      (p, q) => p & q, 1},
                },

                Log = logger
            };

            // Internal tables
            var debugView = burw.DebugView;

            Assert.IsTrue(!string.IsNullOrEmpty(debugView));

            var e = !(!BoolConst.True & !BoolConst.False);

            var res = burw.Optimize(e);

            Assert.AreEqual("True", res.ToString());
            Assert.AreEqual(e.Eval(), res.Eval());
        }
Exemplo n.º 5
0
        private static BottomUpOptimizer <NumTree, Num, NumWildcards> GetNumOptimizer()
        {
            var burs = new BottomUpOptimizer <NumTree, Num, NumWildcards>
            {
                Rules =
                {
                    { n => new BinaryNumTree(NumKind.Add,      n,                 ConstNumTree.Zero),     n => n,                 1 },
                    { n => new BinaryNumTree(NumKind.Add,      ConstNumTree.Zero, n),                     n => n,                 1 },
                    { n => new BinaryNumTree(NumKind.Multiply, n,                 ConstNumTree.One),      n => n,                 1 },
                    { n => new BinaryNumTree(NumKind.Multiply, ConstNumTree.One,  n),                     n => n,                 1 },
                    { n => new BinaryNumTree(NumKind.Multiply, n,                 ConstNumTree.Zero),     n => ConstNumTree.Zero, 1 },
                    { n => new BinaryNumTree(NumKind.Multiply, ConstNumTree.Zero, n),                     n => ConstNumTree.Zero, 1 },
                    { n => new UnaryNumTree(NumKind.Negate,    n),                n => ConstNumTree.Zero,                    1 },
                },
            };

            return(burs);
        }