示例#1
0
        public static void Main(string[] args)
        {
            INodeDescriber implementation = new NodeDescriber();

            var testData = new SingleChildNode("root", new TwoChildrenNode("child1", new NoChildrenNode("child3"), new SingleChildNode("child2", new NoChildrenNode("leaf1"))));
            var result   = implementation.Describe(testData);
            INodeTransformer implementation2 = new NodeTransformer();
            var testData2 = new ManyChildrenNode("root",
                                                 new ManyChildrenNode("child1",
                                                                      new ManyChildrenNode("leaf1"),
                                                                      new ManyChildrenNode("child2",
                                                                                           new ManyChildrenNode("leaf2"))));
            var result2 = implementation2.Transform(testData2);
            var result3 = implementation.Describe(result2);

            Console.WriteLine(result);

            INodeWriter implementation3 = new NodeWriter(NodeDesFactory.GetNodeDescriber());
            string      filepath        = @"G:\WriteLines.txt";

            implementation3.WriteToFileAsync(testData, filepath);
            var result5 = File.ReadAllText(filepath);

            Console.ReadKey();
        }
示例#2
0
        private string Subdescribe(Node node, int count)
        {
            string result = "";

            result += Environment.NewLine;
            string spaces = "";

            for (int i = 0; i < count; i++)
            {
                spaces += "    ";
            }
            result += spaces;
            if (node as NoChildrenNode != null)
            {
                NoChildrenNode ncn = (NoChildrenNode)node;
                result += "new NoChildrenNode(" + node.Name + ")";
            }
            else if (node as SingleChildNode != null)
            {
                SingleChildNode scn = (SingleChildNode)node;
                result += "new SingleChildNodef(" + node.Name + ",";
                result += Subdescribe(scn.Child, count + 1);
                result += ")";
            }
            else if (node as TwoChildrenNode != null)
            {
                TwoChildrenNode wcn = (TwoChildrenNode)node;
                result += "new TwoChildrenNode(" + node.Name + ",";
                result += Subdescribe(wcn.FirstChild, count + 1);
                result += Subdescribe(wcn.SecondChild, count + 1);
                result += ")";
            }
            else if (node as ManyChildrenNode != null)
            {
                ManyChildrenNode mcn   = (ManyChildrenNode)node;
                List <Node>      child = mcn.Children.ToList();
                result += "new ManyChildrenNode(" + node.Name + ",";
                foreach (var cnode in child)
                {
                    result += Subdescribe(cnode, count + 1);
                }
                result += ")";
            }
            return(result);
        }
示例#3
0
 public Node Transform(Node node)
 {
     if (node as NoChildrenNode != null)
     {
         return(node);
     }
     else if (node as ManyChildrenNode != null)
     {
         ManyChildrenNode mcd = (ManyChildrenNode)node;
         if (mcd.Children.Count() > 2)
         {
             ManyChildrenNode root = new ManyChildrenNode(mcd.Name, mcd.Children.ToArray());
             foreach (var cnode in mcd.Children)
             {
                 Transform(cnode);
             }
             return(root);
         }
         else if (mcd.Children.Count() == 2)
         {
             List <Node>     lchildren = mcd.Children.ToList();
             TwoChildrenNode tcn       = new TwoChildrenNode(mcd.Name, Transform(lchildren[0]), Transform(lchildren[1]));
             return(tcn);
         }
         else if (mcd.Children.Count() == 1)
         {
             List <Node> child = mcd.Children.ToList();
             //Transform(child[0]);
             SingleChildNode scn = new SingleChildNode(mcd.Name, Transform(child[0]));
             return(scn);
         }
         else
         {
             NoChildrenNode ncn = new NoChildrenNode(mcd.Name);
             return(ncn);
         }
     }
     else if (node as TwoChildrenNode != null)
     {
         TwoChildrenNode tcd = (TwoChildrenNode)node;
         if ((tcd.FirstChild != null) && (tcd.SecondChild != null))
         {
             TwoChildrenNode ntcn = new TwoChildrenNode(tcd.Name, tcd.FirstChild, tcd.SecondChild);
             Transform(ntcn.FirstChild);
             Transform(ntcn.SecondChild);
             return(ntcn);
         }
         else if ((tcd.FirstChild == null) && (tcd.SecondChild == null))
         {
             NoChildrenNode ncn = new NoChildrenNode(tcd.Name);
             return(ncn);
         }
         else if ((tcd.FirstChild == null) || (tcd.SecondChild == null))
         {
             SingleChildNode scn;
             if (tcd.FirstChild != null)
             {
                 scn = new SingleChildNode(tcd.Name, tcd.FirstChild);
             }
             else
             {
                 scn = new SingleChildNode(tcd.Name, tcd.SecondChild);
             }
             Transform(scn);
             return(scn);
         }
     }
     else if (node as SingleChildNode != null)
     {
         SingleChildNode scn = (SingleChildNode)node;
         if (scn.Child == null)
         {
             NoChildrenNode ncn = new NoChildrenNode(scn.Name);
             return(ncn);
         }
         Transform(scn);
         return(scn);
     }
     return(node);
 }