예제 #1
0
        private static IVertexAndEdgeListGraph<PipeGraphVertex, Edge<PipeGraphVertex>> CreateGraphOfPipeSystem(PipeGraphVertex firstVertex)
        {
            var graph = new AdjacencyGraph<PipeGraphVertex, Edge<PipeGraphVertex>>(false);
            var verticesSeen = new HashSet<PipeGraphVertex>();
            var verticesToCheck = new Stack<PipeGraphVertex>();
            verticesToCheck.Push(firstVertex);

            while (verticesToCheck.Any())
            {
                var vertexToCheck = verticesToCheck.Pop();
                if (verticesSeen.Contains(vertexToCheck)) continue;

                var sendsTo = GetVerticesYouSendMessagesTo(vertexToCheck);
                var receivesFrom = GetVerticesYouReceiveMessagesFrom(vertexToCheck);

                foreach (var vertex in sendsTo) verticesToCheck.Push(vertex);
                foreach (var vertex in receivesFrom) verticesToCheck.Push(vertex);

                graph.AddVertex(vertexToCheck);

                graph.AddVerticesAndEdgeRange(sendsTo.Select(v => new Edge<PipeGraphVertex>(vertexToCheck, v)));
                graph.AddVerticesAndEdgeRange(receivesFrom.Select(v => new Edge<PipeGraphVertex>(v, vertexToCheck)));

                verticesSeen.Add(vertexToCheck);
            }

            return graph;
        }
예제 #2
0
        public void CreateGraphOfPipeSystem_GivenAPipeConnectedToItself_ReturnsAGraphWithASelfLoop()
        {
            // Arrange
            var basicPipe = PipeBuilder.New.BasicPipe <int>().Build();

            basicPipe.Inlet.ConnectedOutlet = basicPipe.Outlet;
            basicPipe.Outlet.ConnectedInlet = basicPipe.Inlet;

            // Act
            var graph = basicPipe.CreateGraphOfPipeSystem();

            // Assert
            graph.Vertices.Count().Should().Be(3);
            graph.Vertices.Should().Contain(new[]
            {
                PipeGraphVertex.Create(basicPipe),
                PipeGraphVertex.Create(basicPipe.Inlet),
                PipeGraphVertex.Create(basicPipe.Outlet)
            });

            graph.EdgeCount.Should().Be(3);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe.Outlet))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe.Outlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe.Inlet))).Should().Be(1);
        }
예제 #3
0
        public void CreateGraphOfPipeSystem_GivenPipesIncludingACycle_ReturnsAGraphWithACycle()
        {
            // Arrange
            var basicPipe1    = PipeBuilder.New.BasicPipe <int>().Build();
            var basicPipe2    = PipeBuilder.New.BasicPipe <int>().Build();
            var basicPipe3    = PipeBuilder.New.BasicPipe <int>().Build();
            var twoOutletPipe = PipeBuilder.New.EitherOutletPipe <int>().Build();

            basicPipe1.Outlet.ConnectTo(basicPipe2.Inlet);
            basicPipe2.Outlet.ConnectTo(twoOutletPipe.Inlet);
            twoOutletPipe.RightOutlet.ConnectTo(basicPipe3.Inlet);
            twoOutletPipe.LeftOutlet.ConnectedInlet = basicPipe1.Inlet;
            basicPipe1.Inlet.ConnectedOutlet        = twoOutletPipe.LeftOutlet;

            // Act
            var graph = basicPipe1.CreateGraphOfPipeSystem();

            // Assert
            graph.Vertices.Count().Should().Be(13);
            graph.Vertices.Should().Contain(new[]
            {
                PipeGraphVertex.Create(basicPipe1),
                PipeGraphVertex.Create(basicPipe1.Inlet),
                PipeGraphVertex.Create(basicPipe1.Outlet),
                PipeGraphVertex.Create(basicPipe2),
                PipeGraphVertex.Create(basicPipe2.Inlet),
                PipeGraphVertex.Create(basicPipe2.Outlet),
                PipeGraphVertex.Create(basicPipe3),
                PipeGraphVertex.Create(basicPipe3.Inlet),
                PipeGraphVertex.Create(basicPipe3.Outlet),
                PipeGraphVertex.Create(twoOutletPipe),
                PipeGraphVertex.Create(twoOutletPipe.Inlet),
                PipeGraphVertex.Create(twoOutletPipe.LeftOutlet),
                PipeGraphVertex.Create(twoOutletPipe.RightOutlet)
            });

            graph.EdgeCount.Should().Be(13);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe1.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe1))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe1)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe1.Outlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe2.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe2))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe2)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe2.Outlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe3.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe3))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe3)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe3.Outlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe.LeftOutlet))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe.RightOutlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe1.Outlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe2.Inlet))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe2.Outlet)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe.Inlet))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe.RightOutlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe3.Inlet))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe.LeftOutlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe1.Inlet))).Should().Be(1);
        }
