Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2588: fixed small bug after uploading solution

File size: 12.7 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;
24using HeuristicLab.Core.Views;
25using HeuristicLab.Data;
26using HeuristicLab.MainForm;
27using HeuristicLab.Optimization;
28using HeuristicLab.PluginInfrastructure;
29using System;
30using System.Collections.Generic;
31using System.Drawing;
32using System.Linq;
33using System.Windows.Forms;
34
35namespace HeuristicLab.Clients.OKB.RunCreation {
36  [View("OKBProblem View")]
37  [Content(typeof(SingleObjectiveOKBProblem), true)]
38  [Content(typeof(MultiObjectiveOKBProblem), true)]
39  public sealed partial class OKBProblemView : NamedItemView {
40    private readonly CheckedItemList<ICharacteristicCalculator> calculatorList;
41
42    public new OKBProblem Content {
43      get { return (OKBProblem)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public OKBProblemView() {
48      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();
83    }
84
85    protected override void OnInitialized(System.EventArgs e) {
86      base.OnInitialized(e);
87      RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
88      RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
89      PopulateComboBox();
90    }
91
92    protected override void DeregisterContentEvents() {
93      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
94      base.DeregisterContentEvents();
95    }
96    protected override void RegisterContentEvents() {
97      base.RegisterContentEvents();
98      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      SetEnabledStateOfControls();
107    }
108
109    protected override void OnContentChanged() {
110      base.OnContentChanged();
111      if (Content == null) {
112        problemComboBox.SelectedIndex = -1;
113        parameterCollectionView.Content = null;
114        solutionsViewHost.Content = null;
115      } else {
116        problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
117        parameterCollectionView.Content = Content.Parameters;
118        solutionsViewHost.Content = Content.Solutions;
119      }
120      UpdateCharacteristicCalculators();
121    }
122
123    protected override void SetEnabledStateOfControls() {
124      base.SetEnabledStateOfControls();
125      problemComboBox.Enabled = (Content != null) && !ReadOnly && !Locked && (problemComboBox.Items.Count > 0);
126      cloneProblemButton.Enabled = (Content != null) && (Content.ProblemId != -1) && !ReadOnly && !Locked;
127      refreshButton.Enabled = (Content != null) && !ReadOnly && !Locked;
128      parameterCollectionView.Enabled = Content != null;
129      characteristicsMatrixView.Enabled = Content != null;
130      downloadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked;
131      uploadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly
132        && characteristicsMatrixView.Content != null && characteristicsMatrixView.Content.Rows > 0;
133      calculateButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly && calculatorList.CheckedItems.Any();
134      refreshSolutionsButton.Enabled = Content != null && !ReadOnly && !Locked && Content.ProblemId != -1;
135      uploadSolutionsButton.Enabled = Content != null && !ReadOnly && !Locked && Content.ProblemId != -1 && Content.Solutions.Any(x => x.SolutionId == -1);
136    }
137
138    protected override void OnClosed(FormClosedEventArgs e) {
139      RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
140      RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
141      base.OnClosed(e);
142    }
143
144    private void UpdateCharacteristicCalculators() {
145      calculatorList.Clear();
146      if (Content == null || Content.ProblemId == -1) return;
147      var problem = Content.CloneProblem();
148      var calculators = ApplicationManager.Manager.GetInstances<ICharacteristicCalculator>().ToList();
149      foreach (var calc in calculators) {
150        calc.Problem = problem;
151        if (!calc.CanCalculate()) continue;
152        calculatorList.Add(calc, true);
153      }
154    }
155
156    private void RunCreationClient_Refreshing(object sender, EventArgs e) {
157      if (InvokeRequired) {
158        Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
159      } else {
160        Cursor = Cursors.AppStarting;
161        problemComboBox.Enabled = cloneProblemButton.Enabled = refreshButton.Enabled = parameterCollectionView.Enabled = false;
162      }
163    }
164    private void RunCreationClient_Refreshed(object sender, EventArgs e) {
165      if (InvokeRequired) {
166        Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
167      } else {
168        PopulateComboBox();
169        SetEnabledStateOfControls();
170        Cursor = Cursors.Default;
171      }
172    }
173
174    #region Content Events
175    private void Content_ProblemChanged(object sender, EventArgs e) {
176      if (InvokeRequired)
177        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
178      else {
179        OnContentChanged();
180        SetEnabledStateOfControls();
181      }
182    }
183    #endregion
184
185    #region Control Events
186    private void cloneProblemButton_Click(object sender, EventArgs e) {
187      MainFormManager.MainForm.ShowContent(Content.CloneProblem());
188    }
189    private void refreshButton_Click(object sender, System.EventArgs e) {
190      RunCreationClient.Instance.Refresh();
191    }
192    private void problemComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
193      Problem problem = problemComboBox.SelectedValue as Problem;
194      if ((problem != null) && (Content != null)) {
195        Content.Load(problem.Id);
196        if (Content.ProblemId != problem.Id)  // reset selected item if load was not successful
197          problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
198      }
199    }
200    private void downloadCharacteristicsButton_Click(object sender, EventArgs e) {
201      var values = RunCreationClient.Instance.GetCharacteristicValues(Content.ProblemId).ToList();
202      var content = new StringMatrix(values.Count, 3);
203      for (var i = 0; i < values.Count; i++) {
204        content[i, 0] = values[i].Name;
205        content[i, 1] = values[i].GetValue();
206        content[i, 2] = values[i].GetType().Name;
207      }
208      characteristicsMatrixView.Content = content;
209      SetEnabledStateOfControls();
210    }
211    private void uploadCharacteristicsButton_Click(object sender, EventArgs e) {
212      var matrix = characteristicsMatrixView.Content as StringMatrix;
213      if (matrix == null) return;
214      var values = new List<Value>(matrix.Rows);
215      for (var i = 0; i < matrix.Rows; i++) {
216        var name = matrix[i, 0];
217        var strValue = matrix[i, 1];
218        var type = matrix[i, 2];
219        values.Add(Value.Create(name, strValue, type));
220      }
221      try {
222        RunCreationClient.Instance.SetCharacteristicValues(Content.ProblemId, values);
223      } catch (Exception ex) { ErrorHandling.ShowErrorDialog(ex); }
224    }
225    private void calculateButton_Click(object sender, EventArgs e) {
226      var calculators = calculatorList.CheckedItems.Select(x => x.Value).Where(x => x.CanCalculate()).ToList();
227      if (calculators.Count == 0) return;
228
229      var results = new Dictionary<string, Value>();
230      foreach (var calc in calculators) {
231        foreach (var result in calc.Calculate())
232          results[result.Name] = RunCreationClient.Instance.ConvertToValue(result.Value, result.Name);
233      }
234      var matrix = (characteristicsMatrixView.Content as StringMatrix) ?? (new StringMatrix(results.Count, 3));
235      try {
236        for (var i = 0; i < matrix.Rows; i++) {
237          Value r;
238          if (results.TryGetValue(matrix[i, 0], out r)) {
239            matrix[i, 1] = r.GetValue();
240            matrix[i, 2] = r.GetType().Name;
241            results.Remove(matrix[i, 0]);
242          }
243        }
244        if (results.Count == 0) return;
245        var resultsList = results.ToList();
246        var counter = resultsList.Count - 1;
247        for (var i = 0; i < matrix.Rows; i++) {
248          if (string.IsNullOrEmpty(matrix[i, 0])) {
249            matrix[i, 0] = resultsList[counter].Key;
250            matrix[i, 1] = resultsList[counter].Value.GetValue();
251            matrix[i, 2] = resultsList[counter].Value.GetType().Name;
252            resultsList.RemoveAt(counter);
253            counter--;
254            if (counter < 0) return;
255          }
256        }
257        if (counter >= 0) {
258          ((IStringConvertibleMatrix)matrix).Rows += counter + 1;
259          for (var i = matrix.Rows - 1; counter >= 0; i--) {
260            matrix[i, 0] = resultsList[0].Key;
261            matrix[i, 1] = resultsList[0].Value.GetValue();
262            matrix[i, 2] = resultsList[0].Value.GetType().Name;
263            resultsList.RemoveAt(0);
264            counter--;
265          }
266        }
267      } finally {
268        characteristicsMatrixView.Content = matrix;
269        SetEnabledStateOfControls();
270      }
271    }
272    #endregion
273
274    #region Helpers
275    private void PopulateComboBox() {
276      problemComboBox.DataSource = null;
277      problemComboBox.DataSource = RunCreationClient.Instance.Problems.ToList();
278      problemComboBox.DisplayMember = "Name";
279    }
280    #endregion
281
282    private void refreshSolutionsButton_Click(object sender, EventArgs e) {
283      Content.RefreshSolutions();
284    }
285
286    private void uploadSolutionsButton_Click(object sender, EventArgs e) {
287      foreach (var solution in Content.Solutions.Where(x => x.SolutionId == -1))
288        solution.Upload();
289      SetEnabledStateOfControls();
290    }
291
292  }
293}
Note: See TracBrowser for help on using the repository browser.