Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Views/3.3/ExpertSystemView.cs @ 12860

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

#2457: worked on expert system

File size: 10.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.ComponentModel;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.Optimization;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.OptimizationExpertSystem.Views {
32  [View("Expert-system Optimizer View")]
33  [Content(typeof(ExpertSystem), IsDefaultView = true)]
34  public partial class ExpertSystemView : NamedItemView {
35    protected TypeSelectorDialog problemTypeSelectorDialog;
36    protected virtual bool SuppressEvents { get; set; }
37
38    public new ExpertSystem Content {
39      get { return (ExpertSystem)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public ExpertSystemView() {
44      InitializeComponent();
45    }
46
47    protected override void Dispose(bool disposing) {
48      if (disposing) {
49        if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose();
50        if (components != null) components.Dispose();
51      }
52      base.Dispose(disposing);
53    }
54
55    protected override void DeregisterContentEvents() {
56      Content.PropertyChanged -= Content_PropertyChanged;
57      Content.SuggestedInstances.CollectionReset -= SuggestedInstancesOnChanged;
58      Content.SuggestedInstances.ItemsAdded -= SuggestedInstancesOnChanged;
59      Content.SuggestedInstances.ItemsMoved -= SuggestedInstancesOnChanged;
60      Content.SuggestedInstances.ItemsRemoved -= SuggestedInstancesOnChanged;
61      Content.SuggestedInstances.ItemsReplaced -= SuggestedInstancesOnChanged;
62      base.DeregisterContentEvents();
63    }
64    protected override void RegisterContentEvents() {
65      base.RegisterContentEvents();
66      Content.PropertyChanged += Content_PropertyChanged;
67      Content.SuggestedInstances.CollectionReset += SuggestedInstancesOnChanged;
68      Content.SuggestedInstances.ItemsAdded += SuggestedInstancesOnChanged;
69      Content.SuggestedInstances.ItemsMoved += SuggestedInstancesOnChanged;
70      Content.SuggestedInstances.ItemsRemoved += SuggestedInstancesOnChanged;
71      Content.SuggestedInstances.ItemsReplaced += SuggestedInstancesOnChanged;
72    }
73
74    protected override void OnContentChanged() {
75      base.OnContentChanged();
76      SuppressEvents = true;
77      try {
78        if (Content == null) {
79          maxEvaluationsTextBox.Text = String.Empty;
80
81          problemViewHost.Content = null;
82          algorithmViewHost.Content = null;
83          runsView.Content = null;
84          algorithmInstancesViewHost.Content = null;
85        } else {
86          maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString();
87          problemViewHost.Content = Content.Problem;
88          runsView.Content = Content.Runs;
89          algorithmInstancesViewHost.Content = Content.AlgorithmInstances;
90        }
91      } finally { SuppressEvents = false; }
92      UpdateSuggestedInstancesCombobox();
93    }
94
95    protected override void SetEnabledStateOfControls() {
96      base.SetEnabledStateOfControls();
97      maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked;
98      newProblemButton.Enabled = Content != null && !ReadOnly && !Locked;
99      openProblemButton.Enabled = Content != null && !ReadOnly && !Locked;
100      problemViewHost.Enabled = Content != null && !ReadOnly && !Locked;
101      suggestedInstancesComboBox.Enabled = Content != null && !ReadOnly && !Locked;
102      algorithmViewHost.Enabled = Content != null && !ReadOnly && !Locked;
103      runsView.Enabled = Content != null;
104      algorithmInstancesViewHost.Enabled = Content != null;
105    }
106
107    private void UpdateSuggestedInstancesCombobox() {
108      var prevSelection = (IAlgorithm)suggestedInstancesComboBox.SelectedItem;
109      var prevNewIndex = -1;
110      suggestedInstancesComboBox.Items.Clear();
111      if (Content == null) return;
112
113      for (var i = 0; i < Content.SuggestedInstances.Count; i++) {
114        suggestedInstancesComboBox.Items.Add(Content.SuggestedInstances[i]);
115        if (prevSelection == null || Content.SuggestedInstances[i].Name == prevSelection.Name)
116          prevNewIndex = prevSelection == null ? 0 : i;
117      }
118      if (prevNewIndex >= 0) {
119        suggestedInstancesComboBox.SelectedIndex = prevNewIndex;
120      }
121    }
122
123    #region Event Handlers
124    #region Content events
125    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
126      if (InvokeRequired) {
127        Invoke((Action<object, PropertyChangedEventArgs>)Content_PropertyChanged, sender, e);
128        return;
129      }
130      SuppressEvents = true;
131      try {
132        switch (e.PropertyName) {
133          case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break;
134          case "Problem": problemViewHost.Content = Content.Problem; break;
135        }
136      } finally { SuppressEvents = false; }
137    }
138
139    private void SuggestedInstancesOnChanged(object sender, EventArgs e) {
140      UpdateSuggestedInstancesCombobox();
141    }
142    #endregion
143
144    #region Control events
145    private void maxEvaluationsTextBox_Validating(object sender, CancelEventArgs e) {
146      if (SuppressEvents) return;
147      if (InvokeRequired) {
148        Invoke((Action<object, CancelEventArgs>)maxEvaluationsTextBox_Validating, sender, e);
149        return;
150      }
151      int value;
152      if (!int.TryParse(maxEvaluationsTextBox.Text, out value)) {
153        e.Cancel = !maxEvaluationsTextBox.ReadOnly && maxEvaluationsTextBox.Enabled;
154        errorProvider.SetError(maxEvaluationsTextBox, "Please enter a valid integer number.");
155      } else {
156        Content.MaximumEvaluations = value;
157        e.Cancel = false;
158        errorProvider.SetError(maxEvaluationsTextBox, null);
159      }
160    }
161
162    private void newProblemButton_Click(object sender, EventArgs e) {
163      if (problemTypeSelectorDialog == null) {
164        problemTypeSelectorDialog = new TypeSelectorDialog { Caption = "Select Problem" };
165        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
166        problemTypeSelectorDialog.TypeSelector.Configure(typeof(ISingleObjectiveHeuristicOptimizationProblem)
167          , showNotInstantiableTypes: false, showGenericTypes: false);
168      }
169      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
170        try {
171          Content.Problem = (ISingleObjectiveHeuristicOptimizationProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
172        } catch (Exception ex) {
173          ErrorHandling.ShowErrorDialog(this, ex);
174        }
175      }
176    }
177
178    private void openProblemButton_Click(object sender, EventArgs e) {
179      openFileDialog.Title = "Open Problem";
180      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
181        newProblemButton.Enabled = openProblemButton.Enabled = false;
182        problemViewHost.Enabled = false;
183
184        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
185          try {
186            if (error != null) throw error;
187            var problem = content as ISingleObjectiveHeuristicOptimizationProblem;
188            if (problem == null) {
189              var algorithm = content as IAlgorithm;
190              if (algorithm == null || !(algorithm.Problem is ISingleObjectiveHeuristicOptimizationProblem))
191                MessageBox.Show(this, "The selected file is not a problem, nor an algorithm with a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
192              else Content.Problem = (ISingleObjectiveHeuristicOptimizationProblem)algorithm.Problem;
193            } else
194              Content.Problem = problem;
195          } catch (Exception ex) {
196            ErrorHandling.ShowErrorDialog(this, ex);
197          } finally {
198            Invoke(new Action(delegate() {
199              problemViewHost.Enabled = true;
200              newProblemButton.Enabled = openProblemButton.Enabled = true;
201            }));
202          }
203        });
204      }
205    }
206
207    private void problemTabPage_DragEnterOver(object sender, DragEventArgs e) {
208      e.Effect = DragDropEffects.None;
209      var prob = e.Data.GetData(Constants.DragDropDataFormat) as ISingleObjectiveHeuristicOptimizationProblem;
210      if (!ReadOnly && prob != null) {
211        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
212        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
213        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
214        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
215        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
216      }
217    }
218
219    private void problemTabPage_DragDrop(object sender, DragEventArgs e) {
220      if (e.Effect != DragDropEffects.None) {
221        var prob = e.Data.GetData(Constants.DragDropDataFormat) as ISingleObjectiveHeuristicOptimizationProblem;
222        if (e.Effect.HasFlag(DragDropEffects.Copy)) prob = (ISingleObjectiveHeuristicOptimizationProblem)prob.Clone();
223        Content.Problem = prob;
224      }
225    }
226
227    private void suggestedInstancesComboBox_SelectedIndexChanged(object sender, EventArgs e) {
228      if (SuppressEvents) return;
229      if (InvokeRequired) { Invoke((Action<object, EventArgs>)suggestedInstancesComboBox_SelectedIndexChanged, sender, e); return; }
230      if (suggestedInstancesComboBox.SelectedIndex >= 0)
231        algorithmViewHost.Content = (IAlgorithm)suggestedInstancesComboBox.SelectedItem;
232      else algorithmViewHost.Content = null;
233    }
234    #endregion
235    #endregion
236  }
237}
Note: See TracBrowser for help on using the repository browser.