Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Vehicle.vb 13.4 KiB
Newer Older
' Copyright 2014 European Union.
' Licensed under the EUPL (the 'Licence');
'
' * You may not use this work except in compliance with the Licence.
' * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
' * Unless required by applicable law or agreed to in writing,
'   software distributed under the Licence is distributed on an "AS IS" basis,
'   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'
' See the LICENSE.txt for the specific language governing permissions and limitations.

Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations
Imports System.IO
Imports System.Linq
Imports TUGraz.VECTO.Input_Files
Imports TUGraz.VectoCommon.InputData
Imports TUGraz.VectoCommon.Models
Imports TUGraz.VectoCommon.Utils
Imports TUGraz.VectoCore.InputData.Impl
Imports TUGraz.VectoCore.InputData.Reader.DataObjectAdapter
Imports TUGraz.VectoCore.Models.Declaration
Imports TUGraz.VectoCore.Models.SimulationComponent.Data
Imports TUGraz.VectoCore.Utils
<CustomValidation(GetType(Vehicle), "ValidateVehicle")>
Markus Quaritsch's avatar
Markus Quaritsch committed
	Implements IVehicleEngineeringInputData, IVehicleDeclarationInputData, IRetarderInputData, IPTOTransmissionInputData, 
	Private _filePath As String
	Private _path As String
	Public Mass As Double
	Public Loading As Double
	Public CrossWindCorrectionMode As CrossWindCorrectionMode
	Public ReadOnly CrossWindCorrectionFile As SubPath
Michael KRISPER's avatar
Michael KRISPER committed
	<ValidateObject> Public RetarderType As RetarderType
	Public RetarderRatio As Double = 0
	Public ReadOnly RetarderLossMapFile As SubPath
	Public DynamicTyreRadius As Double
	Public ReadOnly Axles As List(Of Axle)


	Public VehicleCategory As VehicleCategory
	Public MassExtra As Double
	Public MassMax As Double
	Public AxleConfiguration As AxleConfiguration
	Public SavedInDeclMode As Boolean
Michael KRISPER's avatar
Michael KRISPER committed

	Public AngledriveType As AngledriveType
	Public AngledriveRatio As Double
	Public ReadOnly AngledriveLossMapFile As SubPath
	Public PtoType As String
	Public ReadOnly PtoLossMap As SubPath
	Public ReadOnly PtoCycle As SubPath
		Public RRC As Double
		Public Share As Double
		Public TwinTire As Boolean
		_path = ""
		_filePath = ""
		CrossWindCorrectionFile = New SubPath

		RetarderLossMapFile = New SubPath
		AngledriveLossMapFile = New SubPath()
		PtoLossMap = New SubPath()
		PtoCycle = New SubPath()
	' ReSharper disable once UnusedMember.Global  -- used for Validation
	Public Shared Function ValidateVehicle(vehicle As Vehicle, validationContext As ValidationContext) As ValidationResult

		Dim vehicleData As VehicleData
		Dim retarderData As RetarderData
		Dim ptoData As PTOData = Nothing
		Dim angledriveData As AngledriveData
