Exemplo n.º 1
0
            // note: can save a lot of space by moving this to a static / separate 'ends' hash/flag set

            public DictionaryNode(SingleChildNode source)
            {
                Children = new Dictionary <char, INode> {
                    [source.NextChar] = source.NextNode
                };
                End = source.End;
            }
Exemplo n.º 2
0
            public INode Split()
            {
                INode node = new SingleChildNode();

                node.End = true;
                return(node);
            }
Exemplo n.º 3
0
        public void ManyChildrenNodeTransform()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType <NodeTransformer>().As <INodeTransformer>();
            builder.RegisterType <NodeDescriber>().As <INodeDescriber>();
            Container = builder.Build();

            using (var scope = Container.BeginLifetimeScope())
            {
                var nodeDescriber   = scope.Resolve <INodeDescriber>();
                var nodeTransformer = scope.Resolve <INodeTransformer>();
                var testData        = new ManyChildrenNode("root",
                                                           new ManyChildrenNode("child1",
                                                                                new ManyChildrenNode("leaf1"),
                                                                                new ManyChildrenNode("child2",
                                                                                                     new ManyChildrenNode("leaf2"))));
                var result = nodeTransformer.Transform(testData);

                var expected = new SingleChildNode("root",
                                                   new TwoChildrenNode("child1",
                                                                       new NoChildrenNode("leaf1"),
                                                                       new SingleChildNode("child2",
                                                                                           new NoChildrenNode("leaf2"))));

                Assert.AreEqual(nodeDescriber.Describe(expected), nodeDescriber.Describe(result));
            }
        }
Exemplo n.º 4
0
        public void DefaultTreeDescribe()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType <NodeTransformer>().As <INodeTransformer>();
            builder.RegisterType <NodeDescriber>().As <INodeDescriber>();
            Container = builder.Build();

            using (var scope = Container.BeginLifetimeScope())
            {
                var nodeDescriber   = scope.Resolve <INodeDescriber>();
                var nodeTransformer = scope.Resolve <INodeTransformer>();
                var testdata        = new SingleChildNode("root",
                                                          new TwoChildrenNode("child1",
                                                                              new NoChildrenNode("leaf1"),
                                                                              new SingleChildNode("child2",
                                                                                                  new NoChildrenNode("leaf2"))));
                var result = nodeDescriber.Describe(testdata);

                System.IO.StringWriter basetextwriter = new System.IO.StringWriter();
                IndentedTextWriter     indentwriter   = new IndentedTextWriter(basetextwriter, "    ");

                Dictionary <int, List <string> > outPutDic = new Dictionary <int, List <string> >();
                outPutDic.Add(0, new List <string>(new string[] { @"new SingleChildNode(""root""," }));
                outPutDic.Add(1, new List <string>(new string[] { @"new TwoChildrenNode(""child1""," }));
                outPutDic.Add(2, new List <string>(new string[] { @"new NoChildrenNode(""leaf1""),", @"new SingleChildNode(""child2""," }));
                outPutDic.Add(3, new List <string>(new string[] { @"new NoChildrenNode(""leaf2""))))" }));

                Utility.WriteLevel(indentwriter, outPutDic);
                Assert.AreEqual(basetextwriter.ToString(), result);
            }
        }
Exemplo n.º 5
0
            public void Add(char[] word, int start)
            {
                if (word.Length - start == 0)
                {
                    End = true;
                    return;
                }
                char c = word[start];

                if (Children == null)
                {
                    Children = new Dictionary <char, INode>();
                }
                INode target;

                if (!Children.TryGetValue(c, out target))
                {
                    target      = new SingleChildNode();
                    Children[c] = target;
                }
                try
                {
                    target.Add(word, start + 1);
                }
                catch (MustBeSplitException)
                {
                    Children[c] = target.Split();
                    Add(word, start);
                }
            }
Exemplo n.º 6
0
            // breaks PNode into singlechildnode + remainder
            public INode Split()
            {
                INode next = new SingleChildNode();

                next.Add(Chars, 0);
                return(next);
            }
Exemplo n.º 7
0
        public void Transform_SingleChildWithNull_ReturnNoChildNode()
        {
            var Transformr = _container.Resolve <TypeNodeTransformer>();
            var testData   = new SingleChildNode("root", null);
            var result     = Transformr.Transform(testData);

            Assert.IsTrue(result != null && result.GetType().Name == "NoChildrenNode");
        }
Exemplo n.º 8
0
        public void Describe_SingleChildWithOneLeaf_SingleChildWithOneLeaf()
        {
            var describer = _container.Resolve <TextNodeDescriber>();
            var testData  = new SingleChildNode("root", new NoChildrenNode("leaf1"));
            var result    = describer.Describe(testData);

            Assert.IsTrue(result != null && result.Contains("root") && result.Contains("leaf1"));
        }
