From fcb4161301b37516d57724cf9142483581f6ea67 Mon Sep 17 00:00:00 2001 From: Michael Krisper <michael.krisper@tugraz.at> Date: Thu, 20 Aug 2015 17:16:37 +0200 Subject: [PATCH] some refactorings, finally removed all ResponseTypes (only type-switch is used now) --- .../Models/Connector/Ports/Impl/Response.cs | 5 +- .../Models/Simulation/Impl/DistanceRun.cs | 22 +++---- VectoCore/Models/Simulation/Impl/TimeRun.cs | 4 -- VectoCore/Models/Simulation/Impl/VectoRun.cs | 2 +- .../Data/AccelerationCurve.cs | 2 - .../Impl/CombustionEngine.cs | 4 +- .../Models/SimulationComponent/Impl/Driver.cs | 21 ++++--- .../Impl/EngineOnlyCombustionEngine.cs | 4 +- .../SimulationComponent/Impl/Gearbox.cs | 2 +- VectoCore/Utils/SwitchExtension.cs | 63 +++++++++---------- 10 files changed, 60 insertions(+), 69 deletions(-) diff --git a/VectoCore/Models/Connector/Ports/Impl/Response.cs b/VectoCore/Models/Connector/Ports/Impl/Response.cs index 55929de638..bbe2b2c24c 100644 --- a/VectoCore/Models/Connector/Ports/Impl/Response.cs +++ b/VectoCore/Models/Connector/Ports/Impl/Response.cs @@ -63,9 +63,8 @@ namespace TUGraz.VectoCore.Models.Connector.Ports.Impl internal class ResponseDryRun : AbstractResponse { - public Watt EngineDeltaFullLoad { get; set; } - public Watt EngineDeltaDragLoad { get; set; } - public Watt GearboxDeltaFullLoad { get; set; } + public Watt DeltaFullLoad { get; set; } + public Watt DeltaDragLoad { get; set; } } internal class ResponseGearShift : AbstractResponse {} diff --git a/VectoCore/Models/Simulation/Impl/DistanceRun.cs b/VectoCore/Models/Simulation/Impl/DistanceRun.cs index 4f0b205b7c..460271afe1 100644 --- a/VectoCore/Models/Simulation/Impl/DistanceRun.cs +++ b/VectoCore/Models/Simulation/Impl/DistanceRun.cs @@ -20,28 +20,26 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl ds = Constants.SimulationSettings.DriveOffDistance; } - IResponse response; + IResponse response = null; var requestDone = false; - - do { + while (!requestDone) { response = CyclePort.Request(AbsTime, ds); + response.Switch(). - Case<ResponseSuccess>(_ => requestDone = true). - Case<ResponseCycleFinished>(_ => requestDone = true). + Case<ResponseSuccess>(r => { + AbsTime = AbsTime + r.SimulationInterval; + dt = r.SimulationInterval; + requestDone = true; + }). + Case<ResponseCycleFinished>(() => requestDone = true). Case<ResponseDrivingCycleDistanceExceeded>(r => { ds = r.MaxDistance; }); - } while (!requestDone); + } //while (response is ResponseFailTimeInterval) { // _dt = (response as ResponseFailTimeInterval).DeltaT; // response = CyclePort.Request(_absTime, _dt); //} - if (response is ResponseCycleFinished) { - return response; - } - - AbsTime = AbsTime + response.SimulationInterval; - dt = response.SimulationInterval; return response; } diff --git a/VectoCore/Models/Simulation/Impl/TimeRun.cs b/VectoCore/Models/Simulation/Impl/TimeRun.cs index b9e4f36453..e97bab4dbb 100644 --- a/VectoCore/Models/Simulation/Impl/TimeRun.cs +++ b/VectoCore/Models/Simulation/Impl/TimeRun.cs @@ -11,10 +11,6 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl protected override IResponse DoSimulationStep() { var response = CyclePort.Request(AbsTime, dt); - - if (response is ResponseCycleFinished) { - return response; - } return response; } diff --git a/VectoCore/Models/Simulation/Impl/VectoRun.cs b/VectoCore/Models/Simulation/Impl/VectoRun.cs index 7ec4b31e23..6483914b3b 100644 --- a/VectoCore/Models/Simulation/Impl/VectoRun.cs +++ b/VectoCore/Models/Simulation/Impl/VectoRun.cs @@ -49,7 +49,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl Container.CommitSimulationStep(AbsTime, dt); } - // set _dt to difference to next full second. + // todo set _dt to difference to next full second. AbsTime += dt; } while (response is ResponseSuccess); diff --git a/VectoCore/Models/SimulationComponent/Data/AccelerationCurve.cs b/VectoCore/Models/SimulationComponent/Data/AccelerationCurve.cs index 1dd22d194b..c07ae3c86f 100644 --- a/VectoCore/Models/SimulationComponent/Data/AccelerationCurve.cs +++ b/VectoCore/Models/SimulationComponent/Data/AccelerationCurve.cs @@ -138,8 +138,6 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data var b = -d / k; var c = 0.SI<Meter>() - m / k; var t = Math.Log(((v2 * k + d) / (v1 * k + d)).Cast<Scalar>()) / k; - // TODO @@@quam: remove .SI<> when bug #VECTO-111 is fixed... - return m / k * Math.Exp((k * t).Value()) + b * t + c; } } diff --git a/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs b/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs index eb0e39937d..3d7682ac3c 100644 --- a/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs +++ b/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs @@ -98,8 +98,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl if (dryRun) { return new ResponseDryRun { - EngineDeltaFullLoad = (requestedEnginePower - _currentState.DynamicFullLoadPower), - EngineDeltaDragLoad = (requestedEnginePower - _currentState.FullDragPower), + DeltaFullLoad = (requestedEnginePower - _currentState.DynamicFullLoadPower), + DeltaDragLoad = (requestedEnginePower - _currentState.FullDragPower), EnginePowerRequest = requestedEnginePower }; } diff --git a/VectoCore/Models/SimulationComponent/Impl/Driver.cs b/VectoCore/Models/SimulationComponent/Impl/Driver.cs index fddfa9d21c..ad178ed433 100644 --- a/VectoCore/Models/SimulationComponent/Impl/Driver.cs +++ b/VectoCore/Models/SimulationComponent/Impl/Driver.cs @@ -177,11 +177,10 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl CurrentState.Acceleration); var retVal = Next.Request(absTime, CurrentState.dt, CurrentState.Acceleration, gradient); CurrentState.Response = retVal; - if (retVal is ResponseSuccess) { - retVal.SimulationInterval = CurrentState.dt; - } else { - Log.DebugFormat("unhandled response from powertrain: {0}", retVal); - } + + retVal.Switch(). + Case<ResponseSuccess>(r => r.SimulationInterval = CurrentState.dt). + Default(() => Log.DebugFormat("unhandled response from powertrain: {0}", retVal)); return retVal; //new ResponseDrivingCycleDistanceExceeded() { SimulationInterval = CurrentState.dt }; } @@ -189,7 +188,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl private bool SearchBreakingPower(Second absTime, ref Meter ds, Radian gradient, ResponseDryRun response, bool coasting) { var exceeded = new List<Watt>(); // only used while testing - var breakingPower = response.EngineDeltaDragLoad.Abs(); + var breakingPower = response.DeltaDragLoad.Abs(); if (DataBus.ClutchState() != ClutchState.ClutchClosed) { breakingPower = response.AxlegearPowerRequest.Abs(); } @@ -199,7 +198,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl do { ds = originalDs; var delta = DataBus.ClutchState() == ClutchState.ClutchClosed - ? -response.EngineDeltaDragLoad + ? -response.DeltaDragLoad : -response.AxlegearPowerRequest; exceeded.Add(delta); @@ -408,14 +407,16 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl ds = originalDs; response.Switch(). Case<ResponseEngineOverload>(r => delta = r.Delta). - Case<ResponseDryRun>(r => delta = coasting ? -r.EngineDeltaDragLoad : r.EngineDeltaFullLoad). + Case<ResponseGearboxOverload>(r => delta = r.Delta). + Case<ResponseDryRun>(r => delta = coasting ? -r.DeltaDragLoad : r.DeltaFullLoad). Default(r => { throw new VectoSimulationException(string.Format("Unknown response type. {0}", r)); }); exceeded.Add(delta); acceleration.Add(CurrentState.Acceleration.Value()); if (delta.IsEqual(0, Constants.SimulationSettings.EngineFLDPowerTolerance)) { - Log.DebugFormat("found operating point in {0} iterations. Engine Power req: {2}, delta: {1}", exceeded.Count, - delta, response.EnginePowerRequest); + Log.DebugFormat( + "found operating point in {0} iterations. Engine Power req: {2}, Gearbox Power req: {3} delta: {1}", + exceeded.Count, delta, response.EnginePowerRequest, response.GearboxPowerRequest); return true; } if (delta > 0) { diff --git a/VectoCore/Models/SimulationComponent/Impl/EngineOnlyCombustionEngine.cs b/VectoCore/Models/SimulationComponent/Impl/EngineOnlyCombustionEngine.cs index 2dfea89992..d071c7ff4f 100644 --- a/VectoCore/Models/SimulationComponent/Impl/EngineOnlyCombustionEngine.cs +++ b/VectoCore/Models/SimulationComponent/Impl/EngineOnlyCombustionEngine.cs @@ -31,8 +31,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl if (dryRun) { return new ResponseDryRun { - EngineDeltaFullLoad = (requestedEnginePower - _currentState.DynamicFullLoadPower), - EngineDeltaDragLoad = (requestedEnginePower - _currentState.FullDragPower) + DeltaFullLoad = (requestedEnginePower - _currentState.DynamicFullLoadPower), + DeltaDragLoad = (requestedEnginePower - _currentState.FullDragPower) }; } diff --git a/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs b/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs index 157ed811eb..29cf28ff99 100644 --- a/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs +++ b/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs @@ -132,7 +132,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl response.GearboxPowerRequest = inTorque * inEngineSpeed; response.Switch(). - Case<ResponseDryRun>(r => r.GearboxDeltaFullLoad = (maxTorque - inTorque) * inEngineSpeed); + Case<ResponseDryRun>(r => r.DeltaFullLoad = (maxTorque - inTorque) * inEngineSpeed); return response; } diff --git a/VectoCore/Utils/SwitchExtension.cs b/VectoCore/Utils/SwitchExtension.cs index 8694ff548c..1912bc1555 100644 --- a/VectoCore/Utils/SwitchExtension.cs +++ b/VectoCore/Utils/SwitchExtension.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.CompilerServices; namespace TUGraz.VectoCore.Utils { @@ -11,46 +10,46 @@ namespace TUGraz.VectoCore.Utils /// </remarks> public static class SwitchExtension { - public static Switcher<T> Switch<T>(this T self) + public static Switch<T> Switch<T>(this T self) { - return new Switcher<T>(self); + return new Switch<T>(self); } + } - public class Switcher<T> - { - private readonly T _value; - private bool _handled; + public class Switch<T> + { + private readonly T _value; + private bool _handled; - internal Switcher(T value) - { - _value = value; - _handled = false; - } + internal Switch(T value) + { + _value = value; + _handled = false; + } - public Switcher<T> Case<TTarget>(Action action) where TTarget : T - { - return Case<TTarget>(_ => action()); - } + public Switch<T> Case<TFilter>(Action action) where TFilter : T + { + return Case<TFilter>(_ => action()); + } - public Switcher<T> Case<TTarget>(Action<TTarget> action) where TTarget : T - { - if (!_handled && _value is TTarget) { - action((TTarget)_value); - _handled = true; - } - return this; + public Switch<T> Case<TFilter>(Action<TFilter> action) where TFilter : T + { + if (!_handled && _value is TFilter) { + action((TFilter)_value); + _handled = true; } + return this; + } - public void Default(Action action) - { - Default(_ => action()); - } + public void Default(Action action) + { + Default(_ => action()); + } - public void Default(Action<T> action) - { - if (!_handled) { - action(_value); - } + public void Default(Action<T> action) + { + if (!_handled) { + action(_value); } } } -- GitLab