Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/12/12 16:50:46 (11 years ago)
Author:
abeham
Message:

#1985:

  • Moved code to format and parse timespans to a TimeSpanHelper class
  • Added a dialog for defining arithmetic sequences of timespans
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/RuntimeOptimizer/HeuristicLab.Optimization.Views/3.3/TimeLimitRunView.cs

    r9040 r9041  
    8484      try {
    8585        if (Content == null) {
    86           timeLimitTextBox.Text = FormatTimeSpan(TimeSpan.FromSeconds(60));
     86          timeLimitTextBox.Text = TimeSpanHelper.FormatNatural(TimeSpan.FromSeconds(60));
    8787          snapshotsTextBox.Text = String.Empty;
    8888          storeAlgorithmInEachSnapshotCheckBox.Checked = false;
     
    9191          runsView.Content = null;
    9292        } else {
    93           timeLimitTextBox.Text = FormatTimeSpan(Content.MaximumExecutionTime);
    94           snapshotsTextBox.Text = String.Join(" ; ", Content.SnapshotTimes.Select(x => FormatTimeSpan(x, true)));
     93          timeLimitTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime);
     94          snapshotsTextBox.Text = String.Join(" ; ", Content.SnapshotTimes.Select(x => TimeSpanHelper.FormatNatural(x, true)));
    9595          storeAlgorithmInEachSnapshotCheckBox.Checked = Content.StoreAlgorithmInEachSnapshot;
    9696          algorithmViewHost.Content = Content.Algorithm;
     
    137137          SuppressEvents = true;
    138138          try {
    139             timeLimitTextBox.Text = FormatTimeSpan(Content.MaximumExecutionTime);
     139            timeLimitTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime);
    140140          } finally { SuppressEvents = false; }
    141141          break;
     
    144144          try {
    145145            if (Content.SnapshotTimes.Any())
    146               snapshotsTextBox.Text = String.Join(" ; ", Content.SnapshotTimes.Select(x => FormatTimeSpan(x, true)));
     146              snapshotsTextBox.Text = String.Join(" ; ", Content.SnapshotTimes.Select(x => TimeSpanHelper.FormatNatural(x, true)));
    147147            else snapshotsTextBox.Text = String.Empty;
    148148            Content.SnapshotTimes.ItemsAdded += Content_SnapshotTimes_Changed;
     
    184184      try {
    185185        if (Content.SnapshotTimes.Any())
    186           snapshotsTextBox.Text = string.Join(" ; ", Content.SnapshotTimes.Select(x => FormatTimeSpan(x, true)));
     186          snapshotsTextBox.Text = string.Join(" ; ", Content.SnapshotTimes.Select(x => TimeSpanHelper.FormatNatural(x, true)));
    187187        else snapshotsTextBox.Text = String.Empty;
    188188      } finally { SuppressEvents = false; }
     
    194194      if (SuppressEvents) return;
    195195      TimeSpan ts;
    196       if (!TryGetTimeSpanFromFormat(timeLimitTextBox.Text, out ts)) {
     196      if (!TimeSpanHelper.TryGetFromNaturalFormat(timeLimitTextBox.Text, out ts)) {
    197197        e.Cancel = true;
    198198        errorProvider.SetError(timeLimitTextBox, "Please enter a valid time span, e.g. 60, 20s, 45 seconds, 3h, 1 hour, 4 d, 2 days");
     
    200200        Content.MaximumExecutionTime = ts;
    201201        e.Cancel = false;
    202         errorProvider.SetError(timeLimitTextBox, String.Empty);
     202        errorProvider.SetError(timeLimitTextBox, null);
    203203      }
    204204    }
     
    207207      if (SuppressEvents) return;
    208208      e.Cancel = false;
    209       errorProvider.SetError(timeLimitTextBox, String.Empty);
     209      errorProvider.SetError(snapshotsTextBox, null);
    210210
    211211      var snapshotTimes = new ObservableList<TimeSpan>();
     
    213213      foreach (Match m in matches) {
    214214        TimeSpan value;
    215         if (!TryGetTimeSpanFromFormat(m.Value, out value)) {
     215        if (!TimeSpanHelper.TryGetFromNaturalFormat(m.Value, out value)) {
    216216          e.Cancel = !snapshotsTextBox.ReadOnly && snapshotsTextBox.Enabled; // don't cancel an operation that cannot be edited
    217217          errorProvider.SetError(snapshotsTextBox, "Error parsing " + m.Value + ", please provide a valid time span, e.g. 60, 20s, 45 seconds, 3h, 1 hour, 4 d, 2 days");
     
    297297
    298298    private void sequenceButton_Click(object sender, EventArgs e) {
    299       using (var dialog = new DefineArithmeticProgressionDialog(false, 1, Content.MaximumExecutionTime.TotalSeconds, 1)) {
     299      using (var dialog = new DefineArithmeticTimeSpanProgressionDialog(TimeSpan.FromSeconds(1), Content.MaximumExecutionTime, TimeSpan.FromSeconds(1))) {
    300300        if (dialog.ShowDialog() == DialogResult.OK) {
    301301          if (dialog.Values.Any())
    302             Content.SnapshotTimes = new ObservableList<TimeSpan>(dialog.Values.Select(TimeSpan.FromSeconds));
     302            Content.SnapshotTimes = new ObservableList<TimeSpan>(dialog.Values);
    303303          else Content.SnapshotTimes = new ObservableList<TimeSpan>();
    304304        }
     
    307307    #endregion
    308308    #endregion
    309 
    310     private string FormatTimeSpan(TimeSpan ts, bool abbrevUnit = false) {
    311       if (ts.TotalSeconds < 180) return ts.TotalSeconds + (abbrevUnit ? "s" : " seconds");
    312       if (ts.TotalMinutes < 180) return ts.TotalMinutes + (abbrevUnit ? "min" : " minutes");
    313       if (ts.TotalHours < 96) return ts.TotalHours + (abbrevUnit ? "h" : " hours");
    314       return ts.TotalDays + (abbrevUnit ? "d" : " days");
    315     }
    316 
    317     private bool TryGetTimeSpanFromFormat(string text, out TimeSpan result) {
    318       double value;
    319       result = TimeSpan.Zero;
    320 
    321       var numberMatch = Regex.Match(text, @"\d+");
    322       if (!numberMatch.Success) return false;
    323 
    324       if (!double.TryParse(numberMatch.Value, out value))
    325         return false;
    326 
    327       var unitMatch = Regex.Match(text, @"[a-zA-Z]+$");
    328       if (!unitMatch.Success) return false;
    329 
    330       switch (unitMatch.Value) {
    331         case "d":
    332         case "day":
    333         case "days": result = TimeSpan.FromDays(value); break;
    334         case "h":
    335         case "hour":
    336         case "hours": result = TimeSpan.FromHours(value); break;
    337         case "min":
    338         case "minute":
    339         case "minutes": result = TimeSpan.FromMinutes(value); break;
    340         case "s":
    341         case "second":
    342         case "seconds": result = TimeSpan.FromSeconds(value); break;
    343         default: return false;
    344       }
    345       return true;
    346     }
    347309  }
    348310}
Note: See TracChangeset for help on using the changeset viewer.