Skip to content

Commit

Permalink
type cycles feature: window shows type cycles as list or graph
Browse files Browse the repository at this point in the history
  • Loading branch information
halllo committed Oct 5, 2013
1 parent 360ccb8 commit 6b24db5
Show file tree
Hide file tree
Showing 49 changed files with 845 additions and 739 deletions.
Expand Up @@ -3,19 +3,19 @@

namespace Usus.net.Core.UnitTests
{
[TestClass]
public class AverageCumulativeComponentDependencyTest
{
[TestMethod]
public void Rate_0CumulativeComponentDependencies_AverageRated0()
{
Assert.AreEqual(0.0, CreateAverage.CumulativeComponentDependency(), Constants.DELTA);
}
[TestMethod]
public void Rate_4And3And3And3CumulativeComponentDependencies_RatedPoint8125()
{
Assert.AreEqual(0.8125, CreateAverage.CumulativeComponentDependency(4, 3, 3, 3), Constants.DELTA);
}
}
[TestClass]
public class AverageCumulativeComponentDependencyTest
{
[TestMethod]
public void Rate_0CumulativeComponentDependencies_AverageRated0()
{
Assert.IsTrue(double.IsNaN(CreateAverage.CumulativeComponentDependency()));
}

[TestMethod]
public void Rate_4And3And3And3CumulativeComponentDependencies_RatedPoint8125()
{
Assert.AreEqual(0.8125, CreateAverage.CumulativeComponentDependency(4, 3, 3, 3), Constants.DELTA);
}
}
}
Expand Up @@ -3,19 +3,19 @@

