/// <summary> /// Display constraint window /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Start_Click(object sender, RoutedEventArgs e) { // Create a resultGraph instance StartWindow startWindow = new StartWindow(graph.CommonAttributes); if (startWindow.ShowDialog() != true) { MessageBox.Show("Task imcompleted.\nAborted.", "Algorithm not running."); } else { logTab.IsSelected = true; logger.Content = ""; logger.Content += "\nStart Checking observability....\n"; var observers = graph.Call(graph => graph.AllNodes.Values.Where(node => node.IsObserver)).ToArray(); AllPaths algorithm = new AllPaths(); var result = new ConnectivityObserver().Observe(graph.Impl, observers, startWindow.returnValue, algorithm); logger.Content += "Observation Completed.\n"; ResultGraph resultGraph = new ResultGraph(); foreach (var pair in result) { INode from = pair.Key.From, to = pair.Key.To; IEnumerable <Route> observedRoutes = pair.Value.Item1; IEnumerable <Route> unobservedRoutes = pair.Value.Item2; if (observedRoutes.Count() > 0) { Route shortestObRoute = observedRoutes.OrderBy(p => p.PathCost).First(); CanvasNode tempSrcNode = new CanvasNode(graph[from]); CanvasNode tempDestNode = new CanvasNode(graph[to]); if (resultGraph.CGraph.Call(graph => !graph.Contains(from))) { resultGraph.CGraph.Call(graph => graph.Add(from)); resultGraph.CGraph[from] = tempSrcNode; } else { tempSrcNode = resultGraph.CGraph[from]; } if (resultGraph.CGraph.Call(graph => !graph.Contains(to))) { resultGraph.CGraph.Call(graph => graph.Add(to)); resultGraph.CGraph[to] = tempDestNode; } else { tempDestNode = resultGraph.CGraph[to]; } double shortestDistance = Double.MaxValue; if (unobservedRoutes.Count() > 0) { Route shortestUnobRoute = unobservedRoutes.OrderBy(p => p.PathCost).First(); shortestDistance = shortestUnobRoute.PathCost; } if ((from.IsVisible && to.IsVisible) && shortestObRoute.PathCost < shortestDistance) { //resultGraph.logger.Content += String.Format("\nNode {0} to Node {1} : observed\n", from.Id, to.Id); //logger.Content += String.Format("The path from Node {0} to Node {1} is : {2}\n", from.Id, to.Id, shortestObRoute); resultGraph.observedLog.Content += String.Format("{0} (This is the shortest path)\n", shortestObRoute); AddOutputEdge(resultGraph, tempSrcNode, tempDestNode, unobservedRoutes.Count() == 0); } else { resultGraph.observedLog.Content += String.Format("{0}\n", shortestObRoute); } } // foreach (Route through in observedRoutes) //{ // logger.Content += String.Format("Node {0} to Node {1} : observed\n", from.Id, to.Id); // logger.Content += String.Format("The path from Node {0} to Node {1} is : {2}\n", from.Id, to.Id, through); //CanvasNode tempSrcNode = new CanvasNode(graph[from]); //CanvasNode tempDestNode = new CanvasNode(graph[to]); //DrawNode(tempSrcNode, resultGraph.ResultCanvas); //DrawNode(tempDestNode, resultGraph.ResultCanvas); // if (from.IsVisible && to.IsVisible) // { // DrawOutputEdge(resultGraph, tempSrcNode, tempDestNode); // } //} foreach (Route through in unobservedRoutes) { // logger.Content += String.Format("\nNode {0} to Node {1} : not observed\n", from.Id, to.Id); resultGraph.unobservedLog.Content += String.Format("{0}\n", through); } } foreach (var node in resultGraph.CGraph.Call(graph => graph.AllNodes.Values)) { var cnode = resultGraph.CGraph[node]; DrawNode(cnode, resultGraph.ResultCanvas); } foreach (var edge in resultGraph.CGraph.Call(graph => graph.AllEdges.Values)) { var cedge = resultGraph.CGraph[edge]; DrawOutputEdge(resultGraph, cedge); } logger.Content += "Task Finished."; // Display the resultGraph window resultGraph.Show(); } }
public void TestObserveConnectivity() { Graph graph = new Graph(); var A = new Node() { Label = "A" }; var B = new Node() { Label = "B" }; var C = new Node() { Label = "C" }; var D = new Node() { Label = "D" }; var E = new Node() { Label = "E" }; graph.Add(A); graph.Add(B); graph.Add(C); graph.Add(D); graph.Add(E); graph.ConnectNodeToWith(A, B, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 4 })); graph.ConnectNodeToWith(B, E, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 5 })); graph.ConnectNodeToWith(A, C, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 5 })); graph.ConnectNodeToWith(C, E, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 4 })); graph.ConnectNodeToWith(A, D, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 9 })); graph.ConnectNodeToWith(D, E, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 7 })); B.IsObserver = true; /* * graph.ConnectNodeToWith(B, C, new Edge({Name="Weight", Attribute=)(1)); * graph.ConnectNodeToWith(C, D, new Edge({Name="Weight", Attribute=)(3)); * graph.ConnectNodeToWith(D, C, new Edge({Name="Weight", Attribute=)(2)); * graph.ConnectNodeToWith(E, C, new Edge({Name="Weight", Attribute=)(1)); */ var result = new ConnectivityObserver().Observe(graph, new List <INode>() { B }, Tuple.Create("Weight", Constraint <IEdge> .Default), new AllPaths()); foreach (var pair in result) { var observedRoutes = pair.Value.Item1; var unobservedRoutes = pair.Value.Item2; foreach (var route in observedRoutes) { if (!route.Contains(B)) { Assert.Fail(); } } foreach (var route in unobservedRoutes) { if (route.Contains(B)) { Assert.Fail(); } } } }
public void Initialize() { co = new ConnectivityObserver(); graph = new Graph(); var A = new TestSubNode() { Label = "A" }; var B = new Node() { Label = "B" }; var C = new Node() { Label = "C" }; var D = new TestSubNode() { Label = "D" }; var E = new TesteSubNode2() { Label = "E" }; E.DescriptiveAttributes["test"] = "test"; B["dummy"] = 15; C.DescriptiveAttributes["time"] = "NOthing"; A["3"] = 0.99; graph.Add(A); graph.Add(B); graph.Add(C); graph.Add(D); graph.Add(E); graph.ConnectNodeToWith(A, B, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 8 })); graph.ConnectNodeToWith(A, D, new TestSubEdge(9)); graph.ConnectNodeToWith(A, E, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 4 })); graph.ConnectNodeToWith(B, C, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 1 })); graph.ConnectNodeToWith(C, B, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 2 })); graph.ConnectNodeToWith(C, D, new TestSubEdge(3)); graph.ConnectNodeToWith(D, C, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 2 })); graph.ConnectNodeToWith(D, E, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 7 })); graph.ConnectNodeToWith(E, C, new Edge(new AttributePair <Double> { Name = "Weight", Attribute = 1 })); observers = new List <INode>(); observers.Add(B); observers.Add(C); }