Esempio n. 1
0
        static void Main(string[] args)
        {
            var s0 =
                new StratTerm[] {
                new StratTerm {
                    variable = "z", condition = StratTermVal.gt, constant = "50"
                },
                new StratTerm {
                    variable = "y", condition = StratTermVal.lte, constant = "20"
                },
                new StratTerm {
                    variable = "x", condition = StratTermVal.gt, constant = "10"
                }
            };
            var s1 =
                new StratTerm[] {
                new StratTerm {
                    variable = "q", condition = StratTermVal.gt, constant = "10"
                },
                new StratTerm {
                    variable = "z", condition = StratTermVal.lte, constant = "30"
                },
                new StratTerm {
                    variable = "x", condition = StratTermVal.lte, constant = "20"
                }
            };

            var f = new Finder();

            f.AddStataDef("s0", s0);
            f.AddStataDef("s1", s1);

            f.Preprocess();
            f.RenderToText(Console.Out);
        }
Esempio n. 2
0
 public StratTree(string refname, StratTerm st)
 {
     refName = refname;
     Term    = st;
     Left    = null;
     Right   = null;
     Parent  = null;
 }
Esempio n. 3
0
        static void Main2(string[] args)
        {
            Console.WriteLine(""); Console.WriteLine("");
            Console.WriteLine("**************************");
            Console.WriteLine(" Stratification Tree v0.1");
            Console.WriteLine("**************************");

            var s0 =
                new StratTerm[] {
                new StratTerm {
                    variable = "z", condition = StratTermVal.gt, constant = "50"
                },
                new StratTerm {
                    variable = "y", condition = StratTermVal.lte, constant = "20"
                },
                new StratTerm {
                    variable = "x", condition = StratTermVal.gt, constant = "10"
                }
            };

            var p0 =
                new Dictionary <string, string> [] {
                new Dictionary <string, string> {   //T
                    { "x", "11" },
                    { "y", "20" },
                    { "z", "51" }
                },
                new Dictionary <string, string> {   //F
                    { "x", "10" },
                    { "y", "21" },
                    { "z", "50" }
                }
            };

            var s1 =
                new StratTerm[] {
                new StratTerm {
                    variable = "q", condition = StratTermVal.gt, constant = "10"
                },
                new StratTerm {
                    variable = "z", condition = StratTermVal.lte, constant = "30"
                },
                new StratTerm {
                    variable = "x", condition = StratTermVal.lte, constant = "20"
                }
            };
            var p1 =
                new Dictionary <string, string> [] {
                new Dictionary <string, string> {   //T
                    { "q", "11" },
                    { "z", "30" },
                    { "x", "20" }
                },
                new Dictionary <string, string> {   //F
                    { "q", "10" },
                    { "z", "31" },
                    { "x", "21" }
                }
            };

            var s2 =
                new StratTerm[] {
                s1[0],
                s1[1],
                new StratTerm {
                    variable = "x", condition = StratTermVal.gt, constant = "20"
                }
            };
            var p2 =
                p1;

/*
 *          var s1 =
 *              new StratTerm[] {
 *                  new StratTerm { variable = "lot_sqft", condition = StratTermVal.gt, constant = "5000"},
 *                  new StratTerm { variable = "mfla_sqft", condition = StratTermVal.gt, constant = "2200"},
 *                  new StratTerm { variable = "usd", condition = StratTermVal.inlist, constant = "A,B"}
 *              };
 *
 *          var s2 =
 *              new StratTerm[] {
 *                  new StratTerm { variable = "lot_sqft", condition = StratTermVal.gt, constant = "3000"},
 *                  new StratTerm { variable = "mfla_sqft", condition = StratTermVal.gt, constant = "1200"},
 *                  new StratTerm { variable = "usd", condition = StratTermVal.inlist, constant = "C,D"}
 *              };
 *
 *          var s3 =
 *              new StratTerm[] {
 *                  new StratTerm { variable = "lot_sqft", condition = StratTermVal.gt, constant = "3000"},
 *                  new StratTerm { variable = "lot_sqft", condition = StratTermVal.lte, constant = "5000"},
 *                  new StratTerm { variable = "mfla_sqft", condition = StratTermVal.gt, constant = "1200"},
 *                  new StratTerm { variable = "mfla_sqft", condition = StratTermVal.lte, constant = "2200"},
 *                  new StratTerm { variable = "usd", condition = StratTermVal.inlist, constant = "A,B"}
 *              };
 */
            var f0 = new BasicFinder();

            f0.AddStataDef("s0", s0);
            f0.Preprocess();
            f0.RenderToText(Console.Out);

            var r0 = new string [] {
                f0.FindStrata(p0[0]),
                f0.FindStrata(p0[1]),
            };

            Debug.Assert(r0[0] == "s0", "strata s1 was not returned");
            Debug.Assert(r0[1] == null, "a null strata should have been returned");

            var f1 = new BasicFinder();

            f1.AddStataDef("s1", s1);
            f1.Preprocess();
            f1.RenderToText(Console.Out);

            var r1 = new string [] {
                f1.FindStrata(p1[0]),
                f1.FindStrata(p1[1]),
            };

            Debug.Assert(r1[0] == "s1", "strata s1 was not returned");
            Debug.Assert(r1[1] == null, "a null strata should have been returned");

            var f2 = new BasicFinder();

            f2.AddStataDef("s2", s2);
            f2.Preprocess();
            f2.RenderToText(Console.Out);


            var r2 = new string [] {
                f2.FindStrata(p2[0]),
                f2.FindStrata(p2[1]),
            };


            return;
        }
