public void TestAddingDuplicateEdge()
        {
            var graph = new MemoryGraph<LiveEdge>();

            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = 1
            }, null);

            // should overwrite.
            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = 2
            }, null);

            var edges = graph.GetEdges(vertex1, vertex2).ToKeyValuePairs();
            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(2, edges[0].Value.Tags);

            // should overwrite again.
            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = 1
            }, null);

            edges = graph.GetEdges(vertex1, vertex2).ToKeyValuePairs();
            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(1, edges[0].Value.Tags);
        }
        public void TestLiveEdgeDynamicGraphAddRemove1()
        {
            uint tagsId = 10;
            var graph = new MemoryGraph<LiveEdge>();
            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = tagsId
            }, null);

            // test forward edge.
            var arcs = graph.GetEdges(vertex1).ToKeyValuePairs();
            Assert.AreEqual(1, arcs.Length);
            Assert.AreEqual(tagsId, arcs[0].Value.Tags);
            Assert.AreEqual(vertex2, arcs[0].Key);
            Assert.AreEqual(true, arcs[0].Value.Forward);

            // remove edge again.
            graph.RemoveEdge(vertex1, vertex2);

            // check if the edge is gone.
            arcs = graph.GetEdges(vertex1).ToKeyValuePairs();
            Assert.AreEqual(0, arcs.Length);
        }
        public void TestLiveEdgeDynamicGraphAddReverse()
        {
            var graph = new MemoryGraph <LiveEdge>();

            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 1
            }, null);

            graph.AddEdge(vertex2, vertex1, new LiveEdge()
            {
                Forward = true,
                Tags    = 2
            }, null);

            LiveEdge edge;

            Assert.IsTrue(graph.GetEdge(vertex1, vertex2, out edge));
            Assert.AreEqual(2, edge.Tags);
            Assert.IsTrue(graph.GetEdge(vertex2, vertex1, out edge));
            Assert.AreEqual(2, edge.Tags);
        }
        public void TestLiveEdgeDynamicGraphAddRemove1()
        {
            uint tagsId  = 10;
            var  graph   = new MemoryGraph <LiveEdge>();
            var  vertex1 = graph.AddVertex(51, 1);
            var  vertex2 = graph.AddVertex(51, 2);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = tagsId
            }, null);

            // test forward edge.
            var arcs = graph.GetEdges(vertex1).ToKeyValuePairs();

            Assert.AreEqual(1, arcs.Length);
            Assert.AreEqual(tagsId, arcs[0].Value.Tags);
            Assert.AreEqual(vertex2, arcs[0].Key);
            Assert.AreEqual(true, arcs[0].Value.Forward);

            // remove edge again.
            graph.RemoveEdge(vertex1, vertex2);

            // check if the edge is gone.
            arcs = graph.GetEdges(vertex1).ToKeyValuePairs();
            Assert.AreEqual(0, arcs.Length);
        }
        public void TestSparseRemoval1()
        {
            // use one edge definition everywhere.
            var edge = new LiveEdge();
            edge.Forward = true;
            edge.Tags = 1;

            var graph = new MemoryGraph<LiveEdge>();
            uint vertex1 = graph.AddVertex(0, 0);
            uint vertex2 = graph.AddVertex(1, 1);
            uint vertex3 = graph.AddVertex(2, 2);

            graph.AddEdge(vertex1, vertex2, edge, null);
            graph.AddEdge(vertex2, vertex1, edge, null);
            graph.AddEdge(vertex2, vertex3, edge, null);
            graph.AddEdge(vertex3, vertex2, edge, null);

            // execute pre-processor.
            var preProcessor = new LiveEdgePreprocessor(graph);
            preProcessor.Start();

            // test resulting graph.
            Assert.AreEqual(2, graph.VertexCount);
            Assert.AreEqual(1, graph.GetEdges(1).ToKeyValuePairs().Length);
            Assert.AreEqual(2, graph.GetEdges(1).ToKeyValuePairs()[0].Key);
            Assert.AreEqual(1, graph.GetEdges(2).ToKeyValuePairs().Length);
            Assert.AreEqual(1, graph.GetEdges(2).ToKeyValuePairs()[0].Key);
        }
