diff --git a/VECTO/GUI/F_MAINForm.vb b/VECTO/GUI/F_MAINForm.vb index e3bfb20fbf6803be8824ee6befe3a925528d1231..e92ae1648f4fd0a833be3104f315c0fb7ba02c67 100644 --- a/VECTO/GUI/F_MAINForm.vb +++ b/VECTO/GUI/F_MAINForm.vb @@ -1594,6 +1594,7 @@ Imports System.Text End If Dim progress As Dictionary(Of String, JobContainer.ProgressEntry) = jobContainer.GetProgress() + Dim sumProgress As Double = progress.Sum(Function(pair) pair.Value.Progress) Dim duration As Double = (DateTime.Now() - start).TotalSeconds diff --git a/VectoCore/FileIO/Reader/Impl/EngineeringModeSimulationDataReader.cs b/VectoCore/FileIO/Reader/Impl/EngineeringModeSimulationDataReader.cs index b9ecf580b1c7d90d9856ce0ce03c5efff0b50342..461f859b3fa7e042b1b2f371a7bb2e4db2413bdc 100644 --- a/VectoCore/FileIO/Reader/Impl/EngineeringModeSimulationDataReader.cs +++ b/VectoCore/FileIO/Reader/Impl/EngineeringModeSimulationDataReader.cs @@ -237,7 +237,7 @@ namespace TUGraz.VectoCore.FileIO.Reader.Impl tmp.BasePath = file; return tmp; default: - throw new UnsupportedFileVersionException("Unsopported Version of engine-file. Got version " + fileInfo.Version); + throw new UnsupportedFileVersionException("Unsupported Version of engine-file. Got version " + fileInfo.Version); } } diff --git a/VectoCore/Models/Connector/Ports/ISimulationPort.cs b/VectoCore/Models/Connector/Ports/ISimulationPort.cs index a30199ffef7b5f969a9b719d6bc301e3b60c4c84..f1ae591529e942006c253c3dc494ad2758f4df88 100644 --- a/VectoCore/Models/Connector/Ports/ISimulationPort.cs +++ b/VectoCore/Models/Connector/Ports/ISimulationPort.cs @@ -33,6 +33,7 @@ namespace TUGraz.VectoCore.Models.Connector.Ports IResponse Request(Second absTime, Second dt); IResponse Initialize(); + double Progress { get; } } } \ No newline at end of file diff --git a/VectoCore/Models/Simulation/IVectoRun.cs b/VectoCore/Models/Simulation/IVectoRun.cs index 63a7b080ad722371fc46b8d95223a5c20f7f9618..495adbe37346626d0b610487d39ae246737e6768 100644 --- a/VectoCore/Models/Simulation/IVectoRun.cs +++ b/VectoCore/Models/Simulation/IVectoRun.cs @@ -1,3 +1,4 @@ +using System; using System.ComponentModel; namespace TUGraz.VectoCore.Models.Simulation @@ -10,7 +11,7 @@ namespace TUGraz.VectoCore.Models.Simulation /// <summary> /// Run the simulation. /// </summary> - void Run(BackgroundWorker worker = null); + void Run(BackgroundWorker worker = null, Action<double> ReportProgress = null); string Name { get; } diff --git a/VectoCore/Models/Simulation/Impl/JobContainer.cs b/VectoCore/Models/Simulation/Impl/JobContainer.cs index 7144cc766938b63c9315efe3998bcb33320f59ca..499a1f5edf7d8a565693f5882454f1b1de8f3ef5 100644 --- a/VectoCore/Models/Simulation/Impl/JobContainer.cs +++ b/VectoCore/Models/Simulation/Impl/JobContainer.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; +using System.Linq.Expressions; using System.Threading; using TUGraz.VectoCore.Models.Simulation.Data; @@ -35,7 +36,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl public void AddRun(IVectoRun run) { _jobNumber++; - Runs.Add(new RunEntry { Run = run, Container = this }); + Runs.Add(new RunEntry { Run = run, JobContainer = this }); } public void AddRuns(IEnumerable<IVectoRun> runs) @@ -43,7 +44,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl _jobNumber++; //Runs.AddRange(runs); foreach (var run in runs) { - Runs.Add(new RunEntry { Run = run, Container = this }); + Runs.Add(new RunEntry { Run = run, JobContainer = this }); } } @@ -62,40 +63,29 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl Log.Info("VectoRun started running. Executing Runs."); foreach (var job in Runs) { - job.Worker = new BackgroundWorker() { - WorkerSupportsCancellation = true, - WorkerReportsProgress = true, - }; - job.Worker.DoWork += job.DoWork; - job.Worker.ProgressChanged += job.ProgressChanged; - job.Worker.RunWorkerCompleted += job.RunWorkerCompleted; if (multithreaded) { job.Started = true; - job.Worker.RunWorkerAsync(); + job.RunWorkerAsync(); } } if (!multithreaded) { var entry = Runs.First(); entry.Started = true; - entry.Worker.RunWorkerAsync(); + entry.RunWorkerAsync(); } } public void Cancel() { foreach (var job in Runs) { - if (job.Worker != null && job.Worker.WorkerSupportsCancellation) { - job.Worker.CancelAsync(); - } + job.CancelAsync(); } } public void CancelCurrent() { foreach (var job in Runs) { - if (job.Worker != null && job.Worker.IsBusy && job.Worker.WorkerSupportsCancellation) { - job.Worker.CancelAsync(); - } + job.CancelAsync(); } } @@ -107,12 +97,12 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl } - private void JobCompleted(RunEntry runEntry) + private void JobCompleted() { var next = Runs.FirstOrDefault(x => x.Started == false); if (next != null) { next.Started = true; - next.Worker.RunWorkerAsync(); + next.RunWorkerAsync(); } if (AllCompleted) { _sumWriter.Finish(); @@ -152,7 +142,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl internal class RunEntry : LoggingObject { public IVectoRun Run; - public JobContainer Container; + public JobContainer JobContainer; public double Progress; public bool Done; public bool Started; @@ -161,20 +151,35 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl public double ExecTime; public Exception ExecException; - public BackgroundWorker Worker; + private readonly BackgroundWorker _worker = new BackgroundWorker(); + + public RunEntry() + { + _worker.DoWork += OnDoWork; + _worker.RunWorkerCompleted += OnRunWorkerCompleted; + _worker.WorkerSupportsCancellation = true; + } + + public void RunWorkerAsync() + { + _worker.RunWorkerAsync(); + } + + public void CancelAsync() + { + _worker.CancelAsync(); + } - public void DoWork(object sender, DoWorkEventArgs e) + private void OnDoWork(object sender, DoWorkEventArgs e) { - var stopWatch = new Stopwatch(); - stopWatch.Start(); - var worker = sender as BackgroundWorker; + var stopWatch = Stopwatch.StartNew(); try { - Run.Run(worker); + Run.Run(_worker, (x => Progress = x)); } catch (Exception ex) { Log.Error(ex, "Error during simulation run!"); ExecException = ex; } - if (worker != null && worker.CancellationPending) { + if (_worker.CancellationPending) { e.Cancel = true; Canceled = true; } @@ -182,20 +187,15 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl Success = Run.FinishedWithoutErrors; Done = true; ExecTime = stopWatch.Elapsed.TotalMilliseconds; - Container.JobCompleted(this); + JobContainer.JobCompleted(); } - public void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) + private void OnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { ExecException = e.Error; } } - - public void ProgressChanged(object sender, ProgressChangedEventArgs e) - { - Progress = e.ProgressPercentage / 10000.0; - } } } } \ No newline at end of file diff --git a/VectoCore/Models/Simulation/Impl/VectoRun.cs b/VectoCore/Models/Simulation/Impl/VectoRun.cs index b570be7764ec04d7bd881b513ec528a305b55ea5..5705c81e653ae67a2d88fdb067cabba87aa853b6 100644 --- a/VectoCore/Models/Simulation/Impl/VectoRun.cs +++ b/VectoCore/Models/Simulation/Impl/VectoRun.cs @@ -39,7 +39,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl } - public void Run(BackgroundWorker worker = null) + public void Run(BackgroundWorker worker = null, Action<double> ReportProgress = null) { Log.Info("VectoJob started running."); @@ -51,10 +51,10 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl if (response is ResponseSuccess) { Container.CommitSimulationStep(AbsTime, dt); AbsTime += dt; - } - if (worker != null) { - worker.ReportProgress((int)(CyclePort.Progress * 10000)); - if (worker.CancellationPending) { + if (ReportProgress != null) { + ReportProgress(CyclePort.Progress); + } + if (worker != null && worker.CancellationPending) { Log.Error("Background Task canceled!"); Container.RunStatus = Status.Canceled; Container.FinishSimulation(); diff --git a/VectoCore/Models/SimulationComponent/Impl/DistanceBasedDrivingCycle.cs b/VectoCore/Models/SimulationComponent/Impl/DistanceBasedDrivingCycle.cs index c8143ba76a049f8171dce496e50bddb508125d4c..5192052509da306b5d4deeead02fc2d3dc61820f 100644 --- a/VectoCore/Models/SimulationComponent/Impl/DistanceBasedDrivingCycle.cs +++ b/VectoCore/Models/SimulationComponent/Impl/DistanceBasedDrivingCycle.cs @@ -22,7 +22,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl protected const double LookaheadTimeSafetyMargin = 1.5; protected readonly DrivingCycleData Data; - internal DrivingCycleState PreviousState = null; + internal DrivingCycleState PreviousState; internal DrivingCycleState CurrentState = new DrivingCycleState(); internal readonly DrivingCycleEnumerator CycleIntervalIterator; @@ -67,10 +67,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl IResponse ISimulationOutPort.Request(Second absTime, Meter ds) { var retVal = DoHandleRequest(absTime, ds); - CurrentState.Response = retVal; - - //switch (retVal.ResponseType) {} return retVal; } @@ -196,6 +193,9 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl return null; } + /// <summary> + /// time request not implemented in distance based driving cycle (method <see cref="DriveTimeInterval"/> is used). + /// </summary> IResponse ISimulationOutPort.Request(Second absTime, Second dt) { throw new NotImplementedException(); @@ -242,11 +242,6 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl #endregion - protected IResponse ProcessResponse(IResponse response) - { - throw new NotImplementedException(); - } - #region VectoSimulationComponent protected override void DoWriteModalResults(IModalDataWriter writer) @@ -387,6 +382,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl if (CurrentCycleIndex == Data.Entries.Count - 2) { LastEntry = true; } + return true; } @@ -425,7 +421,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl public IResponse Response; - public bool RequestToNextSamplePointDone = false; + public bool RequestToNextSamplePointDone; public Meter SimulationDistance; }