예제 #4
0
        public void CreateGraphOfPipeSystem_OfPipesFormingABinaryTree_ReturnsAGraphOfABinaryTree()
        {
            // Arrange
            var twoOutletPipe1 = PipeBuilder.New.EitherOutletPipe <int>().Build();
            var twoOutletPipe2 = PipeBuilder.New.EitherOutletPipe <int>().Build();
            var twoOutletPipe3 = PipeBuilder.New.EitherOutletPipe <int>().Build();

            twoOutletPipe1.LeftOutlet.ConnectTo(twoOutletPipe2.Inlet);
            twoOutletPipe1.RightOutlet.ConnectTo(twoOutletPipe3.Inlet);

            // Act
            var graph = twoOutletPipe1.CreateGraphOfPipeSystem();

            // Assert
            graph.Vertices.Count().Should().Be(12);
            graph.Vertices.Should().Contain(new[]
            {
                PipeGraphVertex.Create(twoOutletPipe1),
                PipeGraphVertex.Create(twoOutletPipe1.Inlet),
                PipeGraphVertex.Create(twoOutletPipe1.LeftOutlet),
                PipeGraphVertex.Create(twoOutletPipe1.RightOutlet),
                PipeGraphVertex.Create(twoOutletPipe2),
                PipeGraphVertex.Create(twoOutletPipe2.Inlet),
                PipeGraphVertex.Create(twoOutletPipe2.LeftOutlet),
                PipeGraphVertex.Create(twoOutletPipe2.RightOutlet),
                PipeGraphVertex.Create(twoOutletPipe3),
                PipeGraphVertex.Create(twoOutletPipe3.Inlet),
                PipeGraphVertex.Create(twoOutletPipe3.LeftOutlet),
                PipeGraphVertex.Create(twoOutletPipe3.RightOutlet)
            });

            graph.EdgeCount.Should().Be(11);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe1.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe1))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe1)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe1.LeftOutlet))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe1)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe1.RightOutlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe1.LeftOutlet)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe2.Inlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe2.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe2))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe2)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe2.LeftOutlet))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe2)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe2.RightOutlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe1.RightOutlet)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe3.Inlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe3.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe3))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe3)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe3.LeftOutlet))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe3)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe3.RightOutlet))).Should().Be(1);
        }
예제 #5
0
        private static IReadOnlyCollection<PipeGraphVertex> GetVerticesYouSendMessagesTo(PipeGraphVertex vertex)
        {
            if (vertex.IsPipe)
            {
                var pipe = vertex.GetPipe();
                return pipe.ConnectableOutlets.Select(PipeGraphVertex.Create).ToList();
            }

            if (vertex.IsInlet)
            {
                var inlet = vertex.GetInlet();
                return new List<PipeGraphVertex> {PipeGraphVertex.Create(inlet.Pipe)};
            }

            var outlet = vertex.GetOutlet();
            return outlet.TypelessConnectedInlet != null ?
                new List<PipeGraphVertex> {PipeGraphVertex.Create(outlet.TypelessConnectedInlet)} :
                new List<PipeGraphVertex>();
        }
