Пример #1
0
        public void Test()
        {
            var counter    = 0;
            var baseVertex = new TypeVertex(typeof(IBase), counter++);
            var a          = new TypeVertex(typeof(IA), counter++);

            var propBaseA1 = typeof(IBase).GetProperty("A1");
            var propBaseA2 = typeof(IBase).GetProperty("A2");

            var basea1 = new PropertyEdge(baseVertex, propBaseA1, a);
            var basea2 = new PropertyEdge(baseVertex, propBaseA2, a);

            var expectedGraph = GetGraphArray(baseVertex, a);

            //// IBase -> [{IBase.A1, IA}, {IBase.A2, IA}]
            ////       ~> [IA]
            ////
            ////    IA -> []
            ////       ~> [IBase]

            expectedGraph[baseVertex.Index].Children.AddRange(new[] { basea1, basea2 });
            expectedGraph[baseVertex.Index].Casts.AddLast(new CastEdge(baseVertex, a));

            expectedGraph[a.Index].Parents.AddRange(new[] { basea1, basea2 });
            expectedGraph[a.Index].Casts.AddLast(new CastEdge(a, baseVertex));

            var typeGraph = BuildTypeGraph <IBase>();

            Assert.True(IsEqual(expectedGraph, typeGraph.Vertices));
        }
Пример #2
0
        public void Test()
        {
            var counter    = 0;
            var baseVertex = new TypeVertex(typeof(IBase), counter++);
            var a          = new TypeVertex(typeof(IA), counter++);
            var b          = new TypeVertex(typeof(IB), counter++);
            var c          = new TypeVertex(typeof(IC), counter++);
            var d          = new TypeVertex(typeof(ID), counter++);

            var propAC = typeof(IA).GetProperty("C");
            var propCD = typeof(IC).GetProperty("D");

            var ac = new PropertyEdge(a, propAC, c);
            var cd = new PropertyEdge(c, propCD, d);

            var expectedGraph = GetGraphArray(baseVertex, a, b, c, d);

            //// IBase -> []
            ////       ~> [IA, IB, IC, ID]
            ////
            ////    IA -> [{IA.C, IC}]
            ////       ~> [IBase]
            ////
            ////    IB -> []
            ////       ~> [IBase, IC]
            ////
            ////    IC -> [{IC.D, ID}]
            ////       ~> [IBase, IB]
            ////
            ////    ID -> []
            ////       ~> [IBase]

            expectedGraph[baseVertex.Index].Casts.AddRange(new[]
            {
                new CastEdge(baseVertex, a), new CastEdge(baseVertex, b), new CastEdge(baseVertex, c),
                new CastEdge(baseVertex, d)
            });

            expectedGraph[a.Index].Children.AddLast(ac);
            expectedGraph[a.Index].Casts.AddLast(new CastEdge(a, baseVertex));

            expectedGraph[b.Index].Casts.AddLast(new CastEdge(b, baseVertex));
            expectedGraph[b.Index].Casts.AddLast(new CastEdge(b, c));

            expectedGraph[c.Index].Parents.AddLast(ac);
            expectedGraph[c.Index].Children.AddLast(cd);
            expectedGraph[c.Index].Casts.AddLast(new CastEdge(c, baseVertex));
            expectedGraph[c.Index].Casts.AddLast(new CastEdge(c, b));

            expectedGraph[d.Index].Parents.AddLast(cd);
            expectedGraph[d.Index].Casts.AddLast(new CastEdge(d, baseVertex));

            var typeGraph = BuildTypeGraph <IBase>();

            Assert.True(IsEqual(expectedGraph, typeGraph.Vertices));
        }
