Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/08/16 14:40:02 (8 years ago)
Author:
gkronber
Message:

#2434: merged trunk changes r12934:14026 from trunk to branch

Location:
branches/crossvalidation-2434
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/crossvalidation-2434

  • branches/crossvalidation-2434/HeuristicLab.Clients.OKB.Views/3.3/RunCreation/Views/OKBProblemView.cs

    r12012 r14029  
    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      refreshSolutionsButton.Text = string.Empty;
     76      refreshSolutionsButton.Image = VSImageLibrary.Refresh;
     77      uploadSolutionsButton.Text = string.Empty;
     78      uploadSolutionsButton.Image = VSImageLibrary.PublishToWeb;
     79    }
     80
     81    private void CalculatorListOnChanged(object sender, EventArgs e) {
     82      SetEnabledStateOfControls();
    4083    }
    4184
     
    5497      base.RegisterContentEvents();
    5598      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
     99      Content.Solutions.ItemsAdded += SolutionsOnChanged;
     100      Content.Solutions.ItemsReplaced += SolutionsOnChanged;
     101      Content.Solutions.ItemsRemoved += SolutionsOnChanged;
     102      Content.Solutions.CollectionReset += SolutionsOnChanged;
     103    }
     104
     105    private void SolutionsOnChanged(object sender, EventArgs e) {
     106      if (InvokeRequired) { Invoke((Action<object, EventArgs>)SolutionsOnChanged, sender, e); return; }
     107      SetEnabledStateOfControls();
    56108    }
    57109
     
    61113        problemComboBox.SelectedIndex = -1;
    62114        parameterCollectionView.Content = null;
     115        solutionsViewHost.Content = null;
    63116      } else {
    64117        problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
    65118        parameterCollectionView.Content = Content.Parameters;
    66       }
     119        solutionsViewHost.Content = Content.Solutions;
     120      }
     121      UpdateCharacteristicCalculators();
    67122    }
    68123
     
    73128      refreshButton.Enabled = (Content != null) && !ReadOnly && !Locked;
    74129      parameterCollectionView.Enabled = Content != null;
     130      characteristicsMatrixView.Enabled = Content != null;
     131      downloadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked;
     132      uploadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly
     133        && characteristicsMatrixView.Content != null && characteristicsMatrixView.Content.Rows > 0;
     134      calculateButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly && calculatorList.CheckedItems.Any();
     135      refreshSolutionsButton.Enabled = Content != null && !ReadOnly && !Locked && Content.ProblemId != -1;
     136      uploadSolutionsButton.Enabled = Content != null && !ReadOnly && !Locked && Content.ProblemId != -1 && Content.Solutions.Any(x => x.SolutionId == -1);
    75137    }
    76138
     
    79141      RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
    80142      base.OnClosed(e);
     143    }
     144
     145    private void UpdateCharacteristicCalculators() {
     146      calculatorList.Clear();
     147      if (Content == null || Content.ProblemId == -1) return;
     148      var problem = Content.CloneProblem();
     149      var calculators = ApplicationManager.Manager.GetInstances<ICharacteristicCalculator>().ToList();
     150      foreach (var calc in calculators) {
     151        calc.Problem = problem;
     152        if (!calc.CanCalculate()) continue;
     153        calculatorList.Add(calc, true);
     154      }
    81155    }
    82156
     
    103177      if (InvokeRequired)
    104178        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
    105       else
     179      else {
    106180        OnContentChanged();
     181        SetEnabledStateOfControls();
     182      }
    107183    }
    108184    #endregion
     
    123199      }
    124200    }
     201    private void downloadCharacteristicsButton_Click(object sender, EventArgs e) {
     202      var values = RunCreationClient.Instance.GetCharacteristicValues(Content.ProblemId).ToList();
     203      var content = new StringMatrix(values.Count, 3);
     204      for (var i = 0; i < values.Count; i++) {
     205        content[i, 0] = values[i].Name;
     206        content[i, 1] = values[i].GetValue();
     207        content[i, 2] = values[i].GetType().Name;
     208      }
     209      characteristicsMatrixView.Content = content;
     210      SetEnabledStateOfControls();
     211    }
     212    private void uploadCharacteristicsButton_Click(object sender, EventArgs e) {
     213      var matrix = characteristicsMatrixView.Content as StringMatrix;
     214      if (matrix == null) return;
     215      var values = new List<Value>(matrix.Rows);
     216      for (var i = 0; i < matrix.Rows; i++) {
     217        var name = matrix[i, 0];
     218        var strValue = matrix[i, 1];
     219        var type = matrix[i, 2];
     220        values.Add(Value.Create(name, strValue, type));
     221      }
     222      try {
     223        RunCreationClient.Instance.SetCharacteristicValues(Content.ProblemId, values);
     224      } catch (Exception ex) { ErrorHandling.ShowErrorDialog(ex); }
     225    }
     226    private void calculateButton_Click(object sender, EventArgs e) {
     227      var calculators = calculatorList.CheckedItems.Select(x => x.Value).Where(x => x.CanCalculate()).ToList();
     228      if (calculators.Count == 0) return;
     229
     230      var results = new Dictionary<string, Value>();
     231      foreach (var calc in calculators) {
     232        foreach (var result in calc.Calculate())
     233          results[result.Name] = RunCreationClient.Instance.ConvertToValue(result.Value, result.Name);
     234      }
     235      var matrix = (characteristicsMatrixView.Content as StringMatrix) ?? (new StringMatrix(results.Count, 3));
     236      try {
     237        for (var i = 0; i < matrix.Rows; i++) {
     238          Value r;
     239          if (results.TryGetValue(matrix[i, 0], out r)) {
     240            matrix[i, 1] = r.GetValue();
     241            matrix[i, 2] = r.GetType().Name;
     242            results.Remove(matrix[i, 0]);
     243          }
     244        }
     245        if (results.Count == 0) return;
     246        var resultsList = results.ToList();
     247        var counter = resultsList.Count - 1;
     248        for (var i = 0; i < matrix.Rows; i++) {
     249          if (string.IsNullOrEmpty(matrix[i, 0])) {
     250            matrix[i, 0] = resultsList[counter].Key;
     251            matrix[i, 1] = resultsList[counter].Value.GetValue();
     252            matrix[i, 2] = resultsList[counter].Value.GetType().Name;
     253            resultsList.RemoveAt(counter);
     254            counter--;
     255            if (counter < 0) return;
     256          }
     257        }
     258        if (counter >= 0) {
     259          ((IStringConvertibleMatrix)matrix).Rows += counter + 1;
     260          for (var i = matrix.Rows - 1; counter >= 0; i--) {
     261            matrix[i, 0] = resultsList[0].Key;
     262            matrix[i, 1] = resultsList[0].Value.GetValue();
     263            matrix[i, 2] = resultsList[0].Value.GetType().Name;
     264            resultsList.RemoveAt(0);
     265            counter--;
     266          }
     267        }
     268      } finally {
     269        characteristicsMatrixView.Content = matrix;
     270        SetEnabledStateOfControls();
     271      }
     272    }
    125273    #endregion
    126274
     
    132280    }
    133281    #endregion
     282
     283    private void refreshSolutionsButton_Click(object sender, EventArgs e) {
     284      Content.RefreshSolutions();
     285    }
     286
     287    private void uploadSolutionsButton_Click(object sender, EventArgs e) {
     288      foreach (var solution in Content.Solutions.Where(x => x.SolutionId == -1))
     289        solution.Upload();
     290      SetEnabledStateOfControls();
     291    }
     292
    134293  }
    135294}
Note: See TracChangeset for help on using the changeset viewer.