Markus Quaritsch's avatar
Markus Quaritsch committed
		Dim modeService As ExecutionModeServiceContainer = TryCast(validationContext.GetService(GetType(ExecutionMode)), 
																	ExecutionModeServiceContainer)
		Dim mode As ExecutionMode = If(modeService Is Nothing, ExecutionMode.Declaration, modeService.Mode)

		Try
			If mode = ExecutionMode.Declaration Then
				Dim doa As DeclarationDataAdapter = New DeclarationDataAdapter()
				Dim segment As Segment = DeclarationData.Segments.Lookup(vehicle.VehicleCategory, vehicle.AxleConfiguration,
																		vehicle.GrossVehicleMassRating, vehicle.CurbWeightChassis)
				vehicleData = doa.CreateVehicleData(vehicle, segment.Missions.First(),
													segment.Missions.First().Loadings.First().Value)
				retarderData = doa.CreateRetarderData(vehicle)
				angledriveData = doa.CreateAngledriveData(vehicle, False)
				Dim doa As EngineeringDataAdapter = New EngineeringDataAdapter()
				vehicleData = doa.CreateVehicleData(vehicle)
				retarderData = doa.CreateRetarderData(vehicle)
				angledriveData = doa.CreateAngledriveData(vehicle, True)
				ptoData = doa.CreatePTOTransmissionData(vehicle)
			Dim result As IList(Of ValidationResult) =
					vehicleData.Validate(If(Cfg.DeclMode, ExecutionMode.Declaration, ExecutionMode.Engineering))
			If result.Any() Then
				Return _
					New ValidationResult("Vehicle Configuration is invalid. ", result.Select(Function(r) r.ErrorMessage).ToList())
			End If

			result = retarderData.Validate(If(Cfg.DeclMode, ExecutionMode.Declaration, ExecutionMode.Engineering))
			If result.Any() Then
				Return _
					New ValidationResult("Retarder Configuration is invalid. ", result.Select(Function(r) r.ErrorMessage).ToList())
			End If

			If vehicle.AngledriveType = AngledriveType.SeparateAngledrive Then
				result = angledriveData.Validate(If(Cfg.DeclMode, ExecutionMode.Declaration, ExecutionMode.Engineering))
				If result.Any() Then
					Return _
						New ValidationResult("AngleDrive Configuration is invalid. ", result.Select(Function(r) r.ErrorMessage).ToList())
				End If
			If Not vehicle.PTOTransmissionType = "None" Then
				result = ptoData.Validate(If(Cfg.DeclMode, ExecutionMode.Declaration, ExecutionMode.Engineering))
				If result.Any() Then
					Return _
						New ValidationResult("PTO Configuration is invalid. ", result.Select(Function(r) r.ErrorMessage).ToList())
				End If
			End If

		Catch ex As Exception
			Return New ValidationResult(ex.Message)
		End Try
	Private Sub SetDefault()
		Mass = 0
		MassExtra = 0
		Loading = 0
		CdA0 = 0
		CrossWindCorrectionFile.Clear()
		CrossWindCorrectionMode = CrossWindCorrectionMode.NoCorrection

		DynamicTyreRadius = 0

		RetarderType = RetarderType.None
		RetarderRatio = 1
		RetarderLossMapFile.Clear()
		AngledriveType = AngledriveType.None
		AngledriveLossMapFile.Clear()
		AngledriveRatio = 1
		PtoType = PTOTransmission.NoPTO
		PtoLossMap.Clear()
		PtoCycle.Clear()
Michael KRISPER's avatar
Michael KRISPER committed
		VehicleCategory = VehicleCategory.RigidTruck
Michael KRISPER's avatar
Michael KRISPER committed
		AxleConfiguration = AxleConfiguration.AxleConfig_4x2

		SavedInDeclMode = False
	End Sub


	Public Function SaveFile() As Boolean
		SavedInDeclMode = Cfg.DeclMode

		Dim validationResults As IList(Of ValidationResult) =
				Validate(If(Cfg.DeclMode, ExecutionMode.Declaration, ExecutionMode.Engineering))

		If validationResults.Count > 0 Then
			Dim messages As IEnumerable(Of String) =
					validationResults.Select(Function(r) r.ErrorMessage + String.Join(", ", r.MemberNames.Distinct()))
			MsgBox("Invalid input." + Environment.NewLine + String.Join(Environment.NewLine, messages), MsgBoxStyle.OkOnly,
					"Failed to save vehicle")
		Try
			Dim writer As JSONFileWriter = JSONFileWriter.Instance
			writer.SaveVehicle(Me, Me, Me, Me, _filePath)
		Catch ex As Exception
			MsgBox("Failed to save Vehicle file: " + ex.Message)
			Return False
		End Try
		Return True
	End Function


#Region "Properties"


	Public Property FilePath() As String
		Get
			_filePath = value
			If _filePath = "" Then
				_path = ""
				_path = Path.GetDirectoryName(_filePath) & "\"
