public void TestRemoveEdgeDoesntRemoveNodes() { GraphData g = new GraphData("scope"); Node source = new Node("Source"); Node target = new Node("Target"); Edge edge = new Edge(source, target); g.AddNode(source); g.AddNode(target); g.AddEdge(edge); if (!g.ContainsEdge(edge)) { Assert.Inconclusive("Edge not found in the collection after adding it."); } g.RemoveEdge(edge); if (g.ContainsEdge(edge)) { Assert.Inconclusive("Edge still found in the collection after removing it."); } Assert.IsTrue(g.ContainsNode(source)); Assert.IsTrue(g.ContainsNode(target)); }
public void Verify_Edge_Added_To_Physical_Graph_Once_Corresponding_Target_Node_Added() { int actualSourceEdges; int actualTargetEdges; int expectedSourceEdges = 1; int expectedTargetEdges = 1; INode source = PrepareNode("Node1", ghostNode: false); INode target = PrepareNode("Node2", ghostNode: true); IEdge orphan = new Edge(source, target); GraphData graphData = new GraphData("scope"); // Add an edge with ghost nodes to the graph graphData.AddNode(source); graphData.AddEdge(orphan); // Add the missing node target = PrepareNode("Node2", ghostNode: false); graphData.AddNode(target); // Get the actual count for nodes actualSourceEdges = graphData.Edges(source).Count(); actualTargetEdges = graphData.Edges(target).Count(); Assert.AreEqual(expectedSourceEdges, actualSourceEdges); Assert.AreEqual(expectedTargetEdges, actualTargetEdges); }
public void TestAddingEdgeFiresCollectionChangedEvent() { bool eventFired = false; NotifyCollectionChangedAction action = NotifyCollectionChangedAction.Reset; GraphData g = new GraphData("scope"); Node source = new Node("Source"); Node target = new Node("Target"); Edge edge = new Edge(source, target); // TODO: This test is invalid because added Node objects to the collection will fire the same event g.CollectionChanged += (sender, e) => { action = e.Action; eventFired = true; }; EnqueueCallback(() => g.AddNode(source)); EnqueueCallback(() => g.AddNode(target)); EnqueueCallback(() => g.AddEdge(edge)); EnqueueConditional(() => eventFired); EnqueueCallback(() => Assert.IsTrue(eventFired)); EnqueueCallback(() => Assert.AreEqual <NotifyCollectionChangedAction>(NotifyCollectionChangedAction.Add, action)); EnqueueTestComplete(); }
//TODO: WE SHOULD HAVE EVENTS FOR WHEN NODE VIEWMODELS ARE REMOVED OR ADDED #region Edge View Models /// <summary> /// Creates a new EdgeViewModel /// </summary> /// <param name="edge">The IEdge instance that is responsible /// for the edge view model being created</param> private void CreateEdgeViewModel(IEdge edge) { IEdgeViewModel edgeViewModel; bool isNew = true; // Check if the edge view model already exists if (edgeToEdgeViewModel.ContainsKey(edge)) { isNew = false; } // Make sure that the edge exists in the graph data structure if (!graphData.ContainsEdge(edge)) { // Since the edge doesn't exist in the graph // data structure, we need to add it graphData.AddEdge(edge); } // Create a new edge viewmodel if (isNew) { edgeViewModel = EdgeViewModelBase.GetEdgeViewModel(edge, graphData.Scope); } else { edgeViewModel = edgeToEdgeViewModel[edge]; } // Get the source node view model INodeShape sourceNodeViewModel = null; INodeShape targetNodeViewModel = null; nodeToNodeViewModel.TryGetValue(edgeViewModel.ParentEdge.Source, out sourceNodeViewModel); nodeToNodeViewModel.TryGetValue(edgeViewModel.ParentEdge.Target, out targetNodeViewModel); if (sourceNodeViewModel != null) { edgeViewModel.X1 = sourceNodeViewModel.CenterPoint.X; edgeViewModel.Y2 = sourceNodeViewModel.CenterPoint.Y; } if (targetNodeViewModel != null) { edgeViewModel.X1 = targetNodeViewModel.CenterPoint.X; edgeViewModel.Y2 = targetNodeViewModel.CenterPoint.Y; } // If this is a new node, add it if (isNew) { // Add the edge to the edge/edgeviewmodel collection edgeToEdgeViewModel.Add(edge, edgeViewModel); // Fire the EdgeViewModelRemoved event OnEdgeViewModelAdded(new EdgeViewModelEventArgs(edgeViewModel, this.scope)); } }
public void TestMultipleSuccessors() { GraphData g = new GraphData("scope"); Node source = new Node("Source"); Node target = new Node("Target"); Node superTarget = new Node("SuperTarget"); Edge edge1 = new Edge(source, target); Edge edge2 = new Edge(target, superTarget); List <INode> predecessors; g.AddNode(source); g.AddNode(target); g.AddNode(superTarget); g.AddEdge(edge1); g.AddEdge(edge2); predecessors = new List <INode>(g.Successors(source)); Assert.IsTrue(predecessors.Contains(target)); }
public void TestAddEdge() { GraphData g = new GraphData("scope"); Node source = new Node("Source"); Node target = new Node("Target"); Edge edge = new Edge(source, target); g.AddNode(source); g.AddNode(target); g.AddEdge(edge); Assert.IsTrue(g.ContainsEdge(edge)); }
public void TestAddEdge() { GraphData g = new GraphData("scope"); Node source = new Node("Source"); Node target = new Node("Target"); Edge edge = new Edge(source, target); g.AddNode(source); g.AddNode(target); g.AddEdge(edge); Assert.IsTrue(g.ContainsEdge(edge)); }
public void TestOneSuccessor() { GraphData g = new GraphData("scope"); INode source = new Node("Source"); INode target = new Node("Target"); Edge edge = new Edge(source, target); g.AddNode(source); g.AddNode(target); g.AddEdge(edge); INode actual = g.Successors(source).First(); Assert.AreEqual <INode>(target, actual); }
public void Verify_Edge_Added_To_Physical_Graph_Once_All_Corresponding_Target_Nodes_Added() { int actualSourceEdges; int actualTargetEdges; int expectedSourceEdges = 1; int expectedTargetEdges = 1; INode temp1 = new Node("temp1"); // Hack because an exception is thrown if you attempt to add an edge to a graph if the graph doesn't have at least one node INode source = PrepareNode("Node1", ghostNode: true); INode target = PrepareNode("Node2", ghostNode: true); IEdge orphan = new Edge(source, target); IEdge actualSource; IEdge actualTarget; GraphData graphData = new GraphData("scope"); // Hack (see temp1 comment above) graphData.AddNode(temp1); // Add an edge with ghost nodes to the graph graphData.AddEdge(orphan); // Add the missing node source = PrepareNode("Node1", ghostNode: false); target = PrepareNode("Node2", ghostNode: false); graphData.AddNode(source); graphData.AddNode(target); // Get the actual count for nodes actualSourceEdges = graphData.Edges(source).Count(); actualTargetEdges = graphData.Edges(target).Count(); actualSource = graphData.Edges(source).FirstOrDefault(); actualTarget = graphData.Edges(target).FirstOrDefault(); Assert.AreEqual(expectedSourceEdges, actualSourceEdges); Assert.AreEqual(expectedTargetEdges, actualTargetEdges); Assert.AreEqual(orphan, actualSource); Assert.AreEqual(orphan, actualTarget); }
public void TestRemovingEdgeFiresCollectionChangedEvent() { SnaglEventAggregator eventAggregator = SnaglEventAggregator.DefaultInstance; GraphData g = new GraphData("scope"); Node source = new Node("Source"); Node target = new Node("Target"); Edge edge = new Edge(source, target); g.AddNode(source); g.AddNode(target); g.AddEdge(edge); // Don't replace this with a lambda eventAggregator.GetEvent <EdgeRemovedEvent>().Subscribe(OnEdgeRemoved); EnqueueCallback(() => g.RemoveEdge(edge)); EnqueueConditional(() => _eventFired); EnqueueCallback(() => Assert.IsTrue(_eventFired)); EnqueueCallback(() => Assert.AreEqual <IEdge>(edge, _affectedEdge)); EnqueueTestComplete(); }
public void Verify_Edge_Added_To_Physical_Graph_Once_All_Corresponding_Target_Nodes_Added() { int actualSourceEdges; int actualTargetEdges; int expectedSourceEdges = 1; int expectedTargetEdges = 1; INode temp1 = new Node("temp1"); // Hack because an exception is thrown if you attempt to add an edge to a graph if the graph doesn't have at least one node INode source = PrepareNode("Node1", ghostNode: true); INode target = PrepareNode("Node2", ghostNode: true); IEdge orphan = new Edge(source, target); IEdge actualSource; IEdge actualTarget; GraphData graphData = new GraphData("scope"); // Hack (see temp1 comment above) graphData.AddNode(temp1); // Add an edge with ghost nodes to the graph graphData.AddEdge(orphan); // Add the missing node source = PrepareNode("Node1", ghostNode: false); target = PrepareNode("Node2", ghostNode: false); graphData.AddNode(source); graphData.AddNode(target); // Get the actual count for nodes actualSourceEdges = graphData.Edges(source).Count(); actualTargetEdges = graphData.Edges(target).Count(); actualSource = graphData.Edges(source).FirstOrDefault(); actualTarget = graphData.Edges(target).FirstOrDefault(); Assert.AreEqual(expectedSourceEdges, actualSourceEdges); Assert.AreEqual(expectedTargetEdges, actualTargetEdges); Assert.AreEqual(orphan, actualSource); Assert.AreEqual(orphan, actualTarget); }
public void Verify_Edge_With_No_Ghost_Nodes_Is_Added_To_Physical_Graph() { int actualSourceEdges; int actualTargetEdges; int expectedSourceEdges = 1; int expectedTargetEdges = 1; INode source = PrepareNode("Node1", ghostNode: false); INode target = PrepareNode("Node2", ghostNode: false); IEdge edge = new Edge(source, target); GraphData graphData = new GraphData("scope"); // Add an edge without ghost nodes to the graph graphData.AddNode(source); graphData.AddNode(target); graphData.AddEdge(edge); // Get the actual count for nodes actualSourceEdges = graphData.Edges(source).Count(); actualTargetEdges = graphData.Edges(target).Count(); Assert.AreEqual(expectedSourceEdges, actualSourceEdges); Assert.AreEqual(expectedTargetEdges, actualTargetEdges); }
public void Verify_Edge_With_Two_Ghost_Nodes_Not_Added_To_Physical_Graph() { IEnumerable <IEdge> actualSourceEdges; IEnumerable <IEdge> actualTargetEdges; IEnumerable <IEdge> expectedSourceEdges = null; IEnumerable <IEdge> expectedTargetEdges = null; INode temp1 = new Node("temp1"); // Hack because an exception is thrown if you attempt to add an edge to a graph if the graph doesn't have at least one node INode source = PrepareNode("Node1", ghostNode: true); INode target = PrepareNode("Node2", ghostNode: true); IEdge orphan = new Edge(source, target); GraphData graphData = new GraphData("scope"); graphData.AddNode(temp1); // Hack (see temp1 comment above) // Add an edge with ghost nodes to the graph graphData.AddEdge(orphan); // Get the actual count for nodes actualSourceEdges = graphData.Edges(source); actualTargetEdges = graphData.Edges(target); Assert.AreEqual(expectedSourceEdges, actualSourceEdges); Assert.AreEqual(expectedTargetEdges, actualTargetEdges); }
private BidirectionalGraph <DataVertex, DataEdge> GenerateGraph(int[,] matrix) { var dataGraph = new GraphData(); List <string> colours = new List <string>() { "Gray", "AntiqueWhite", "Aquamarine", "CornflowerBlue", "BurlyWood", "Cyan", "DarkSeaGreen", "Crimson", "GreenYellow", "IndianRed", "LightBlue", "LightGreen", "LightPink", "LightSalmon", "Teal", "MistyRose", "PaleTurquoise", "RosyBrown", "Peru", "SpringGreen" }; for (int i = 0; i < G1.IncidenceMatrix.GetLength(0); i++) { var vertex = new DataVertex($"V:{i} K:{G1.Vertices[i].Color}"); vertex.Id = i; vertex.Color = colours[G1.Vertices[i].Color]; dataGraph.AddVertex(vertex); } var vlist = dataGraph.Vertices.ToList(); for (int i = 0; i < G1.IncidenceMatrix.GetLength(0); i++) { for (int j = 0; j < G1.IncidenceMatrix.GetLength(1); j++) { if ((G1.IncidenceMatrix[i, j] == G1.IncidenceMatrix[j, i]) && G1.IncidenceMatrix[i, j] == 1) { var edge = new DataEdge(vlist[i], vlist[j]); edge.Id = i; edge.Color = "Black"; dataGraph.AddEdge(edge); } } } Area.GenerateGraph(true, true); return(dataGraph); }
public void TestMultipleSuccessors() { GraphData g = new GraphData("scope"); Node source = new Node("Source"); Node target = new Node("Target"); Node superTarget = new Node("SuperTarget"); Edge edge1 = new Edge(source, target); Edge edge2 = new Edge(target, superTarget); List<INode> predecessors; g.AddNode(source); g.AddNode(target); g.AddNode(superTarget); g.AddEdge(edge1); g.AddEdge(edge2); predecessors = new List<INode>(g.Successors(source)); Assert.IsTrue(predecessors.Contains(target)); }
public void TestRemovingEdgeFiresCollectionChangedEvent() { SnaglEventAggregator eventAggregator = SnaglEventAggregator.DefaultInstance; GraphData g = new GraphData("scope"); Node source = new Node("Source"); Node target = new Node("Target"); Edge edge = new Edge(source, target); g.AddNode(source); g.AddNode(target); g.AddEdge(edge); // Don't replace this with a lambda eventAggregator.GetEvent<EdgeRemovedEvent>().Subscribe(OnEdgeRemoved); EnqueueCallback(() => g.RemoveEdge(edge)); EnqueueConditional(() => _eventFired); EnqueueCallback(() => Assert.IsTrue(_eventFired)); EnqueueCallback(() => Assert.AreEqual<IEdge>(edge, _affectedEdge)); EnqueueTestComplete(); }
public void TestRemoveEdgeDoesntRemoveNodes() { GraphData g = new GraphData("scope"); Node source = new Node("Source"); Node target = new Node("Target"); Edge edge = new Edge(source, target); g.AddNode(source); g.AddNode(target); g.AddEdge(edge); if (!g.ContainsEdge(edge)) { Assert.Inconclusive("Edge not found in the collection after adding it."); } g.RemoveEdge(edge); if (g.ContainsEdge(edge)) { Assert.Inconclusive("Edge still found in the collection after removing it."); } Assert.IsTrue(g.ContainsNode(source)); Assert.IsTrue(g.ContainsNode(target)); }
public void TestAddingEdgeFiresCollectionChangedEvent() { bool eventFired = false; NotifyCollectionChangedAction action = NotifyCollectionChangedAction.Reset; GraphData g = new GraphData("scope"); Node source = new Node("Source"); Node target = new Node("Target"); Edge edge = new Edge(source, target); // TODO: This test is invalid because added Node objects to the collection will fire the same event g.CollectionChanged += (sender, e) => { action = e.Action; eventFired = true; }; EnqueueCallback(() => g.AddNode(source)); EnqueueCallback(() => g.AddNode(target)); EnqueueCallback(() => g.AddEdge(edge)); EnqueueConditional(() => eventFired); EnqueueCallback(() => Assert.IsTrue(eventFired)); EnqueueCallback(() => Assert.AreEqual<NotifyCollectionChangedAction>(NotifyCollectionChangedAction.Add, action)); EnqueueTestComplete(); }
public void TestOneSuccessor() { GraphData g = new GraphData("scope"); INode source = new Node("Source"); INode target = new Node("Target"); Edge edge = new Edge(source, target); g.AddNode(source); g.AddNode(target); g.AddEdge(edge); INode actual = g.Successors(source).First(); Assert.AreEqual<INode>(target, actual); }
public void Verify_Edge_With_No_Ghost_Nodes_Is_Added_To_Physical_Graph() { int actualSourceEdges; int actualTargetEdges; int expectedSourceEdges = 1; int expectedTargetEdges = 1; INode source = PrepareNode("Node1", ghostNode: false); INode target = PrepareNode("Node2", ghostNode: false); IEdge edge = new Edge(source, target); GraphData graphData = new GraphData("scope"); // Add an edge without ghost nodes to the graph graphData.AddNode(source); graphData.AddNode(target); graphData.AddEdge(edge); // Get the actual count for nodes actualSourceEdges = graphData.Edges(source).Count(); actualTargetEdges = graphData.Edges(target).Count(); Assert.AreEqual(expectedSourceEdges, actualSourceEdges); Assert.AreEqual(expectedTargetEdges, actualTargetEdges); }
public void Verify_Edge_Added_To_Physical_Graph_Once_Corresponding_Source_Node_Added() { int actualSourceEdges; int actualTargetEdges; int expectedSourceEdges = 1; int expectedTargetEdges = 1; INode source = PrepareNode("Node1", ghostNode: true); INode target = PrepareNode("Node2", ghostNode: false); IEdge orphan = new Edge(source, target); GraphData graphData = new GraphData("scope"); // Add an edge with ghost nodes to the graph graphData.AddNode(target); graphData.AddEdge(orphan); // Add the missing node source = PrepareNode("Node1", ghostNode: false); graphData.AddNode(source); // Get the actual count for nodes actualSourceEdges = graphData.Edges(source).Count(); actualTargetEdges = graphData.Edges(target).Count(); Assert.AreEqual(expectedSourceEdges, actualSourceEdges); Assert.AreEqual(expectedTargetEdges, actualTargetEdges); }
public void Verify_Edge_With_Two_Ghost_Nodes_Not_Added_To_Physical_Graph() { IEnumerable<IEdge> actualSourceEdges; IEnumerable<IEdge> actualTargetEdges; IEnumerable<IEdge> expectedSourceEdges = null; IEnumerable<IEdge> expectedTargetEdges = null; INode temp1 = new Node("temp1"); // Hack because an exception is thrown if you attempt to add an edge to a graph if the graph doesn't have at least one node INode source = PrepareNode("Node1", ghostNode: true); INode target = PrepareNode("Node2", ghostNode: true); IEdge orphan = new Edge(source, target); GraphData graphData = new GraphData("scope"); graphData.AddNode(temp1); // Hack (see temp1 comment above) // Add an edge with ghost nodes to the graph graphData.AddEdge(orphan); // Get the actual count for nodes actualSourceEdges = graphData.Edges(source); actualTargetEdges = graphData.Edges(target); Assert.AreEqual(expectedSourceEdges, actualSourceEdges); Assert.AreEqual(expectedTargetEdges, actualTargetEdges); }