Пример #3
0
        public void Test()
        {
            var counter    = 0;
            var baseVertex = new TypeVertex(typeof(IBase), counter++);
            var a          = new TypeVertex(typeof(IA), counter++);
            var b          = new TypeVertex(typeof(IB), counter++);
            var c          = new TypeVertex(typeof(IC), counter++);

            var propAB = typeof(IA).GetProperty("B");
            var propBC = typeof(IB).GetProperty("C");
            var propCA = typeof(IC).GetProperty("A");

            var ab = new PropertyEdge(a, propAB, b);
            var bc = new PropertyEdge(b, propBC, c);
            var ca = new PropertyEdge(c, propCA, a);

            var expectedGraph = GetGraphArray(baseVertex, a, b, c);

            //// IBase -> []
            ////       ~> [IA, IB, IC]
            ////
            ////    IA -> [{IA.B, IB}]
            ////       ~> [IBase]
            ////
            ////    IB -> [{IB.C, IC}]
            ////       ~> [IBase]
            ////
            ////    IC -> [{IC.A, IA}]
            ////       ~> [IBase]

            expectedGraph[baseVertex.Index].Casts.AddRange(new[]
                                                           { new CastEdge(baseVertex, a), new CastEdge(baseVertex, b), new CastEdge(baseVertex, c) });

            expectedGraph[a.Index].Parents.AddLast(ca);
            expectedGraph[a.Index].Children.AddLast(ab);
            expectedGraph[a.Index].Casts.AddLast(new CastEdge(a, baseVertex));

            expectedGraph[b.Index].Parents.AddLast(ab);
            expectedGraph[b.Index].Children.AddLast(bc);
            expectedGraph[b.Index].Casts.AddLast(new CastEdge(b, baseVertex));

            expectedGraph[c.Index].Parents.AddLast(bc);
            expectedGraph[c.Index].Children.AddLast(ca);
            expectedGraph[c.Index].Casts.AddLast(new CastEdge(c, baseVertex));

            var typeGraph = BuildTypeGraph <IBase>();

            Assert.True(IsEqual(expectedGraph, typeGraph.Vertices));
        }
Пример #4
0
        public HashSet <PropertyInfoVertex> BuildSearchTree(
            TypeVertex currentVertex,
            TypeVertex searchVertex,
            PathBuilderSearchCache cache)
        {
            var properties = new HashSet <PropertyInfoVertex>();

            HashSet <PropertyInfoVertex> cachedProperties;

            if (cache.AllVertices.TryGetValue(currentVertex, out cachedProperties))
            {
                return(cachedProperties);
            }

            cache.AllVertices[currentVertex] = properties;

            var allEdges = new LinkedList <PropertyEdge>();

            allEdges.AddRange(currentVertex.Children);
            currentVertex.Casts.ForEach(
                castEdge =>
            {
                if (!_typeGraph.PathExists(castEdge.Value.CastTo, searchVertex))
                {
                    return;
                }

                allEdges.AddRange(castEdge.Value.CastTo.Children);
            });

            allEdges.ForEach(
                searchNode =>
            {
                var childVertex  = searchNode.Value.Child;
                var propertyInfo = searchNode.Value.PropertyInfo;

                if (!_typeGraph.PathExists(childVertex, searchVertex))
                {
                    return;
                }

                PropertyInfoVertex rootProperty;
                if (!cache.AllProperties.TryGetValue(propertyInfo, out rootProperty))
                {
                    rootProperty = new PropertyInfoVertex(propertyInfo);
                    cache.AllProperties[propertyInfo] = rootProperty;
                }

                properties.Add(rootProperty);
            });

            allEdges.ForEach(
                searchNode =>
            {
                var childVertex  = searchNode.Value.Child;
                var propertyInfo = searchNode.Value.PropertyInfo;

                PropertyInfoVertex rootProperty;
                if (!cache.AllProperties.TryGetValue(propertyInfo, out rootProperty))
                {
                    return;
                }

                var childProperties = BuildSearchTree(childVertex, searchVertex, cache);

                rootProperty.Children.UnionWith(childProperties);

                foreach (var childProperty in childProperties)
                {
                    childProperty.Parents.Add(rootProperty);
                }
            });

            return(properties);
        }