Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.OKB.Views/3.3/RunCreation/Views/OKBProblemView.cs @ 17116

Last change on this file since 17116 was 13593, checked in by abeham, 9 years ago

#2457:

  • Changed ICharacteristicCalculator interface
  • Updated OKB Problem view
File size: 11.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Common.Resources;
23using HeuristicLab.Core.Views;
24using HeuristicLab.Data;
25using HeuristicLab.MainForm;
26using HeuristicLab.Optimization;
27using HeuristicLab.PluginInfrastructure;
28using System;
29using System.Collections.Generic;
30using System.Linq;
31using System.Windows.Forms;
32
33namespace HeuristicLab.Clients.OKB.RunCreation {
34  [View("OKBProblem View")]
35  [Content(typeof(SingleObjectiveOKBProblem), true)]
36  [Content(typeof(MultiObjectiveOKBProblem), true)]
37  public sealed partial class OKBProblemView : NamedItemView {
38    public new OKBProblem Content {
39      get { return (OKBProblem)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public OKBProblemView() {
44      InitializeComponent();
45      refreshButton.Text = string.Empty;
46      refreshButton.Image = VSImageLibrary.Refresh;
47      cloneProblemButton.Text = string.Empty;
48      cloneProblemButton.Image = VSImageLibrary.Clone;
49      downloadCharacteristicsButton.Text = string.Empty;
50      downloadCharacteristicsButton.Image = VSImageLibrary.Refresh;
51      uploadCharacteristicsButton.Text = string.Empty;
52      uploadCharacteristicsButton.Image = VSImageLibrary.PublishToWeb;
53    }
54
55    protected override void OnInitialized(System.EventArgs e) {
56      base.OnInitialized(e);
57      RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
58      RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
59      PopulateComboBox();
60    }
61
62    protected override void DeregisterContentEvents() {
63      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
64      base.DeregisterContentEvents();
65    }
66    protected override void RegisterContentEvents() {
67      base.RegisterContentEvents();
68      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
69    }
70
71    protected override void OnContentChanged() {
72      base.OnContentChanged();
73      if (Content == null) {
74        problemComboBox.SelectedIndex = -1;
75        parameterCollectionView.Content = null;
76      } else {
77        problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
78        parameterCollectionView.Content = Content.Parameters;
79      }
80      UpdateCharacteristicCalculators();
81    }
82
83    protected override void SetEnabledStateOfControls() {
84      base.SetEnabledStateOfControls();
85      problemComboBox.Enabled = (Content != null) && !ReadOnly && !Locked && (problemComboBox.Items.Count > 0);
86      cloneProblemButton.Enabled = (Content != null) && (Content.ProblemId != -1) && !ReadOnly && !Locked;
87      refreshButton.Enabled = (Content != null) && !ReadOnly && !Locked;
88      parameterCollectionView.Enabled = Content != null;
89      characteristicsMatrixView.Enabled = Content != null;
90      downloadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked;
91      uploadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly
92        && characteristicsMatrixView.Content != null && characteristicsMatrixView.Content.Rows > 0;
93      calculateButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly;
94    }
95
96    protected override void OnClosed(FormClosedEventArgs e) {
97      RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
98      RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
99      base.OnClosed(e);
100    }
101
102    private void UpdateCharacteristicCalculators() {
103      calculatorListView.Items.Clear();
104      calculatorListView.Groups.Clear();
105      if (Content == null || Content.ProblemId == -1) return;
106      var problem = Content.CloneProblem();
107      var calculators = ApplicationManager.Manager.GetInstances<ICharacteristicCalculator>().ToList();
108      try {
109        calculatorListView.BeginUpdate();
110        foreach (var calc in calculators) {
111          calc.Problem = problem;
112          var group = calculatorListView.Groups.Add(calc.Name, calc.Name);
113          group.Tag = calc;
114
115          var paramItem = calculatorListView.Items.Add(calc.Name + ".Parameters", "Parameters");
116          paramItem.Group = group;
117          paramItem.Tag = "param";
118
119          var charactItem = calculatorListView.Items.Add(calc.Name + ".Characteristics", "Characteristics");
120          charactItem.Group = group;
121          charactItem.Tag = "chara";
122        }
123      } finally { calculatorListView.EndUpdate(); }
124    }
125
126    private void RunCreationClient_Refreshing(object sender, EventArgs e) {
127      if (InvokeRequired) {
128        Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
129      } else {
130        Cursor = Cursors.AppStarting;
131        problemComboBox.Enabled = cloneProblemButton.Enabled = refreshButton.Enabled = parameterCollectionView.Enabled = false;
132      }
133    }
134    private void RunCreationClient_Refreshed(object sender, EventArgs e) {
135      if (InvokeRequired) {
136        Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
137      } else {
138        PopulateComboBox();
139        SetEnabledStateOfControls();
140        Cursor = Cursors.Default;
141      }
142    }
143
144    #region Content Events
145    private void Content_ProblemChanged(object sender, EventArgs e) {
146      if (InvokeRequired)
147        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
148      else {
149        OnContentChanged();
150        SetEnabledStateOfControls();
151      }
152    }
153    #endregion
154
155    #region Control Events
156    private void cloneProblemButton_Click(object sender, EventArgs e) {
157      MainFormManager.MainForm.ShowContent(Content.CloneProblem());
158    }
159    private void refreshButton_Click(object sender, System.EventArgs e) {
160      RunCreationClient.Instance.Refresh();
161    }
162    private void problemComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
163      Problem problem = problemComboBox.SelectedValue as Problem;
164      if ((problem != null) && (Content != null)) {
165        Content.Load(problem.Id);
166        if (Content.ProblemId != problem.Id)  // reset selected item if load was not successful
167          problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
168      }
169    }
170    private void downloadCharacteristicsButton_Click(object sender, EventArgs e) {
171      var values = RunCreationClient.GetCharacteristicValues(Content.ProblemId).ToList();
172      var content = new StringMatrix(values.Count, 3);
173      for (var i = 0; i < values.Count; i++) {
174        content[i, 0] = values[i].Name;
175        content[i, 1] = values[i].GetValue();
176        content[i, 2] = values[i].GetType().Name;
177      }
178      characteristicsMatrixView.Content = content;
179      SetEnabledStateOfControls();
180    }
181    private void uploadCharacteristicsButton_Click(object sender, EventArgs e) {
182      var matrix = characteristicsMatrixView.Content as StringMatrix;
183      if (matrix == null) return;
184      var values = new List<Value>(matrix.Rows);
185      for (var i = 0; i < matrix.Rows; i++) {
186        var name = matrix[i, 0];
187        var strValue = matrix[i, 1];
188        var type = matrix[i, 2];
189        values.Add(Value.Create(name, strValue, type));
190      }
191      try {
192        RunCreationClient.SetCharacteristicValues(Content.ProblemId, values);
193      } catch (Exception ex) { ErrorHandling.ShowErrorDialog(ex); }
194    }
195    private void calculatorListView_SelectedIndexChanged(object sender, EventArgs e) {
196      if (InvokeRequired) {
197        Invoke((Action<object, EventArgs>)calculatorListView_SelectedIndexChanged, sender, e);
198        return;
199      }
200      if (calculatorListView.SelectedIndices.Count == 0) calculatorViewHost.Content = null;
201      else {
202        var item = calculatorListView.SelectedItems[0];
203        if ((string)item.Tag == "param") {
204          calculatorViewHost.Content = ((ICharacteristicCalculator)item.Group.Tag);
205        } else if ((string)item.Tag == "chara") {
206          calculatorViewHost.Content = ((ICharacteristicCalculator)item.Group.Tag).Characteristics;
207        }
208      }
209    }
210    private void calculateButton_Click(object sender, EventArgs e) {
211      var calculators = calculatorListView.Groups.OfType<ListViewGroup>()
212                                                     .Select(x => (ICharacteristicCalculator)x.Tag)
213                                                     .Where(x => x.Characteristics.CheckedItems.Any()
214                                                              && x.CanCalculate()).ToList();
215      if (calculators.Count == 0) return;
216
217      var results = new Dictionary<string, Value>();
218      foreach (var calc in calculators) {
219        foreach (var result in calc.Calculate())
220          results[result.Name] = RunCreationClient.Instance.ConvertToValue(result.Value, result.Name);
221      }
222      var matrix = (characteristicsMatrixView.Content as StringMatrix) ?? (new StringMatrix(results.Count, 3));
223      for (var i = 0; i < matrix.Rows; i++) {
224        Value r;
225        if (results.TryGetValue(matrix[i, 0], out r)) {
226          matrix[i, 1] = r.GetValue();
227          matrix[i, 2] = r.GetType().Name;
228          results.Remove(matrix[i, 0]);
229        }
230      }
231      if (results.Count == 0) return;
232      var resultsList = results.ToList();
233      var counter = resultsList.Count - 1;
234      for (var i = 0; i < matrix.Rows; i++) {
235        if (string.IsNullOrEmpty(matrix[i, 0])) {
236          matrix[i, 0] = resultsList[counter].Key;
237          matrix[i, 1] = resultsList[counter].Value.GetValue();
238          matrix[i, 2] = resultsList[counter].Value.GetType().Name;
239          resultsList.RemoveAt(counter);
240          counter--;
241          if (counter < 0) return;
242        }
243      }
244      if (counter >= 0) {
245        ((IStringConvertibleMatrix)matrix).Rows += counter + 1;
246        for (var i = matrix.Rows - 1; counter >= 0; i--) {
247          matrix[i, 0] = resultsList[0].Key;
248          matrix[i, 1] = resultsList[0].Value.GetValue();
249          matrix[i, 2] = resultsList[0].Value.GetType().Name;
250          resultsList.RemoveAt(0);
251          counter--;
252        }
253      }
254      characteristicsMatrixView.Content = matrix;
255      SetEnabledStateOfControls();
256    }
257    #endregion
258
259    #region Helpers
260    private void PopulateComboBox() {
261      problemComboBox.DataSource = null;
262      problemComboBox.DataSource = RunCreationClient.Instance.Problems.ToList();
263      problemComboBox.DisplayMember = "Name";
264    }
265    #endregion
266
267  }
268}
Note: See TracBrowser for help on using the repository browser.