Exemple #6
0
        public void TestSparseRemoval1()
        {
            // use one edge definition everywhere.
            var edge = new LiveEdge();

            edge.Forward = true;
            edge.Tags    = 1;

            var  graph   = new MemoryGraph <LiveEdge>();
            uint vertex1 = graph.AddVertex(0, 0);
            uint vertex2 = graph.AddVertex(1, 1);
            uint vertex3 = graph.AddVertex(2, 2);

            graph.AddEdge(vertex1, vertex2, edge, null);
            graph.AddEdge(vertex2, vertex1, edge, null);
            graph.AddEdge(vertex2, vertex3, edge, null);
            graph.AddEdge(vertex3, vertex2, edge, null);

            // execute pre-processor.
            var preProcessor = new LiveEdgePreprocessor(graph);

            preProcessor.Start();

            // test resulting graph.
            Assert.AreEqual(2, graph.VertexCount);
            Assert.AreEqual(1, graph.GetEdges(1).ToKeyValuePairs().Length);
            Assert.AreEqual(2, graph.GetEdges(1).ToKeyValuePairs()[0].Key);
            Assert.AreEqual(1, graph.GetEdges(2).ToKeyValuePairs().Length);
            Assert.AreEqual(1, graph.GetEdges(2).ToKeyValuePairs()[0].Key);
        }
        public void TestLiveEdgeDynamicGraphEdge1()
        {
            uint tagsId  = 10;
            var  graph   = new MemoryGraph <LiveEdge>();
            var  vertex1 = graph.AddVertex(51, 1);
            var  vertex2 = graph.AddVertex(51, 2);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = tagsId
            }, null);

            // test forward edge.
            var arcs = graph.GetEdges(vertex1).ToKeyValuePairs();

            Assert.AreEqual(1, arcs.Length);
            Assert.AreEqual(tagsId, arcs[0].Value.Tags);
            Assert.AreEqual(vertex2, arcs[0].Key);
            Assert.AreEqual(true, arcs[0].Value.Forward);

            // test backward edge: backward edge is added automatically.
            arcs = graph.GetEdges(vertex2).ToKeyValuePairs();
            Assert.AreEqual(1, arcs.Length);
            Assert.AreEqual(tagsId, arcs[0].Value.Tags);
            Assert.AreEqual(vertex1, arcs[0].Key);
            Assert.AreEqual(false, arcs[0].Value.Forward);

            // add a third vertex.
            var vertex3 = graph.AddVertex(51, 2);
            var edge    = new LiveEdge()
            {
                Forward = true,
                Tags    = tagsId
            };

            graph.AddEdge(vertex1, vertex3, edge, null);

            // test forward edges.
            arcs = graph.GetEdges(vertex1).ToKeyValuePairs();
            Assert.AreEqual(2, arcs.Length);
            Assert.AreEqual(tagsId, arcs[0].Value.Tags);
            Assert.AreEqual(vertex2, arcs[0].Key);
            Assert.AreEqual(true, arcs[0].Value.Forward);
            Assert.AreEqual(tagsId, arcs[1].Value.Tags);
            Assert.AreEqual(vertex3, arcs[1].Key);
            Assert.AreEqual(true, arcs[1].Value.Forward);

            // test backward edge: backward edge is added automatically.
            arcs = graph.GetEdges(vertex3).ToKeyValuePairs();
            Assert.AreEqual(1, arcs.Length);
            Assert.AreEqual(tagsId, arcs[0].Value.Tags);
            Assert.AreEqual(vertex1, arcs[0].Key);
            Assert.AreEqual(false, arcs[0].Value.Forward);
        }
        public void TestLiveEdgeDynamicGraphAddRemove2()
        {
            uint tagsId  = 10;
            var  graph   = new MemoryGraph <LiveEdge>();
            var  vertex1 = graph.AddVertex(51, 1);
            var  vertex2 = graph.AddVertex(51, 2);
            var  vertex3 = graph.AddVertex(51, 3);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = tagsId
            }, null);

            graph.AddEdge(vertex2, vertex3, new LiveEdge()
            {
                Forward = true,
                Tags    = tagsId
            }, null);

            // test edges.
            var edges = graph.GetEdges(vertex1).ToKeyValuePairs();

            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(tagsId, edges[0].Value.Tags);
            Assert.AreEqual(vertex2, edges[0].Key);
            Assert.AreEqual(true, edges[0].Value.Forward);
            edges = graph.GetEdges(vertex2).ToKeyValuePairs();
            Assert.AreEqual(2, edges.Length);
            edges = graph.GetEdges(vertex3).ToKeyValuePairs();
            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(tagsId, edges[0].Value.Tags);
            Assert.AreEqual(vertex2, edges[0].Key);
            Assert.AreEqual(false, edges[0].Value.Forward);

            // remove edge again.
            graph.RemoveEdge(vertex1, vertex2);

            // test edges.
            edges = graph.GetEdges(vertex1).ToKeyValuePairs();
            Assert.AreEqual(0, edges.Length);
            edges = graph.GetEdges(vertex2).ToKeyValuePairs();
            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(tagsId, edges[0].Value.Tags);
            Assert.AreEqual(vertex3, edges[0].Key);
            Assert.AreEqual(true, edges[0].Value.Forward);
            edges = graph.GetEdges(vertex3).ToKeyValuePairs();
            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(tagsId, edges[0].Value.Tags);
            Assert.AreEqual(vertex2, edges[0].Key);
            Assert.AreEqual(false, edges[0].Value.Forward);
        }
        public void TestLiveEdgeDynamicGraphAddRemove2()
        {
            uint tagsId = 10;
            var graph = new MemoryGraph<LiveEdge>();
            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);
            var vertex3 = graph.AddVertex(51, 3);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = tagsId
            }, null);

            graph.AddEdge(vertex2, vertex3, new LiveEdge()
            {
                Forward = true,
                Tags = tagsId
            }, null);

            // test edges.
            var edges = graph.GetEdges(vertex1).ToKeyValuePairs();
            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(tagsId, edges[0].Value.Tags);
            Assert.AreEqual(vertex2, edges[0].Key);
            Assert.AreEqual(true, edges[0].Value.Forward);
            edges = graph.GetEdges(vertex2).ToKeyValuePairs();
            Assert.AreEqual(2, edges.Length);
            edges = graph.GetEdges(vertex3).ToKeyValuePairs();
            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(tagsId, edges[0].Value.Tags);
            Assert.AreEqual(vertex2, edges[0].Key);
            Assert.AreEqual(false, edges[0].Value.Forward);

            // remove edge again.
            graph.RemoveEdge(vertex1, vertex2);

            // test edges.
            edges = graph.GetEdges(vertex1).ToKeyValuePairs();
            Assert.AreEqual(0, edges.Length);
            edges = graph.GetEdges(vertex2).ToKeyValuePairs();
            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(tagsId, edges[0].Value.Tags);
            Assert.AreEqual(vertex3, edges[0].Key);
            Assert.AreEqual(true, edges[0].Value.Forward);
            edges = graph.GetEdges(vertex3).ToKeyValuePairs();
            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(tagsId, edges[0].Value.Tags);
            Assert.AreEqual(vertex2, edges[0].Key);
            Assert.AreEqual(false, edges[0].Value.Forward);
        }
        public void TestLiveEdgeDynamicGraphCompressVertices()
        {
            var graph = new MemoryGraph <LiveEdge>();

            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);
            var vertex3 = graph.AddVertex(51, 3);
            var vertex4 = graph.AddVertex(51, 3);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 1
            }, null);
            graph.AddEdge(vertex2, vertex3, new LiveEdge()
            {
                Forward = true,
                Tags    = 2
            }, null);
            graph.AddEdge(vertex3, vertex4, new LiveEdge()
            {
                Forward = true,
                Tags    = 3
            }, null);

            graph.AddEdge(vertex4, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 4
            }, null);

            // make vertex4 obsolete.
            graph.RemoveEdges(vertex4);

            graph.Compress();

            Assert.AreEqual(3, graph.VertexCount);

            Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
            Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 2);
            Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
        }
        public void TestLiveEdgeDynamicGraphRemoveBegin()
        {
            var graph = new MemoryGraph <LiveEdge>();

            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);
            var vertex3 = graph.AddVertex(51, 3);
            var vertex4 = graph.AddVertex(51, 3);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 1
            }, null);
            graph.AddEdge(vertex2, vertex3, new LiveEdge()
            {
                Forward = true,
                Tags    = 2
            }, null);
            graph.AddEdge(vertex3, vertex4, new LiveEdge()
            {
                Forward = true,
                Tags    = 3
            }, null);

            graph.AddEdge(vertex4, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 4
            }, null);

            graph.RemoveEdge(vertex2, vertex1);
            Assert.IsFalse(graph.ContainsEdges(vertex2, vertex1));
            Assert.IsFalse(graph.ContainsEdges(vertex1, vertex2));

            Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 0);
            Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 2);
            Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 2);
            Assert.AreEqual(graph.GetEdges(vertex4).ToKeyValuePairs().Length, 2);
        }
        public void TestLiveEdgeDynamicGraphRemoveAll()
        {
            var graph = new MemoryGraph <LiveEdge>();

            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);
            var vertex3 = graph.AddVertex(51, 3);
            var vertex4 = graph.AddVertex(51, 3);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 1
            }, null);
            graph.AddEdge(vertex2, vertex3, new LiveEdge()
            {
                Forward = true,
                Tags    = 2
            }, null);
            graph.AddEdge(vertex3, vertex4, new LiveEdge()
            {
                Forward = true,
                Tags    = 3
            }, null);

            graph.AddEdge(vertex4, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 4
            }, null);

            graph.RemoveEdge(vertex2, vertex1);
            graph.RemoveEdge(vertex2, vertex3);
            graph.RemoveEdge(vertex4, vertex3);
            graph.RemoveEdge(vertex4, vertex2);
            Assert.IsFalse(graph.ContainsEdges(vertex2, vertex1));
            Assert.IsFalse(graph.ContainsEdges(vertex2, vertex3));
            Assert.IsFalse(graph.ContainsEdges(vertex4, vertex3));
            Assert.IsFalse(graph.ContainsEdges(vertex4, vertex2));
        }
        public void TestAddingDuplicateEdge()
        {
            var graph = new MemoryGraph <LiveEdge>();

            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 1
            }, null);

            // should overwrite.
            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 2
            }, null);

            var edges = graph.GetEdges(vertex1, vertex2).ToKeyValuePairs();

            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(2, edges[0].Value.Tags);

            // should overwrite again.
            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 1
            }, null);

            edges = graph.GetEdges(vertex1, vertex2).ToKeyValuePairs();
            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(1, edges[0].Value.Tags);
        }