예제 #6
0
        public void CreateGraphOfPipeSystem_GivenPipesConnectedInALine_ReturnsAGraphWithVerticesConnectedInALine()
        {
            // Arrange
            var basicPipe1 = PipeBuilder.New.BasicPipe <int>().Build();
            var basicPipe2 = PipeBuilder.New.BasicPipe <int>().Build();
            var basicPipe3 = PipeBuilder.New.BasicPipe <int>().Build();

            basicPipe1.Outlet.ConnectTo(basicPipe2.Inlet);
            basicPipe2.Outlet.ConnectTo(basicPipe3.Inlet);

            // Act
            var graph = basicPipe1.CreateGraphOfPipeSystem();

            // Assert
            graph.Vertices.Count().Should().Be(9);
            graph.Vertices.Should().Contain(new[]
            {
                PipeGraphVertex.Create(basicPipe1),
                PipeGraphVertex.Create(basicPipe1.Inlet),
                PipeGraphVertex.Create(basicPipe1.Outlet),
                PipeGraphVertex.Create(basicPipe2),
                PipeGraphVertex.Create(basicPipe2.Inlet),
                PipeGraphVertex.Create(basicPipe2.Outlet),
                PipeGraphVertex.Create(basicPipe3),
                PipeGraphVertex.Create(basicPipe3.Inlet),
                PipeGraphVertex.Create(basicPipe3.Outlet)
            });

            graph.EdgeCount.Should().Be(8);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe1.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe1))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe1)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe1.Outlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe1.Outlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe2.Inlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe2.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe2))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe2)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe2.Outlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe2.Outlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe3.Inlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe3.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe3))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe3)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe3.Outlet))).Should().Be(1);
        }
예제 #7
0
        public void CreateGraphOfPipeSystem_GivenASinglePipe_ReturnsAGraphRepresentingThatPipe()
        {
            // Arrange
            var basicPipe = PipeBuilder.New.BasicPipe <int>().Build();

            // Act
            var graph = basicPipe.CreateGraphOfPipeSystem();

            // Assert
            graph.VertexCount.Should().Be(3);
            graph.Vertices.Should().Contain(new[]
            {
                PipeGraphVertex.Create(basicPipe),
                PipeGraphVertex.Create(basicPipe.Inlet),
                PipeGraphVertex.Create(basicPipe.Outlet)
            });

            graph.EdgeCount.Should().Be(2);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(basicPipe)) && e.Target.Equals(PipeGraphVertex.Create(basicPipe.Outlet))).Should().Be(1);
        }
예제 #8
0
        public void CreateGraphOfPipeSystem_GivenATwoOutletPipeDoublyConnectedToATwoInletPipe_ReturnsTheCorrectGraph()
        {
            // Arrange
            var twoOutletPipe = PipeBuilder.New.EitherOutletPipe <int>().Build();
            var twoInletPipe  = PipeBuilder.New.EitherInletPipe <int>().Build();

            twoOutletPipe.LeftOutlet.ConnectTo(twoInletPipe.LeftInlet);
            twoOutletPipe.RightOutlet.ConnectedInlet = twoInletPipe.RightInlet;
            twoInletPipe.RightInlet.ConnectedOutlet  = twoOutletPipe.RightOutlet;

            // Act
            var graph = twoOutletPipe.CreateGraphOfPipeSystem();

            // Assert
            graph.Vertices.Count().Should().Be(8);
            graph.Vertices.Should().Contain(new[]
            {
                PipeGraphVertex.Create(twoOutletPipe),
                PipeGraphVertex.Create(twoOutletPipe.Inlet),
                PipeGraphVertex.Create(twoOutletPipe.LeftOutlet),
                PipeGraphVertex.Create(twoOutletPipe.RightOutlet),
                PipeGraphVertex.Create(twoInletPipe),
                PipeGraphVertex.Create(twoInletPipe.LeftInlet),
                PipeGraphVertex.Create(twoInletPipe.RightInlet),
                PipeGraphVertex.Create(twoInletPipe.Outlet)
            });

            graph.EdgeCount.Should().Be(8);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe.Inlet)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe.LeftOutlet))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe)) && e.Target.Equals(PipeGraphVertex.Create(twoOutletPipe.RightOutlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoInletPipe.LeftInlet)) && e.Target.Equals(PipeGraphVertex.Create(twoInletPipe))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoInletPipe.RightInlet)) && e.Target.Equals(PipeGraphVertex.Create(twoInletPipe))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoInletPipe)) && e.Target.Equals(PipeGraphVertex.Create(twoInletPipe.Outlet))).Should().Be(1);

            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe.LeftOutlet)) && e.Target.Equals(PipeGraphVertex.Create(twoInletPipe.LeftInlet))).Should().Be(1);
            graph.Edges.Count(e => e.Source.Equals(PipeGraphVertex.Create(twoOutletPipe.RightOutlet)) && e.Target.Equals(PipeGraphVertex.Create(twoInletPipe.RightInlet))).Should().Be(1);
        }
