示例#1
0
        private string toString(Codification codebook)
        {
            if (IsRoot)
            {
                return("Root");
            }

            String name = Owner.Attributes[Parent.Branches.AttributeIndex].Name;

            if (String.IsNullOrEmpty(name))
            {
                name = "x" + Parent.Branches.AttributeIndex;
            }

            String op = ComparisonExtensions.ToString(Comparison);

            String value;

            if (codebook != null && Value.HasValue && codebook.Columns.Contains(name))
            {
                value = codebook.Translate(name, (int)Value.Value);
            }

            else
            {
                value = Value.ToString();
            }


            return(String.Format("{0} {1} {2}", name, op, value));
        }
示例#2
0
        private void create(DecisionNode node, int depth)
        {
            string indent = new string(' ', depth * 4);

            if (!node.IsLeaf)
            {
                int attributeIndex = node.Branches.AttributeIndex;

                // Create all comparison expressions
                for (int i = 0; i < node.Branches.Count; i++)
                {
                    DecisionNode child = node.Branches[i];
                    string       cmp   = ComparisonExtensions.ToString(child.Comparison);

                    if (i == 0)
                    {
                        writer.Write(indent + "if ");
                    }
                    else
                    {
                        writer.Write(indent + "else if ");
                    }

                    writer.Write("(input[{0}] {1} {2}) {{", attributeIndex, cmp, child.Value);
                    writer.WriteLine();
                    create(child, depth + 1);
                    writer.WriteLine(indent + "}");
                }

                writer.WriteLine(indent + "else throw new ArgumentException(\"input\", \"Unexpected value at position " + attributeIndex + ".\");");
            }

            else // node is a leaf
            {
                if (node.Output.HasValue)
                {
                    writer.WriteLine(indent + "return " + node.Output.Value + ";");
                }
                else
                {
                    writer.WriteLine(indent + "return -1;");
                }
            }
        }