Exemple #14
0
        public void TestSparseRemoval2()
        {
            // use one edge definition everywhere.
            var edge = new LiveEdge();

            edge.Forward = true;
            edge.Tags    = 1;

            var  graph   = new MemoryGraph <LiveEdge>();
            uint vertex1 = graph.AddVertex(0, 0);
            uint vertex2 = graph.AddVertex(1, 1);
            uint vertex3 = graph.AddVertex(2, 2);
            uint vertex4 = graph.AddVertex(3, 3);
            uint vertex5 = graph.AddVertex(4, 4);
            uint vertex6 = graph.AddVertex(5, 5);

            graph.AddEdge(vertex1, vertex2, edge, null); // 1 <-> 2
            graph.AddEdge(vertex2, vertex1, edge, null); // 1 <-> 2
            graph.AddEdge(vertex2, vertex3, edge, null); // 2 <-> 3
            graph.AddEdge(vertex3, vertex2, edge, null); // 2 <-> 3
            graph.AddEdge(vertex3, vertex4, edge, null); // 3 <-> 4
            graph.AddEdge(vertex4, vertex3, edge, null); // 3 <-> 4
            graph.AddEdge(vertex4, vertex5, edge, null); // 4 <-> 5
            graph.AddEdge(vertex5, vertex4, edge, null); // 4 <-> 5
            graph.AddEdge(vertex3, vertex6, edge, null); // 3 <-> 6
            graph.AddEdge(vertex6, vertex3, edge, null); // 3 <-> 6

            // execute pre-processor.
            var preProcessor = new LiveEdgePreprocessor(graph);

            preProcessor.Start();

            // test resulting graph.
            Assert.AreEqual(4, graph.VertexCount);

            Assert.AreEqual(1, graph.GetEdges(1).ToKeyValuePairs().Length);
            Assert.AreEqual(2, graph.GetEdges(1).ToKeyValuePairs()[0].Key);

            Assert.AreEqual(3, graph.GetEdges(2).ToKeyValuePairs().Length);
            Assert.IsTrue(graph.GetEdges(2).ToKeyValuePairs().Any(x => x.Key == 1));
            Assert.IsTrue(graph.GetEdges(2).ToKeyValuePairs().Any(x => x.Key == 3));
            Assert.IsTrue(graph.GetEdges(2).ToKeyValuePairs().Any(x => x.Key == 4));

            Assert.AreEqual(1, graph.GetEdges(3).ToKeyValuePairs().Length);
            Assert.AreEqual(2, graph.GetEdges(3).ToKeyValuePairs()[0].Key);

            Assert.AreEqual(1, graph.GetEdges(4).ToKeyValuePairs().Length);
            Assert.AreEqual(2, graph.GetEdges(4).ToKeyValuePairs()[0].Key);
        }
        public void TestLiveEdgeDynamicGraphEdge1()
        {
            uint tagsId = 10;
            var graph = new MemoryGraph<LiveEdge>();
            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = tagsId
            }, null);

            // test forward edge.
            var arcs = graph.GetEdges(vertex1).ToKeyValuePairs();
            Assert.AreEqual(1, arcs.Length);
            Assert.AreEqual(tagsId, arcs[0].Value.Tags);
            Assert.AreEqual(vertex2, arcs[0].Key);
            Assert.AreEqual(true, arcs[0].Value.Forward);

            // test backward edge: backward edge is added automatically.
            arcs = graph.GetEdges(vertex2).ToKeyValuePairs();
            Assert.AreEqual(1, arcs.Length);
            Assert.AreEqual(tagsId, arcs[0].Value.Tags);
            Assert.AreEqual(vertex1, arcs[0].Key);
            Assert.AreEqual(false, arcs[0].Value.Forward);

            // add a third vertex.
            var vertex3 = graph.AddVertex(51, 2);
            var edge = new LiveEdge()
            {
                Forward = true,
                Tags = tagsId
            };
            graph.AddEdge(vertex1, vertex3, edge, null);

            // test forward edges.
            arcs = graph.GetEdges(vertex1).ToKeyValuePairs();
            Assert.AreEqual(2, arcs.Length);
            Assert.AreEqual(tagsId, arcs[0].Value.Tags);
            Assert.AreEqual(vertex2, arcs[0].Key);
            Assert.AreEqual(true, arcs[0].Value.Forward);
            Assert.AreEqual(tagsId, arcs[1].Value.Tags);
            Assert.AreEqual(vertex3, arcs[1].Key);
            Assert.AreEqual(true, arcs[1].Value.Forward);

            // test backward edge: backward edge is added automatically.
            arcs = graph.GetEdges(vertex3).ToKeyValuePairs();
            Assert.AreEqual(1, arcs.Length);
            Assert.AreEqual(tagsId, arcs[0].Value.Tags);
            Assert.AreEqual(vertex1, arcs[0].Key);
            Assert.AreEqual(false, arcs[0].Value.Forward);
        }
        public void TestLiveEdgeDynamicGraphCompressEdges()
        {
            var graph = new MemoryGraph <LiveEdge>();

            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);
            var vertex3 = graph.AddVertex(51, 3);
            var vertex4 = graph.AddVertex(51, 3);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 1
            }, null);
            graph.AddEdge(vertex2, vertex3, new LiveEdge()
            {
                Forward = true,
                Tags    = 2
            }, null);
            graph.AddEdge(vertex3, vertex4, new LiveEdge()
            {
                Forward = true,
                Tags    = 3
            }, null);

            graph.AddEdge(vertex4, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 4
            }, null);

            graph.RemoveEdge(vertex2, vertex3);

            graph.Compress();

            Assert.IsFalse(graph.ContainsEdges(vertex2, vertex3));
            Assert.IsFalse(graph.ContainsEdges(vertex3, vertex2));

            Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
            Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 2);
            Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
            Assert.AreEqual(graph.GetEdges(vertex4).ToKeyValuePairs().Length, 2);


            graph = new MemoryGraph <LiveEdge>();

            vertex1 = graph.AddVertex(51, 1);
            vertex2 = graph.AddVertex(51, 2);
            vertex3 = graph.AddVertex(51, 3);
            vertex4 = graph.AddVertex(51, 3);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 1
            }, null);
            graph.AddEdge(vertex2, vertex3, new LiveEdge()
            {
                Forward = true,
                Tags    = 2
            }, null);
            graph.AddEdge(vertex3, vertex4, new LiveEdge()
            {
                Forward = true,
                Tags    = 3
            }, null);

            graph.AddEdge(vertex4, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags    = 4
            }, null);

            graph.RemoveEdge(vertex3, vertex4);

            graph.Compress();

            Assert.IsFalse(graph.ContainsEdges(vertex3, vertex4));
            Assert.IsFalse(graph.ContainsEdges(vertex4, vertex3));

            Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
            Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 3);
            Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
            Assert.AreEqual(graph.GetEdges(vertex4).ToKeyValuePairs().Length, 1);

            LiveEdge edge;

            Assert.IsTrue(graph.GetEdge(vertex1, vertex2, out edge));
            Assert.AreEqual(1, edge.Tags);
            Assert.IsTrue(graph.GetEdge(vertex2, vertex3, out edge));
            Assert.AreEqual(2, edge.Tags);
            Assert.IsTrue(graph.GetEdge(vertex4, vertex2, out edge));
            Assert.AreEqual(4, edge.Tags);
        }
        public void TestSparseRemoval1Routing()
        {
            // use one edge definition everywhere.
            var tagsIndex = new TagsTableCollectionIndex();
            var tags = new TagsCollection(new Tag("highway","residential"));
            var edge = new LiveEdge();
            edge.Forward = true;
            edge.Tags = tagsIndex.Add(tags);

            var graph = new MemoryGraph<LiveEdge>();
            uint vertex1 = graph.AddVertex(51.267797f, 4.8013623f);
            uint vertex2 = graph.AddVertex(51.267702f, 4.8013396f);
            uint vertex3 = graph.AddVertex(51.267592f, 4.8013024f);

            graph.AddEdge(vertex1, vertex2, edge, null);
            graph.AddEdge(vertex2, vertex3, edge, null);
            graph.AddEdge(vertex3, vertex2, edge, null);

            // save vertex coordinates for later use.
            float latitude, longitude;
            graph.GetVertex(vertex1, out latitude, out longitude);
            var vertex1Coordinate = new GeoCoordinate(latitude, longitude);
            graph.GetVertex(vertex2, out latitude, out longitude);
            var vertex2Coordinate = new GeoCoordinate(latitude, longitude);
            graph.GetVertex(vertex3, out latitude, out longitude);
            var vertex3Coordinate = new GeoCoordinate(latitude, longitude);

            // execute pre-processor.
            var preProcessor = new LiveEdgePreprocessor(graph);
            preProcessor.Start();

            // create router.
            var source = new DynamicGraphRouterDataSource<LiveEdge>(
                graph, tagsIndex);
            var router = Router.CreateLiveFrom(source, new OsmRoutingInterpreter());

            // test some basic routing requests.
            // 1 -> 3: 1 -> 2 -> 3.
            var resolved1 = router.Resolve(Vehicle.Car, vertex1Coordinate);
            var resolved3 = router.Resolve(Vehicle.Car, vertex3Coordinate);
            var route = router.Calculate(Vehicle.Car, resolved1, resolved3);

            // verify the simple route result.
            Assert.IsNotNull(route);
            Assert.AreEqual(3, route.Segments.Length);
            Assert.AreEqual(vertex1Coordinate.Latitude, route.Segments[0].Latitude);
            Assert.AreEqual(vertex1Coordinate.Longitude, route.Segments[0].Longitude);
            Assert.AreEqual(vertex2Coordinate.Latitude, route.Segments[1].Latitude);
            Assert.AreEqual(vertex2Coordinate.Longitude, route.Segments[1].Longitude);
            Assert.AreEqual(vertex3Coordinate.Latitude, route.Segments[2].Latitude);
            Assert.AreEqual(vertex3Coordinate.Longitude, route.Segments[2].Longitude);

            // 1 -> 2: 1 -> 2.
            router = Router.CreateLiveFrom(source, new OsmRoutingInterpreter());
            resolved1 = router.Resolve(Vehicle.Car, vertex1Coordinate);
            var resolved2 = router.Resolve(Vehicle.Car, vertex2Coordinate);
            route = router.Calculate(Vehicle.Car, resolved1, resolved2);

            // verify the simple route result.
            Assert.IsNotNull(route);
            Assert.AreEqual(2, route.Segments.Length);
            Assert.AreEqual(vertex1Coordinate.Latitude, route.Segments[0].Latitude);
            Assert.AreEqual(vertex1Coordinate.Longitude, route.Segments[0].Longitude);
            Assert.AreEqual(vertex2Coordinate.Latitude, route.Segments[1].Latitude);
            Assert.AreEqual(vertex2Coordinate.Longitude, route.Segments[1].Longitude);
        }
        public void TestSparseRemoval2()
        {
            // use one edge definition everywhere.
            var edge = new LiveEdge();
            edge.Forward = true;
            edge.Tags = 1;

            var graph = new MemoryGraph<LiveEdge>();
            uint vertex1 = graph.AddVertex(0, 0);
            uint vertex2 = graph.AddVertex(1, 1);
            uint vertex3 = graph.AddVertex(2, 2);
            uint vertex4 = graph.AddVertex(3, 3);
            uint vertex5 = graph.AddVertex(4, 4);
            uint vertex6 = graph.AddVertex(5, 5);

            graph.AddEdge(vertex1, vertex2, edge, null); // 1 <-> 2
            graph.AddEdge(vertex2, vertex1, edge, null); // 1 <-> 2
            graph.AddEdge(vertex2, vertex3, edge, null); // 2 <-> 3
            graph.AddEdge(vertex3, vertex2, edge, null); // 2 <-> 3
            graph.AddEdge(vertex3, vertex4, edge, null); // 3 <-> 4
            graph.AddEdge(vertex4, vertex3, edge, null); // 3 <-> 4
            graph.AddEdge(vertex4, vertex5, edge, null); // 4 <-> 5
            graph.AddEdge(vertex5, vertex4, edge, null); // 4 <-> 5
            graph.AddEdge(vertex3, vertex6, edge, null); // 3 <-> 6
            graph.AddEdge(vertex6, vertex3, edge, null); // 3 <-> 6

            // execute pre-processor.
            var preProcessor = new LiveEdgePreprocessor(graph);
            preProcessor.Start();

            // test resulting graph.
            Assert.AreEqual(4, graph.VertexCount);

            Assert.AreEqual(1, graph.GetEdges(1).ToKeyValuePairs().Length);
            Assert.AreEqual(2, graph.GetEdges(1).ToKeyValuePairs()[0].Key);

            Assert.AreEqual(3, graph.GetEdges(2).ToKeyValuePairs().Length);
            Assert.IsTrue(graph.GetEdges(2).ToKeyValuePairs().Any(x => x.Key == 1));
            Assert.IsTrue(graph.GetEdges(2).ToKeyValuePairs().Any(x => x.Key == 3));
            Assert.IsTrue(graph.GetEdges(2).ToKeyValuePairs().Any(x => x.Key == 4));

            Assert.AreEqual(1, graph.GetEdges(3).ToKeyValuePairs().Length);
            Assert.AreEqual(2, graph.GetEdges(3).ToKeyValuePairs()[0].Key);

            Assert.AreEqual(1, graph.GetEdges(4).ToKeyValuePairs().Length);
            Assert.AreEqual(2, graph.GetEdges(4).ToKeyValuePairs()[0].Key);
        }
        public void TestLiveEdgeDynamicGraphAddReverse()
        {
            var graph = new MemoryGraph<LiveEdge>();

            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = 1
            }, null);

            graph.AddEdge(vertex2, vertex1, new LiveEdge()
            {
                Forward = true,
                Tags = 2
            }, null);

            LiveEdge edge;
            Assert.IsTrue(graph.GetEdge(vertex1, vertex2, out edge));
            Assert.AreEqual(2, edge.Tags);
            Assert.IsTrue(graph.GetEdge(vertex2, vertex1, out edge));
            Assert.AreEqual(2, edge.Tags);
        }
        public void TestLiveEdgeDynamicGraphCompressVertices()
        {
            var graph = new MemoryGraph<LiveEdge>();

            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);
            var vertex3 = graph.AddVertex(51, 3);
            var vertex4 = graph.AddVertex(51, 3);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = 1
            }, null);
            graph.AddEdge(vertex2, vertex3, new LiveEdge()
            {
                Forward = true,
                Tags = 2
            }, null);
            graph.AddEdge(vertex3, vertex4, new LiveEdge()
            {
                Forward = true,
                Tags = 3
            }, null);

            graph.AddEdge(vertex4, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = 4
            }, null);

            // make vertex4 obsolete.
            graph.RemoveEdges(vertex4);

            graph.Compress();

            Assert.AreEqual(3, graph.VertexCount);

            Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
            Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 2);
            Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
        }
        public void TestLiveEdgeDynamicGraphRemoveMiddle()
        {
            var graph = new MemoryGraph<LiveEdge>();

            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);
            var vertex3 = graph.AddVertex(51, 3);
            var vertex4 = graph.AddVertex(51, 3);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = 1
            }, null);
            graph.AddEdge(vertex2, vertex3, new LiveEdge()
            {
                Forward = true,
                Tags = 2
            }, null);
            graph.AddEdge(vertex3, vertex4, new LiveEdge()
            {
                Forward = true,
                Tags = 3
            }, null);

            graph.AddEdge(vertex4, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = 4
            }, null);

            graph.RemoveEdge(vertex2, vertex3);
            Assert.IsFalse(graph.ContainsEdge(vertex2, vertex3));
            Assert.IsFalse(graph.ContainsEdge(vertex3, vertex2));

            Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
            Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 2);
            Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
            Assert.AreEqual(graph.GetEdges(vertex4).ToKeyValuePairs().Length, 2);
        }
        public void TestLiveEdgeDynamicGraphRemoveAllOneVertex()
        {
            var graph = new MemoryGraph<LiveEdge>();

            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);
            var vertex3 = graph.AddVertex(51, 3);
            var vertex4 = graph.AddVertex(51, 3);

            graph.AddEdge(vertex1, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = 1
            });
            graph.AddEdge(vertex2, vertex3, new LiveEdge()
            {
                Forward = true,
                Tags = 2
            });
            graph.AddEdge(vertex3, vertex4, new LiveEdge()
            {
                Forward = true,
                Tags = 3
            });

            graph.AddEdge(vertex4, vertex2, new LiveEdge()
            {
                Forward = true,
                Tags = 4
            });

            graph.RemoveEdges(vertex2);
            Assert.IsFalse(graph.ContainsEdge(vertex2, vertex1));
            Assert.IsFalse(graph.ContainsEdge(vertex2, vertex3));
            Assert.IsFalse(graph.ContainsEdge(vertex4, vertex2));
            Assert.IsTrue(graph.ContainsEdge(vertex3, vertex4));
        }