Exemplo n.º 9
0
 private Node TransformSingleChildNode(SingleChildNode node)
 {
     Node child = node.Child;
     if(child == null)
     {
         return new NoChildrenNode(node.Name);
     }
     return node;
 }
Exemplo n.º 10
0
        public void DescribeSingleChildNodeTest()
        {
            var describer = NodeDescriberFactory.GetDescriber();

            var node1 = new SingleChildNode("node1", null);
            var node2 = new SingleChildNode("node2", new NoChildrenNode("node2.1"));

            Assert.AreEqual("new SingleChildNode(\"node1\")", describer.Describe(node1));
            Assert.AreEqual("new SingleChildNode(\"node2\",\n    new NoChildrenNode(\"node2.1\"))", describer.Describe(node2));
        }
Exemplo n.º 11
0
        public void Describe_Given_SingleChildNode_Returns_SingleChildNodeDescription()
        {
            //arrange
            var expected = "new SingleChildNode(\"root\",\n" + Indentation + "new NoChildrenNode(\"leaf1\"))";
            var testData = new SingleChildNode("root", new NoChildrenNode("leaf1"));
            //act
            var actual = _nodeDescriber.Describe(testData);

            //assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 12
0
        public void Transform_Given_ManyChildrenNode_And_OneChild_Returns_SingleChildNode()
        {
            //arrange
            var expected = new SingleChildNode("root",
                                               new NoChildrenNode("leaf"));
            var testInput = new ManyChildrenNode("root",
                                                 new ManyChildrenNode("leaf"));
            //act
            var transformer = new NodeTransformer();
            var actual      = transformer.Transform(testInput);

            //assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            //INodeDescriber describer = new TextNodeDescriber(4);
            //var testData = new SingleChildNode("root", new TwoChildrenNode("child1", new NoChildrenNode("leaf1"), new SingleChildNode("child2", new NoChildrenNode("leaf2"))));
            var testData = new SingleChildNode("root", new TwoChildrenNode("child1", new NoChildrenNode("leaf1"), new SingleChildNode("child2", null)));
            // testData = new SingleChildNode("root", null);

            //var result = describer.Describe(testData);
            //Console.WriteLine(result);


            //INodeDescriber describer2 = new TextNodeDescriber(4);
            //INodeTransformer transformer = new TypeNodeTransformer();
            //var transformedData=transformer.Transform(testData);
            //var transformedresult=describer2.Describe(transformedData);
            //Console.WriteLine(transformedresult);


            //INodeWriter writer = new FileNodeWriter();
            //var filePath = @"C:\Work\Repository\NodeTreeAnalyzer\test.txt";
            //writer.WriteToFileAsync(testData,filePath );
            //var persistedText = File.ReadAllText(filePath);
            //Console.WriteLine(persistedText);

            //configure
            var container = new UnityContainer();

            container.RegisterType(typeof(INodeDescriber), typeof(TextNodeDescriber));
            container.RegisterType(typeof(INodeTransformer), typeof(TypeNodeTransformer));
            container.RegisterType(typeof(INodeWriter), typeof(FileNodeWriter));

            var describer = container.Resolve <TextNodeDescriber>();

            Console.WriteLine(describer.Describe(testData));

            var writer   = container.Resolve <FileNodeWriter>();
            var filePath = @"C:\Work\Repository\NodeTreeAnalyzer\test.txt";

            writer.WriteToFileAsync(testData, filePath);
            var persistedText = File.ReadAllText(filePath);

            Console.WriteLine(persistedText);

            Console.Read();
        }
Exemplo n.º 14
0
        /// <summary>
        /// Method which transforms the tree of nodes into a matching tree that uses the correct node types
        /// </summary>
        /// <param name="node">Source node</param>
        /// <returns>Transformed node</returns>
        public Node Transform(Node node)
        {
            if (node == null)
            {
                return(null);
            }
            if (!(node is ManyChildrenNode))
            {
                throw new Exception("Node must be of ManyChildrenNode type");
            }

            Node             result     = null;
            ManyChildrenNode sourceNode = (ManyChildrenNode)node;

            Node [] transformedChild = this.TransforChildren(sourceNode);

            switch (transformedChild.Length)
            {
            case 0:
                result = new NoChildrenNode(sourceNode.Name);
                break;

            case 1:
                result = new SingleChildNode(
                    sourceNode.Name,
                    transformedChild[0]
                    );
                break;

            case 2:
                result = new TwoChildrenNode(
                    sourceNode.Name,
                    transformedChild[0],
                    transformedChild[1]
                    );
                break;

            default:
                throw new Exception("Unknown transformation");
            }

            return(result);
        }
Exemplo n.º 15
0
        public void DescribeTreeTest()
        {
            var describer = NodeDescriberFactory.GetDescriber();

            var node = new SingleChildNode("root",
                                           new TwoChildrenNode("child1",
                                                               new NoChildrenNode("leaf1"),
                                                               new SingleChildNode("child2",
                                                                                   new NoChildrenNode("leaf2"))));

            Assert.AreEqual(
                "new SingleChildNode(\"root\",\n" +
                "    new TwoChildrenNode(\"child1\",\n" +
                "        new NoChildrenNode(\"leaf1\"),\n" +
                "        new SingleChildNode(\"child2\",\n" +
                "            new NoChildrenNode(\"leaf2\"))))",
                describer.Describe(node)
                );
        }
Exemplo n.º 16
0
        public void Describe_Given_ThreeLevelsOfNestedNodes_Returns_ThreeLevelsOfNestedNodesDescription()
        {
            //arrange
            var expected = "new SingleChildNode(\"root\",\n"
                           + Indentation + "new TwoChildrenNode(\"child1\",\n"
                           + Indentation + Indentation + "new NoChildrenNode(\"leaf1\"),\n"
                           + Indentation + Indentation + "new SingleChildNode(\"child2\",\n"
                           + Indentation + Indentation + Indentation + "new NoChildrenNode(\"leaf2\"))))";
            var testData = new SingleChildNode("root",
                                               new TwoChildrenNode("child1",
                                                                   new NoChildrenNode("leaf1"),
                                                                   new SingleChildNode("child2",
                                                                                       new NoChildrenNode("leaf2"))));
            //act
            var actual = _nodeDescriber.Describe(testData);

            //assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 17
0
        public void Transform_Given_ManyChildrenNode_And_ManyChildren_Returns_DerivedNodes()
        {
            //arrange
            var expected = new SingleChildNode("root",
                                               new TwoChildrenNode("child1",
                                                                   new NoChildrenNode("leaf1"),
                                                                   new SingleChildNode("child2",
                                                                                       new NoChildrenNode("leaf2"))));

            var testInput = new ManyChildrenNode("root",
                                                 new ManyChildrenNode("child1",
                                                                      new ManyChildrenNode("leaf1"),
                                                                      new ManyChildrenNode("child2",
                                                                                           new ManyChildrenNode("leaf2"))));
            //act
            var transformer = new NodeTransformer();
            var actual      = transformer.Transform(testInput);

            //assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 18
0
        public async Task WriteToFileAsync_Given_MultipleNodes_Writes_To_File_MultipleNodes()
        {
            //arrange
            var filePath = "tester.txt";
            var testData = new SingleChildNode("root",
                                               new TwoChildrenNode("child1",
                                                                   new NoChildrenNode("leaf1"),
                                                                   new SingleChildNode("child2",
                                                                                       new NoChildrenNode("leaf2"))));
            var expected = "new SingleChildNode(\"root\",\n"
                           + Indentation + "new TwoChildrenNode(\"child1\",\n"
                           + Indentation + Indentation + "new NoChildrenNode(\"leaf1\"),\n"
                           + Indentation + Indentation + "new SingleChildNode(\"child2\",\n"
                           + Indentation + Indentation + Indentation + "new NoChildrenNode(\"leaf2\"))))";
            //act
            await _nodeWriter.WriteToFileAsync(testData, filePath);

            var actual = File.ReadAllText(filePath, Encoding.Unicode);

            //assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 19
0
        public async Task WriteToFileAsyncTest()
        {
            var filePath = "test-tree.txt";
            var node     = new SingleChildNode("root",
                                               new TwoChildrenNode("child1",
                                                                   new NoChildrenNode("leaf1"),
                                                                   new SingleChildNode("child2",
                                                                                       new NoChildrenNode("leaf2"))));

            INodeWriter implementation = new NodeWriter();
            await implementation.WriteToFileAsync(node, filePath);

            var result = File.ReadAllText(filePath);

            Assert.AreEqual(
                "new SingleChildNode(\"root\",\n" +
                "    new TwoChildrenNode(\"child1\",\n" +
                "        new NoChildrenNode(\"leaf1\"),\n" +
                "        new SingleChildNode(\"child2\",\n" +
                "            new NoChildrenNode(\"leaf2\"))))",
                result
                );
        }
Exemplo n.º 20
0
 protected bool Equals(SingleChildNode other)
 {
     return(Equals(Child, other.Child));
 }
Exemplo n.º 21
0
            // note: can save a lot of space by moving this to a static / separate 'ends' hash/flag set

            public SmallDictionaryNode(SingleChildNode source)
            {
                NextChar1 = source.NextChar;
                NextNode1 = source.NextNode;
                End       = source.End;
            }
Exemplo n.º 22
0
        public Node CreateTransformedNode(Node originNode)
        {
            var myList   = new List <Node>();
            var nodeType = originNode.GetType();
            //Check the real type of node
            var properties  = nodeType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var isManyChild = false;
            var nodeCount   = 0;
            var nodeName    = string.Empty;

            foreach (var propertyInfo in properties)
            {
                var propertyValue = propertyInfo.GetValue(originNode, null);
                if (propertyValue != null)
                {
                    if (!(propertyValue is ValueType) && !(propertyValue is string))
                    {
                        var nodeClassType = typeof(Node);
                        //if node has IEnumerable type
                        isManyChild = typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType);
                        if (isManyChild)
                        {
                            foreach (var item in propertyValue as IEnumerable <Node> )
                            {
                                if (item != null && nodeClassType.IsAssignableFrom(item.GetType()))
                                {
                                    nodeCount++;
                                    myList.Add(item);
                                }
                            }
                        }

                        if (nodeClassType.IsAssignableFrom(propertyInfo.PropertyType))
                        {
                            nodeCount++;
                            myList.Add(propertyValue as Node);
                        }
                    }
                    else
                    {
                        nodeName = propertyValue.ToString();
                    }
                }
            }



            switch (nodeCount)
            {
            case 0:
                originNode = new NoChildrenNode(nodeName);
                break;

            case 1:
                originNode = new SingleChildNode(nodeName, CreateTransformedNode(myList[0]));
                break;

            case 2:
                originNode = new TwoChildrenNode(nodeName, CreateTransformedNode(myList[0]), CreateTransformedNode(myList[1]));
                break;

            default:

                break;
            }

            return(originNode);
        }
        public void NodeDescriberTest()
        {
            // Traverse down and across tree
            INodeDescriber implementation = new NodeDescriber(new NodesHelper());
            var            testData       = new SingleChildNode("root",
                                                                new TwoChildrenNode("child1",
                                                                                    new NoChildrenNode("leaf1"),
                                                                                    new SingleChildNode("child2",
                                                                                                        new NoChildrenNode("leaf2"))));

            var result = implementation.Describe(testData);

            StringBuilder sb = new StringBuilder();

            sb.Append("new SingleChildNode(\"root\","); sb.Append(Environment.NewLine);
            sb.Append(tab); sb.Append("new TwoChildrenNode(\"child1\","); sb.Append(Environment.NewLine);
            sb.Append(tab); sb.Append(tab); sb.Append("new NoChildrenNode(\"leaf1\"),"); sb.Append(Environment.NewLine);
            sb.Append(tab); sb.Append(tab); sb.Append("new SingleChildNode(\"child2\","); sb.Append(Environment.NewLine);
            sb.Append(tab); sb.Append(tab); sb.Append(tab); sb.Append("new NoChildrenNode(\"leaf2\"))));");
            string expectedResult = sb.ToString();

            Assert.AreEqual(expectedResult, result);


            // Traverse down tree, back up, then back down
            implementation = new NodeDescriber(new NodesHelper());
            testData       = new SingleChildNode("root",
                                                 new ManyChildrenNode("child1",
                                                                      new SingleChildNode("child2",
                                                                                          new NoChildrenNode("leaf1")),
                                                                      new NoChildrenNode("leaf2")));

            result = implementation.Describe(testData);

            sb = new StringBuilder();
            sb.Append("new SingleChildNode(\"root\","); sb.Append(Environment.NewLine);
            sb.Append(tab); sb.Append("new ManyChildrenNode(\"child1\","); sb.Append(Environment.NewLine);
            sb.Append(tab); sb.Append(tab); sb.Append("new SingleChildNode(\"child2\","); sb.Append(Environment.NewLine);
            sb.Append(tab); sb.Append(tab); sb.Append(tab); sb.Append("new NoChildrenNode(\"leaf1\")),"); sb.Append(Environment.NewLine);
            sb.Append(tab); sb.Append(tab); sb.Append("new NoChildrenNode(\"leaf2\")));");
            expectedResult = sb.ToString();

            Assert.AreEqual(expectedResult, result);


            // Traverse tree, removing null nodes
            implementation = new NodeDescriber(new NodesHelper());
            var testDataWithNulls = new ManyChildrenNode("root",
                                                         new TwoChildrenNode("child1",
                                                                             null,
                                                                             new SingleChildNode("leaf1",
                                                                                                 null)));

            result = implementation.Describe(testDataWithNulls);

            sb = new StringBuilder();
            sb.Append("new ManyChildrenNode(\"root\","); sb.Append(Environment.NewLine);
            sb.Append(tab); sb.Append("new TwoChildrenNode(\"child1\","); sb.Append(Environment.NewLine);
            sb.Append(tab); sb.Append(tab); sb.Append("new SingleChildNode(\"leaf1\")));");
            expectedResult = sb.ToString();

            Assert.AreEqual(expectedResult, result);
        }