private void PrintShortestPathFromAllShortestPaths(AnalysisFact sourceFact, AnalysisFact targetFact) { Func <AnalysisReason, double> weights = (reason) => 1.0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var fw = new QuickGraph.Algorithms.ShortestPath.FloydWarshallAllShortestPathAlgorithm <AnalysisFact, AnalysisReason>(this, weights); // compute fw.Compute(); IEnumerable <AnalysisReason> path; if (fw.TryGetPath(sourceFact, targetFact, out path)) { Console.WriteLine("Got shortest path of length {0}", path.Count()); PrintPath(path); } else { Console.WriteLine("Couldn't get path!"); } stopwatch.Stop(); Console.WriteLine("Finding shortest path via ALL shortest paths took {0} seconds", stopwatch.Elapsed); }
private void PrintShortestPath(AnalysisFact sourceFact, AnalysisFact targetFact) { Func <AnalysisReason, double> weights = (reason) => 1.0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); TryFunc <AnalysisFact, IEnumerable <AnalysisReason> > tryGetPath = this.ShortestPathsDijkstra(weights, sourceFact); IEnumerable <AnalysisReason> path; if (tryGetPath(targetFact, out path)) { Console.WriteLine("Got shortest path of length {0}", path.Count()); PrintPath(path); } else { Console.WriteLine("Couldn't get path!"); } stopwatch.Stop(); Console.WriteLine("Finding shortest path took {0} seconds", stopwatch.Elapsed); }
public void CalculateBestReasons() { Contract.Assert(Contract.ForAll <AnalysisReason>(allReasons, (reason) => reason.Result != null)); Contract.Assert(Contract.ForAll <AnalysisReason>(allReasons, (reason) => reason.MainCause != null)); Console.WriteLine("There are {0} facts and {1} reasons.", allFacts.Count(), allReasons.Count()); // Choose an arbitrary fact to print a reason for. AnalysisFact sourceFact = methodReachedFactsByMethod.Values.ElementAt(100); AnalysisFact targetFact = entryPointReachedFact; Console.WriteLine("SourceFact is {0} TargetFact is {1}", sourceFact, targetFact); Console.WriteLine("Calculating distance from {0} to {1}", sourceFact, targetFact); PrintShortestPath(sourceFact, targetFact); PrintSomePath(sourceFact, targetFact); //PrintShortestPathFromAllShortestPaths(sourceFact, targetFact); }
public AnalysisReasons() #if QUICKGRAPH : base(AnalysisReasons.TryReasonsForFact) #endif { entryPointReachedFact = new EntryPointReachedFact(this); // escaping this; have to be careful if ever subclass allFacts.Add(entryPointReachedFact); }
private void PrintSomePath(AnalysisFact sourceFact, AnalysisFact targetFact) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var dfs = new DepthFirstSearchAlgorithm <AnalysisFact, AnalysisReason>(this); Dictionary <AnalysisFact, AnalysisReason> vertexPredecessors = new Dictionary <AnalysisFact, AnalysisReason>(); dfs.TreeEdge += (AnalysisReason reason) => { vertexPredecessors.Add(reason.MainCause, reason); if (reason.MainCause == targetFact) { dfs.Services.CancelManager.Cancel(); } }; dfs.SetRootVertex(sourceFact); //do the search dfs.Compute(); // reconstruct the path IList <AnalysisReason> path = new List <AnalysisReason>(); AnalysisFact iterator = targetFact; while (iterator != sourceFact) { AnalysisReason reason; if (vertexPredecessors.TryGetValue(iterator, out reason)) { path.Add(reason); iterator = reason.Result; } else { throw new Exception("Couldn't get predecessor reason for iterator " + iterator); } } Console.WriteLine("Got some path of length {0}", path.Count()); //PrintPath(path.Reverse()); Console.WriteLine("Finding some path took {0} seconds", stopwatch.Elapsed); }
// Delegates public static bool TryReasonsForFact(AnalysisFact fact, out IEnumerable <AnalysisReason> result) { result = fact.GetReasons(); return(true); }
// Construction Notification (this is absurd) internal void NoteAnalysisFactCreated(AnalysisFact fact) { allFacts.Add(fact); }