Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/28/17 22:10:45 (7 years ago)
Author:
gkronber
Message:

#2560 merged r13501, r13503, r13511, r13513, r13534, r13535, r13540, r13550, r13552, r13593, r13666 from trunk to stable

Location:
stable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Clients.OKB.Views/3.3/RunCreation/Views/OKBProblemView.cs

    r14186 r15081  
    2020#endregion
    2121
     22using HeuristicLab.Common.Resources;
     23using HeuristicLab.Core;
     24using HeuristicLab.Core.Views;
     25using HeuristicLab.Data;
     26using HeuristicLab.MainForm;
     27using HeuristicLab.Optimization;
     28using HeuristicLab.PluginInfrastructure;
    2229using System;
     30using System.Collections.Generic;
     31using System.Drawing;
    2332using System.Linq;
    2433using System.Windows.Forms;
    25 using HeuristicLab.Core.Views;
    26 using HeuristicLab.MainForm;
    2734
    2835namespace HeuristicLab.Clients.OKB.RunCreation {
     
    3138  [Content(typeof(MultiObjectiveOKBProblem), true)]
    3239  public sealed partial class OKBProblemView : NamedItemView {
     40    private readonly CheckedItemList<ICharacteristicCalculator> calculatorList;
     41
    3342    public new OKBProblem Content {
    3443      get { return (OKBProblem)base.Content; }
     
    3847    public OKBProblemView() {
    3948      InitializeComponent();
     49      var calculatorListView = new CheckedItemListView<ICharacteristicCalculator>() {
     50        Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top,
     51        Location = new Point(flaSplitContainer.Padding.Left, calculateButton.Location.Y + calculateButton.Height + calculateButton.Padding.Bottom + 3),
     52      };
     53      calculatorListView.Size = new Size(flaSplitContainer.Panel1.Size.Width - flaSplitContainer.Panel1.Padding.Horizontal,
     54          flaSplitContainer.Panel1.Height - calculatorListView.Location.Y - flaSplitContainer.Panel1.Padding.Bottom);
     55      calculatorList = new CheckedItemList<ICharacteristicCalculator>();
     56      calculatorList.ItemsAdded += CalculatorListOnChanged;
     57      calculatorList.ItemsRemoved += CalculatorListOnChanged;
     58      calculatorList.ItemsReplaced += CalculatorListOnChanged;
     59      calculatorList.CollectionReset += CalculatorListOnChanged;
     60      calculatorList.CheckedItemsChanged += CalculatorListOnChanged;
     61
     62      calculatorListView.Content = calculatorList.AsReadOnly();
     63
     64      flaSplitContainer.Panel1.Controls.Add(calculatorListView);
     65      calculateButton.Text = string.Empty;
     66      calculateButton.Image = VSImageLibrary.Play;
     67      refreshButton.Text = string.Empty;
     68      refreshButton.Image = VSImageLibrary.Refresh;
     69      cloneProblemButton.Text = string.Empty;
     70      cloneProblemButton.Image = VSImageLibrary.Clone;
     71      downloadCharacteristicsButton.Text = string.Empty;
     72      downloadCharacteristicsButton.Image = VSImageLibrary.Refresh;
     73      uploadCharacteristicsButton.Text = string.Empty;
     74      uploadCharacteristicsButton.Image = VSImageLibrary.PublishToWeb;
     75    }
     76
     77    private void CalculatorListOnChanged(object sender, EventArgs e) {
     78      SetEnabledStateOfControls();
    4079    }
    4180
     
    65104        parameterCollectionView.Content = Content.Parameters;
    66105      }
     106      UpdateCharacteristicCalculators();
    67107    }
    68108
     
    73113      refreshButton.Enabled = (Content != null) && !ReadOnly && !Locked;
    74114      parameterCollectionView.Enabled = Content != null;
     115      characteristicsMatrixView.Enabled = Content != null;
     116      downloadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked;
     117      uploadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly
     118        && characteristicsMatrixView.Content != null && characteristicsMatrixView.Content.Rows > 0;
     119      calculateButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly && calculatorList.CheckedItems.Any();
    75120    }
    76121
     
    79124      RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
    80125      base.OnClosed(e);
     126    }
     127
     128    private void UpdateCharacteristicCalculators() {
     129      calculatorList.Clear();
     130      if (Content == null || Content.ProblemId == -1) return;
     131      var problem = Content.CloneProblem();
     132      var calculators = ApplicationManager.Manager.GetInstances<ICharacteristicCalculator>().ToList();
     133      foreach (var calc in calculators) {
     134        calc.Problem = problem;
     135        if (!calc.CanCalculate()) continue;
     136        calculatorList.Add(calc, true);
     137      }
    81138    }
    82139
     
    103160      if (InvokeRequired)
    104161        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
    105       else
     162      else {
    106163        OnContentChanged();
     164        SetEnabledStateOfControls();
     165      }
    107166    }
    108167    #endregion
     
    123182      }
    124183    }
     184    private void downloadCharacteristicsButton_Click(object sender, EventArgs e) {
     185      var values = RunCreationClient.GetCharacteristicValues(Content.ProblemId).ToList();
     186      var content = new StringMatrix(values.Count, 3);
     187      for (var i = 0; i < values.Count; i++) {
     188        content[i, 0] = values[i].Name;
     189        content[i, 1] = values[i].GetValue();
     190        content[i, 2] = values[i].GetType().Name;
     191      }
     192      characteristicsMatrixView.Content = content;
     193      SetEnabledStateOfControls();
     194    }
     195    private void uploadCharacteristicsButton_Click(object sender, EventArgs e) {
     196      var matrix = characteristicsMatrixView.Content as StringMatrix;
     197      if (matrix == null) return;
     198      var values = new List<Value>(matrix.Rows);
     199      for (var i = 0; i < matrix.Rows; i++) {
     200        var name = matrix[i, 0];
     201        var strValue = matrix[i, 1];
     202        var type = matrix[i, 2];
     203        values.Add(Value.Create(name, strValue, type));
     204      }
     205      try {
     206        RunCreationClient.SetCharacteristicValues(Content.ProblemId, values);
     207      } catch (Exception ex) { ErrorHandling.ShowErrorDialog(ex); }
     208    }
     209    private void calculateButton_Click(object sender, EventArgs e) {
     210      var calculators = calculatorList.CheckedItems.Select(x => x.Value).Where(x => x.CanCalculate()).ToList();
     211      if (calculators.Count == 0) return;
     212
     213      var results = new Dictionary<string, Value>();
     214      foreach (var calc in calculators) {
     215        foreach (var result in calc.Calculate())
     216          results[result.Name] = RunCreationClient.Instance.ConvertToValue(result.Value, result.Name);
     217      }
     218      var matrix = (characteristicsMatrixView.Content as StringMatrix) ?? (new StringMatrix(results.Count, 3));
     219      for (var i = 0; i < matrix.Rows; i++) {
     220        Value r;
     221        if (results.TryGetValue(matrix[i, 0], out r)) {
     222          matrix[i, 1] = r.GetValue();
     223          matrix[i, 2] = r.GetType().Name;
     224          results.Remove(matrix[i, 0]);
     225        }
     226      }
     227      if (results.Count == 0) return;
     228      var resultsList = results.ToList();
     229      var counter = resultsList.Count - 1;
     230      for (var i = 0; i < matrix.Rows; i++) {
     231        if (string.IsNullOrEmpty(matrix[i, 0])) {
     232          matrix[i, 0] = resultsList[counter].Key;
     233          matrix[i, 1] = resultsList[counter].Value.GetValue();
     234          matrix[i, 2] = resultsList[counter].Value.GetType().Name;
     235          resultsList.RemoveAt(counter);
     236          counter--;
     237          if (counter < 0) return;
     238        }
     239      }
     240      if (counter >= 0) {
     241        ((IStringConvertibleMatrix)matrix).Rows += counter + 1;
     242        for (var i = matrix.Rows - 1; counter >= 0; i--) {
     243          matrix[i, 0] = resultsList[0].Key;
     244          matrix[i, 1] = resultsList[0].Value.GetValue();
     245          matrix[i, 2] = resultsList[0].Value.GetType().Name;
     246          resultsList.RemoveAt(0);
     247          counter--;
     248        }
     249      }
     250      characteristicsMatrixView.Content = matrix;
     251      SetEnabledStateOfControls();
     252    }
    125253    #endregion
    126254
     
    132260    }
    133261    #endregion
     262
    134263  }
    135264}
Note: See TracChangeset for help on using the changeset viewer.