diff --git a/VectoCommon/VectoCommon/Utils/DoubleExtensionMethods.cs b/VectoCommon/VectoCommon/Utils/DoubleExtensionMethods.cs index 1c8c8d126998c645b3eb7faffec7640b290050a7..0dc5c5fdda2e8bffc8106ff3bd1b1912705eb99c 100644 --- a/VectoCommon/VectoCommon/Utils/DoubleExtensionMethods.cs +++ b/VectoCommon/VectoCommon/Utils/DoubleExtensionMethods.cs @@ -29,278 +29,279 @@ * Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology */ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Globalization; -using System.Linq; -using System.Runtime.CompilerServices; - -namespace TUGraz.VectoCommon.Utils -{ - /// <summary> - /// Extension methods for double. - /// </summary> - public static class DoubleExtensionMethods - { - /// <summary> - /// The tolerance. - /// </summary> - public const double Tolerance = 1e-6; - - /// <summary> - /// The tolerancefactor for relative comparisons. - /// </summary> - public const double ToleranceFactor = 1e-6; - - /// <summary> - /// Determines whether the specified other is equal within tolerance. - /// </summary> - /// <param name="self">The self.</param> - /// <param name="other">The other.</param> - /// <param name="tolerance">The tolerance.</param> - /// <returns></returns> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsEqual(this double self, double other, double tolerance = Tolerance) - { - return Math.Abs(self - other) < tolerance; - } - - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsRelativeEqual(this SI expected, SI actual, double toleranceFactor = ToleranceFactor) - { - return IsRelativeEqual(expected.Value(), actual.Value(), toleranceFactor: toleranceFactor); - } - - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsRelativeEqual(this double expected, double actual, - double toleranceFactor = DoubleExtensionMethods.ToleranceFactor) - { - if (double.IsNaN(expected)) { - return double.IsNaN(actual); - } - - var ratio = expected.IsEqual(0, toleranceFactor) ? Math.Abs(actual) : Math.Abs(actual / expected - 1); - return ratio < toleranceFactor; - } - - /// <summary> - /// Determines whether the specified other is smaller within tolerance. - /// </summary> - /// <param name="self">The self.</param> - /// <param name="other">The other.</param> - /// <param name="tolerance">The tolerance.</param> - /// <returns></returns> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsSmaller(this double self, double other, double tolerance = Tolerance) - { - return self < other - tolerance; - } - - /// <summary> - /// Determines whether the specified other is smaller or equal within tolerance. - /// </summary> - /// <param name="self">The self.</param> - /// <param name="other">The other.</param> - /// <param name="tolerance">The tolerance.</param> - /// <returns></returns> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsSmallerOrEqual(this double self, double other, double tolerance = Tolerance) - { - return self <= other + tolerance; - } - - /// <summary> - /// Determines whether the specified other is greater within tolerance. - /// </summary> - /// <param name="self">The self.</param> - /// <param name="other">The other.</param> - /// <param name="tolerance">The tolerance.</param> - /// <returns></returns> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsGreater(this double self, double other, double tolerance = Tolerance) - { - return self > other + tolerance; - } - - /// <summary> - /// Determines whether the specified other is greater or equal within tolerance. - /// </summary> - /// <param name="self">The self.</param> - /// <param name="other">The other.</param> - /// <param name="tolerance">The tolerance.</param> - /// <returns></returns> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsGreaterOrEqual(this double self, double other, double tolerance = Tolerance) - { - return self >= other - tolerance; - } - - /// <summary> - /// Determines whether the specified tolerance is positive within tolerance. - /// </summary> - /// <param name="self">The self.</param> - /// <param name="tolerance">The tolerance.</param> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsPositive(this double self, double tolerance = Tolerance) - { - return self >= -tolerance; - } - - /// <summary> - /// Checks if a value is between min and max (min <= value <= max) - /// </summary> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsBetween(this double self, double min, double max) - { - return min <= self && self <= max; - } - - /// <summary> - /// Checks if a value is between min and max (min <= value <= max) - /// </summary> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsBetween(this double self, SI min, SI max) - { - return min <= self && self <= max; - } - - /// <summary> - /// Converts the double-value from RPM (rounds per minute) to the SI Unit PerSecond. - /// </summary> - /// <param name="self"></param> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static PerSecond RPMtoRad(this double self) - { - return SI<PerSecond>(self * 2 * Math.PI / 60.0); - } - - /// <summary> - /// Converts the double-value from RPM (rounds per minute) to the SI Unit PerSecond. - /// </summary> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static PerSecond RPMtoRad(this float self) - { - return SI<PerSecond>(self * 2 * Math.PI / 60.0); - } - - /// <summary> - /// Converts the value from rounds per minute to the SI Unit PerSecond - /// </summary> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static PerSecond RPMtoRad(this int self) - { - return SI<PerSecond>(self * 2.0 * Math.PI / 60.0); - } - - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static MeterPerSecond KMPHtoMeterPerSecond(this double self) - { - return SI<MeterPerSecond>(self / 3.6); - } - - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static MeterPerSecond KMPHtoMeterPerSecond(this int self) - { - return SI<MeterPerSecond>(self / 3.6); - } - - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static double ToDegree(this double self) - { - return self * 180.0 / Math.PI; - } - - /// <summary> - /// Creates an SI object for the number (unit-less: [-]). - /// </summary> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static SI SI(this double value) - { - return new SI(value); - } - - /// <summary> - /// Creates an templated SI object for the number. - /// </summary> - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T SI<T>(this double value) where T : SIBase<T> - { - return SIBase<T>.Create(value); - } - - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static IEnumerable<T> SI<T>(this IEnumerable<double> self) where T : SIBase<T> - { - return self.Select(x => x.SI<T>()); - } - - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string ToGUIFormat(this double self) - { - return self.ToString(CultureInfo.InvariantCulture); - } - - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string ToXMLFormat(this double self, uint? decimals = null) - { - decimals = decimals ?? 2; - return self.ToString("F" + decimals.Value); - } - - //[DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string ToMinSignificantDigits(this double self, uint? significant = null, uint? decimals = null) - { - significant = significant ?? 3; - decimals = decimals ?? 1; - var scale = Math.Ceiling(Math.Log10(Math.Abs(self))); - - if (double.IsInfinity(scale) || double.IsNaN(scale)) - return self.ToString("F" + decimals.Value); - - return self.ToString("F" + Math.Max(significant.Value - scale, decimals.Value)); - } - } - - public static class FloatExtensionMethods - { - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T SI<T>(this float value) where T : SIBase<T> - { - return SIBase<T>.Create(value); - } - } - - public static class IntegerExtensionMethods - { - [DebuggerStepThrough] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string ToGUIFormat(this int self) - { - return self.ToString(); - } - } +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.Linq; +using System.Runtime.CompilerServices; + +namespace TUGraz.VectoCommon.Utils +{ + /// <summary> + /// Extension methods for double. + /// </summary> + public static class DoubleExtensionMethods + { + /// <summary> + /// The tolerance. + /// </summary> + public const double Tolerance = 1e-6; + + /// <summary> + /// The tolerancefactor for relative comparisons. + /// </summary> + public const double ToleranceFactor = 1e-6; + + /// <summary> + /// Determines whether the specified other is equal within tolerance. + /// </summary> + /// <param name="self">The self.</param> + /// <param name="other">The other.</param> + /// <param name="tolerance">The tolerance.</param> + /// <returns></returns> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsEqual(this double self, double other, double tolerance = Tolerance) + { + return Math.Abs(self - other) < tolerance; + } + + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsRelativeEqual(this SI expected, SI actual, double toleranceFactor = ToleranceFactor) + { + return IsRelativeEqual(expected.Value(), actual.Value(), toleranceFactor: toleranceFactor); + } + + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsRelativeEqual(this double expected, double actual, + double toleranceFactor = DoubleExtensionMethods.ToleranceFactor) + { + if (double.IsNaN(expected)) { + return double.IsNaN(actual); + } + + var ratio = expected.IsEqual(0, toleranceFactor) ? Math.Abs(actual) : Math.Abs(actual / expected - 1); + return ratio < toleranceFactor; + } + + /// <summary> + /// Determines whether the specified other is smaller within tolerance. + /// </summary> + /// <param name="self">The self.</param> + /// <param name="other">The other.</param> + /// <param name="tolerance">The tolerance.</param> + /// <returns></returns> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsSmaller(this double self, double other, double tolerance = Tolerance) + { + return self < other - tolerance; + } + + /// <summary> + /// Determines whether the specified other is smaller or equal within tolerance. + /// </summary> + /// <param name="self">The self.</param> + /// <param name="other">The other.</param> + /// <param name="tolerance">The tolerance.</param> + /// <returns></returns> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsSmallerOrEqual(this double self, double other, double tolerance = Tolerance) + { + return self <= other + tolerance; + } + + /// <summary> + /// Determines whether the specified other is greater within tolerance. + /// </summary> + /// <param name="self">The self.</param> + /// <param name="other">The other.</param> + /// <param name="tolerance">The tolerance.</param> + /// <returns></returns> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsGreater(this double self, double other, double tolerance = Tolerance) + { + return self > other + tolerance; + } + + /// <summary> + /// Determines whether the specified other is greater or equal within tolerance. + /// </summary> + /// <param name="self">The self.</param> + /// <param name="other">The other.</param> + /// <param name="tolerance">The tolerance.</param> + /// <returns></returns> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsGreaterOrEqual(this double self, double other, double tolerance = Tolerance) + { + return self >= other - tolerance; + } + + /// <summary> + /// Determines whether the specified tolerance is positive within tolerance. + /// </summary> + /// <param name="self">The self.</param> + /// <param name="tolerance">The tolerance.</param> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsPositive(this double self, double tolerance = Tolerance) + { + return self >= -tolerance; + } + + /// <summary> + /// Checks if a value is between min and max (min <= value <= max) + /// </summary> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsBetween(this double self, double min, double max) + { + return min <= self && self <= max; + } + + /// <summary> + /// Checks if a value is between min and max (min <= value <= max) + /// </summary> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsBetween(this double self, SI min, SI max) + { + return min <= self && self <= max; + } + + /// <summary> + /// Converts the double-value from RPM (rounds per minute) to the SI Unit PerSecond. + /// </summary> + /// <param name="self"></param> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static PerSecond RPMtoRad(this double self) + { + return SI<PerSecond>(self * 2 * Math.PI / 60.0); + } + + /// <summary> + /// Converts the double-value from RPM (rounds per minute) to the SI Unit PerSecond. + /// </summary> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static PerSecond RPMtoRad(this float self) + { + return SI<PerSecond>(self * 2 * Math.PI / 60.0); + } + + /// <summary> + /// Converts the value from rounds per minute to the SI Unit PerSecond + /// </summary> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static PerSecond RPMtoRad(this int self) + { + return SI<PerSecond>(self * 2.0 * Math.PI / 60.0); + } + + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static MeterPerSecond KMPHtoMeterPerSecond(this double self) + { + return SI<MeterPerSecond>(self / 3.6); + } + + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static MeterPerSecond KMPHtoMeterPerSecond(this int self) + { + return SI<MeterPerSecond>(self / 3.6); + } + + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double ToDegree(this double self) + { + return self * 180.0 / Math.PI; + } + + /// <summary> + /// Creates an SI object for the number (unit-less: [-]). + /// </summary> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static SI SI(this double value) + { + return new SI(value); + } + + /// <summary> + /// Creates an templated SI object for the number. + /// </summary> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T SI<T>(this double value) where T : SIBase<T> + { + return SIBase<T>.Create(value); + } + + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static IEnumerable<T> SI<T>(this IEnumerable<double> self) where T : SIBase<T> + { + return self.Select(x => x.SI<T>()); + } + + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string ToGUIFormat(this double self) + { + return self.ToString(CultureInfo.InvariantCulture); + } + + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string ToXMLFormat(this double self, uint? decimals = null) + { + decimals = decimals ?? 2; + return self.ToString("F" + decimals.Value, CultureInfo.InvariantCulture); + } + + //[DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string ToMinSignificantDigits(this double self, uint? significant = null, uint? decimals = null) + { + significant = significant ?? 3; + decimals = decimals ?? 1; + var scale = Math.Ceiling(Math.Log10(Math.Abs(self))); + + if (double.IsInfinity(scale) || double.IsNaN(scale)) { + return self.ToString("F" + decimals.Value, CultureInfo.InvariantCulture); + } + + return self.ToString("F" + Math.Max(significant.Value - scale, decimals.Value), CultureInfo.InvariantCulture); + } + } + + public static class FloatExtensionMethods + { + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T SI<T>(this float value) where T : SIBase<T> + { + return SIBase<T>.Create(value); + } + } + + public static class IntegerExtensionMethods + { + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string ToGUIFormat(this int self) + { + return self.ToString(); + } + } } \ No newline at end of file diff --git a/VectoCore/VectoCore/OutputData/XML/XMLDeclarationWriter.cs b/VectoCore/VectoCore/OutputData/XML/XMLDeclarationWriter.cs index 87fb34848e02e6e620f8fc6ae3f8f5afe2c7bb26..709c66864d9be213919b137e9d8b77cac657237a 100644 --- a/VectoCore/VectoCore/OutputData/XML/XMLDeclarationWriter.cs +++ b/VectoCore/VectoCore/OutputData/XML/XMLDeclarationWriter.cs @@ -29,389 +29,390 @@ * Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology */ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Xml; -using System.Xml.Linq; -using TUGraz.IVT.VectoXML; -using TUGraz.IVT.VectoXML.Writer; -using TUGraz.VectoCommon.InputData; -using TUGraz.VectoCommon.Models; -using TUGraz.VectoCommon.Resources; -using TUGraz.VectoCommon.Utils; -using TUGraz.VectoCore.InputData.Reader; -using TUGraz.VectoCore.InputData.Reader.ComponentData; - -namespace TUGraz.VectoCore.OutputData.XML -{ - public class XMLDeclarationWriter : AbstractXMLWriter - { - private XNamespace componentNamespace; - //private readonly XNamespace _vectoNs = @"../../../API/VectoInput.xsd"; - - - public XMLDeclarationWriter(string vendor) : base(null, vendor) - { - tns = "urn:tugraz:ivt:VectoAPI:DeclarationDefinitions:v1.0"; - rootNamespace = "urn:tugraz:ivt:VectoAPI:DeclarationInput:v1.0"; - componentNamespace = "urn:tugraz:ivt:VectoAPI:DeclarationComponent:v1.0"; - } - - public XDocument GenerateVectoJob(IDeclarationInputDataProvider data) - { - var xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance"); - - //<tns:VectoInputDeclaration - // xmlns="urn:tugraz:ivt:VectoAPI:DeclarationDefinitions:v0.6" - // xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaVersion="0.6" - // xmlns:tns="urn:tugraz:ivt:VectoAPI:DeclarationInput:v0.6" - // xsi:schemaLocation="urn:tugraz:ivt:VectoAPI:DeclarationInput:v0.6 http://markus.quaritsch.at/VECTO/VectoInput.xsd"> - var job = new XDocument(); - job.Add(new XElement(rootNamespace + XMLNames.VectoInputDeclaration, - new XAttribute("schemaVersion", SchemaVersion), - new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName), - new XAttribute("xmlns", tns), - new XAttribute(XNamespace.Xmlns + "tns", rootNamespace), - new XAttribute(xsi + "schemaLocation", - string.Format("{0} {1}VectoInput.xsd", rootNamespace, SchemaLocationBaseUrl)), - CreateDeclarationJob(data)) - ); - return job; - } - - public XDocument GenerateVectoComponent(IGearboxDeclarationInputData data, - ITorqueConverterDeclarationInputData torqueConverter) - { - return GenerateComponentDocument(CreateGearbox(data, torqueConverter, componentNamespace)); - } - - public XDocument GenerateVectoComponent(IAxleGearInputData data) - { - return GenerateComponentDocument(CreateAxlegear(data, componentNamespace)); - } - - protected XDocument GenerateComponentDocument(XElement content) - { - var xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance"); - var component = new XDocument(); - component.Add(new XElement(componentNamespace + XMLNames.VectoInputDeclaration, - new XAttribute("schemaVersion", SchemaVersion), - new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName), - new XAttribute("xmlns", tns), - new XAttribute(XNamespace.Xmlns + "tns", componentNamespace), - new XAttribute(xsi + "schemaLocation", - string.Format("{0} {1}VectoComponent.xsd", componentNamespace, SchemaLocationBaseUrl)), - content) - ); - return component; - } - - protected XElement[] CreateDeclarationJob(IDeclarationInputDataProvider data) - { - return new[] { - CreateVehicle(data) - }; - } - - protected XElement CreateVehicle(IDeclarationInputDataProvider data) - { - var retarder = data.RetarderInputData; - var gearbox = data.GearboxInputData; - var vehicle = data.VehicleInputData; - var angledrive = data.AngledriveInputData; - - return new XElement(tns + XMLNames.Component_Vehicle, - new XAttribute(XMLNames.Component_ID_Attr, "VEH-" + vehicle.Model), - GetDefaultComponentElements(vehicle.CertificationNumber, vehicle.Model, "N.A."), - new XElement(tns + XMLNames.Vehicle_LegislativeClass, "N3"), - new XElement(tns + XMLNames.Vehicle_VehicleCategory, vehicle.VehicleCategory.ToXMLFormat()), - new XElement(tns + XMLNames.Vehicle_AxleConfiguration, vehicle.AxleConfiguration.GetName()), - new XElement(tns + XMLNames.Vehicle_CurbMassChassis, vehicle.CurbMassChassis.ToXMLFormat(0)), - new XElement(tns + XMLNames.Vehicle_GrossVehicleMass, vehicle.GrossVehicleMassRating.ToXMLFormat(0)), - new XElement(tns + XMLNames.Vehicle_IdlingSpeed, vehicle.EngineIdleSpeed.AsRPM.ToXMLFormat(0)), - new XElement(tns + XMLNames.Vehicle_RetarderType, retarder.Type.ToXMLFormat()), - retarder.Type.IsDedicatedComponent() - ? new XElement(tns + XMLNames.Vehicle_RetarderRatio, retarder.Ratio.ToXMLFormat(3)) - : null, - new XElement(tns + XMLNames.Vehicle_AngledriveType, angledrive.Type.ToXMLFormat()), - new XElement(tns + XMLNames.Vehicle_PTO, - new XElement(tns + XMLNames.Vehicle_PTO_ShaftsGearWheels, "none"), - new XElement(tns + XMLNames.Vehicle_PTO_OtherElements, "none")), - CreateTorqueLimits(vehicle), - new XElement(tns + XMLNames.Vehicle_Components, - CreateEngine(data.EngineInputData), - CreateGearbox(gearbox, gearbox.Type.AutomaticTransmission() ? data.TorqueConverterInputData : null), - angledrive.Type == AngledriveType.SeparateAngledrive ? CreateAngleDrive(angledrive) : null, - retarder.Type.IsDedicatedComponent() ? CreateRetarder(retarder) : null, - CreateAxlegear(data.AxleGearInputData), - CreateAxleWheels(data.VehicleInputData), - CreateAuxiliaries(data.AuxiliaryInputData()), - CreateAirdrag(data.AirdragInputData) - ) - ); - } - - - protected XElement CreateEngine(IEngineDeclarationInputData data, XNamespace ns = null) - { - var id = string.Format("ENG-{0}", data.Model.RemoveWhitespace()); - var fld = FullLoadCurveReader.Create(data.FullLoadCurve, true); - return new XElement((ns ?? tns) + XMLNames.Component_Engine, - //new XAttribute(XMLNames.Component_CertificationNumber_Attr, string.Format("ENG-{0}", data.Model)), - new XElement(tns + XMLNames.ComponentDataWrapper, - new XAttribute(XMLNames.Component_ID_Attr, id), - GetDefaultComponentElements(string.Format("ENG-{0}", data.Model), data.Model), - new XElement(tns + XMLNames.Engine_Displacement, (data.Displacement.Value() * 1000 * 1000).ToXMLFormat(0)), - new XElement(tns + XMLNames.Engine_IdlingSpeed, data.IdleSpeed.AsRPM.ToXMLFormat(0)), - new XElement(tns + XMLNames.Engine_RatedSpeed, fld.RatedSpeed.AsRPM.ToXMLFormat(0)), - new XElement(tns + XMLNames.Engine_RatedPower, fld.FullLoadStationaryPower(fld.RatedSpeed).ToXMLFormat(0)), - new XElement(tns + XMLNames.Engine_MaxTorque, fld.MaxTorque.ToXMLFormat(0)), - new XElement(tns + XMLNames.Engine_WHTCUrban, data.WHTCUrban.ToXMLFormat(4)), - new XElement(tns + XMLNames.Engine_WHTCRural, data.WHTCRural.ToXMLFormat(4)), - new XElement(tns + XMLNames.Engine_WHTCMotorway, data.WHTCMotorway.ToXMLFormat(4)), - new XElement(tns + XMLNames.Engine_ColdHotBalancingFactor, data.ColdHotBalancingFactor.ToXMLFormat(4)), - new XElement(tns + XMLNames.Engine_CorrectionFactor_RegPer, "1.0000"), - new XElement(tns + XMLNames.Engine_CorrecionFactor_NCV, "1.0000"), - new XElement(tns + XMLNames.Engine_FuelType, "Diesel CI"), - new XElement(tns + XMLNames.Engine_FuelConsumptionMap, - EmbedDataTable(data.FuelConsumptionMap, AttributeMappings.FuelConsumptionMapMapping)), - new XElement(tns + XMLNames.Engine_FullLoadAndDragCurve, - EmbedDataTable(data.FullLoadCurve, AttributeMappings.EngineFullLoadCurveMapping) - ) - ), - AddSignatureDummy(id) - ); - } - - - protected XElement CreateGearbox(IGearboxDeclarationInputData gbxData, - ITorqueConverterDeclarationInputData torqueConverter, XNamespace ns = null) - { - var gears = new XElement(tns + XMLNames.Gearbox_Gears); - var i = 1; - foreach (var gearData in gbxData.Gears) { - var gear = new XElement(tns + XMLNames.Gearbox_Gears_Gear, - new XAttribute(XMLNames.Gearbox_Gear_GearNumber_Attr, i++), - new XElement(tns + XMLNames.Gearbox_Gear_Ratio, gearData.Ratio.ToXMLFormat(3)), - gearData.MaxTorque != null - ? new XElement(tns + XMLNames.Gearbox_Gears_MaxTorque, gearData.MaxTorque.Value().ToXMLFormat(0)) - : null, - gearData.MaxInputSpeed != null - ? new XElement(tns + XMLNames.Gearbox_Gear_MaxSpeed, gearData.MaxInputSpeed.AsRPM.ToXMLFormat(0)) - : null, - new XElement(tns + XMLNames.Gearbox_Gear_TorqueLossMap, - EmbedDataTable(gearData.LossMap, AttributeMappings.TransmissionLossmapMapping)) - ); - gears.Add(gear); - } - var id = string.Format("GBX-{0}", gbxData.Model.RemoveWhitespace()); - return new XElement((ns ?? tns) + XMLNames.Component_Gearbox, - //new XAttribute(XMLNames.Component_CertificationNumber_Attr, string.Format("GBX-{0}", gbxData.Model)), - new XElement(tns + XMLNames.ComponentDataWrapper, - new XAttribute(XMLNames.Component_ID_Attr, id), - GetDefaultComponentElements(string.Format("GBX-{0}", gbxData.Model), gbxData.Model), - new XElement(tns + XMLNames.Gearbox_TransmissionType, gbxData.Type.ToXMLFormat()), - new XElement(tns + XMLNames.Component_Gearbox_CertificationMethod, "Standard values"), - gears - ), - AddSignatureDummy(id), - gbxData.Type.AutomaticTransmission() ? CreateTorqueConverter(torqueConverter) : null - ); - } - - - private XElement CreateTorqueConverter(ITorqueConverterDeclarationInputData data) - { - if (data == null) { - throw new Exception("Torque Converter is required!"); - } - var id = string.Format("TC-{0}", data.Model.RemoveWhitespace()); - return new XElement(tns + XMLNames.Component_TorqueConverter, - //new XAttribute(XMLNames.Component_CertificationNumber_Attr, id), - new XElement(tns + XMLNames.ComponentDataWrapper, - new XAttribute(XMLNames.Component_ID_Attr, id), - GetDefaultComponentElements(data.CertificationNumber, data.Model), - new XElement(tns + XMLNames.Component_CertificationMethod, "Standard values"), - new XElement(tns + XMLNames.TorqueConverter_Characteristics, - EmbedDataTable(data.TCData, AttributeMappings.TorqueConverterDataMapping, - precision: new Dictionary<string, uint>() { - { TorqueConverterDataReader.Fields.SpeedRatio, 4 } - }) - ) - ), - AddSignatureDummy(id)); - } - - private XElement CreateAngleDrive(IAngledriveInputData data, XNamespace ns = null) - { - var id = string.Format("ANGL-{0}", data.Model.RemoveWhitespace()); - return new XElement((ns ??tns) + XMLNames.Component_Angledrive, - //new XAttribute(XMLNames.Component_CertificationNumber_Attr, "ANGL-" + data.Model), - new XElement(tns + XMLNames.ComponentDataWrapper, - new XAttribute(XMLNames.Component_ID_Attr, id), - GetDefaultComponentElements(data.CertificationNumber, data.Model), - new XElement(tns + XMLNames.AngleDrive_Ratio, data.Ratio.ToXMLFormat(3)), - new XElement(tns + XMLNames.Component_CertificationMethod, "Standard values"), - new XElement(tns + XMLNames.AngleDrive_TorqueLossMap, - EmbedDataTable(data.LossMap, AttributeMappings.TransmissionLossmapMapping))), - AddSignatureDummy(id)); - } - - public XElement CreateRetarder(IRetarderInputData data, XNamespace ns = null) - { - var id = string.Format("RET-{0}", data.Model.RemoveWhitespace()); - return new XElement((ns ?? tns) + XMLNames.Component_Retarder, - //new XAttribute(XMLNames.Component_CertificationNumber_Attr, "RET-none"), - new XElement(tns + XMLNames.ComponentDataWrapper, - new XAttribute(XMLNames.Component_ID_Attr, id), - GetDefaultComponentElements(data.CertificationNumber, data.Model), - new XElement(tns + XMLNames.Component_CertificationMethod, "Standard values"), - new XElement(tns + XMLNames.Retarder_RetarderLossMap, - EmbedDataTable(data.LossMap, AttributeMappings.RetarderLossmapMapping) - ) - ), - AddSignatureDummy(id) - ); - } - - public XElement CreateAxlegear(IAxleGearInputData data, XNamespace ns = null) - { - var typeId = string.Format("AXLGEAR-{0:0.000}", data.Ratio); - return new XElement((ns ?? tns) + XMLNames.Component_Axlegear, - //new XAttribute(XMLNames.Component_CertificationNumber_Attr, string.Format("AXL-{0}", data.Model)), - new XElement(tns + XMLNames.ComponentDataWrapper, - new XAttribute(XMLNames.Component_ID_Attr, typeId), - GetDefaultComponentElements(typeId, "N.A."), - new XElement(tns + "LineType", "Single portal axle"), - new XElement(tns + XMLNames.Axlegear_Ratio, data.Ratio.ToXMLFormat(3)), - new XElement(tns + XMLNames.Component_CertificationMethod, "Standard values"), - new XElement(tns + XMLNames.Axlegear_TorqueLossMap, - EmbedDataTable(data.LossMap, AttributeMappings.TransmissionLossmapMapping))), - AddSignatureDummy(typeId) - ); - } - - public XElement CreateAxleWheels(IVehicleDeclarationInputData data, XNamespace ns = null) - { - var axleData = data.Axles; - var numAxles = axleData.Count; - var axles = new List<XElement>(numAxles); - for (var i = 0; i < numAxles; i++) { - var axle = axleData[i]; - axles.Add(new XElement(tns + XMLNames.AxleWheels_Axles_Axle, - new XAttribute(XMLNames.AxleWheels_Axles_Axle_AxleNumber_Attr, i + 1), - new XElement(tns + XMLNames.AxleWheels_Axles_Axle_AxleType_Attr, - i == 1 ? AxleType.VehicleDriven.ToString() : AxleType.VehicleNonDriven.ToString()), - new XElement(tns + XMLNames.AxleWheels_Axles_Axle_TwinTyres_Attr, axle.TwinTyres), - new XElement(tns + XMLNames.AxleWheels_Axles_Axle_Steered, i == 0), - CreateTyre(axle) - )); - } - - return new XElement((ns ??tns) + XMLNames.Component_AxleWheels, - new XElement(tns + XMLNames.ComponentDataWrapper, - //new XAttribute(XMLNames.Component_ID_Attr, - // string.Format("AXLWHL-{0}", data.AxleConfiguration.GetName())), - new XElement(tns + XMLNames.AxleWheels_Axles, axles)) - ); - } - - private XElement CreateTyre(IAxleDeclarationInputData axle) - { - var id = string.Format("TYRE-{0}", axle.Wheels).RemoveWhitespace().Replace("/", "_"); - return new XElement(tns + "Tyre", - //new XAttribute(XMLNames.Component_CertificationNumber_Attr, id), - new XElement(tns + XMLNames.ComponentDataWrapper, - new XAttribute(XMLNames.Component_ID_Attr, id), - GetDefaultComponentElements(string.Format("TYRE-{0}", axle.Wheels), axle.Wheels), - new XElement(tns + XMLNames.AxleWheels_Axles_Axle_Dimension, axle.Wheels), - new XElement(tns + XMLNames.AxleWheels_Axles_Axle_RRCDeclared, axle.RollResistanceCoefficient.ToXMLFormat(4)), - new XElement(tns + XMLNames.AxleWheels_Axles_Axle_FzISO, axle.TyreTestLoad.Value().ToXMLFormat(0)) - ), - AddSignatureDummy(id)); - } - - public XElement CreateAuxiliaries(IAuxiliariesDeclarationInputData data) - { - var auxList = new Dictionary<AuxiliaryType, XElement>(); - foreach (var auxData in data.Auxiliaries) { - var entry = new XElement(tns + AuxTypeToXML(auxData.Type), - auxData.Technology.Select(x => new XElement(tns + XMLNames.Auxiliaries_Auxiliary_Technology, x)).ToArray<object>()); - auxList[auxData.Type] = entry; - } - var aux = new XElement(tns + XMLNames.ComponentDataWrapper); - foreach ( - var key in - new[] { - AuxiliaryType.Fan, AuxiliaryType.SteeringPump, AuxiliaryType.ElectricSystem, AuxiliaryType.PneumaticSystem, - AuxiliaryType.HVAC - }) { - aux.Add(auxList[key]); - } - return new XElement(tns + XMLNames.Component_Auxiliaries, aux); - } - - private XElement CreateAirdrag(IAirdragDeclarationInputData data, XNamespace ns = null) - { - var id = string.Format("Airdrag-{0}", data.Model); - return new XElement((ns ?? tns) + XMLNames.Component_AirDrag, - //new XAttribute(XMLNames.Component_CertificationNumber_Attr, string.Format("AD-{0}", data.Model)), - new XElement(tns + XMLNames.ComponentDataWrapper, - new XAttribute(XMLNames.Component_ID_Attr, id), - GetDefaultComponentElements(data.Model, "N.A."), - new XElement(tns + "CdxA_0", data.AirDragArea.Value().ToXMLFormat(2)), // TODO - new XElement(tns + "TransferredCdxA", data.AirDragArea.Value().ToXMLFormat(2)), // TODO - new XElement(tns + XMLNames.AirDrag_DeclaredCdxA, data.AirDragArea.Value().ToXMLFormat(2))), - AddSignatureDummy(id) - ); - } - - private string AuxTypeToXML(AuxiliaryType type) - { - return type.ToString(); - } - - - private XElement AddSignatureDummy(string id) - { - return new XElement(tns + XMLNames.DI_Signature, - new XElement(di + XMLNames.DI_Signature_Reference, - new XAttribute(XMLNames.DI_Signature_Reference_URI_Attr, "#" + id), - new XElement(di + XMLNames.DI_Signature_Reference_Transforms, - new XElement(di + XMLNames.DI_Signature_Reference_Transforms_Transform, - new XAttribute(XMLNames.DI_Signature_Algorithm_Attr, - "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithoutComments")), - new XElement(di + XMLNames.DI_Signature_Reference_Transforms_Transform, - new XAttribute(XMLNames.DI_Signature_Algorithm_Attr, "urn:vecto:xml:2017:canonicalization")) - ), - new XElement(di + XMLNames.DI_Signature_Reference_DigestMethod, - new XAttribute(XMLNames.DI_Signature_Algorithm_Attr, "http://www.w3.org/2001/04/xmlenc#sha256")), - new XElement(di + XMLNames.DI_Signature_Reference_DigestValue, "") - ) - ); - } - - protected XElement[] GetDefaultComponentElements(string componentId, string makeAndModel) - { - return new[] { - new XElement(tns + XMLNames.Component_Manufacturer, Vendor), - new XElement(tns + XMLNames.Component_Model, makeAndModel), - new XElement(tns + XMLNames.Component_CertificationNumber, componentId), - new XElement(tns + XMLNames.Component_Date, XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.Utc)), - new XElement(tns + XMLNames.Component_AppVersion, "VectoCore"), - }; - } - - protected XElement[] GetDefaultComponentElements(string vin, string makeAndModel, string address) - { - return new[] { - new XElement(tns + XMLNames.Component_Manufacturer, Vendor), - new XElement(tns + XMLNames.Component_ManufacturerAddress, address), - new XElement(tns + XMLNames.Component_Model, makeAndModel), - new XElement(tns + XMLNames.Vehicle_VIN, vin), - new XElement(tns + XMLNames.Component_Date, XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.Utc)), - }; - } - } +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Xml; +using System.Xml.Linq; +using TUGraz.IVT.VectoXML; +using TUGraz.IVT.VectoXML.Writer; +using TUGraz.VectoCommon.InputData; +using TUGraz.VectoCommon.Models; +using TUGraz.VectoCommon.Resources; +using TUGraz.VectoCommon.Utils; +using TUGraz.VectoCore.InputData.Reader; +using TUGraz.VectoCore.InputData.Reader.ComponentData; + +namespace TUGraz.VectoCore.OutputData.XML +{ + public class XMLDeclarationWriter : AbstractXMLWriter + { + private XNamespace componentNamespace; + //private readonly XNamespace _vectoNs = @"../../../API/VectoInput.xsd"; + + + public XMLDeclarationWriter(string vendor) : base(null, vendor) + { + tns = "urn:tugraz:ivt:VectoAPI:DeclarationDefinitions:v1.0"; + rootNamespace = "urn:tugraz:ivt:VectoAPI:DeclarationInput:v1.0"; + componentNamespace = "urn:tugraz:ivt:VectoAPI:DeclarationComponent:v1.0"; + } + + public XDocument GenerateVectoJob(IDeclarationInputDataProvider data) + { + var xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance"); + + //<tns:VectoInputDeclaration + // xmlns="urn:tugraz:ivt:VectoAPI:DeclarationDefinitions:v0.6" + // xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaVersion="0.6" + // xmlns:tns="urn:tugraz:ivt:VectoAPI:DeclarationInput:v0.6" + // xsi:schemaLocation="urn:tugraz:ivt:VectoAPI:DeclarationInput:v0.6 http://markus.quaritsch.at/VECTO/VectoInput.xsd"> + var job = new XDocument(); + job.Add(new XElement(rootNamespace + XMLNames.VectoInputDeclaration, + new XAttribute("schemaVersion", SchemaVersion), + new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName), + new XAttribute("xmlns", tns), + new XAttribute(XNamespace.Xmlns + "tns", rootNamespace), + new XAttribute(xsi + "schemaLocation", + string.Format("{0} {1}VectoInput.xsd", rootNamespace, SchemaLocationBaseUrl)), + CreateDeclarationJob(data)) + ); + return job; + } + + public XDocument GenerateVectoComponent(IGearboxDeclarationInputData data, + ITorqueConverterDeclarationInputData torqueConverter) + { + return GenerateComponentDocument(CreateGearbox(data, torqueConverter, componentNamespace)); + } + + public XDocument GenerateVectoComponent(IAxleGearInputData data) + { + return GenerateComponentDocument(CreateAxlegear(data, componentNamespace)); + } + + protected XDocument GenerateComponentDocument(XElement content) + { + var xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance"); + var component = new XDocument(); + component.Add(new XElement(componentNamespace + XMLNames.VectoInputDeclaration, + new XAttribute("schemaVersion", SchemaVersion), + new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName), + new XAttribute("xmlns", tns), + new XAttribute(XNamespace.Xmlns + "tns", componentNamespace), + new XAttribute(xsi + "schemaLocation", + string.Format("{0} {1}VectoComponent.xsd", componentNamespace, SchemaLocationBaseUrl)), + content) + ); + return component; + } + + protected XElement[] CreateDeclarationJob(IDeclarationInputDataProvider data) + { + return new[] { + CreateVehicle(data) + }; + } + + protected XElement CreateVehicle(IDeclarationInputDataProvider data) + { + var retarder = data.RetarderInputData; + var gearbox = data.GearboxInputData; + var vehicle = data.VehicleInputData; + var angledrive = data.AngledriveInputData; + + return new XElement(tns + XMLNames.Component_Vehicle, + new XAttribute(XMLNames.Component_ID_Attr, "VEH-" + vehicle.Model), + GetDefaultComponentElements(vehicle.CertificationNumber, vehicle.Model, "N.A."), + new XElement(tns + XMLNames.Vehicle_LegislativeClass, "N3"), + new XElement(tns + XMLNames.Vehicle_VehicleCategory, vehicle.VehicleCategory.ToXMLFormat()), + new XElement(tns + XMLNames.Vehicle_AxleConfiguration, vehicle.AxleConfiguration.GetName()), + new XElement(tns + XMLNames.Vehicle_CurbMassChassis, vehicle.CurbMassChassis.ToXMLFormat(0)), + new XElement(tns + XMLNames.Vehicle_GrossVehicleMass, vehicle.GrossVehicleMassRating.ToXMLFormat(0)), + new XElement(tns + XMLNames.Vehicle_IdlingSpeed, vehicle.EngineIdleSpeed.AsRPM.ToXMLFormat(0)), + new XElement(tns + XMLNames.Vehicle_RetarderType, retarder.Type.ToXMLFormat()), + retarder.Type.IsDedicatedComponent() + ? new XElement(tns + XMLNames.Vehicle_RetarderRatio, retarder.Ratio.ToXMLFormat(3)) + : null, + new XElement(tns + XMLNames.Vehicle_AngledriveType, angledrive.Type.ToXMLFormat()), + new XElement(tns + XMLNames.Vehicle_PTO, + new XElement(tns + XMLNames.Vehicle_PTO_ShaftsGearWheels, "none"), + new XElement(tns + XMLNames.Vehicle_PTO_OtherElements, "none")), + CreateTorqueLimits(vehicle), + new XElement(tns + XMLNames.Vehicle_Components, + CreateEngine(data.EngineInputData), + CreateGearbox(gearbox, gearbox.Type.AutomaticTransmission() ? data.TorqueConverterInputData : null), + angledrive.Type == AngledriveType.SeparateAngledrive ? CreateAngleDrive(angledrive) : null, + retarder.Type.IsDedicatedComponent() ? CreateRetarder(retarder) : null, + CreateAxlegear(data.AxleGearInputData), + CreateAxleWheels(data.VehicleInputData), + CreateAuxiliaries(data.AuxiliaryInputData()), + CreateAirdrag(data.AirdragInputData) + ) + ); + } + + + protected XElement CreateEngine(IEngineDeclarationInputData data, XNamespace ns = null) + { + var id = string.Format("ENG-{0}", data.Model.RemoveWhitespace()); + var fld = FullLoadCurveReader.Create(data.FullLoadCurve, true); + return new XElement((ns ?? tns) + XMLNames.Component_Engine, + //new XAttribute(XMLNames.Component_CertificationNumber_Attr, string.Format("ENG-{0}", data.Model)), + new XElement(tns + XMLNames.ComponentDataWrapper, + new XAttribute(XMLNames.Component_ID_Attr, id), + GetDefaultComponentElements(string.Format("ENG-{0}", data.Model), data.Model), + new XElement(tns + XMLNames.Engine_Displacement, (data.Displacement.Value() * 1000 * 1000).ToXMLFormat(0)), + new XElement(tns + XMLNames.Engine_IdlingSpeed, data.IdleSpeed.AsRPM.ToXMLFormat(0)), + new XElement(tns + XMLNames.Engine_RatedSpeed, fld.RatedSpeed.AsRPM.ToXMLFormat(0)), + new XElement(tns + XMLNames.Engine_RatedPower, fld.FullLoadStationaryPower(fld.RatedSpeed).ToXMLFormat(0)), + new XElement(tns + XMLNames.Engine_MaxTorque, fld.MaxTorque.ToXMLFormat(0)), + new XElement(tns + XMLNames.Engine_WHTCUrban, data.WHTCUrban.ToXMLFormat(4)), + new XElement(tns + XMLNames.Engine_WHTCRural, data.WHTCRural.ToXMLFormat(4)), + new XElement(tns + XMLNames.Engine_WHTCMotorway, data.WHTCMotorway.ToXMLFormat(4)), + new XElement(tns + XMLNames.Engine_ColdHotBalancingFactor, data.ColdHotBalancingFactor.ToXMLFormat(4)), + new XElement(tns + XMLNames.Engine_CorrectionFactor_RegPer, "1.0000"), + new XElement(tns + XMLNames.Engine_CorrecionFactor_NCV, "1.0000"), + new XElement(tns + XMLNames.Engine_FuelType, "Diesel CI"), + new XElement(tns + XMLNames.Engine_FuelConsumptionMap, + EmbedDataTable(data.FuelConsumptionMap, AttributeMappings.FuelConsumptionMapMapping)), + new XElement(tns + XMLNames.Engine_FullLoadAndDragCurve, + EmbedDataTable(data.FullLoadCurve, AttributeMappings.EngineFullLoadCurveMapping) + ) + ), + AddSignatureDummy(id) + ); + } + + + protected XElement CreateGearbox(IGearboxDeclarationInputData gbxData, + ITorqueConverterDeclarationInputData torqueConverter, XNamespace ns = null) + { + var gears = new XElement(tns + XMLNames.Gearbox_Gears); + var i = 1; + foreach (var gearData in gbxData.Gears) { + var gear = new XElement(tns + XMLNames.Gearbox_Gears_Gear, + new XAttribute(XMLNames.Gearbox_Gear_GearNumber_Attr, i++), + new XElement(tns + XMLNames.Gearbox_Gear_Ratio, gearData.Ratio.ToXMLFormat(3)), + gearData.MaxTorque != null + ? new XElement(tns + XMLNames.Gearbox_Gears_MaxTorque, gearData.MaxTorque.Value().ToXMLFormat(0)) + : null, + gearData.MaxInputSpeed != null + ? new XElement(tns + XMLNames.Gearbox_Gear_MaxSpeed, gearData.MaxInputSpeed.AsRPM.ToXMLFormat(0)) + : null, + new XElement(tns + XMLNames.Gearbox_Gear_TorqueLossMap, + EmbedDataTable(gearData.LossMap, AttributeMappings.TransmissionLossmapMapping)) + ); + gears.Add(gear); + } + var id = string.Format("GBX-{0}", gbxData.Model.RemoveWhitespace()); + return new XElement((ns ?? tns) + XMLNames.Component_Gearbox, + //new XAttribute(XMLNames.Component_CertificationNumber_Attr, string.Format("GBX-{0}", gbxData.Model)), + new XElement(tns + XMLNames.ComponentDataWrapper, + new XAttribute(XMLNames.Component_ID_Attr, id), + GetDefaultComponentElements(string.Format("GBX-{0}", gbxData.Model), gbxData.Model), + new XElement(tns + XMLNames.Gearbox_TransmissionType, gbxData.Type.ToXMLFormat()), + new XElement(tns + XMLNames.Component_Gearbox_CertificationMethod, "Standard values"), + gears + ), + AddSignatureDummy(id), + gbxData.Type.AutomaticTransmission() ? CreateTorqueConverter(torqueConverter) : null + ); + } + + + private XElement CreateTorqueConverter(ITorqueConverterDeclarationInputData data) + { + if (data == null) { + throw new Exception("Torque Converter is required!"); + } + var id = string.Format("TC-{0}", data.Model.RemoveWhitespace()); + return new XElement(tns + XMLNames.Component_TorqueConverter, + //new XAttribute(XMLNames.Component_CertificationNumber_Attr, id), + new XElement(tns + XMLNames.ComponentDataWrapper, + new XAttribute(XMLNames.Component_ID_Attr, id), + GetDefaultComponentElements(data.CertificationNumber, data.Model), + new XElement(tns + XMLNames.Component_CertificationMethod, "Standard values"), + new XElement(tns + XMLNames.TorqueConverter_Characteristics, + EmbedDataTable(data.TCData, AttributeMappings.TorqueConverterDataMapping, + precision: new Dictionary<string, uint>() { + { TorqueConverterDataReader.Fields.SpeedRatio, 4 } + }) + ) + ), + AddSignatureDummy(id)); + } + + private XElement CreateAngleDrive(IAngledriveInputData data, XNamespace ns = null) + { + var id = string.Format("ANGL-{0}", data.Model.RemoveWhitespace()); + return new XElement((ns ?? tns) + XMLNames.Component_Angledrive, + //new XAttribute(XMLNames.Component_CertificationNumber_Attr, "ANGL-" + data.Model), + new XElement(tns + XMLNames.ComponentDataWrapper, + new XAttribute(XMLNames.Component_ID_Attr, id), + GetDefaultComponentElements(data.CertificationNumber, data.Model), + new XElement(tns + XMLNames.AngleDrive_Ratio, data.Ratio.ToXMLFormat(3)), + new XElement(tns + XMLNames.Component_CertificationMethod, "Standard values"), + new XElement(tns + XMLNames.AngleDrive_TorqueLossMap, + EmbedDataTable(data.LossMap, AttributeMappings.TransmissionLossmapMapping))), + AddSignatureDummy(id)); + } + + public XElement CreateRetarder(IRetarderInputData data, XNamespace ns = null) + { + var id = string.Format("RET-{0}", data.Model.RemoveWhitespace()); + return new XElement((ns ?? tns) + XMLNames.Component_Retarder, + //new XAttribute(XMLNames.Component_CertificationNumber_Attr, "RET-none"), + new XElement(tns + XMLNames.ComponentDataWrapper, + new XAttribute(XMLNames.Component_ID_Attr, id), + GetDefaultComponentElements(data.CertificationNumber, data.Model), + new XElement(tns + XMLNames.Component_CertificationMethod, "Standard values"), + new XElement(tns + XMLNames.Retarder_RetarderLossMap, + EmbedDataTable(data.LossMap, AttributeMappings.RetarderLossmapMapping) + ) + ), + AddSignatureDummy(id) + ); + } + + public XElement CreateAxlegear(IAxleGearInputData data, XNamespace ns = null) + { + var typeId = string.Format("AXLGEAR-{0}", data.Ratio.ToString("F3", CultureInfo.InvariantCulture)); + return new XElement((ns ?? tns) + XMLNames.Component_Axlegear, + //new XAttribute(XMLNames.Component_CertificationNumber_Attr, string.Format("AXL-{0}", data.Model)), + new XElement(tns + XMLNames.ComponentDataWrapper, + new XAttribute(XMLNames.Component_ID_Attr, typeId), + GetDefaultComponentElements(typeId, "N.A."), + new XElement(tns + "LineType", "Single portal axle"), + new XElement(tns + XMLNames.Axlegear_Ratio, data.Ratio.ToXMLFormat(3)), + new XElement(tns + XMLNames.Component_CertificationMethod, "Standard values"), + new XElement(tns + XMLNames.Axlegear_TorqueLossMap, + EmbedDataTable(data.LossMap, AttributeMappings.TransmissionLossmapMapping))), + AddSignatureDummy(typeId) + ); + } + + public XElement CreateAxleWheels(IVehicleDeclarationInputData data, XNamespace ns = null) + { + var axleData = data.Axles; + var numAxles = axleData.Count; + var axles = new List<XElement>(numAxles); + for (var i = 0; i < numAxles; i++) { + var axle = axleData[i]; + axles.Add(new XElement(tns + XMLNames.AxleWheels_Axles_Axle, + new XAttribute(XMLNames.AxleWheels_Axles_Axle_AxleNumber_Attr, i + 1), + new XElement(tns + XMLNames.AxleWheels_Axles_Axle_AxleType_Attr, + i == 1 ? AxleType.VehicleDriven.ToString() : AxleType.VehicleNonDriven.ToString()), + new XElement(tns + XMLNames.AxleWheels_Axles_Axle_TwinTyres_Attr, axle.TwinTyres), + new XElement(tns + XMLNames.AxleWheels_Axles_Axle_Steered, i == 0), + CreateTyre(axle) + )); + } + + return new XElement((ns ?? tns) + XMLNames.Component_AxleWheels, + new XElement(tns + XMLNames.ComponentDataWrapper, + //new XAttribute(XMLNames.Component_ID_Attr, + // string.Format("AXLWHL-{0}", data.AxleConfiguration.GetName())), + new XElement(tns + XMLNames.AxleWheels_Axles, axles)) + ); + } + + private XElement CreateTyre(IAxleDeclarationInputData axle) + { + var id = string.Format("TYRE-{0}", axle.Wheels).RemoveWhitespace().Replace("/", "_"); + return new XElement(tns + "Tyre", + //new XAttribute(XMLNames.Component_CertificationNumber_Attr, id), + new XElement(tns + XMLNames.ComponentDataWrapper, + new XAttribute(XMLNames.Component_ID_Attr, id), + GetDefaultComponentElements(string.Format("TYRE-{0}", axle.Wheels), axle.Wheels), + new XElement(tns + XMLNames.AxleWheels_Axles_Axle_Dimension, axle.Wheels), + new XElement(tns + XMLNames.AxleWheels_Axles_Axle_RRCDeclared, axle.RollResistanceCoefficient.ToXMLFormat(4)), + new XElement(tns + XMLNames.AxleWheels_Axles_Axle_FzISO, axle.TyreTestLoad.Value().ToXMLFormat(0)) + ), + AddSignatureDummy(id)); + } + + public XElement CreateAuxiliaries(IAuxiliariesDeclarationInputData data) + { + var auxList = new Dictionary<AuxiliaryType, XElement>(); + foreach (var auxData in data.Auxiliaries) { + var entry = new XElement(tns + AuxTypeToXML(auxData.Type), + auxData.Technology.Select(x => new XElement(tns + XMLNames.Auxiliaries_Auxiliary_Technology, x)).ToArray<object>()); + auxList[auxData.Type] = entry; + } + var aux = new XElement(tns + XMLNames.ComponentDataWrapper); + foreach ( + var key in + new[] { + AuxiliaryType.Fan, AuxiliaryType.SteeringPump, AuxiliaryType.ElectricSystem, AuxiliaryType.PneumaticSystem, + AuxiliaryType.HVAC + }) { + aux.Add(auxList[key]); + } + return new XElement(tns + XMLNames.Component_Auxiliaries, aux); + } + + private XElement CreateAirdrag(IAirdragDeclarationInputData data, XNamespace ns = null) + { + var id = string.Format("Airdrag-{0}", data.Model); + return new XElement((ns ?? tns) + XMLNames.Component_AirDrag, + //new XAttribute(XMLNames.Component_CertificationNumber_Attr, string.Format("AD-{0}", data.Model)), + new XElement(tns + XMLNames.ComponentDataWrapper, + new XAttribute(XMLNames.Component_ID_Attr, id), + GetDefaultComponentElements(data.Model, "N.A."), + new XElement(tns + "CdxA_0", data.AirDragArea.Value().ToXMLFormat(2)), // TODO + new XElement(tns + "TransferredCdxA", data.AirDragArea.Value().ToXMLFormat(2)), // TODO + new XElement(tns + XMLNames.AirDrag_DeclaredCdxA, data.AirDragArea.Value().ToXMLFormat(2))), + AddSignatureDummy(id) + ); + } + + private string AuxTypeToXML(AuxiliaryType type) + { + return type.ToString(); + } + + + private XElement AddSignatureDummy(string id) + { + return new XElement(tns + XMLNames.DI_Signature, + new XElement(di + XMLNames.DI_Signature_Reference, + new XAttribute(XMLNames.DI_Signature_Reference_URI_Attr, "#" + id), + new XElement(di + XMLNames.DI_Signature_Reference_Transforms, + new XElement(di + XMLNames.DI_Signature_Reference_Transforms_Transform, + new XAttribute(XMLNames.DI_Signature_Algorithm_Attr, + "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithoutComments")), + new XElement(di + XMLNames.DI_Signature_Reference_Transforms_Transform, + new XAttribute(XMLNames.DI_Signature_Algorithm_Attr, "urn:vecto:xml:2017:canonicalization")) + ), + new XElement(di + XMLNames.DI_Signature_Reference_DigestMethod, + new XAttribute(XMLNames.DI_Signature_Algorithm_Attr, "http://www.w3.org/2001/04/xmlenc#sha256")), + new XElement(di + XMLNames.DI_Signature_Reference_DigestValue, "") + ) + ); + } + + protected XElement[] GetDefaultComponentElements(string componentId, string makeAndModel) + { + return new[] { + new XElement(tns + XMLNames.Component_Manufacturer, Vendor), + new XElement(tns + XMLNames.Component_Model, makeAndModel), + new XElement(tns + XMLNames.Component_CertificationNumber, componentId), + new XElement(tns + XMLNames.Component_Date, XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.Utc)), + new XElement(tns + XMLNames.Component_AppVersion, "VectoCore"), + }; + } + + protected XElement[] GetDefaultComponentElements(string vin, string makeAndModel, string address) + { + return new[] { + new XElement(tns + XMLNames.Component_Manufacturer, Vendor), + new XElement(tns + XMLNames.Component_ManufacturerAddress, address), + new XElement(tns + XMLNames.Component_Model, makeAndModel), + new XElement(tns + XMLNames.Vehicle_VIN, vin), + new XElement(tns + XMLNames.Component_Date, XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.Utc)), + }; + } + } } \ No newline at end of file