using System;
using System.Runtime.Serialization;
using AnticipatingMinds.PlatformServices.Configuration;
using AnticipatingMinds.Genesis.CodeDOM;
using AnticipatingMinds.Genesis.CodeDOM.Utilities;
namespace AnticipatingMinds.KnowledgePack.Design{
public class MethodElementMetricsExceededRuleUtils
{
public static string GetMemberName(CodeTypeMemberDeclaration member)
{
string memberName = "";
if(member is CodeTypeMethodDeclaration)
{
CodeTypeMethodDeclaration methodDeclaration = member as CodeTypeMethodDeclaration;
memberName = (methodDeclaration.Name == ".ctor") ? CodeTypeReferenceUtils.GetShortestTypeNameReference(methodDeclaration.DeclaringType, methodDeclaration.DeclaringType.FullName) + "." + methodDeclaration.DeclaringType.Name : CodeTypeReferenceUtils.GetShortestTypeNameReference(methodDeclaration.DeclaringType, methodDeclaration.DeclaringType.FullName) + "." + methodDeclaration.Name ;
}
else if(member is CodeTypeIndexerDeclaration)
{
CodeTypeIndexerDeclaration indexerDeclaration = member as CodeTypeIndexerDeclaration;
memberName = CodeTypeReferenceUtils.GetShortestTypeNameReference(indexerDeclaration.DeclaringType, indexerDeclaration.DeclaringType.FullName)+".this";
}
else if(member is CodeTypePropertyDeclaration)
{
CodeTypePropertyDeclaration propertyDeclaration = member as CodeTypePropertyDeclaration;
memberName = CodeTypeReferenceUtils.GetShortestTypeNameReference(propertyDeclaration.DeclaringType, propertyDeclaration.DeclaringType.FullName) + "." + propertyDeclaration.Name;
}
else if(member is CodeTypeEventDeclaration)
{
CodeTypeEventDeclaration eventDeclaration = member as CodeTypeEventDeclaration;
memberName = CodeTypeReferenceUtils.GetShortestTypeNameReference(eventDeclaration.DeclaringType, eventDeclaration.DeclaringType.FullName)+ "." + eventDeclaration.Name;
}
return memberName;
}
}
/// <summary>
/// Several classes for metric
/// ( statements, local variables , method arguments, return points )
/// </summary>
public class MetricClasses
{
const string CONFIG_UI_ROOT = @"SOFTWARE\Anticipating Minds\DevAdvantage\Appearance\Rules\MethodElementMetricsExceededRuleTemplate";
private RegistryConfiguration config = new RegistryConfiguration(CONFIG_UI_ROOT, RegistryConfiguration.RegistryTarget.LocalMachine);
#region Default Values
public virtual int MaximumDefault{get{return 0;}}
public virtual int MinimumDefault{get{return 0;}}
public virtual int LargeChangeDefault{get{return 1;}}
public virtual int SmallChangeDefault{get{return 1;}}
public virtual int Increment{ get{return 1;}}
public virtual int TickFrequency{ get{return 1;}}
public MetricDefaultValue MetricDefaultValue;
public int DefaultValue
{
get
{
return this.MetricDefaultValue.GetValue(this.MetricType);
}
}
#endregion
public string MetricObjectName;
public MetricClasses(MetricDefaultValue metricDefaultValue, string name)
{
MetricDefaultValue = metricDefaultValue;
this.Init(name);
}
public void Update()
{
config.SetValue(this.MetricType, "Maximum", this.maximum);
config.SetValue(this.MetricType, "Minimum", this.minimum);
config.SetValue(this.MetricObjectName, "Value", this.localvalue);
this.MetricDefaultValue.SetValue(this.MetricType, this.localvalue);
config.SetValue(this.MetricType, "SmallChange", this.smallChange);
config.SetValue(this.MetricType, "LargeChange", this.largeChange);
}
public void Init(string name)
{
this.MetricObjectName = this.MetricType + ":" + name;
this.maximum = this.Maximum;
this.minimum = this.Minimum;
this.localvalue = this.Value;
this.smallChange = this.SmallChange;
this.largeChange = this.LargeChange;
}
public string MetricName
{
get{ return ResourceManager.GetLocalizedString("MethodElementMetricsExceeded|"+this.MetricType);}
}
public virtual string MetricType{get{ return "";}}
private int maximum;
public int Maximum{
get{
if(config.GetInt32(this.MetricType, "Maximum", -1) == -1)
{
config.SetValue(this.MetricType, "Maximum", this.MaximumDefault);
}
return config.GetInt32(this.MetricType, "Maximum");
}
set{
this.maximum = value;
}
}
private int minimum;
public int Minimum
{
get
{
if(config.GetInt32(this.MetricType, "Minimum", -1) == -1)
{
config.SetValue(this.MetricType, "Minimum", this.MinimumDefault);
}
return config.GetInt32(this.MetricType, "Minimum");
}
set
{
this.minimum = value;
}
}
private int localvalue;
public int Value
{
get
{
if(config.GetInt32(this.MetricObjectName, "Value", -1) == -1)
{
config.SetValue(this.MetricObjectName, "Value", this.DefaultValue);
}
return config.GetInt32(this.MetricObjectName, "Value");
}
set
{
this.localvalue = value;
}
}
private int smallChange;
public int SmallChange
{
get
{
if(config.GetInt32(this.MetricType, "SmallChange", -1) == -1)
{
config.SetValue(this.MetricType, "SmallChange", this.SmallChangeDefault);
}
return config.GetInt32(this.MetricType, "SmallChange");
}
set
{
this.smallChange = value;
}
}
private int largeChange;
public int LargeChange
{
get
{
if(config.GetInt32(this.MetricType, "LargeChange", -1) == -1)
{
config.SetValue(this.MetricType, "LargeChange", this.LargeChangeDefault);
}
return config.GetInt32(this.MetricType, "LargeChange");
}
set
{
this.largeChange = value;
}
}
}
public class StatementsMetricClass : MetricClasses
{
#region Default Values
public override int MaximumDefault{ get{return 100;}}
public override int MinimumDefault{ get{return 0;}}
public override int Increment{ get{ return 10; }}
public override int TickFrequency{ get{return 10;}}
public override int LargeChangeDefault{ get{return 10;}}
public override int SmallChangeDefault{ get{return 10;}}
#endregion
public override string MetricType{get{return "Statements";}}
public StatementsMetricClass(MetricDefaultValue metricDefaultValue, string metricRuleName): base (metricDefaultValue, metricRuleName){}
}
public class LocalVariablesMetricClass : MetricClasses
{
#region Default Values
public override int MaximumDefault{ get{return 20;}}
public override int MinimumDefault{ get{return 0;}}
public override int Increment{ get{ return 2; }}
public override int TickFrequency{ get{return 2;}}
public override int LargeChangeDefault{ get{return 1;}}
public override int SmallChangeDefault{ get{return 1;}}
#endregion
public override string MetricType{get{return "LocalVariables";}}
public LocalVariablesMetricClass(MetricDefaultValue metricDefaultValue, string metricRuleName): base (metricDefaultValue, metricRuleName){}
}
public class MethodArgumentsMetricClass : MetricClasses
{
#region Default Values
public override int MaximumDefault{ get{return 8;}}
public override int MinimumDefault{ get{return 0;}}
public override int Increment{ get{ return 1; }}
public override int TickFrequency{ get{return 1;}}
public override int LargeChangeDefault{ get{return 1;}}
public override int SmallChangeDefault{ get{return 1;}}
#endregion
public override string MetricType{get{return "MethodArguments";}}
public MethodArgumentsMetricClass(MetricDefaultValue metricDefaultValue, string metricRuleName): base (metricDefaultValue, metricRuleName){}
}
public class ReturnPointsMetricClass : MetricClasses
{
#region Default Values
public override int MaximumDefault{ get{return 8;}}
public override int MinimumDefault{ get{return 0;}}
public override int Increment{ get{ return 1; }}
public override int TickFrequency{ get{return 1;}}
public override int LargeChangeDefault{ get{return 1;}}
public override int SmallChangeDefault{ get{return 1;}}
#endregion
public override string MetricType{get{return "ReturnPoints";}}
public ReturnPointsMetricClass(MetricDefaultValue metricDefaultValue, string metricRuleName): base (metricDefaultValue, metricRuleName){}
}
public class CyclomaticClass : MetricClasses
{
#region Default Values
public override int MaximumDefault{ get{return 50;}}
public override int MinimumDefault{ get{return 0;}}
public override int Increment{ get{ return 10; }}
public override int TickFrequency{ get{return 10;}}
public override int LargeChangeDefault{ get{return 10;}}
public override int SmallChangeDefault{ get{return 10;}}
#endregion
public override string MetricType{get{return "Cyclomatic";}}
public CyclomaticClass(MetricDefaultValue metricDefaultValue, string metricRuleName): base (metricDefaultValue, metricRuleName){}
}
public class CyclomaticDensityMetricClass : MetricClasses
{
#region Default Values
public override int MaximumDefault{ get{return 8;}}
public override int MinimumDefault{ get{return 0;}}
public override int Increment{ get{ return 1; }}
public override int TickFrequency{ get{return 1;}}
public override int LargeChangeDefault{ get{return 1;}}
public override int SmallChangeDefault{ get{return 1;}}
#endregion
public override string MetricType{get{return "CyclomaticDensity";}}
public CyclomaticDensityMetricClass(MetricDefaultValue metricDefaultValue, string metricRuleName): base (metricDefaultValue, metricRuleName){}
}
[Serializable]
public class MetricDefaultValue : ISerializable
{
#region Serialization Support
// A method called when serializing
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("MethodArgumentsDefault", this.MethodArgumentsDefault);
info.AddValue("ReturnPointsDefault", this.ReturnPointsDefault);
info.AddValue("CyclomaticDefault", this.CyclomaticDefault);
info.AddValue("CyclomaticDensityDefault", this.CyclomaticDensityDefault);
info.AddValue("StatementsDefault", this.StatementsDefault);
info.AddValue("LocalVariablesDefault", this.LocalVariablesDefault);
}
private MetricDefaultValue(SerializationInfo info, StreamingContext context)
{
this.MethodArgumentsDefault = info.GetInt32("MethodArgumentsDefault");
this.ReturnPointsDefault = info.GetInt32("ReturnPointsDefault");
this.CyclomaticDefault = info.GetInt32("CyclomaticDefault");
this.CyclomaticDensityDefault = info.GetInt32("CyclomaticDensityDefault");
this.StatementsDefault = info.GetInt32("StatementsDefault");
this.LocalVariablesDefault = info.GetInt32("LocalVariablesDefault");
}
#endregion
private int StatementsDefault = 100;
private int LocalVariablesDefault = 20;
private int MethodArgumentsDefault = 8;
private int ReturnPointsDefault = 8;
private int CyclomaticDefault = 50;
private int CyclomaticDensityDefault = 6;
public int GetValue(string typeName)
{
int localValue = 0;
switch(typeName)
{
case "Statements":localValue = this.StatementsDefault; break;
case "LocalVariables":localValue = this.LocalVariablesDefault; break;
case "MethodArguments":localValue = this.MethodArgumentsDefault; break;
case "ReturnPoints":localValue = this.ReturnPointsDefault; break;
case "Cyclomatic":localValue = this.CyclomaticDefault; break;
case "CyclomaticDensity":localValue = this.CyclomaticDensityDefault; break;
}
return localValue;
}
public void SetValue(string typeName, int typeValue)
{
switch(typeName)
{
case "Statements": this.StatementsDefault = typeValue; break;
case "LocalVariables": this.LocalVariablesDefault = typeValue; break;
case "MethodArguments":this.MethodArgumentsDefault = typeValue; break;
case "ReturnPoints":this.ReturnPointsDefault = typeValue; break;
case "Cyclomatic":this.CyclomaticDefault = typeValue; break;
case "CyclomaticDensity":this.CyclomaticDensityDefault = typeValue; break;
}
}
public MetricDefaultValue(){}
}
}
|