예제 #9
0
        private static IReadOnlyCollection <PipeGraphVertex> GetVerticesYouReceiveMessagesFrom(PipeGraphVertex vertex)
        {
            if (vertex.IsPipe)
            {
                var pipe = vertex.GetPipe();
                return(pipe.ConnectableInlets.Select(PipeGraphVertex.Create).ToList());
            }

            if (vertex.IsOutlet)
            {
                var outlet = vertex.GetOutlet();
                return(new List <PipeGraphVertex> {
                    PipeGraphVertex.Create(outlet.Pipe)
                });
            }

            var inlet = vertex.GetInlet();

            return(inlet.TypelessConnectedOutlet != null ?
                   new List <PipeGraphVertex> {
                PipeGraphVertex.Create(inlet.TypelessConnectedOutlet)
            } :
                   new List <PipeGraphVertex>());
        }
예제 #10
0
        private static IVertexAndEdgeListGraph <PipeGraphVertex, Edge <PipeGraphVertex> > CreateGraphOfPipeSystem(PipeGraphVertex firstVertex)
        {
            var graph           = new AdjacencyGraph <PipeGraphVertex, Edge <PipeGraphVertex> >(false);
            var verticesSeen    = new HashSet <PipeGraphVertex>();
            var verticesToCheck = new Stack <PipeGraphVertex>();

            verticesToCheck.Push(firstVertex);

            while (verticesToCheck.Any())
            {
                var vertexToCheck = verticesToCheck.Pop();
                if (verticesSeen.Contains(vertexToCheck))
                {
                    continue;
                }

                var sendsTo      = GetVerticesYouSendMessagesTo(vertexToCheck);
                var receivesFrom = GetVerticesYouReceiveMessagesFrom(vertexToCheck);

                foreach (var vertex in sendsTo)
                {
                    verticesToCheck.Push(vertex);
                }
                foreach (var vertex in receivesFrom)
                {
                    verticesToCheck.Push(vertex);
                }

                graph.AddVertex(vertexToCheck);

                graph.AddVerticesAndEdgeRange(sendsTo.Select(v => new Edge <PipeGraphVertex>(vertexToCheck, v)));
                graph.AddVerticesAndEdgeRange(receivesFrom.Select(v => new Edge <PipeGraphVertex>(v, vertexToCheck)));

                verticesSeen.Add(vertexToCheck);
            }

            return(graph);
        }
예제 #11
0
 public static IVertexAndEdgeListGraph <PipeGraphVertex, Edge <PipeGraphVertex> > CreateGraphOfPipeSystem(this IOutlet oneOutletInThePipeSystem)
 {
     return(CreateGraphOfPipeSystem(PipeGraphVertex.Create(oneOutletInThePipeSystem)));
 }