예제 #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.Revert(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 ");
                    }

                    string value = child.Value.Value.ToString(CultureInfo.InvariantCulture);

                    writer.Write("(input[{0}] {1} {2}) {{", attributeIndex, cmp, 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)
                {
                    string value = node.Output.Value.ToString(CultureInfo.InvariantCulture);
                    writer.WriteLine(indent + "return " + value + ";");
                }
                else
                {
                    writer.WriteLine(indent + "return -1;");
                }
            }
        }