Exemple #23
0
        public void TestSparseRemoval1Routing()
        {
            // use one edge definition everywhere.
            var tagsIndex = new TagsTableCollectionIndex();
            var tags      = new TagsCollection(new Tag("highway", "residential"));
            var edge      = new LiveEdge();

            edge.Forward = true;
            edge.Tags    = tagsIndex.Add(tags);

            var  graph   = new MemoryGraph <LiveEdge>();
            uint vertex1 = graph.AddVertex(51.267797f, 4.8013623f);
            uint vertex2 = graph.AddVertex(51.267702f, 4.8013396f);
            uint vertex3 = graph.AddVertex(51.267592f, 4.8013024f);

            graph.AddEdge(vertex1, vertex2, edge, null);
            graph.AddEdge(vertex2, vertex3, edge, null);
            graph.AddEdge(vertex3, vertex2, edge, null);

            // save vertex coordinates for later use.
            float latitude, longitude;

            graph.GetVertex(vertex1, out latitude, out longitude);
            var vertex1Coordinate = new GeoCoordinate(latitude, longitude);

            graph.GetVertex(vertex2, out latitude, out longitude);
            var vertex2Coordinate = new GeoCoordinate(latitude, longitude);

            graph.GetVertex(vertex3, out latitude, out longitude);
            var vertex3Coordinate = new GeoCoordinate(latitude, longitude);

            // execute pre-processor.
            var preProcessor = new LiveEdgePreprocessor(graph);

            preProcessor.Start();

            // create router.
            var source = new DynamicGraphRouterDataSource <LiveEdge>(
                graph, tagsIndex);
            var router = Router.CreateLiveFrom(source, new OsmRoutingInterpreter());

            // test some basic routing requests.
            // 1 -> 3: 1 -> 2 -> 3.
            var resolved1 = router.Resolve(Vehicle.Car, vertex1Coordinate);
            var resolved3 = router.Resolve(Vehicle.Car, vertex3Coordinate);
            var route     = router.Calculate(Vehicle.Car, resolved1, resolved3);

            // verify the simple route result.
            Assert.IsNotNull(route);
            Assert.AreEqual(3, route.Segments.Length);
            Assert.AreEqual(vertex1Coordinate.Latitude, route.Segments[0].Latitude);
            Assert.AreEqual(vertex1Coordinate.Longitude, route.Segments[0].Longitude);
            Assert.AreEqual(vertex2Coordinate.Latitude, route.Segments[1].Latitude);
            Assert.AreEqual(vertex2Coordinate.Longitude, route.Segments[1].Longitude);
            Assert.AreEqual(vertex3Coordinate.Latitude, route.Segments[2].Latitude);
            Assert.AreEqual(vertex3Coordinate.Longitude, route.Segments[2].Longitude);

            // 1 -> 2: 1 -> 2.
            router    = Router.CreateLiveFrom(source, new OsmRoutingInterpreter());
            resolved1 = router.Resolve(Vehicle.Car, vertex1Coordinate);
            var resolved2 = router.Resolve(Vehicle.Car, vertex2Coordinate);

            route = router.Calculate(Vehicle.Car, resolved1, resolved2);

            // verify the simple route result.
            Assert.IsNotNull(route);
            Assert.AreEqual(2, route.Segments.Length);
            Assert.AreEqual(vertex1Coordinate.Latitude, route.Segments[0].Latitude);
            Assert.AreEqual(vertex1Coordinate.Longitude, route.Segments[0].Longitude);
            Assert.AreEqual(vertex2Coordinate.Latitude, route.Segments[1].Latitude);
            Assert.AreEqual(vertex2Coordinate.Longitude, route.Segments[1].Longitude);
        }