Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS has been phased out. To see alternatives please check here

Skip to content
Snippets Groups Projects
Commit 422d4fb6 authored by Markus Quaritsch's avatar Markus Quaritsch
Browse files

introducing InputDataProvider and additional interfaces, Json parser refactored

parent e70d1371
No related branches found
No related tags found
No related merge requests found
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
using TUGraz.VectoCore.Exceptions;
using TUGraz.VectoCore.Models.Declaration;
using TUGraz.VectoCore.Models.SimulationComponent.Data;
using TUGraz.VectoCore.Utils;
namespace TUGraz.VectoCore.InputData.FileIO.JSON
{
public abstract class JSONFile
{
private string _basePath;
protected JObject Header;
protected JObject Body;
protected JSONFile(JObject data, string filename)
{
Header = (JObject)data["Header"];
Body = (JObject)data["Body"];
BasePath = filename;
}
public int FileVersion
{
get { return Header[JsonKeys.JsonHeader_FileVersion].Value<int>(); }
}
public bool SavedInDeclarationMode
{
get { return Body[JsonKeys.SavedInDeclMode].Value<bool>(); }
}
internal string BasePath
{
get { return _basePath; }
set { _basePath = Path.GetDirectoryName(Path.GetFullPath(value)); }
}
}
public class JSONInputDataV2 : JSONFile, IInputDataProvider, IJobInputData, IDriverInputData
{
protected IGearboxInputData Gearbox;
protected IAxleGearInputData AxleGear;
protected IEngineInputData Engine;
protected IVehicleInputData VehicleData;
protected IRetarderInputData Retarder;
public JSONInputDataV2(JObject data, string filename) : base(data, filename)
{
Gearbox = JSONInputDataFactory.ReadGearbox(
Path.Combine(BasePath, Body[JsonKeys.Vehicle_GearboxFile].Value<string>()));
var axleGear = Gearbox as IAxleGearInputData;
if (axleGear != null) {
AxleGear = axleGear;
}
Engine = JSONInputDataFactory.ReadEngine(
Path.Combine(BasePath, Body[JsonKeys.Vehicle_EngineFile].Value<string>()));
VehicleData = JSONInputDataFactory.ReadJsonVehicle(
Path.Combine(BasePath, Body[JsonKeys.Vehicle_VehicleFile].Value<string>()));
var retarder = VehicleData as IRetarderInputData;
if (retarder != null) {
Retarder = retarder;
}
}
#region IInputDataProvider
public IJobInputData JobInputData()
{
return this;
}
public IVehicleInputData VehicleInputData
{
get { return VehicleData; }
}
public IGearboxInputData GearboxInputData
{
get { return Gearbox; }
}
public IAxleGearInputData AxleGearInputData
{
get { return AxleGear; }
}
public IEngineInputData EngineInputData
{
get { return Engine; }
}
public IEnumerable<IAuxiliaryInputData> AuxiliaryInputData()
{
return null;
}
public IRetarderInputData RetarderInputData
{
get { return Retarder; }
}
public IDriverInputData DriverInputData
{
get { return this; }
}
#endregion
#region IJobInputData
public IVehicleInputData Vehicle
{
get { return VehicleData; }
}
public IList<DataTable> Cycles
{
get
{
return Body["Cycles"].Select(cycle => VectoCSVFile.Read(Path.Combine(BasePath, cycle.Value<string>()))).ToList();
}
}
public bool EngineOnlyMode
{
get { return Body["EngineOnlyMode"].Value<bool>(); }
}
#endregion
}
public class JSONVehicleDataV7 : JSONFile, IVehicleInputData, IRetarderInputData
{
public JSONVehicleDataV7(JObject data, string fileName) : base(data, fileName) {}
#region IVehicleInputData
public VehicleCategory VehicleCategory
{
get
{
return
(VehicleCategory)Enum.Parse(typeof(VehicleCategory), Body[JsonKeys.Vehicle_VehicleCategory].Value<string>(), true);
}
}
public Kilogram CurbWeight
{
get { return Body[JsonKeys.Vehicle_CurbWeight].Value<double>().SI<Kilogram>(); }
}
public Kilogram GrossVehicleMassRating
{
get { return Body[JsonKeys.Vehicle_GrossVehicleMassRating].Value<double>().SI<Ton>().Cast<Kilogram>(); }
}
public SquareMeter DragCoefficient
{
get { return Body["CdA"].Value<double>().SI<SquareMeter>(); }
}
public SquareMeter DragCoefficientRigidTruck
{
get { return Body["CdA2"].Value<double>().SI<SquareMeter>(); }
}
public string Rim
{
get { return Body["Rim"].Value<string>(); }
}
public AxleConfiguration AxleConfiguration
{
get { return AxleConfigurationHelper.Parse(Body["AxleConfig"]["Type"].Value<string>()); }
}
public IList<IAxleInputData> Axles
{
get
{
return Body["AxleConfig"]["Axles"].Select(axle => new JSONAxleInputData() {
Inertia = axle["Inertia"].Value<double>().SI<KilogramSquareMeter>(),
Wheels = axle["Wheels"].Value<string>(),
TwinTyres = axle["TwinTyres"].Value<bool>(),
RollResistanceCoefficient = axle["RRCISO"].Value<double>(),
TyreTestLoad = axle["FzISO"].Value<double>().SI<Newton>()
}).Cast<IAxleInputData>().ToList();
}
}
#endregion
#region IRetarderInputData
public RetarderData.RetarderType Type
{
get
{
return
(RetarderData.RetarderType)
Enum.Parse(typeof(RetarderData.RetarderType), Body["Retarder"]["Type"].Value<string>(), true);
}
}
public double Ratio
{
get { return Body["Retarder"]["Ratio"].Value<double>(); }
}
public DataTable LossMap
{
get
{
var filename = Body["Retarder"]["File"].Value<string>();
if (filename == null || !filename.Any() || filename.Equals("<NOFILE>", StringComparison.InvariantCultureIgnoreCase)) {
throw new VectoException("Invalid Lossmap: {0}", filename);
}
return VectoCSVFile.Read(Path.Combine(BasePath, filename));
}
}
#endregion
}
public class JSONEngineDataV3 : JSONFile, IEngineInputData
{
public JSONEngineDataV3(JObject data, string fileName) : base(data, fileName) {}
public string ModelName
{
get { return Body["ModelName"].Value<string>(); }
}
public CubicMeter Displacement
{
get { return Body["Displacement"].Value<double>().SI().Cubic.Centi.Meter.Cast<CubicMeter>(); }
}
public RoundsPerMinute IdleSpeed
{
get { return Body["IdlingSpeed"].Value<double>().SI<RoundsPerMinute>(); }
}
public DataTable FullLoadCurve
{
get
{
var filename = Body["FullLoadCurve"].Value<string>();
if (filename == null || !filename.Any() || filename.Equals("<NOFILE>", StringComparison.InvariantCultureIgnoreCase)) {
throw new VectoException("Invalid FullLoadCurve: {0}", filename);
}
return VectoCSVFile.Read(Path.Combine(BasePath, filename));
}
}
public KilogramSquareMeter Inertia
{
get { return Body["Inertia"].Value<double>().SI<KilogramSquareMeter>(); }
}
public KilogramPerWattSecond WHTCMotorway
{
get { return Body["WHTC-Motorway"].Value<double>().SI<KilogramPerWattSecond>(); }
}
public KilogramPerWattSecond WHTCRural
{
get { return Body["WHTC-Rural"].Value<double>().SI<KilogramPerWattSecond>(); }
}
public KilogramPerWattSecond WHTCUrban
{
get { return Body["WHTC-Urban"].Value<double>().SI<KilogramPerWattSecond>(); }
}
}
public class JSONGearboxDataV5 : JSONFile, IGearboxInputData, IAxleGearInputData, ITorqueConverterInputData
{
public JSONGearboxDataV5(JObject data, string filename) : base(data, filename) {}
#region IAxleGearInputData
public double Ratio
{
get { return Body["Gears"][0]["Ratio"].Value<double>(); }
}
public DataTable LossMap
{
get
{
var filename = Body["Gears"][0]["LossMap"].Value<string>();
if (filename == null || !filename.Any() || filename.Equals("<NOFILE>", StringComparison.InvariantCultureIgnoreCase)) {
throw new VectoException("Invalid AxleGear LossMap: {0}", filename);
}
return VectoCSVFile.Read(Path.Combine(BasePath, filename));
}
}
#endregion
#region IGearboxInputData
public string ModelName
{
get { return Body["ModelName"].Value<string>(); }
}
public GearboxType Type
{
get { return Body["GearboxType"].Value<string>().Parse<GearboxType>(); }
}
public KilogramSquareMeter Inertia
{
get { return Body["Inertia"].Value<double>().SI<KilogramSquareMeter>(); }
}
public Second TractionInterruption
{
get { return Body["TracInt"].Value<double>().SI<Second>(); }
}
public IList<ITransmissionInputData> Gears
{
get
{
var retVal = new List<ITransmissionInputData>();
var i = 0;
foreach (var gear in Body["Gears"]) {
if (i++ == 0) {
continue;
}
var lossMapFile = gear["LossMap"].Value<string>();
if (lossMapFile == null || !lossMapFile.Any() ||
lossMapFile.Equals("<NOFILE>", StringComparison.InvariantCultureIgnoreCase)) {
throw new VectoException("Invalid AxleGear LossMap: {0}", lossMapFile);
}
var lossMap = VectoCSVFile.Read(Path.Combine(BasePath, lossMapFile));
var fullLoadCurveFile = gear["FullLoadCurve"].Value<string>();
DataTable fullLoadCurve = null;
if (fullLoadCurveFile != null && fullLoadCurveFile.Any()) {
fullLoadCurve = VectoCSVFile.Read(Path.Combine(BasePath, fullLoadCurveFile));
}
var shiftPolygonFile = gear["ShiftPolygon"].Value<string>();
DataTable shiftPolygon = null;
if (shiftPolygonFile != null && shiftPolygonFile.Any() && !shiftPolygonFile.Equals("-")) {
fullLoadCurve = VectoCSVFile.Read(Path.Combine(BasePath, shiftPolygonFile));
}
retVal.Add(new JSONTransmissionInputData() {
Gear = i,
Ratio = gear["Ratio"].Value<double>(),
FullLoadCurve = fullLoadCurve,
LossMap = lossMap,
ShiftPolygon = shiftPolygon,
TorqueConverterActive = gear["TCactive"].Value<bool>()
});
}
return retVal;
}
}
public bool SkipGears
{
get { return Body["SkipGears"].Value<bool>(); }
}
public Second ShiftTime
{
get { return Body["ShiftTime"].Value<double>().SI<Second>(); }
}
public bool EarlyShiftUp
{
get { return Body["EarlyShiftUp"].Value<bool>(); }
}
public double TorqueReserve
{
get { return Body["TqReserve"].Value<double>() / 100.0; }
}
public MeterPerSecond StartSpeed
{
get { return Body["StartSpeed"].Value<double>().SI<MeterPerSecond>(); }
}
public MeterPerSquareSecond StartAcceleration
{
get { return Body["StartAcc"].Value<double>().SI<MeterPerSquareSecond>(); }
}
public double StartTorqueReserve
{
get { return Body["StartTqReserve"].Value<double>() / 100.0; }
}
public ITorqueConverterInputData TorqueConverter
{
get { return this; }
}
#endregion
#region ITorqueConverterInputData
public RoundsPerMinute ReferenceRPM
{
get { return Body["TorqueConverter"]["RefRPM"].Value<double>().SI<RoundsPerMinute>(); }
}
public DataTable TCData
{
get
{
var filename = Body["TorqueConverter"]["File"].Value<string>();
if (filename == null || !filename.Any() || filename.Equals("<NOFILE>", StringComparison.InvariantCultureIgnoreCase)) {
throw new VectoException("Invalid TroqueConverter Curve: {0}", filename);
}
return VectoCSVFile.Read(Path.Combine(BasePath, filename));
}
}
KilogramSquareMeter ITorqueConverterInputData.Inertia
{
get { return Body["TorqueConverter"]["Inertia"].Value<double>().SI<KilogramSquareMeter>(); }
}
#endregion
}
public class JSONTransmissionInputData : ITransmissionInputData
{
public int Gear { get; internal set; }
public double Ratio { get; internal set; }
public DataTable LossMap { get; internal set; }
public DataTable FullLoadCurve { get; internal set; }
public DataTable ShiftPolygon { get; internal set; }
public bool TorqueConverterActive { get; internal set; }
}
public class JSONAxleInputData : IAxleInputData
{
public string Wheels { get; internal set; }
public bool TwinTyres { get; internal set; }
public double RollResistanceCoefficient { get; internal set; }
public Newton TyreTestLoad { get; internal set; }
public double AxleWeightShare { get; internal set; }
public KilogramSquareMeter Inertia { get; internal set; }
}
}
\ No newline at end of file
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using TUGraz.VectoCore.Exceptions;
using TUGraz.VectoCore.InputData.FileIO.JSON;
namespace TUGraz.VectoCore.InputData.FileIO
{
public class JSONInputDataFactory
{
protected static JObject ReadFile(string fileName)
{
using (StreamReader reader = File.OpenText(fileName)) {
return (JObject)JToken.ReadFrom(new JsonTextReader(reader));
}
}
public static IInputDataProvider ReadJsonJob(string filename)
{
var json = ReadFile(filename);
var version = json[JsonKeys.JsonHeader][JsonKeys.JsonHeader_FileVersion].Value<int>();
switch (version) {
case 2:
return new JSONInputDataV2(json, filename);
default:
throw new VectoException("Job-File: Unsupported FileVersion. Got: {0} ", version);
}
}
public static IVehicleInputData ReadJsonVehicle(string filename)
{
var json = ReadFile(filename);
var version = json[JsonKeys.JsonHeader][JsonKeys.JsonHeader_FileVersion].Value<int>();
switch (version) {
case 7:
return new JSONVehicleDataV7(json, filename);
default:
throw new VectoException("Vehicle-File: Unsupported FileVersion. Got {0}", version);
}
}
public static IGearboxInputData ReadGearbox(string filename)
{
var json = ReadFile(filename);
var version = json[JsonKeys.JsonHeader][JsonKeys.JsonHeader_FileVersion].Value<int>();
switch (version) {
case 5:
return new JSONGearboxDataV5(json, filename);
default:
throw new VectoException("Gearbox-File: Unsupported FileVersion. Got {0}", version);
}
}
public static IEngineInputData ReadEngine(string filename)
{
var json = ReadFile(filename);
var version = json[JsonKeys.JsonHeader][JsonKeys.JsonHeader_FileVersion].Value<int>();
switch (version) {
case 3:
return new JSONEngineDataV3(json, filename);
default:
throw new VectoException("Engine-File: Unsupported FileVersion. Got {0}", version);
}
}
}
}
\ No newline at end of file
using System.Collections.Generic;
using System.Deployment.Internal;
namespace TUGraz.VectoCore.InputData
{
public interface IInputDataProvider
{
IJobInputData JobInputData();
IVehicleInputData VehicleInputData { get; }
IGearboxInputData GearboxInputData { get; }
IAxleGearInputData AxleGearInputData { get; }
IEngineInputData EngineInputData { get; }
IEnumerable<IAuxiliaryInputData> AuxiliaryInputData();
IRetarderInputData RetarderInputData { get; }
IDriverInputData DriverInputData { get; }
}
}
\ No newline at end of file
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data;
using System.Linq; using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json;
using TUGraz.VectoCore.InputData.FileIO.DeclarationFile;
using TUGraz.VectoCore.Models.Declaration;
using TUGraz.VectoCore.Models.SimulationComponent.Data;
using TUGraz.VectoCore.Utils;
namespace TUGraz.VectoCore.InputData namespace TUGraz.VectoCore.InputData
{ {
internal interface IJobInputData {} public interface IJobInputData
{
IVehicleInputData Vehicle { get; }
IList<DataTable> Cycles { get; }
bool EngineOnlyMode { get; }
}
public interface IVehicleInputData
{
bool SavedInDeclarationMode { get; }
VehicleCategory VehicleCategory { get; }
Kilogram CurbWeight { get; }
Kilogram GrossVehicleMassRating { get; }
SquareMeter DragCoefficient { get; }
SquareMeter DragCoefficientRigidTruck { get; } // without trailer
string Rim { get; }
//IRetarderInputData Retarder { get; }
AxleConfiguration AxleConfiguration { get; }
IList<IAxleInputData> Axles { get; }
}
public interface IRetarderInputData
{
RetarderData.RetarderType Type { get; }
double Ratio { get; }
DataTable LossMap { get; }
}
public interface IAxleInputData
{
string Wheels { get; }
bool TwinTyres { get; }
double RollResistanceCoefficient { get; }
Newton TyreTestLoad { get; }
double AxleWeightShare { get; }
KilogramSquareMeter Inertia { get; }
}
public interface IGearboxInputData
{
bool SavedInDeclarationMode { get; }
string ModelName { get; }
GearboxType Type { get; }
KilogramSquareMeter Inertia { get; }
Second TractionInterruption { get; }
internal interface IVehicleInputData {} IList<ITransmissionInputData> Gears { get; }
internal interface IGearboxInputData {} bool SkipGears { get; }
Second ShiftTime { get; }
bool EarlyShiftUp { get; }
internal interface IAxleGearInputData {} double TorqueReserve { get; }
internal interface IEngineInputData {} MeterPerSecond StartSpeed { get; }
internal interface IAuxiliaryInputData {} MeterPerSquareSecond StartAcceleration { get; }
/// <summary>
/// [%] (0-1)
/// </summary>
double StartTorqueReserve { get; }
ITorqueConverterInputData TorqueConverter { get; }
}
public interface ITransmissionInputData
{
int Gear { get; }
double Ratio { get; }
DataTable LossMap { get; }
DataTable FullLoadCurve { get; }
DataTable ShiftPolygon { get; }
bool TorqueConverterActive { get; }
}
public interface IAxleGearInputData
{
bool SavedInDeclarationMode { get; }
double Ratio { get; }
DataTable LossMap { get; }
}
public interface ITorqueConverterInputData
{
RoundsPerMinute ReferenceRPM { get; }
KilogramSquareMeter Inertia { get; }
DataTable TCData { get; }
}
public interface IEngineInputData
{
bool SavedInDeclarationMode { get; }
string ModelName { get; }
CubicMeter Displacement { get; }
RoundsPerMinute IdleSpeed { get; }
/// <summary>
/// engine speed in rpm, torque in NM, fuel consumption in g/h
/// </summary>
DataTable FullLoadCurve { get; }
KilogramSquareMeter Inertia { get; }
KilogramPerWattSecond WHTCMotorway { get; }
KilogramPerWattSecond WHTCRural { get; }
KilogramPerWattSecond WHTCUrban { get; }
}
public interface IDriverInputData {}
public interface IAuxiliaryInputData
{
bool SavedInDeclarationMode { get; }
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment