From 03360868a003ebf3b17ba4992c1dc34659f63bb2 Mon Sep 17 00:00:00 2001 From: Markus Quaritsch <markus.quaritsch@tugraz.at> Date: Tue, 20 Sep 2016 13:45:52 +0200 Subject: [PATCH] removing vecto 2 JSON Read method, validation for vecto Job (GUI) works --- VECTO/Configuration.vb | 38 +++++----- VECTO/GUI/EngineForm.vb | 2 +- VECTO/GUI/GearboxForm.vb | 7 +- VECTO/GUI/VectoJobForm.vb | 26 ++++--- VECTO/GUI/VehicleForm.vb | 12 ++-- VECTO/Input Files/VectoJob.vb | 70 +++++++++++++++++-- .../InputData/FileIO/JSON/JSONInputData.cs | 2 +- 7 files changed, 109 insertions(+), 48 deletions(-) diff --git a/VECTO/Configuration.vb b/VECTO/Configuration.vb index 0c178ba3ec..add79515f3 100644 --- a/VECTO/Configuration.vb +++ b/VECTO/Configuration.vb @@ -10,6 +10,7 @@ ' See the LICENSE.txt for the specific language governing permissions and limitations. Imports System.Collections.Generic Imports System.IO +Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq Imports TUGraz.VectoCore.InputData.FileIO.JSON Imports TUGraz.VectoCore.Models.Declaration @@ -59,26 +60,25 @@ Public Class Configuration Exit Sub End If - Dim json As New JSONParser - If Not json.ReadFile(FilePath) Then - GUIMsg(MessageType.Err, "Failed to load settings! Using default settings.") - Exit Sub - End If Try - Dim body As JToken = json.Content.GetEx("Body") - Try - Mod1Hz = body.GetEx(Of Boolean)("Mod1Hz") - Catch - End Try - ModOut = body.GetEx(Of Boolean)("ModOut") - LogSize = body.GetEx(Of Double)("LogSize") - AirDensity = body.GetEx(Of Double)("AirDensity") - FuelDens = body.GetEx(Of Double)("FuelDensity") - CO2perFC = body.GetEx(Of Double)("CO2perFC") - OpenCmd = body.GetEx(Of String)("OpenCmd") - OpenCmdName = body.GetEx(Of String)("OpenCmdName") - FirstRun = body.GetEx(Of Boolean)("FirstRun") - DeclMode = body.GetEx(Of Boolean)("DeclMode") + Using reader As TextReader = File.OpenText(FilePath) + Dim content As JToken = JToken.ReadFrom(New JsonTextReader(reader)) + + Dim body As JToken = content.GetEx("Body") + Try + Mod1Hz = body.GetEx(Of Boolean)("Mod1Hz") + Catch + End Try + ModOut = body.GetEx(Of Boolean)("ModOut") + LogSize = body.GetEx(Of Double)("LogSize") + AirDensity = body.GetEx(Of Double)("AirDensity") + FuelDens = body.GetEx(Of Double)("FuelDensity") + CO2perFC = body.GetEx(Of Double)("CO2perFC") + OpenCmd = body.GetEx(Of String)("OpenCmd") + OpenCmdName = body.GetEx(Of String)("OpenCmdName") + FirstRun = body.GetEx(Of Boolean)("FirstRun") + DeclMode = body.GetEx(Of Boolean)("DeclMode") + End Using Catch ex As Exception GUIMsg(MessageType.Err, "Error while loading settings!") End Try diff --git a/VECTO/GUI/EngineForm.vb b/VECTO/GUI/EngineForm.vb index c4f1bca0ad..6298a88a92 100644 --- a/VECTO/GUI/EngineForm.vb +++ b/VECTO/GUI/EngineForm.vb @@ -358,7 +358,7 @@ Public Class EngineForm 'Dim fldOK As Boolean = False 'Dim mapOK As Boolean = False - Dim fullLoadCurve As FullLoadCurve + Dim fullLoadCurve As FullLoadCurve = Nothing Dim fcMap As FuelConsumptionMap Dim chart As Chart Dim series As Series diff --git a/VECTO/GUI/GearboxForm.vb b/VECTO/GUI/GearboxForm.vb index b13735b818..b51dd3ea8e 100644 --- a/VECTO/GUI/GearboxForm.vb +++ b/VECTO/GUI/GearboxForm.vb @@ -250,7 +250,7 @@ Public Class GearboxForm For Each gear As ITransmissionInputData In gearbox.Gears LvGears.Items.Add(CreateListviewItem(gear.Gear.ToString("00"), "-", gear.Ratio, gear.LossMap.Source, - GetRelativePath(gear.ShiftPolygon.Source, basePath), + If(gear.ShiftPolygon Is Nothing, "", GetRelativePath(gear.ShiftPolygon.Source, basePath)), If(gear.MaxTorque Is Nothing, "", gear.MaxTorque.ToGUIFormat()))) Next @@ -269,10 +269,11 @@ Public Class GearboxForm TbTCinertia.Text = "" TBTCShiftPolygon.Text = "" Else - TbTCfile.Text = GetRelativePath(torqueConverter.TCData.Source, basePath) + TbTCfile.Text = If(torqueConverter.TCData Is Nothing, "", GetRelativePath(torqueConverter.TCData.Source, basePath)) TbTCrefrpm.Text = torqueConverter.ReferenceRPM.AsRPM.ToGUIFormat() TbTCinertia.Text = torqueConverter.Inertia.ToGUIFormat() - TBTCShiftPolygon.Text = GetRelativePath(torqueConverter.ShiftPolygon.Source, basePath) + TBTCShiftPolygon.Text = + If(torqueConverter.ShiftPolygon Is Nothing, "", GetRelativePath(torqueConverter.ShiftPolygon.Source, basePath)) End If tbUpshiftMinAcceleration.Text = gearbox.UpshiftMinAcceleration.ToGUIFormat() diff --git a/VECTO/GUI/VectoJobForm.vb b/VECTO/GUI/VectoJobForm.vb index 1327b7cff2..1efc812e59 100644 --- a/VECTO/GUI/VectoJobForm.vb +++ b/VECTO/GUI/VectoJobForm.vb @@ -399,7 +399,7 @@ Public Class VectoJobForm 'Start/Stop Dim driver As IDriverEngineeringInputData = inputData.DriverInputData ChBStartStop.Checked = driver.StartStop.Enabled - TbSSspeed.Text = driver.StartStop.MaxSpeed.ToGUIFormat() + TbSSspeed.Text = driver.StartStop.MaxSpeed.AsKmph().ToGUIFormat() TbSStime.Text = driver.StartStop.MinTime.ToGUIFormat() TbSSdelay.Text = driver.StartStop.Delay.ToGUIFormat() @@ -423,11 +423,9 @@ Public Class VectoJobForm Next Else 'VACC - Try - TbDesMaxFile.Text = GetRelativePath(driver.AccelerationCurve.Source, _basePath) - Catch - TbDesMaxFile.Text = "" - End Try + TbDesMaxFile.Text = + If(driver.AccelerationCurve Is Nothing, "", GetRelativePath(driver.AccelerationCurve.Source, _basePath)) + Dim auxInput As IAuxiliariesEngineeringInputData = inputData.AuxiliaryInputData() For Each item As AdvancedAuxiliary In cboAdvancedAuxiliaries.Items @@ -469,9 +467,9 @@ Public Class VectoJobForm Else RdOff.Checked = True End If - TbOverspeed.Text = driver.OverSpeedEcoRoll.MinSpeed.ToGUIFormat() - TbUnderSpeed.Text = driver.OverSpeedEcoRoll.UnderSpeed.ToGUIFormat() - TbVmin.Text = driver.OverSpeedEcoRoll.MinSpeed.ToGUIFormat() + TbOverspeed.Text = driver.OverSpeedEcoRoll.OverSpeed.AsKmph().ToGUIFormat() + TbUnderSpeed.Text = driver.OverSpeedEcoRoll.UnderSpeed.AsKmph().ToGUIFormat() + TbVmin.Text = driver.OverSpeedEcoRoll.MinSpeed.AsKmph().ToGUIFormat() CbLookAhead.Checked = driver.Lookahead.Enabled 'TbAlookahead.Text = CStr(VEC0.ALookahead) 'TbVminLA.Text = CStr(VEC0.VMinLa) @@ -479,10 +477,10 @@ Public Class VectoJobForm tbDfCoastingOffset.Text = driver.Lookahead.CoastingDecisionFactorOffset.ToGUIFormat() tbDfCoastingScale.Text = driver.Lookahead.CoastingDecisionFactorScaling.ToGUIFormat() - tbLacDfTargetSpeedFile.Text = GetRelativePath(driver.Lookahead.CoastingDecisionFactorTargetSpeedLookup.Source, - _basePath) - tbLacDfVelocityDropFile.Text = GetRelativePath(driver.Lookahead.CoastingDecisionFactorVelocityDropLookup.Source, - _basePath) + tbLacDfTargetSpeedFile.Text = If(driver.Lookahead.CoastingDecisionFactorTargetSpeedLookup Is Nothing, "", + GetRelativePath(driver.Lookahead.CoastingDecisionFactorTargetSpeedLookup.Source, _basePath)) + tbLacDfVelocityDropFile.Text = If(driver.Lookahead.CoastingDecisionFactorVelocityDropLookup Is Nothing, "", + GetRelativePath(driver.Lookahead.CoastingDecisionFactorVelocityDropLookup.Source, _basePath)) '------------------------------------------------------------- @@ -1186,7 +1184,7 @@ lbDlog: Else For Each gear As ITransmissionInputData In gearbox.Gears - If gear.ShiftPolygon.Rows.Count = 0 Then Continue For + If gear.ShiftPolygon Is Nothing OrElse gear.ShiftPolygon.Rows.Count = 0 Then Continue For Dim shiftPolygon As ShiftPolygon = ShiftPolygonReader.Create(gear.ShiftPolygon) s = New Series s.Points.DataBindXY(shiftPolygon.Upshift.Select(Function(x) x.AngularSpeed), diff --git a/VECTO/GUI/VehicleForm.vb b/VECTO/GUI/VehicleForm.vb index 42b43120bd..f8d3a15cea 100644 --- a/VECTO/GUI/VehicleForm.vb +++ b/VECTO/GUI/VehicleForm.vb @@ -350,16 +350,18 @@ Public Class VehicleForm TBrdyn.Text = vehicle.DynamicTyreRadius.ToGUIFormat() CbCdMode.SelectedValue = vehicle.CrossWindCorrectionMode - TbCdFile.Text = GetRelativePath(vehicle.CrosswindCorrectionMap.Source, basePath) + TbCdFile.Text = + If(vehicle.CrosswindCorrectionMap Is Nothing, "", GetRelativePath(vehicle.CrosswindCorrectionMap.Source, basePath)) CbRtType.SelectedValue = retarder.Type TbRtRatio.Text = retarder.Ratio.ToGUIFormat() - TbRtPath.Text = GetRelativePath(retarder.LossMap.Source, basePath) + TbRtPath.Text = If(retarder.LossMap Is Nothing, "", GetRelativePath(retarder.LossMap.Source, basePath)) cbAngularGearType.SelectedValue = angularGear.Type tbAngularGearRatio.Text = angularGear.Ratio.ToGUIFormat() - tbAngularGearLossMapPath.Text = GetRelativePath(angularGear.LossMap.Source, basePath) + tbAngularGearLossMapPath.Text = + If(angularGear.LossMap Is Nothing, "", GetRelativePath(angularGear.LossMap.Source, basePath)) LvRRC.Items.Clear() Dim i As Integer = 0 @@ -386,8 +388,8 @@ Public Class VehicleForm TBcdA.Text = vehicle.AirDragArea.ToGUIFormat() cbPTOType.SelectedValue = pto.PTOTransmissionType - tbPTOLossMap.Text = GetRelativePath(pto.PTOLossMap.Source, basePath) - tbPTOCycle.Text = GetRelativePath(pto.PTOCycle.Source, basePath) + tbPTOLossMap.Text = If(pto.PTOLossMap Is Nothing, "", GetRelativePath(pto.PTOLossMap.Source, basePath)) + tbPTOCycle.Text = If(pto.PTOCycle Is Nothing, "", GetRelativePath(pto.PTOCycle.Source, basePath)) DeclInit() diff --git a/VECTO/Input Files/VectoJob.vb b/VECTO/Input Files/VectoJob.vb index 95d8e2c00b..9f254bced5 100644 --- a/VECTO/Input Files/VectoJob.vb +++ b/VECTO/Input Files/VectoJob.vb @@ -17,6 +17,7 @@ Imports System.IO Imports System.Linq Imports Newtonsoft.Json.Linq Imports TUGraz.VECTO.Input_Files +Imports TUGraz.VectoCommon.Exceptions Imports TUGraz.VectoCommon.InputData Imports TUGraz.VectoCommon.Models Imports TUGraz.VectoCommon.Utils @@ -24,6 +25,7 @@ Imports TUGraz.VectoCore.InputData.FileIO.JSON Imports TUGraz.VectoCore.InputData.Impl Imports TUGraz.VectoCore.InputData.Reader.DataObjectAdapter Imports TUGraz.VectoCore.InputData.Reader.Impl +Imports TUGraz.VectoCore.Models.Declaration Imports TUGraz.VectoCore.Models.Simulation.Data Imports TUGraz.VectoCore.Models.SimulationComponent.Data Imports TUGraz.VectoCore.Utils @@ -495,6 +497,7 @@ Public Class VectoJob Public ReadOnly Property IDriverDeclarationInputData_SavedInDeclarationMode As Boolean _ Implements IDriverDeclarationInputData.SavedInDeclarationMode Get + Return SavedInDeclMode End Get End Property @@ -547,19 +550,34 @@ Public Class VectoJob Public ReadOnly Property AccelerationCurve As TableData Implements IDriverEngineeringInputData.AccelerationCurve Get + If String.IsNullOrWhiteSpace(_driverAccelerationFile.FullPath) Then Return Nothing + If Not File.Exists(_driverAccelerationFile.FullPath) Then + Try + Dim cycleDataRes As Stream = + RessourceHelper.ReadStream(RessourceHelper.Namespace + "VACC." + _driverAccelerationFile.OriginalPath + + TUGraz.VectoCore.Configuration.Constants.FileExtensions.DriverAccelerationCurve) + Return VectoCSVFile.ReadStream(cycleDataRes) + Catch ex As Exception + Return Nothing + End Try + End If Return VectoCSVFile.Read(_driverAccelerationFile.FullPath) End Get End Property Public ReadOnly Property Lookahead As ILookaheadCoastingInputData Implements IDriverEngineeringInputData.Lookahead Get + Dim lacTargetLookup As TableData = + If(File.Exists(LacDfTargetSpeedFile), VectoCSVFile.Read(LacDfTargetSpeedFile), Nothing) + Dim lacVdropLookup As TableData = + If(File.Exists(LacDfVelocityDropFile), VectoCSVFile.Read(LacDfVelocityDropFile), Nothing) Return New LookAheadCoastingInputData With { .CoastingDecisionFactorScaling = LacDfScale, .CoastingDecisionFactorOffset = LacDfOffset, .Enabled = LookAheadOn, .LookaheadDistanceFactor = LacPreviewFactor, - .CoastingDecisionFactorTargetSpeedLookup = VectoCSVFile.Read(LacDfTargetSpeedFile), - .CoastingDecisionFactorVelocityDropLookup = VectoCSVFile.Read(LacDfVelocityDropFile) + .CoastingDecisionFactorTargetSpeedLookup = lacTargetLookup, + .CoastingDecisionFactorVelocityDropLookup = lacVdropLookup } End Get End Property @@ -599,17 +617,46 @@ Public Class VectoJob vectoJob._engineInputData = New JSONComponentInputData(vectoJob._engineFile.FullPath) vectoJob._gearboxInputData = New JSONComponentInputData(vectoJob._gearboxFile.FullPath) + Dim result As IList(Of ValidationResult) = New List(Of ValidationResult) Try If mode = ExecutionMode.Declaration Then + If Not vectoJob._vehicleInputData.VehicleInputData.SavedInDeclarationMode Then + result.Add(New ValidationResult("Vehicle File is not in Declaration Mode")) + End If + If Not vectoJob._engineInputData.EngineInputData.SavedInDeclarationMode Then + result.Add(New ValidationResult("Engine File is not in Declaration Mode")) + End If + If Not vectoJob._gearboxInputData.GearboxInputData.SavedInDeclarationMode Then + result.Add(New ValidationResult("Gearbox File is not in Declaration Mode")) + End If + If result.Any() Then + Return _ + New ValidationResult("Vecto Job Configuration is invalid. ", result.Select(Function(r) r.ErrorMessage).ToList()) + End If + Dim dataFactory As DeclarationModeVectoRunDataFactory = New DeclarationModeVectoRunDataFactory(vectoJob, Nothing) + jobData = dataFactory.NextRun() Else + If vectoJob._vehicleInputData.VehicleInputData.SavedInDeclarationMode Then + result.Add(New ValidationResult("Vehicle File is not in Engineering Mode")) + End If + If vectoJob._engineInputData.EngineInputData.SavedInDeclarationMode Then + result.Add(New ValidationResult("Engine File is not in Engineering Mode")) + End If + If vectoJob._gearboxInputData.GearboxInputData.SavedInDeclarationMode Then + result.Add(New ValidationResult("Gearbox File is not in Engineering Mode")) + End If + If result.Any() Then + Return _ + New ValidationResult("Vecto Job Configuration is invalid. ", result.Select(Function(r) r.ErrorMessage).ToList()) + End If Dim dataFactory As EngineeringModeVectoRunDataFactory = New EngineeringModeVectoRunDataFactory(vectoJob) jobData = dataFactory.NextRun() End If - Dim result As IList(Of ValidationResult) = - jobData.Validate(If(Cfg.DeclMode, ExecutionMode.Declaration, ExecutionMode.Engineering)) + + jobData.Validate(If(Cfg.DeclMode, ExecutionMode.Declaration, ExecutionMode.Engineering)) If result.Any() Then Return _ New ValidationResult("Vecto Job Configuration is invalid. ", result.Select(Function(r) r.ErrorMessage).ToList()) @@ -627,6 +674,7 @@ Public Class VectoJob End Try End Function + #Region "IInputData" Public Function JobInputData() As IEngineeringJobInputData Implements IEngineeringInputDataProvider.JobInputData @@ -800,9 +848,21 @@ Public Class VectoJob Dim retVal As ICycleData() = New ICycleData(CycleFiles.Count) {} Dim i As Integer = 0 For Each cycleFile As SubPath In CycleFiles + Dim cycleData As TableData + If (File.Exists(cycleFile.FullPath)) Then + cycleData = VectoCSVFile.Read(cycleFile.FullPath) + Else + Try + Dim cycleDataRes As Stream = + RessourceHelper.ReadStream(RessourceHelper.Namespace + "MissionCycles." + cycleFile.OriginalPath + ".vdri") + cycleData = VectoCSVFile.ReadStream(cycleDataRes) + Catch ex As Exception + Throw New VectoException("Driving Cycle could not be read: " + cycleFile.OriginalPath) + End Try + End If retVal(i) = New CycleInputData With { .Name = Path.GetFileNameWithoutExtension(cycleFile.FullPath), - .CycleData = VectoCSVFile.Read(cycleFile.FullPath) + .CycleData = cycleData } i += 1 Next diff --git a/VectoCore/VectoCore/InputData/FileIO/JSON/JSONInputData.cs b/VectoCore/VectoCore/InputData/FileIO/JSON/JSONInputData.cs index e78c720a5d..63e91182b9 100644 --- a/VectoCore/VectoCore/InputData/FileIO/JSON/JSONInputData.cs +++ b/VectoCore/VectoCore/InputData/FileIO/JSON/JSONInputData.cs @@ -95,7 +95,7 @@ namespace TUGraz.VectoCore.InputData.FileIO.JSON if (required) { throw new VectoException("Invalid filename for {0}: {1}", tableType, filename); } - return new TableData(""); + return null; } internal static bool EmptyOrInvalidFileName(string filename) -- GitLab