Esempio n. 4
0
        private List <Tuple <string, StratTerm> > SortTermList(List <Tuple <string, StratTerm> > l)
        {
            int termCompare(Tuple <string, StratTerm> xt, Tuple <string, StratTerm> yt)
            {
                bool IsNumeric(string value, out double result)
                {
                    return(Double.TryParse(value, out result));
                }

                StratTerm x = xt.Item2;
                StratTerm y = yt.Item2;

                int d = x.variable.CompareTo(y.variable);

                if (d != 0)
                {
                    return(d);
                }
                else
                {
                    double xd, yd;
                    bool   isXaNumeric = IsNumeric(x.constant, out xd);
                    bool   isYaNumeric = IsNumeric(y.constant, out yd);
                    switch (x.condition)
                    {
                    case StratTermVal.gt:
                    case StratTermVal.lte:
                        Debug.Assert(isXaNumeric == true, "Type error: expected a 'constant' double for <= and > operands");
                        if (isXaNumeric)
                        {
                            goto default;
                        }
                        else
                        {
                            throw new FormatException("Type error: expected a 'constant' double for <= and > operands");
                        }

                    default:
                        switch (y.condition)
                        {
                        case StratTermVal.gt:
                        case StratTermVal.lte:
                            Debug.Assert(isYaNumeric == true, "Type error: expected a 'constant' double for <= and > operands");
                            if (isYaNumeric)
                            {
                                goto default;
                            }
                            else
                            {
                                throw new FormatException("Type error: expected a 'constant' double for <= and > operands");
                            }

                        default:
                            if (xd - yd < 0.0 - 0.00000001)
                            {
                                return(-1);
                            }
                            else if (xd - yd > 0.0 + 0.00000001)
                            {
                                return(1);
                            }
                            else
                            {
                                return(0);
                            }
                        }
                    }
                }
            }

            l.Sort(termCompare);
            return(l);
        }
Esempio n. 5
0
        public void InsertSort(StratTree node)
        {
            StratTree target = this;

            //DRY utils
            void SwapNodeData()
            {
                StratTerm t = node.Term; //struct so copy by value

                node.Term   = target.Term;
                target.Term = t;
                string n = node.refName;

                node.refName   = target.refName;
                target.refName = n;
            }

            while (true)
            {
                if (node.Term.variable == target.Term.variable)
                {     //case: same var
                    if (node.Term.condition != target.Term.condition)
                    { //same var, diff cond
                        if (target.Right != null)
                        {
                            target = target.Right;
                            continue;
                        }
                        else
                        {
                            target.Right = node;
                            break;
                        }
                    }
                    else
                    {//same var, same cond
                        if (node.Term.condition == StratTermVal.gt ||
                            node.Term.condition == StratTermVal.lte)
                        {//numer constant
                            double nodec = 0.0;
                            double targc = 0.0;
                            try
                            {
                                nodec = Double.Parse(node.Term.constant);
                                targc = Double.Parse(target.Term.constant);
                            }
                            catch
                            {
                                break; //skip invalid - correctness requires aprior validation i.e no type op issues
                            }
                            if (target.Right != null)
                            {
                                target = target.Right;
                                continue;
                            }
                            else
                            {
                                target.Right = node;
                                break;
                            }
                        }
                        else
                        {//categ constant i.e. IN { A,B }
                         //ignored
                        }
                    }
                }
                else
                {         //case: diff var
                    if (target.Right != null)
                    {     //case: interior node
                        if (String.Compare(target.Term.variable, node.Term.variable) > 0)
                        { //target.refName follows node.refName in the sort order
                            target = target.Right;
                        }

                        else
                        {
                            target = target.Right;
                        }

                        continue;
                    }
                    else
                    {     //case: leaf node
                        if (String.Compare(target.Term.variable, node.Term.variable) > 0)
                        { //target.refName follows node.refName in the sort order
                         // so switch target and node i.e. target ends up being the right leaf
                            if (target.Parent == null)
                            {//data swap if target is root
                                SwapNodeData();
                                target.Right = node;
                                node.Parent  = target;
                            }
                            else
                            {
                                target.Parent.Right = node;
                                node.Parent         = target.Parent;
                                node.Right          = target;
                            }
                        }
                        else
                        {//just add as a right leaf of target
                            target.Right = node;
                            node.Parent  = target;
                        }

                        break;
                    }
                }
            }
        }
Esempio n. 6
0
        public void InsertAtTop(StratTree node)
        {//insert a new decision tree node at the top
            StratTree target = this;

            Debug.Assert(node.Left == null && node.Right == null, "Not leaf node passed as node");
            Debug.Assert(target.Parent == null, "Called on not root node");

            //DRY locals
            void SwapNodeData()
            {
                StratTerm t = node.Term; //struct so copy by value

                node.Term   = target.Term;
                target.Term = t;
                string n = node.refName;

                node.refName   = target.refName;
                target.refName = n;
            }

            void SwapNodeLinks()
            {//node is the initial root, target is now the item being inserted on top, since SwapNodeData is called before this
                node.Parent  = target;
                node.Right   = target.Right;
                node.Left    = target.Left;
                target.Right = node;
                target.Left  = node.Copy();
            }

            if (String.Compare(target.Term.variable, node.Term.variable) < 0)
            {//target-var precedes node-var in the sort order
                SwapNodeData();
                SwapNodeLinks();
            }
            else if (String.Compare(target.Term.variable, node.Term.variable) == 0)
            {//same var
                SwapNodeData();
                SwapNodeLinks();

                /*
                 *  the <= / lte are sorted in order of decreasing constant
                 *  x < 10
                 *  x < 5
                 *
                 **
                 ** the > / gt are sorted in order of increasing constant
                 ** x > 8
                 ** x > 2
                 **
                 **
                 ** x < 10
                 ** x > 8
                 ** x < 5
                 ** x > 2
                 */
            }
            else
            {
                throw new Exception("Variable names must be inserted in Sorted Order");
            }
        }