From 55fac82ad6271c22104956332fa6cfa0a216e1db Mon Sep 17 00:00:00 2001 From: "harald.martini@student.tugraz.at" <harald.martini@student.tugraz.at> Date: Thu, 27 May 2021 11:29:44 +0200 Subject: [PATCH] Updated Output view run simulation is task --- .../Implementation/JobListViewModel.cs | 111 ++++++++++++++---- .../ViewModel/Implementation/MessageEntry.cs | 7 ++ .../Implementation/OutputViewModel.cs | 23 +++- VECTO3GUI2020/Views/OutputView.xaml | 19 +-- 4 files changed, 127 insertions(+), 33 deletions(-) diff --git a/VECTO3GUI2020/ViewModel/Implementation/JobListViewModel.cs b/VECTO3GUI2020/ViewModel/Implementation/JobListViewModel.cs index 70c23b77c8..b3097005a9 100644 --- a/VECTO3GUI2020/ViewModel/Implementation/JobListViewModel.cs +++ b/VECTO3GUI2020/ViewModel/Implementation/JobListViewModel.cs @@ -157,11 +157,21 @@ namespace VECTO3GUI2020.ViewModel.Implementation { cancellationTokenSource = new CancellationTokenSource(); SimulationRunning = true; - await RunSimulationAsync(cancellationTokenSource.Token, - new Progress<MessageEntry>((message) => { _outputViewModel.Messages.Add(message); }), - new Progress<int>((i) => _outputViewModel.Progress = i)); + try { + await Task.Run(() => { + return RunSimulationAsync(cancellationTokenSource.Token, + new Progress<MessageEntry>((message) => { _outputViewModel.Messages.Add(message); }), + new Progress<int>((i) => _outputViewModel.SumProgress = i)); + }); + } catch (Exception e) { + _outputViewModel.Messages.Add(new MessageEntry() { + Message = e.Message, + Type = MessageType.ErrorMessage, + }); + } + SimulationRunning = false; - _outputViewModel.Progress = 0; + _outputViewModel.SumProgress = 0; cancellationTokenSource.Dispose(); } @@ -175,8 +185,7 @@ namespace VECTO3GUI2020.ViewModel.Implementation // return; // } //} - - + IDocumentViewModel[] jobs; lock (_jobsLock) { @@ -187,6 +196,7 @@ namespace VECTO3GUI2020.ViewModel.Implementation Time = DateTime.Now, Type = MessageType.InfoMessage, }); + } } @@ -298,24 +308,37 @@ namespace VECTO3GUI2020.ViewModel.Implementation SerializeVectoRunData = Settings.Default.SerializeVectoRunData, }; + + var stopwatch = new Stopwatch(); + stopwatch.Start(); foreach (var runId in jobContainer.AddRuns(runsFactory)) { + if (ct.IsCancellationRequested) { + outputMessages.Report(new MessageEntry() + { + Message = "Simulation canceled", + Type = MessageType.StatusMessage, + }); + return; + } fileWriters.Add(runId, fileWriter); } + stopwatch.Stop(); + // TODO MQ-20200525: Remove the following loop in production (or after evaluation of LAC!! - /* - if (!string.IsNullOrWhiteSpace(LookAheadMinSpeedOverride)) - { - foreach (var run in jobContainer.Runs) - { - var tmpDriver = ((VectoRun)run.Run).GetContainer().RunData.DriverData; - tmpDriver.LookAheadCoasting.Enabled = true; - tmpDriver.LookAheadCoasting.MinSpeed = LookAheadMinSpeedOverride.ToDouble().KMPHtoMeterPerSecond(); - } - } - */ + + //if (!string.IsNullOrWhiteSpace(LookAheadMinSpeedOverride)) + //{ + // foreach (var run in jobContainer.Runs) + // { + // var tmpDriver = ((VectoRun)run.Run).GetContainer().RunData.DriverData; + // tmpDriver.LookAheadCoasting.Enabled = true; + // tmpDriver.LookAheadCoasting.MinSpeed = LookAheadMinSpeedOverride.ToDouble().KMPHtoMeterPerSecond(); + // } + //} + outputMessages.Report( new MessageEntry() @@ -359,12 +382,17 @@ namespace VECTO3GUI2020.ViewModel.Implementation if (ct.IsCancellationRequested) { jobContainer.Cancel(); + outputMessages.Report(new MessageEntry() { + Message = "Simulation canceled", + Type = MessageType.StatusMessage, + }); return; } var jobProgress = jobContainer.GetProgress(); var sumProgress = jobProgress.Sum(x => x.Value.Progress); var duration = start.Elapsed.TotalSeconds; + jobProgress.Select(x => x.Value.Progress); progress.Report(Convert.ToInt32(sumProgress * 100 / jobProgress.Count)); //outputMessages.Report( @@ -379,7 +407,7 @@ namespace VECTO3GUI2020.ViewModel.Implementation .ToDictionary(x => x.Key, x => x.Value); //PrintRuns(justFinished, fileWriters); finishedRuns.AddRange(justFinished.Select(x => x.Key)); - await Task.Delay(100); + await Task.Delay(100, ct); } start.Stop(); @@ -446,9 +474,44 @@ namespace VECTO3GUI2020.ViewModel.Implementation Type = MessageType.StatusMessage, Message = string.Format("Simulation finished in {0:F1}s", start.Elapsed.TotalSeconds) }); - - } + private void PrintRuns(Dictionary<int, JobContainer.ProgressEntry> progress, Dictionary<int, FileOutputWriter> fileWriters, IProgress<MessageEntry> outputMessages) + { + foreach (var p in progress) { + var modFilename = fileWriters[p.Key] + .GetModDataFileName(p.Value.RunName, p.Value.CycleName, p.Value.RunSuffix); + var runName = string.Format("{0} {1} {2}", p.Value.RunName, p.Value.CycleName, p.Value.RunSuffix); + + // if (p.Value.Error != null) + // { + // SimulationWorker.ReportProgress(0, new VectoSimulationProgress() + // { + // Type = VectoSimulationProgress.MsgType.StatusMessage, + // Message = string.Format("Finished Run {0} with ERROR: {1}", runName, + // p.Value.Error.Message), + // Link = "<CSV>" + modFilename + // }); + // } + // else + // { + // SimulationWorker.ReportProgress(0, new VectoSimulationProgress() + // { + // Type = VectoSimulationProgress.MsgType.StatusMessage, + // Message = string.Format("Finished run {0} successfully.", runName) + // }); + // } + // if (File.Exists(modFilename)) + // { + // SimulationWorker.ReportProgress(0, new VectoSimulationProgress() + // { + // Type = VectoSimulationProgress.MsgType.StatusMessage, + // Message = string.Format("Run {0}: Modal results written to {1}", runName, modFilename), + // Link = "<CSV>" + modFilename + // }); + // } + } + } + private string GetOutputDirectory(string jobFilePath) { @@ -480,7 +543,13 @@ namespace VECTO3GUI2020.ViewModel.Implementation { get { - return _cancelSimulationCommand ?? new RelayCommand(() => { cancellationTokenSource.Cancel(); }, + return _cancelSimulationCommand ?? new RelayCommand(() => { + _outputViewModel.Messages.Add(new MessageEntry() { + Message="Canceling Simulation", + Type=MessageType.StatusMessage, + }); + cancellationTokenSource.Cancel(); + }, () => SimulationRunning); } } diff --git a/VECTO3GUI2020/ViewModel/Implementation/MessageEntry.cs b/VECTO3GUI2020/ViewModel/Implementation/MessageEntry.cs index af0ed0fcac..d1bff42290 100644 --- a/VECTO3GUI2020/ViewModel/Implementation/MessageEntry.cs +++ b/VECTO3GUI2020/ViewModel/Implementation/MessageEntry.cs @@ -20,6 +20,7 @@ namespace VECTO3GUI2020.ViewModel.Implementation private DateTime _time = DateTime.Now; private string _source; private MessageType _type; + private string _link; public string Message { @@ -27,6 +28,12 @@ namespace VECTO3GUI2020.ViewModel.Implementation set { SetProperty(ref _message, value); } } + public string Link + { + get { return _link; } + set {} + } + public MessageType Type { get { return _type; } diff --git a/VECTO3GUI2020/ViewModel/Implementation/OutputViewModel.cs b/VECTO3GUI2020/ViewModel/Implementation/OutputViewModel.cs index 88ea0207fc..9ac079c7d2 100644 --- a/VECTO3GUI2020/ViewModel/Implementation/OutputViewModel.cs +++ b/VECTO3GUI2020/ViewModel/Implementation/OutputViewModel.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows.Data; using VECTO3GUI2020.ViewModel.Implementation; @@ -14,7 +15,8 @@ namespace VECTO3GUI2020.ViewModel { private object _messageLock = new Object(); private ObservableCollection<MessageEntry> _messages = new ObservableCollection<MessageEntry>(); - private int _progress; + private double _sumProgress; + private IList<double> _subProgress; public ObservableCollection<MessageEntry> Messages { @@ -24,10 +26,10 @@ namespace VECTO3GUI2020.ViewModel } } - public int Progress + public double SumProgress { - get => _progress; - set => SetProperty(ref _progress, value); + get => _sumProgress; + set => SetProperty(ref _sumProgress, value); } @@ -36,14 +38,25 @@ namespace VECTO3GUI2020.ViewModel BindingOperations.EnableCollectionSynchronization(Messages, _messageLock ); } + public void SetProgress(double sumProgress, IList<double> subProgress) + { + SumProgress = sumProgress; + + SubProgress = subProgress; + } + public IList<double> SubProgress + { + get => _subProgress; + set => SetProperty(ref _subProgress, value); + } } public interface IOutputViewModel : IMainViewModel { ObservableCollection<MessageEntry> Messages { get; } - int Progress { get; set; } + double SumProgress { get; set; } } } \ No newline at end of file diff --git a/VECTO3GUI2020/Views/OutputView.xaml b/VECTO3GUI2020/Views/OutputView.xaml index 89a5c64cec..a0dac2c941 100644 --- a/VECTO3GUI2020/Views/OutputView.xaml +++ b/VECTO3GUI2020/Views/OutputView.xaml @@ -14,13 +14,18 @@ IsReadOnly="True" HeadersVisibility="All" RowHeaderWidth="5" Name="MessageList"> <DataGrid.Columns> - <DataGridTextColumn Header="Message" Binding="{Binding Message}" Width="*"> - <DataGridTextColumn.ElementStyle> - <Style> - <Setter Property="TextBlock.TextWrapping" Value="Wrap" /> - </Style> - </DataGridTextColumn.ElementStyle> - </DataGridTextColumn> + <DataGridTemplateColumn Header="Message" Width="*"> + <DataGridTemplateColumn.CellTemplate> + <DataTemplate> + <StackPanel> + <TextBlock Text="{Binding Message}" Visibility="{Binding Message, Converter={StaticResource NullToVisibilityConverter}}" + TextWrapping="Wrap"/> + <TextBlock Text="{Binding Link}" Visibility="{Binding Message, Converter={StaticResource NullToVisibilityConverter}}" + TextWrapping="Wrap"/> + </StackPanel> + </DataTemplate> + </DataGridTemplateColumn.CellTemplate> + </DataGridTemplateColumn> <DataGridTextColumn Header="Time" Binding="{Binding Time}" Width="130" /> </DataGrid.Columns> <DataGrid.RowStyle> -- GitLab