namespace Usus.net.Core.UnitTests
{
[TestClass]
public class AverageNumberOfNamespacesInCycleTest
{
[TestMethod]
public void Rate_0NumberOfNamespacesInCycle_AverageRated0()
{
Assert.AreEqual(0.0, CreateAverage.RatedNumberOfNamespacesInCycle(), Constants.DELTA);
}
[TestClass]
public class AverageNumberOfNamespacesInCycleTest
{
[TestMethod]
public void Rate_0NumberOfNamespacesInCycle_AverageRated0()
{
Assert.IsTrue(double.IsNaN(CreateAverage.RatedNumberOfNamespacesInCycle()));
}

[TestMethod]
public void Rate_3NumberOfNamespacesInCycle_AverageRated0()
{
Assert.AreEqual(0.6667, CreateAverage.RatedNumberOfNamespacesInCycle(2, 2, 1), Constants.DELTA);
}
}
[TestMethod]
public void Rate_3NumberOfNamespacesInCycle_AverageRated0()
{
Assert.AreEqual(0.6667, CreateAverage.RatedNumberOfNamespacesInCycle(2, 2, 1), Constants.DELTA);
}
}
}
Expand Up @@ -26,7 +26,7 @@ internal static MetricsReport MetricsReport(params TypeMetricsWithMethodMetrics[
{
var metricsReport = new MetricsReport();
foreach (var typeMetric in typeMetrics)
metricsReport.AddTypeReport(TypeMetrics(typeMetric.Itself, typeMetric.Methods));
metricsReport.AddTypeReport(TypeMetrics(typeMetric.Type, typeMetric.Methods));
return metricsReport;
}

Expand All @@ -46,14 +46,14 @@ internal static TypeMetricsWithMethodMetrics TypeMetrics(Func<int, MethodMetrics

private static TypeMetricsWithMethodMetrics TypeMetrics(TypeMetricsReport typeMetrics, IEnumerable<MethodMetricsReport> methodMetrics)
{
var typeWithMethods = new TypeMetricsWithMethodMetrics() { Itself = typeMetrics };
var typeWithMethods = new TypeMetricsWithMethodMetrics() { Type = typeMetrics };
typeWithMethods.AddMethodReports(methodMetrics);
return typeWithMethods;
}

private static NamespaceMetricsWithTypeMetrics NamespaceMetrics(NamespaceMetricsReport namespaceMetrics, IEnumerable<TypeMetricsReport> typeMetrics)
{
var namespaceWithTypes = new NamespaceMetricsWithTypeMetrics() { Itself = namespaceMetrics };
var namespaceWithTypes = new NamespaceMetricsWithTypeMetrics() { Namespace = namespaceMetrics };
foreach (var typeMetric in typeMetrics) namespaceWithTypes.AddTypeReport(typeMetric);
return namespaceWithTypes;
}
Expand Down
Expand Up @@ -24,7 +24,7 @@ private TypeMetricsWithMethodMetrics TypeAndMethods(PdbReader pdb, IMetadataHost
{
var typeAndMethods = new TypeMetricsWithMethodMetrics();
typeAndMethods.AddMethodReports(AnalyzeMethods(type, pdb, host));
typeAndMethods.Itself = AnalyzeType(type, pdb, typeAndMethods.Methods);
typeAndMethods.Type = AnalyzeType(type, pdb, typeAndMethods.Methods);
return typeAndMethods;
}

Expand Down
26 changes: 9 additions & 17 deletions Sources2012/Usus.net.Core/Graphs/GraphExtensions.cs
@@ -1,21 +1,13 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;

namespace andrena.Usus.net.Core.Graphs
{
public static class GraphExtensions
{
public static MutableGraph<V> ToGraph<V>(this IDictionary<V, IEnumerable<V>> graphDict)
where V : class
{
return new MutableGraph<V>(graphDict);
}

public static IGraph<V> Select<V, R>(this IGraph<R> graph, Func<R, V> vertexSelector)
where V : class
where R : class
{
return new GraphSurrogate<V, R>(graph, vertexSelector);
}
}
public static class GraphExtensions
{
public static MutableGraph<V> ToGraph<V>(this IDictionary<V, IEnumerable<V>> graphDict)
where V : class
{
return new MutableGraph<V>(graphDict);
}
}
}
30 changes: 0 additions & 30 deletions Sources2012/Usus.net.Core/Graphs/GraphSurrogate.cs

This file was deleted.

11 changes: 6 additions & 5 deletions Sources2012/Usus.net.Core/Graphs/IGraph.cs
Expand Up @@ -3,9 +3,10 @@

namespace andrena.Usus.net.Core.Graphs
{
public interface IGraph<V> where V : class
{
IEnumerable<V> Vertices { get; }
IEnumerable<Tuple<V, V>> Edges { get; }
}
public interface IGraph<V> where V : class
{
IEnumerable<V> Vertices { get; }
IEnumerable<Tuple<V, V>> Edges { get; }
StronglyConntectedComponents<V> Cycles();
}
}
82 changes: 41 additions & 41 deletions Sources2012/Usus.net.Core/Graphs/MutableGraph.cs
@@ -1,48 +1,48 @@
using QuickGraph;
using System;
using System.Collections.Generic;
using System.Linq;
using QuickGraph;

namespace andrena.Usus.net.Core.Graphs
{
public class MutableGraph<V> : IGraph<V> where V : class
{
BidirectionalGraph<V, Edge<V>> graph;
const bool PARALLEL_EDGES = false;

public IEnumerable<V> Vertices { get { return graph.Vertices; } }
public IEnumerable<Tuple<V, V>> Edges { get { return graph.Edges.Select(e => Tuple.Create(e.Source, e.Target)); } }

public MutableGraph(IDictionary<V, IEnumerable<V>> graphDict)
{
graph = graphDict.Keys.ToBidirectionalGraph(v => graphDict[v].Select(e => new Edge<V>(v, e)), PARALLEL_EDGES);
}

internal MutableGraph(BidirectionalGraph<V, Edge<V>> graph)
{
this.graph = graph;
}

public void Reduce(V vertex, IEnumerable<V> vertices)
{
graph.MergeVertices(vertex, vertices);
}

public MutableGraph<V> Reach(V start)
{
var reachGraph = start.ToNewGraph(PARALLEL_EDGES);
graph.Dfs(start, e => reachGraph.AddVerticesAndEdge(e));
return new MutableGraph<V>(reachGraph);
}
public MutableGraph<R> Cast<R>(Func<V, R> selector, Func<R, bool> condition) where R : class
{
return graph.Clone(selector, condition);
}

public StronglyConntectedComponents<V> Cycles()
{
return graph.Sccs();
}
}
public class MutableGraph<V> : IGraph<V> where V : class
{
BidirectionalGraph<V, Edge<V>> graph;
const bool PARALLEL_EDGES = false;

public IEnumerable<V> Vertices { get { return graph.Vertices; } }
public IEnumerable<Tuple<V, V>> Edges { get { return graph.Edges.Select(e => Tuple.Create(e.Source, e.Target)); } }

public MutableGraph(IDictionary<V, IEnumerable<V>> graphDict)
{
graph = graphDict.Keys.ToBidirectionalGraph(v => graphDict[v].Select(e => new Edge<V>(v, e)), PARALLEL_EDGES);
}

internal MutableGraph(BidirectionalGraph<V, Edge<V>> graph)
{
this.graph = graph;
}

public void Reduce(V vertex, IEnumerable<V> vertices)
{
graph.MergeVertices(vertex, vertices);
}

public MutableGraph<V> Reach(V start)
{
var reachGraph = start.ToNewGraph(PARALLEL_EDGES);
graph.Dfs(start, e => reachGraph.AddVerticesAndEdge(e));
return new MutableGraph<V>(reachGraph);
}

public MutableGraph<R> Cast<R>(Func<V, R> selector, Func<R, bool> condition) where R : class
{
return graph.Clone(selector, condition);
}

public StronglyConntectedComponents<V> Cycles()
{
return graph.Sccs();
}
}
}
20 changes: 10 additions & 10 deletions Sources2012/Usus.net.Core/Graphs/StronglyConntectedComponent.cs
Expand Up @@ -2,15 +2,15 @@

namespace andrena.Usus.net.Core.Graphs
{
public class StronglyConntectedComponent<T>
{
List<T> vertexList;
public IEnumerable<T> Vertices { get { return vertexList; } }
public int VertexCount { get { return vertexList.Count; } }
public class StronglyConntectedComponent<T>
{
List<T> vertexList;
public IEnumerable<T> Vertices { get { return vertexList; } }
public int VertexCount { get { return vertexList.Count; } }

internal StronglyConntectedComponent(List<T> vertices)
{
vertexList = vertices;
}
}
internal StronglyConntectedComponent(List<T> vertices)
{
vertexList = vertices;
}
}
}
47 changes: 29 additions & 18 deletions Sources2012/Usus.net.Core/Graphs/StronglyConntectedComponents.cs
Expand Up @@ -4,25 +4,36 @@

namespace andrena.Usus.net.Core.Graphs
{
public class StronglyConntectedComponents<T>
{
IDictionary<int, List<T>> idToVertices;
IDictionary<T, int> vertexToId;
public class StronglyConntectedComponents<T>
{
IDictionary<int, List<T>> idToVertices;
IDictionary<T, int> vertexToId;

public StronglyConntectedComponents(IDictionary<T, int> vertexToId, IDictionary<int, List<T>> idToVertices)
{
this.vertexToId = vertexToId;
this.idToVertices = idToVertices;
}
public StronglyConntectedComponents(IDictionary<T, int> vertexToId, IDictionary<int, List<T>> idToVertices)
{
this.vertexToId = vertexToId;
this.idToVertices = idToVertices;
}

public StronglyConntectedComponent<T> OfVertex(T vertex)
{
return new StronglyConntectedComponent<T>(idToVertices[vertexToId[vertex]]);
}
public IEnumerable<StronglyConntectedComponent<T>> All
{
get
{
foreach (var scc in idToVertices)
{
yield return new StronglyConntectedComponent<T>(scc.Value);
}
}
}

public int Count(Func<StronglyConntectedComponent<T>, bool> condition)
{
return idToVertices.Select(vs => new StronglyConntectedComponent<T>(vs.Value)).Count(condition);
}
}
public StronglyConntectedComponent<T> OfVertex(T vertex)
{
return new StronglyConntectedComponent<T>(idToVertices[vertexToId[vertex]]);
}

public int Count(Func<StronglyConntectedComponent<T>, bool> condition)
{
return idToVertices.Select(vs => new StronglyConntectedComponent<T>(vs.Value)).Count(condition);
}
}
}
6 changes: 3 additions & 3 deletions Sources2012/Usus.net.Core/Metrics/CreateGraph.cs
Expand Up @@ -16,23 +16,23 @@ public static MutableGraph<TypeMetricsReport> WithTypesOf(MetricsReport metrics)
public static MutableGraph<NamespaceMetricsWithTypeMetrics> WithNamespacesOf(MetricsReport metrics)
{
var namespaceGraph = metrics.GraphOfTypes.Cast(t => t.AsNamespaceWithTypes(), n => n.HasName);
foreach (var namespaceGroup in namespaceGraph.Vertices.GroupBy(n => n.Itself.Name))
foreach (var namespaceGroup in namespaceGraph.Vertices.GroupBy(n => n.Namespace.Name))
namespaceGraph.Reduce(namespaceGroup.AsNamespaceWithTypes(), namespaceGroup);
return namespaceGraph;
}

private static NamespaceMetricsWithTypeMetrics AsNamespaceWithTypes(this IGrouping<string, NamespaceMetricsWithTypeMetrics> namespaceGroup)
{
var namespaceWithTypes = new NamespaceMetricsWithTypeMetrics();
namespaceWithTypes.Itself = new NamespaceMetricsReport { Name = namespaceGroup.Key };
namespaceWithTypes.Namespace = new NamespaceMetricsReport { Name = namespaceGroup.Key };
namespaceWithTypes.AddTypeReports(namespaceGroup);
return namespaceWithTypes;
}

private static NamespaceMetricsWithTypeMetrics AsNamespaceWithTypes(this TypeMetricsReport t)
{
var namespaceWithTypes = new NamespaceMetricsWithTypeMetrics();
namespaceWithTypes.Itself = new NamespaceMetricsReport { Name = t.Namespaces.FirstOrDefault() };
namespaceWithTypes.Namespace = new NamespaceMetricsReport { Name = t.Namespaces.FirstOrDefault() };
namespaceWithTypes.AddTypeReport(t);
return namespaceWithTypes;
}
Expand Down
Expand Up @@ -21,7 +21,7 @@ public static IEnumerable<string> Of(NamespaceMetricsWithTypeMetrics namespaceWi
private static IEnumerable<NamespaceMetricsReport> GetOtherNamespacesInSameCycleIn(this NamespaceMetricsWithTypeMetrics namespaceWithTypes, StronglyConntectedComponents<NamespaceMetricsWithTypeMetrics> cycles)
{
return from n in cycles.OfVertex(namespaceWithTypes).Vertices
select n.Itself;
select n.Namespace;
}
}
}
Expand Up @@ -17,7 +17,7 @@ private static void SetNamespacesWithCyclicDependencies(this MetricsReport metri
foreach (var namespaceWithTypes in metrics.GraphOfNamespaces.Vertices)
{
metrics.AddNamespaceReport(namespaceWithTypes);
namespaceWithTypes.Itself.CyclicDependencies = CyclicDependencies.Of(namespaceWithTypes, cycles);
namespaceWithTypes.Namespace.CyclicDependencies = CyclicDependencies.Of(namespaceWithTypes, cycles);
}
}
}
Expand Down

0 comments on commit 6b24db5

Please sign in to comment.