1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using HeuristicLab.Common.Resources;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Core.Views;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.MainForm;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.PluginInfrastructure;
|
---|
29 | using System;
|
---|
30 | using System.Collections.Generic;
|
---|
31 | using System.Drawing;
|
---|
32 | using System.Linq;
|
---|
33 | using System.Windows.Forms;
|
---|
34 |
|
---|
35 | namespace 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 | }
|
---|
76 |
|
---|
77 | private void CalculatorListOnChanged(object sender, EventArgs e) {
|
---|
78 | SetEnabledStateOfControls();
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected override void OnInitialized(System.EventArgs e) {
|
---|
82 | base.OnInitialized(e);
|
---|
83 | RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
|
---|
84 | RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
|
---|
85 | PopulateComboBox();
|
---|
86 | }
|
---|
87 |
|
---|
88 | protected override void DeregisterContentEvents() {
|
---|
89 | Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
|
---|
90 | base.DeregisterContentEvents();
|
---|
91 | }
|
---|
92 | protected override void RegisterContentEvents() {
|
---|
93 | base.RegisterContentEvents();
|
---|
94 | Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
|
---|
95 | }
|
---|
96 |
|
---|
97 | protected override void OnContentChanged() {
|
---|
98 | base.OnContentChanged();
|
---|
99 | if (Content == null) {
|
---|
100 | problemComboBox.SelectedIndex = -1;
|
---|
101 | parameterCollectionView.Content = null;
|
---|
102 | } else {
|
---|
103 | problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
|
---|
104 | parameterCollectionView.Content = Content.Parameters;
|
---|
105 | }
|
---|
106 | UpdateCharacteristicCalculators();
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected override void SetEnabledStateOfControls() {
|
---|
110 | base.SetEnabledStateOfControls();
|
---|
111 | problemComboBox.Enabled = (Content != null) && !ReadOnly && !Locked && (problemComboBox.Items.Count > 0);
|
---|
112 | cloneProblemButton.Enabled = (Content != null) && (Content.ProblemId != -1) && !ReadOnly && !Locked;
|
---|
113 | refreshButton.Enabled = (Content != null) && !ReadOnly && !Locked;
|
---|
114 | 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();
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
123 | RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
|
---|
124 | RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
|
---|
125 | 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 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | private void RunCreationClient_Refreshing(object sender, EventArgs e) {
|
---|
141 | if (InvokeRequired) {
|
---|
142 | Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
|
---|
143 | } else {
|
---|
144 | Cursor = Cursors.AppStarting;
|
---|
145 | problemComboBox.Enabled = cloneProblemButton.Enabled = refreshButton.Enabled = parameterCollectionView.Enabled = false;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | private void RunCreationClient_Refreshed(object sender, EventArgs e) {
|
---|
149 | if (InvokeRequired) {
|
---|
150 | Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
|
---|
151 | } else {
|
---|
152 | PopulateComboBox();
|
---|
153 | SetEnabledStateOfControls();
|
---|
154 | Cursor = Cursors.Default;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | #region Content Events
|
---|
159 | private void Content_ProblemChanged(object sender, EventArgs e) {
|
---|
160 | if (InvokeRequired)
|
---|
161 | Invoke(new EventHandler(Content_ProblemChanged), sender, e);
|
---|
162 | else {
|
---|
163 | OnContentChanged();
|
---|
164 | SetEnabledStateOfControls();
|
---|
165 | }
|
---|
166 | }
|
---|
167 | #endregion
|
---|
168 |
|
---|
169 | #region Control Events
|
---|
170 | private void cloneProblemButton_Click(object sender, EventArgs e) {
|
---|
171 | MainFormManager.MainForm.ShowContent(Content.CloneProblem());
|
---|
172 | }
|
---|
173 | private void refreshButton_Click(object sender, System.EventArgs e) {
|
---|
174 | RunCreationClient.Instance.Refresh();
|
---|
175 | }
|
---|
176 | private void problemComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
|
---|
177 | Problem problem = problemComboBox.SelectedValue as Problem;
|
---|
178 | if ((problem != null) && (Content != null)) {
|
---|
179 | Content.Load(problem.Id);
|
---|
180 | if (Content.ProblemId != problem.Id) // reset selected item if load was not successful
|
---|
181 | problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
|
---|
182 | }
|
---|
183 | }
|
---|
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 | }
|
---|
253 | #endregion
|
---|
254 |
|
---|
255 | #region Helpers
|
---|
256 | private void PopulateComboBox() {
|
---|
257 | problemComboBox.DataSource = null;
|
---|
258 | problemComboBox.DataSource = RunCreationClient.Instance.Problems.ToList();
|
---|
259 | problemComboBox.DisplayMember = "Name";
|
---|
260 | }
|
---|
261 | #endregion
|
---|
262 |
|
---|
263 | }
|
---|
264 | }
|
---|