#End Region

#Region "IInputData"

	Public ReadOnly Property SourceType As DataSourceType Implements IComponentInputData.SourceType
		Get
			Return DataSourceType.JSONFile
		End Get
	End Property

	Public ReadOnly Property Source As String Implements IComponentInputData.Source
		Get
			Return FilePath
		End Get
	End Property

	Public ReadOnly Property SavedInDeclarationMode As Boolean Implements IComponentInputData.SavedInDeclarationMode
		Get
			Return Cfg.DeclMode
		End Get
	End Property

	Public ReadOnly Property Vendor As String Implements IComponentInputData.Vendor
		Get
			Return "N.A."  ' TODO: MQ  20160908
		End Get
	End Property

	Public ReadOnly Property ModelName As String Implements IComponentInputData.ModelName
		Get
			Return "N.A."  ' Todo: MQ 20160908
		End Get
	End Property

	Public ReadOnly Property Creator As String Implements IComponentInputData.Creator
		Get
			Return Lic.LicString
		End Get
	End Property

	Public ReadOnly Property [Date] As String Implements IComponentInputData.[Date]
		Get
			Return Now.ToUniversalTime().ToString("o")
		End Get
	End Property

	Public ReadOnly Property TypeId As String Implements IComponentInputData.TypeId
		Get
			Return "N.A."	' ToDo: MQ 20160908
		End Get
	End Property

	Public ReadOnly Property DigestValue As String Implements IComponentInputData.DigestValue
		Get
			Return ""
		End Get
	End Property

	Public ReadOnly Property IntegrityStatus As IntegrityStatus Implements IComponentInputData.IntegrityStatus
		Get
			Return IntegrityStatus.NotChecked
		End Get
	End Property

	Public ReadOnly Property IVehicleDeclarationInputData_VehicleCategory As VehicleCategory _
		Implements IVehicleDeclarationInputData.VehicleCategory
		Get
			Return VehicleCategory
		End Get
	End Property

	Public ReadOnly Property IVehicleDeclarationInputData_AxleConfiguration As AxleConfiguration _
		Implements IVehicleDeclarationInputData.AxleConfiguration
		Get
			Return AxleConfiguration
		End Get
	End Property

	Public ReadOnly Property CurbWeightChassis As Kilogram Implements IVehicleDeclarationInputData.CurbWeightChassis
		Get
Markus Quaritsch's avatar
Markus Quaritsch committed
			Return Mass.SI(Of Kilogram)()
		End Get
	End Property

	Public ReadOnly Property GrossVehicleMassRating As Kilogram _
		Implements IVehicleDeclarationInputData.GrossVehicleMassRating
		Get
Markus Quaritsch's avatar
Markus Quaritsch committed
			Return MassMax.SI().Ton.Cast(Of Kilogram)()
		End Get
	End Property

	Public ReadOnly Property AirDragArea As SquareMeter Implements IVehicleDeclarationInputData.AirDragArea
		Get
Markus Quaritsch's avatar
Markus Quaritsch committed
			Return CdA0.SI(Of SquareMeter)()
		End Get
	End Property

	Public ReadOnly Property IVehicleEngineeringInputData_Axles As IList(Of IAxleEngineeringInputData) _
		Implements IVehicleEngineeringInputData.Axles
		Get
Markus Quaritsch's avatar
Markus Quaritsch committed
			Return AxleWheels().Cast(Of IAxleEngineeringInputData)().ToList()
		End Get
	End Property

	Public ReadOnly Property IVehicleDeclarationInputData_Axles As IList(Of IAxleDeclarationInputData) _
		Implements IVehicleDeclarationInputData.Axles
		Get
