From b2223bea7471dfa6a212413d67fe9734fce2f2b2 Mon Sep 17 00:00:00 2001 From: PeterForstmeier Date: Sat, 7 Jan 2012 20:11:35 +0100 Subject: [PATCH] TreeMapViewModel.cs, create Linq Queries for Namespace and Method --- .../CodeQuality/CodeQualityAnalysis.csproj | 1 + .../Analysis/CodeQuality/Src/MainWindow.xaml | 12 --- .../CodeQuality/Src/MainWindowModel.cs | 22 ++--- .../Src/Utility/Queries/QueryAssembly.cs | 2 +- .../Src/Utility/Queries/QueryMethod.cs | 36 +++++-- .../Src/Utility/Queries/QueryNameSpace.cs | 98 ++++++++++++++++--- .../Src/Utility/Queries/QueryType.cs | 2 +- .../Src/Utility/Queries/TreeMapViewModel.cs | 21 ++++ 8 files changed, 144 insertions(+), 50 deletions(-) create mode 100644 src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/TreeMapViewModel.cs diff --git a/src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj b/src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj index 4c19557479..d174c363a0 100644 --- a/src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj +++ b/src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj @@ -156,6 +156,7 @@ + diff --git a/src/AddIns/Analysis/CodeQuality/Src/MainWindow.xaml b/src/AddIns/Analysis/CodeQuality/Src/MainWindow.xaml index d91f48e250..7b0b16c1de 100644 --- a/src/AddIns/Analysis/CodeQuality/Src/MainWindow.xaml +++ b/src/AddIns/Analysis/CodeQuality/Src/MainWindow.xaml @@ -41,18 +41,6 @@ - (list); + Nodes = new ObservableCollection(list); } } } @@ -285,13 +278,13 @@ namespace ICSharpCode.CodeQualityAnalysis #region ShowTreeMap Treemap - private ObservableCollection nodes; + private ObservableCollection nodes; - public ObservableCollection Nodes { + public ObservableCollection Nodes { get { if (nodes == null) { - nodes = new ObservableCollection(); + nodes = new ObservableCollection(); } return nodes; } @@ -341,9 +334,8 @@ namespace ICSharpCode.CodeQualityAnalysis public ItemWithAction() { } - public string Description {get; set;} - public Func> Action {get; set;} + public Func> Action {get; set;} public string Metrics {get;set;} } } diff --git a/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryAssembly.cs b/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryAssembly.cs index 4c41ad0bb9..7cf5ad56a8 100644 --- a/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryAssembly.cs +++ b/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryAssembly.cs @@ -33,7 +33,7 @@ namespace ICSharpCode.CodeQualityAnalysis.Utility.Queries return items; } - private List ExecuteNotImplemented() + private List ExecuteNotImplemented() { MessageService.ShowMessage("Not Implemented yet","CodeQualityAnalysis"); return null; diff --git a/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryMethod.cs b/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryMethod.cs index f9c43ce774..268ad546cc 100644 --- a/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryMethod.cs +++ b/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryMethod.cs @@ -24,7 +24,7 @@ namespace ICSharpCode.CodeQualityAnalysis.Utility.Queries { } - private List QueryForMethod() + private List MethodQuery() { IEnumerable query = new List(); query = from ns in MainModule.Namespaces @@ -44,6 +44,7 @@ namespace ICSharpCode.CodeQualityAnalysis.Utility.Queries Metrics = "Instructions.Count", Action = ExecuteMethodILInstructions }); + /* items.Add(new ItemWithAction() { Description = "IL Cyclomatic Complexity", @@ -56,40 +57,55 @@ namespace ICSharpCode.CodeQualityAnalysis.Utility.Queries Metrics = Metrics.Variables.ToString(), Action = ExecuteMethodVariables }); + */ return items; } - private List ExecuteMethodILInstructions() + private List ExecuteMethodILInstructions() { - var intermediate = QueryForMethod(); + var intermediate = MethodQuery(); var filtered = from method in intermediate where method.Instructions.Count > 0 select method; - return filtered.Cast().ToList(); + + int i = 0; + var list = filtered.Select(m => new TreeMapViewModel() + { + Name = m.Name, + Numval = m.GetAllMethods().Aggregate(i, (current, x) => current + x.Instructions.Count) + }); +// var list = intermediate.Select(m => new TreeMapViewModel() +// { +// Name = m.Name, +// Numval = m.GetAllMethods().Aggregate(i, (current, x) => current + x.Instructions.Count) +// }); + + return list.ToList(); + //return filtered.Cast().ToList(); } - private List ExecuteMethodComplexity () + private List ExecuteMethodComplexity () { - var intermediate = QueryForMethod(); + var intermediate = MethodQuery(); var filtered = from method in intermediate where method.CyclomaticComplexity > 0 select method; - return filtered.Cast().ToList(); + return filtered.Cast().ToList(); } - private List ExecuteMethodVariables () + private List ExecuteMethodVariables () { - var intermediate = QueryForMethod(); + var intermediate = MethodQuery(); // eliminate 0-values reduce time for my test assembly from 6 to 1 sek var filtered = from method in intermediate where method.Variables > 0 select method; - return filtered.Cast().ToList(); + return filtered.Cast().ToList(); } } } diff --git a/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryNameSpace.cs b/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryNameSpace.cs index 1ef384ca44..9903f25ab4 100644 --- a/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryNameSpace.cs +++ b/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryNameSpace.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Generic; using ICSharpCode.Core; +using System.Linq; namespace ICSharpCode.CodeQualityAnalysis.Utility.Queries { @@ -21,6 +22,13 @@ namespace ICSharpCode.CodeQualityAnalysis.Utility.Queries { } + private List NameSpaceQuery() + { + IEnumerable query = new List(); + query = from ns in MainModule.Namespaces + select ns; + return query.ToList(); + } public override List GetQueryList() { @@ -29,40 +37,108 @@ namespace ICSharpCode.CodeQualityAnalysis.Utility.Queries { Description = "# of IL Instructions", Metrics = "Instructions.Count", - Action = ExecuteNotImplemented + Action = ExecuteILInstructions }); items.Add(new ItemWithAction() { Description = "# of Methods", Metrics = Metrics.CyclomaticComplexity.ToString(), - Action = ExecuteNotImplemented + Action = MethodsCount }); items.Add(new ItemWithAction() { Description = "# of Fields", Metrics = Metrics.Variables.ToString(), - Action = ExecuteNotImplemented + Action = FieldsCount }); + items.Add(new ItemWithAction() { Description = "# of Types", Metrics = Metrics.Variables.ToString(), - Action = ExecuteNotImplemented + Action = TypesCount }); - items.Add(new ItemWithAction() - { - Description = "# of Namespaces", - Metrics = Metrics.Variables.ToString(), - Action = ExecuteNotImplemented - }); +// items.Add(new ItemWithAction() +// { +// Description = "# of Namespaces", +// Metrics = Metrics.Variables.ToString(), +// Action = ExecuteNotImplemented +// }); return items; } - private List ExecuteNotImplemented() + private List ExecuteNotImplemented() { MessageService.ShowMessage("Not Implemented yet","CodeQualityAnalysis"); return null; } + + + + private List ExecuteILInstructions () + { + //var list = new List(); + var intermediate = this.NameSpaceQuery(); + var i = 0; + /* + foreach (var element in intermediate) + { + var node = new TreeMapNode(); + node.Name = element.Name; + foreach (var m in element.GetAllMethods()) { + i = i + m.Instructions.Count; + } + node.Numval = i; + list.Add(node); + i = 0; + } + */ + + var list = intermediate.Select(m => new TreeMapViewModel() + { + Name = m.Name, + Numval = m.GetAllMethods().Aggregate(i, (current, x) => current + x.Instructions.Count) + }); + return list.ToList(); + } + + + private List MethodsCount() + { + var intermediate = this.NameSpaceQuery(); + + var list = intermediate.Select(m => new TreeMapViewModel() + { + Name = m.Name, + Numval = m.GetAllMethods().ToList().Count + }); + return list.ToList(); + } + + + private List FieldsCount() + { + var intermediate = this.NameSpaceQuery(); + var list = intermediate.Select(m => new TreeMapViewModel() + { + Name = m.Name, + Numval = m.GetAllFields().ToList().Count + }); + return list.ToList(); + } + + + private List TypesCount() + { + var intermediate = this.NameSpaceQuery(); + + var list = intermediate.Select(m => new TreeMapViewModel() + { + Name = m.Name, + Numval = m.GetAllTypes().ToList().Count + }); + return list.ToList(); + } } } diff --git a/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryType.cs b/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryType.cs index f8747fa7fe..f29f4f1612 100644 --- a/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryType.cs +++ b/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/QueryType.cs @@ -53,7 +53,7 @@ namespace ICSharpCode.CodeQualityAnalysis.Utility.Queries return items; } - private List ExecuteNotImplemented() + private List ExecuteNotImplemented() { MessageService.ShowMessage("Not Implemented yet","CodeQualityAnalysis"); return null; diff --git a/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/TreeMapViewModel.cs b/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/TreeMapViewModel.cs new file mode 100644 index 0000000000..1126bef07c --- /dev/null +++ b/src/AddIns/Analysis/CodeQuality/Src/Utility/Queries/TreeMapViewModel.cs @@ -0,0 +1,21 @@ +/* + * Created by SharpDevelop. + * User: Peter Forstmeier + * Date: 07.01.2012 + * Time: 19:56 + * + * To change this template use Tools | Options | Coding | Edit Standard Headers. + */ +using System; + +namespace ICSharpCode.CodeQualityAnalysis.Utility.Queries +{ + /// + /// Description of TreeMapViewModel. + /// + public class TreeMapViewModel + { + public string Name {get;set;} + public int Numval {get;set;} + } +}