Files
DP44/Common/DTS.Common.Serialization/Test.cs
2026-04-17 14:55:32 -04:00

481 lines
19 KiB
C#

/*
Test.cs
Copyright © 2008
Diversified Technical Systems, Inc.
All Rights Reserved
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using DTS.Utilities;
using DTS.Utilities.DotNetProgrammingConstructs;
using DTS.Utilities.Xml;
namespace DTS.Serialization
{
/// <summary>
/// Representation of a serializable test information.
/// </summary>
[XmlSerializationTag( "Test" )]
public partial class Test : Exceptional, IXmlSerializable
{
private Property<string> _software
= new Property<string>(typeof(Test).Namespace + ".Test.Software", "DataPRO", true);
[XmlSerializationTag("Software")]
public string Software
{
get { return _software.Value; }
set { _software.Value = value; }
}
private Property<string> _softwareVersion
= new Property<string>(typeof(Test).Namespace + ".Test.SoftwareVersion", "", false);
[XmlSerializationTag("SoftwareVersion")]
public string SoftwareVersion
{
get { return _softwareVersion.Value; }
set { _softwareVersion.Value = value; }
}
/*
/// <summary>
/// The string ID of this test.
/// </summary>
[XmlSerializationTag("Id")]
public string Id
{
get { return _Id.Value; }
set { _Id.Value = value; }
}
private Property<string> _Id
= new Property<string>(typeof(Test).Namespace + ".Test.Id", "", false);*/
// Initialize an instance of the Test class.
public Test()
{
TryGetChannelOrder();
//
// Note that the parameterless constructor for this object will leave
// the Id and Description paramters
} //
public Test(string dtsfile)
{
}
/// <summary>
/// Initialize an instance of the Test class.
/// </summary>
///
/// <param name="id">
/// The <see cref="string"/> ID of this test.
/// </param>
///
/// <param name="description">
/// The <see cref="string"/> description of this test.
/// </param>
///
public Test(string id, string description)
{
TryGetChannelOrder();
try
{
Id = id;
Description = description;
}
catch (System.Exception ex)
{
throw new Exception( "encountered problem constructing Test object (Id: " + ( !string.IsNullOrEmpty( id ) ? id : "<<NULL>>" ) + "Description: " + ( !string.IsNullOrEmpty( description ) ? description : "<<NULL>>" ) + ")", ex );
}
}
/// <summary>
/// Get the date of this object's serialization's creation (if applicable).
/// </summary>
public DateTime InceptionDate
{
get { return _InceptionDate.Value; }
set { _InceptionDate.Value = value; }
}
private Property<DateTime> _InceptionDate
= new Property<DateTime>(
typeof( Test ).Namespace + ".Test.InceptionDate",
DateTime.Now,
false
);
private Dictionary<string, int> _channelOrder = new Dictionary<string, int>();
private static string GetID(Test.Module.Channel channel)
{
if (null != channel.SensorID && channel.SensorID.Length > 0) { return channel.SensorID; }
else { return channel.ChannelDescriptionString; }
}
public class ChannelOrderComparor : IComparer<Test.Module.Channel>
{
private IDictionary<string, int> _dictionary;
public ChannelOrderComparor(IDictionary<string, int> dictionary)
{
_dictionary = dictionary;
}
public int Compare(Test.Module.Channel a, Test.Module.Channel b)
{
string keyA = GetID(a);
string keyB = GetID(b);
int iA = _dictionary.ContainsKey(keyA) ? _dictionary[keyA] : int.MaxValue;
int iB = _dictionary.ContainsKey(keyB) ? _dictionary[keyB] : int.MaxValue;
return iA.CompareTo(iB);
}
}
public void TryGetChannelOrder()
{
try
{
_channelOrder.Clear();
if (System.IO.File.Exists("ChannelOrder.txt"))
{
using (System.IO.StreamReader sr = new System.IO.StreamReader("ChannelOrder.txt"))
{
string line;
int i = 0;
while ((line = sr.ReadLine()) != null)
{
_channelOrder.Add(line, i++);
}
}
}
}
catch (System.Exception ex)
{
Utilities.Logging.APILogger.Log("Exception getting channel order", ex);
}
}
/// <summary>
/// Get a named-DAS/numbered-channel accessor to this Event's channels.
/// </summary>
public List<Test.Module.Channel> Channels
{
get
{
try
{
if (_channelOrder.Count < 1) { TryGetChannelOrder(); }
List<Test.Module.Channel> allChannels = new List<Module.Channel>();
foreach (Test.Module testModule in this.Modules)
{
allChannels.AddRange(testModule.Channels);
allChannels.AddRange(testModule.CalculatedChannels);
}
allChannels.Sort(new Comparison<Module.Channel>(CompareChannels));
if (_channelOrder.Count > 0) { allChannels.Sort(new ChannelOrderComparor(_channelOrder)); }
return allChannels;
}
catch (System.Exception ex)
{
throw new Test.Exception("encountered problem getting all test channels", ex);
}
}
}
private int CompareChannels(Test.Module.Channel left, Test.Module.Channel right)
{
if (left == right) { return 0; }
if (null == left) { return -1; }
if (null == right) { return 1; }
int ret = left.AbsoluteDisplayOrder.CompareTo(right.AbsoluteDisplayOrder);
if (0 == ret)
{
ret = left.ParentModule.Number.CompareTo(right.ParentModule.Number);
}
if (0 == ret)
{
return left.Number.CompareTo(right.Number);
}
return ret;
}
/// <summary>
/// The string ID of this test.
/// </summary>
[XmlSerializationTag( "Id" )]
public string Id
{
get { return _Id.Value; }
set { _Id.Value = value; }
}
private Property<string> _Id
= new Property<string>( typeof( Test ).Namespace + ".Test.Id", "", false);
/// <summary>
/// The string description of this test.
/// </summary>
[XmlSerializationTag( "Description" )]
public string Description
{
get { return _Description.Value; }
set { _Description.Value = value; }
}
private Property<string> _Description
= new Property<string>( typeof( Test ).Namespace + ".Test.Description", "", false );
/// <summary>
/// The globally unique identification string for this test.
/// </summary>
[XmlSerializationTag( "Guid" )]
public Guid Guid
{
get { return _Guid.Value; }
set { _Guid.Value = value; }
}
private Property<Guid> _Guid
= new Property<Guid>( typeof( Test ).Namespace + ".Test.Guid", new Guid( "00000000-0000-0000-0000-000000000000" ), false );
/// <summary>
/// The globally unique identification string for this test.
/// </summary>
[XmlSerializationTag("FaultFlags")]
public UInt16 FaultFlags
{
get { return _FaultFlags.Value; }
set { _FaultFlags.Value = value; }
}
private Property<UInt16> _FaultFlags
= new Property<UInt16>(typeof(Test).Namespace + ".Test.FaultFlags", 0, false);
/// <summary>
/// Get/set inline serialized data switch.
/// </summary>
[XmlSerializationTag( "InlineSerializedData" )]
public bool InlineSerializedData
{
get { return _InlineSerializedData.Value; }
set
{
try
{
_InlineSerializedData.Value = value;
foreach ( Test.Module module in Modules )
module.InlineSerializedData = value;
}
catch ( System.Exception ex )
{
throw new Exception( "encountered problem setting test InlineSerializedData state", ex );
}
}
}
private Property<bool> _InlineSerializedData
= new Property<bool>( typeof( Test ).Namespace + ".Test.InlineSerializedData", false, true );
/// <summary>
/// The list of modules in this test.
/// </summary>
[XmlSerializationTag( "Modules" )]
public List<Module> Modules
{
get { return _Modules.Value; }
set { _Modules.Value = value; }
}
private Property<List<Module>> _Modules
= new Property<List<Module>>( typeof( Test ).Namespace + ".Test.Modules", new List<Test.Module>( ), true );
/// <summary>
/// Write XML serialization for this object to the specified writer.
/// </summary>
///
/// <param name="writer">
/// The <see cref="XmlWriter"/> to which this object's XML serialization
/// will be written.
/// </param>
///
public void WriteXml( XmlWriter writer )
{
try
{
AttributeExtractor<XmlSerializationTagAttribute> attributeExtractor = new AttributeExtractor<XmlSerializationTagAttribute>( );
writer.WriteAttributeString( attributeExtractor.ExtractAttachedAttributeFromProperty( this, "Id" ).Value, this.Id );
writer.WriteAttributeString( attributeExtractor.ExtractAttachedAttributeFromProperty( this, "Description" ).Value, this.Description );
writer.WriteAttributeString( attributeExtractor.ExtractAttachedAttributeFromProperty( this, "InlineSerializedData" ).Value, this.InlineSerializedData.ToString( ) );
writer.WriteAttributeString( attributeExtractor.ExtractAttachedAttributeFromProperty( this, "Guid" ).Value, this.Guid.ToString( ) );
writer.WriteAttributeString(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "FaultFlags").Value, this.FaultFlags.ToString());
writer.WriteAttributeString(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "Software").Value, this.Software.ToString());
writer.WriteAttributeString(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "SoftwareVersion").Value, this.SoftwareVersion.ToString());
writer.WriteStartElement ( attributeExtractor.ExtractAttachedAttributeFromProperty( this, "Modules" ).Value );
foreach (var module in Modules)
{
module.WriteXml(writer);
}
writer.WriteEndElement();
}
catch ( System.Exception ex )
{
throw new Exception( "encountered problem converting DTS.Serialization.Test object to XML", ex );
}
}
/// <summary>
/// Read XML serialization for this object from the specified reader.
/// </summary>
///
/// <param name="reader">
/// The <see cref="XmlReader"/> from which this object's XML serialization
/// will be read.
/// </param>
///
public void ReadXml( XmlReader reader )
{
try
{
AttributeExtractor<XmlSerializationTagAttribute> attributeExtractor = new AttributeExtractor<XmlSerializationTagAttribute>();
if (reader.IsStartElement("Test"))
{
this.Id = reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "Id").Value);
this.Description = reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "Description").Value);
this.InlineSerializedData = bool.Parse(reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "InlineSerializedData").Value));
try { this.Guid = new Guid(reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "Guid").Value)); }
catch (System.Exception) { this.Guid = new Guid("00000000-0000-0000-0000-000000000000"); }
try { this.FaultFlags = Convert.ToUInt16(reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "FaultFlags").Value)); }
catch (System.Exception) { this.FaultFlags = 0; }
try { this.Software = reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "Software").Value); }
catch (System.Exception) { Software = "unknown"; }
try { this.SoftwareVersion = reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(this, "SoftwareVersion").Value); }
catch (System.Exception) { SoftwareVersion = "unknown"; }
this.Modules.Clear();
if (reader.ReadToDescendant("Modules"))
{
if (reader.ReadToDescendant("Module"))
{
do
{
Test.Module deserializedModule = new Test.Module(this);
deserializedModule.InlineSerializedData = InlineSerializedData;
//Give the deserializer a separate reader so that it can't go amuck and read past the current module.
deserializedModule.ReadXml(reader.ReadSubtree());
this.Modules.Add(deserializedModule);
}
while (reader.ReadToNextSibling("Module"));
}
}
}
}
catch (System.Exception ex)
{
throw new Exception("encountered problem converting XML to DTS.Serialization.Test object", ex);
}
}
/// <summary>
/// Should normally return a schema representing the form of the XML
/// generated/consumed by WriteXml/ReadXml, but it never called during
/// the serialization process so ours just returns null.
/// </summary>
///
/// <returns>
/// Null <see cref="XmlSchema"/> reference, always.
/// </returns>
///
public XmlSchema GetSchema( )
{
// This method is never invoked during XML object serialization.
return null;
}
/// <summary>
/// Test the specified object for equality with this object.
/// </summary>
///
/// <param name="obj">
/// The <see cref="object"/> to be tested for equality.
/// </param>
///
/// <returns>
/// <see cref="bool"/> true if the specified object has memeberwise equality with
/// this object; false otherwise.
/// </returns>
///
public override bool Equals( object obj )
{
try
{
Test that = obj as Test;
return null != that
&& this.Id.Equals( that.Id )
&& this.Description.Equals( that.Description )
&& this.ModulesEquals( that.Modules );
}
catch ( System.Exception ex )
{
throw new Exception( "encountered problem equality testing object " + ( null != obj ? "\"" + obj.ToString( ) + "\"" : "<<NULL>>" ), ex );
}
}
/// <summary>
/// Test the specified object's module list for equality with this object's
/// module list.
/// </summary>
///
/// <param name="thoseModules">
/// The <see cref="List"/> of <see cref="Dts.Serialization.Test"/> object to be
/// compared for equality with this test's equivalent.
/// </param>
///
/// <returns>
/// <see cref="bool"/> true if the two lists contain equivalent-valued members;
/// false otherwise.
/// </returns>
///
private bool ModulesEquals( List<Test.Module> thoseModules )
{
try
{
if ( this.Modules.Count != thoseModules.Count )
return false;
else for ( int i=0; i < thoseModules.Count; i++ )
if ( !this.Modules[ i ].Equals( thoseModules[ i ] ) )
return false;
return true;
}
catch ( System.Exception ex )
{
throw new Exception( "encountered problem equality-testing module list", ex );
}
}
/// <summary>
/// Return the hash code for this object.
/// </summary>
///
/// <returns>
/// The <see cref="int"/> hash code for this object.
/// </returns>
///
public override int GetHashCode( )
{
return base.GetHashCode( );
}
}
}