Markus Quaritsch's avatar
Markus Quaritsch committed
			Return AxleWheels().Cast(Of IAxleDeclarationInputData)().ToList()
		End Get
	End Property

	Private Function AxleWheels() As IEnumerable(Of AxleInputData)
		Return Axles.Select(Function(axle) New AxleInputData With {
								.SourceType = DataSourceType.JSONFile,
								.Source = FilePath,
Markus Quaritsch's avatar
Markus Quaritsch committed
								.Inertia = axle.Inertia.SI(Of KilogramSquareMeter)(),
								.Wheels = axle.Wheels,
								.AxleWeightShare = axle.Share,
								.TwinTyres = axle.TwinTire,
								.RollResistanceCoefficient = axle.RRC,
Markus Quaritsch's avatar
Markus Quaritsch committed
								.TyreTestLoad = axle.FzISO.SI(Of Newton)()
								})
	End Function

	Public ReadOnly Property CurbWeightExtra As Kilogram Implements IVehicleEngineeringInputData.CurbWeightExtra
		Get
Markus Quaritsch's avatar
Markus Quaritsch committed
			Return MassExtra.SI(Of Kilogram)()
	Public ReadOnly Property CrosswindCorrectionMap As TableData _
		Implements IVehicleEngineeringInputData.CrosswindCorrectionMap
		Get
			Return VectoCSVFile.Read(CrossWindCorrectionFile.FullPath)
		End Get
	End Property

	Public ReadOnly Property IVehicleEngineeringInputData_CrossWindCorrectionMode As CrossWindCorrectionMode _
		Implements IVehicleEngineeringInputData.CrossWindCorrectionMode
		Get
			Return CrossWindCorrectionMode
		End Get
	End Property

	Public ReadOnly Property IVehicleEngineeringInputData_DynamicTyreRadius As Meter _
		Implements IVehicleEngineeringInputData.DynamicTyreRadius
		Get
Markus Quaritsch's avatar
Markus Quaritsch committed
			Return DynamicTyreRadius.SI().Milli.Meter.Cast(Of Meter)()
		End Get
	End Property

	Public ReadOnly Property IVehicleEngineeringInputData_Loading As Kilogram _
		Implements IVehicleEngineeringInputData.Loading
		Get
Markus Quaritsch's avatar
Markus Quaritsch committed
			Return Loading.SI(Of Kilogram)()

	Public ReadOnly Property Type As RetarderType Implements IRetarderInputData.Type
		Get
			Return RetarderType
		End Get
	End Property

	Public ReadOnly Property IRetarderInputData_Ratio As Double Implements IRetarderInputData.Ratio
	Public ReadOnly Property Ratio As Double Implements IAngledriveInputData.Ratio
	Public ReadOnly Property IRetarderInputData_LossMap As TableData Implements IRetarderInputData.LossMap
			Return VectoCSVFile.Read(RetarderLossMapFile.FullPath)
	Public ReadOnly Property AngledriveInputDataType As AngledriveType Implements IAngledriveInputData.Type

	Public ReadOnly Property LossMap As TableData Implements IAngledriveInputData.LossMap
			Return VectoCSVFile.Read(AngledriveLossMapFile.FullPath)
	Public ReadOnly Property Efficiency As Double Implements IAngledriveInputData.Efficiency
Markus Quaritsch's avatar
Markus Quaritsch committed
			Return If(IsNumeric(AngledriveLossMapFile.OriginalPath), AngledriveLossMapFile.OriginalPath.ToDouble(), -1.0)

	Public ReadOnly Property PTOTransmissionType As String Implements IPTOTransmissionInputData.PTOTransmissionType
		Get
		End Get
	End Property

	Public ReadOnly Property IPTOTransmissionInputData_PTOCycle As TableData Implements IPTOTransmissionInputData.PTOCycle
		Get
Michael KRISPER's avatar
Michael KRISPER committed
			If String.IsNullOrWhiteSpace(PtoCycle.FullPath) Then
				Return Nothing
			End If
			Return VectoCSVFile.Read(PtoCycle.FullPath)
		End Get
	End Property

	Public ReadOnly Property IPTOTransmissionInputData_PTOLossMap As TableData _
		Implements IPTOTransmissionInputData.PTOLossMap
		Get
			Return VectoCSVFile.Read(PtoLossMap.FullPath)
Michael KRISPER's avatar
Michael KRISPER committed
End Class