tbf/TestBenchFramework/BenchControl/TestMethods/CombinedWithDetection/CombinedWithDetTestParams.cs
2014-07-28 09:56:40 +02:00

178 lines
4.9 KiB
C#

using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using TBF.BenchControl.Generic;
using TBF.Resources;
namespace TBF.BenchControl.TestMethods.CombinedWithDetection
{
public class CombinedWithDetTestParams : IParamsProvider, ITestParams
{
public float TargetQ; /// Target=terminal flow durning detection when increasing or decreasing the flow
public float RelativeQfrom; /// Tested flow low range related to the detected flow
public float RelativeQto; /// Tested flow high range related to the detected flow
public float RateOfChange; /// Each step is calculated as (TargetQfrom - Qfrom) * RateOfChange, text form is displayed in % (*100)
public float DetectionThreshold;/// Drop of frequency for the detection, text form is displayed in % (*100)
string[] paramNames = new string[]
{
Strings.TargetQ,
Strings.RelativeQfrom,
Strings.RelativeQto,
Strings.RateOfChangePct,
Strings.DetectionThresholdPct,
};
public string ParamName(int i) { return paramNames[i]; }
public string ToString(int i)
{
switch (i)
{
case 0: return TargetQ.ToString();
case 1: return RelativeQfrom.ToString();
case 2: return RelativeQto.ToString();
case 3: return (100.0f * RateOfChange).ToString();
case 4: return (100.0f * DetectionThreshold).ToString();
default: return string.Empty;
}
}
public void UpdateParam(int i, string strValue)
{
switch (i)
{
case 0: TargetQ = Utils.ParseUFloat(strValue); return;
case 1: RelativeQfrom = Utils.ParseSFloat(strValue); return;
case 2: RelativeQto = Utils.ParseSFloat(strValue); return;
case 3: RateOfChange = Utils.ParseUFloat(strValue) / 100.0f; return;
case 4: DetectionThreshold = Utils.ParseUFloat(strValue) / 100.0f; return;
default: return;
}
}
public bool ValidateParam(int i, string strValue, out string message)
{
message = string.Empty;
float dummy;
switch (i)
{
case 0:
case 3:
case 4:
if (Utils.TryParseUFloat(strValue, out dummy)) return true;
break;
case 1:
case 2:
if (Utils.TryParseSFloat(strValue, out dummy)) return true;
break;
default:
message = "Invalid index";
return false;
}
message = ParamName(i) + " is invalid";
return false;
}
#region Boilerplate code
public int ParamsCount() { return paramNames.Length; }
public override string ToString()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ParamsCount(); i++)
{
if (i != 0) sb.Append("; ");
sb.Append(ParamName(i));
sb.Append("=");
sb.Append(ToString(i));
}
return sb.ToString();
}
[XmlIgnore]
public Entities.ComponentTest TestParamsEntity { get { return testParamsEntity; } }
Entities.ComponentTest testParamsEntity;
[XmlIgnore]
public string ComponentName { get { return componentName; } }
string componentName;
[XmlIgnore]
public Entities.Test Test { get { return test; } }
Entities.Test test;
/// Parameterless constructor
public CombinedWithDetTestParams()
{
TargetQ = 3.0f;
RelativeQfrom = -0.4f;
RelativeQto = -0.2f;
RateOfChange = 0.05f;
DetectionThreshold = 0.25f;
}
public CombinedWithDetTestParams(Entities.ComponentTest testParamsEntity, string componentName, Entities.Test test)
: this()
{
this.testParamsEntity = testParamsEntity;
this.componentName = componentName;
this.test = test;
}
public TBF.Entities.ComponentTest ToDbEntity(IComponent component, Entities.Test test)
{
using (var writer = new StringWriter())
{
/// Serialize this class with parameters
(new XmlSerializer(GetType())).Serialize(writer, this);
/// Create and return the ComponentTest entity
TBF.Entities.ComponentTest entity = new TBF.Entities.ComponentTest();
entity.CmpntName = component.Cfg.Name;
entity.Parameters = writer.ToString();
entity.Test = test;
return entity;
}
}
public void UpdateDbEntity()
{
using (var writer = new StringWriter())
{
/// Serialize this class and update the entity
(new XmlSerializer(GetType())).Serialize(writer, this);
testParamsEntity.Parameters = writer.ToString();
testParamsEntity.CmpntName = componentName;
testParamsEntity.Test = test;
}
}
public static IParamsProvider GetTestParams(Entities.ComponentTest testParamsEntity)
{
if (testParamsEntity == null) return new CombinedWithDetTestParams();
CombinedWithDetTestParams testParams;
try
{
testParams = (CombinedWithDetTestParams)(new XmlSerializer(typeof(CombinedWithDetTestParams)))
.Deserialize(new StringReader(testParamsEntity.Parameters));
}
catch
{
testParams = new CombinedWithDetTestParams();
}
testParams.testParamsEntity = testParamsEntity;
testParams.componentName = testParamsEntity.CmpntName;
testParams.test = testParamsEntity.Test;
return testParams;
}
#endregion
}
}