Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.OKB.Views/3.3/RunCreation/Views/OKBProblemView.cs @ 13550

Last change on this file since 13550 was 13550, checked in by abeham, 8 years ago

#2560: changed characteristic calculator to output IItem instead of double

File size: 10.4 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 System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31using HeuristicLab.PluginInfrastructure;
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 && calculatorListView.CheckedItems.Count > 0;
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>().Where(x => x.CanCalculate(problem)).ToList();
108      try {
109        calculatorListView.BeginUpdate();
110        foreach (var calc in calculators) {
111          var group = calculatorListView.Groups.Add(calc.Name, calc.Name);
112          group.Tag = calc;
113          foreach (var c in calc.Characteristics) {
114            var item = calculatorListView.Items.Add(c, c);
115            item.Group = group;
116            item.Checked = true;
117          }
118        }
119      } finally { calculatorListView.EndUpdate(); }
120    }
121
122    private void RunCreationClient_Refreshing(object sender, EventArgs e) {
123      if (InvokeRequired) {
124        Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
125      } else {
126        Cursor = Cursors.AppStarting;
127        problemComboBox.Enabled = cloneProblemButton.Enabled = refreshButton.Enabled = parameterCollectionView.Enabled = false;
128      }
129    }
130    private void RunCreationClient_Refreshed(object sender, EventArgs e) {
131      if (InvokeRequired) {
132        Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
133      } else {
134        PopulateComboBox();
135        SetEnabledStateOfControls();
136        Cursor = Cursors.Default;
137      }
138    }
139
140    #region Content Events
141    private void Content_ProblemChanged(object sender, EventArgs e) {
142      if (InvokeRequired)
143        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
144      else {
145        OnContentChanged();
146        SetEnabledStateOfControls();
147      }
148    }
149    #endregion
150
151    #region Control Events
152    private void cloneProblemButton_Click(object sender, EventArgs e) {
153      MainFormManager.MainForm.ShowContent(Content.CloneProblem());
154    }
155    private void refreshButton_Click(object sender, System.EventArgs e) {
156      RunCreationClient.Instance.Refresh();
157    }
158    private void problemComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
159      Problem problem = problemComboBox.SelectedValue as Problem;
160      if ((problem != null) && (Content != null)) {
161        Content.Load(problem.Id);
162        if (Content.ProblemId != problem.Id)  // reset selected item if load was not successful
163          problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
164      }
165    }
166    private void downloadCharacteristicsButton_Click(object sender, EventArgs e) {
167      var values = RunCreationClient.GetCharacteristicValues(Content.ProblemId).ToList();
168      var content = new StringMatrix(values.Count, 3);
169      for (var i = 0; i < values.Count; i++) {
170        content[i, 0] = values[i].Name;
171        content[i, 1] = values[i].ToString();
172        content[i, 2] = values[i].GetType().Name;
173      }
174      characteristicsMatrixView.Content = content;
175      SetEnabledStateOfControls();
176    }
177    private void uploadCharacteristicsButton_Click(object sender, EventArgs e) {
178      var matrix = characteristicsMatrixView.Content as StringMatrix;
179      if (matrix == null) return;
180      var values = new List<Value>(matrix.Rows);
181      for (var i = 0; i < matrix.Rows; i++) {
182        var name = matrix[i, 0];
183        var strValue = matrix[i, 1];
184        var type = matrix[i, 2];
185        values.Add(Value.Create(name, strValue, type));
186      }
187      try {
188        RunCreationClient.SetCharacteristicValues(Content.ProblemId, values);
189      } catch (Exception ex) { ErrorHandling.ShowErrorDialog(ex); }
190    }
191    private void calculateButton_Click(object sender, EventArgs e) {
192      var characteristics = calculatorListView.CheckedItems.OfType<ListViewItem>().GroupBy(x => x.Group).ToList();
193      if (characteristics.Count == 0) return;
194      var problem = Content.CloneProblem();
195      var results = new Dictionary<string, Value>();
196      foreach (var c in characteristics) {
197        var calc = (ICharacteristicCalculator)c.Key.Tag;
198        foreach (var result in calc.Calculate(problem, c.Select(x => x.Text).ToArray()))
199          results[result.Key] = RunCreationClient.Instance.ConvertToValue(result.Value, result.Key);
200      }
201      var matrix = (characteristicsMatrixView.Content as StringMatrix) ?? (new StringMatrix(results.Count, 3));
202      for (var i = 0; i < matrix.Rows; i++) {
203        Value r;
204        if (results.TryGetValue(matrix[i, 0], out r)) {
205          matrix[i, 1] = r.GetValue();
206          matrix[i, 2] = r.GetType().Name;
207          results.Remove(matrix[i, 0]);
208        }
209      }
210      if (results.Count == 0) return;
211      var resultsList = results.ToList();
212      var counter = resultsList.Count - 1;
213      for (var i = 0; i < matrix.Rows; i++) {
214        if (string.IsNullOrEmpty(matrix[i, 0])) {
215          matrix[i, 0] = resultsList[counter].Key;
216          matrix[i, 1] = resultsList[counter].Value.GetValue();
217          matrix[i, 2] = resultsList[counter].Value.GetType().Name;
218          resultsList.RemoveAt(counter);
219          counter--;
220          if (counter < 0) return;
221        }
222      }
223      if (counter >= 0) {
224        ((IStringConvertibleMatrix)matrix).Rows += counter + 1;
225        for (var i = matrix.Rows - 1; counter >= 0; i--) {
226          matrix[i, 0] = resultsList[0].Key;
227          matrix[i, 1] = resultsList[0].Value.GetValue();
228          matrix[i, 2] = resultsList[0].Value.GetType().Name;
229          resultsList.RemoveAt(0);
230          counter--;
231        }
232      }
233      SetEnabledStateOfControls();
234    }
235    private void calculatorListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
236      SetEnabledStateOfControls();
237    }
238    #endregion
239
240    #region Helpers
241    private void PopulateComboBox() {
242      problemComboBox.DataSource = null;
243      problemComboBox.DataSource = RunCreationClient.Instance.Problems.ToList();
244      problemComboBox.DisplayMember = "Name";
245    }
246    #endregion
247
248  }
249}
Note: See TracBrowser for help on using the repository browser.