Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2719_HeuristicLab.DatastreamAnalysis/HeuristicLab.DatastreamAnalysis.Views/3.4/DatastreamAnalysisOptimizerView.cs @ 15867

Last change on this file since 15867 was 15867, checked in by jzenisek, 5 years ago

#2719: added proxy ensemble model

File size: 8.5 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.Linq;
27using System.Windows.Forms;
28using HeuristicLab.Core;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Views;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace HeuristicLab.DatastreamAnalysis {
35  [View("DatastreamAnalysisOptimizer View", "")]
36  [Content(typeof(DatastreamAnalysisOptimizer), true)]
37  public partial class DatastreamAnalysisOptimizerView : IOptimizerView {
38    public DatastreamAnalysisOptimizerView() {
39      InitializeComponent();
40      Content = new DatastreamAnalysisOptimizer();
41    }
42
43    public new DatastreamAnalysisOptimizer Content {
44      get { return (DatastreamAnalysisOptimizer) base.Content; }
45      set { base.Content = value; }
46    }
47
48    //protected override void Dispose(bool disposing) {
49    //  if (disposing) {
50    //    if (components != null) components.Dispose();
51    //  }
52    //  base.Dispose(disposing);
53    //}
54
55    protected override void OnInitialized(EventArgs e) {
56      // set order of tab pages according to z order.
57      // NOTE: this is required due to a bug in the VS designer.
58      List<Control> tabPages = new List<Control>();
59      for (int i = 0; i < tabControl.Controls.Count; i++) {
60        tabPages.Add(tabControl.Controls[i]);
61      }
62      tabControl.Controls.Clear();
63      foreach(Control control in tabPages)
64        tabControl.Controls.Add(control);
65
66      base.OnInitialized(e);
67    }
68
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      if (Content == null) {
72        ensemblesViewHost.Content = null;
73        datastreamViewHost.Content = null;
74        resultsView.Content = null;
75        runsView.Content = null;
76      } else {
77        ensemblesViewHost.ViewType = null;
78        ensemblesViewHost.Content = Content.Ensembles;
79        datastreamViewHost.ViewType = null;
80        datastreamViewHost.Content = Content.Datastream;
81        resultsView.Content = Content.Results.AsReadOnly();
82        runsView.Content = Content.Runs;
83      }
84    }
85
86    protected override void SetEnabledStateOfControls() {
87      base.SetEnabledStateOfControls();
88
89      resultsView.Enabled = Content != null;
90      runsView.Enabled = Content != null;
91    }
92
93    protected override void OnClosed(FormClosedEventArgs e) {
94      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
95        //The content must be stopped if no other view showing the content is available
96        var optimizers =
97          MainFormManager.MainForm.Views.OfType<IContentView>()
98            .Where(v => v != this)
99            .Select(v => v.Content)
100            .OfType<IOptimizer>();
101        if (!optimizers.Contains(Content)) {
102          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
103          if (!nestedOptimizers.Contains(Content)) Content.Stop();
104        }
105      }
106      base.OnClosed(e);
107    }
108
109    //protected override void startButton_Click(object sender, EventArgs e) {
110    //  base.startButton_Click(sender, e);
111    //}
112
113    protected override void RegisterContentEvents() {
114      base.RegisterContentEvents();
115      Content.EnsemblesChanged += new EventHandler(Content_EnsemblesChanged);
116      Content.DatastreamChanged += new EventHandler(Content_DatastreamChanged);
117    }
118
119    protected override void DeregisterContentEvents() {
120      Content.EnsemblesChanged -= new EventHandler(Content_EnsemblesChanged);
121      Content.DatastreamChanged -= new EventHandler(Content_DatastreamChanged);
122
123      base.DeregisterContentEvents();
124    }
125
126    #region content events
127    protected override void Content_Prepared(object sender, EventArgs e) {
128      if (InvokeRequired)
129        Invoke(new EventHandler(Content_Prepared), sender, e);
130      else {
131        base.Content_Prepared(sender,e);
132        resultsView.Content = Content.Results.AsReadOnly();
133      }
134    }
135
136    protected void Content_EnsemblesChanged(object sender, EventArgs e) {
137      if (InvokeRequired)
138        Invoke(new EventHandler(Content_EnsemblesChanged), sender, e);
139      else {
140        ensemblesViewHost.ViewType = null;
141        ensemblesViewHost.Content = Content.Ensembles;
142      }
143    }
144
145    protected void Content_DatastreamChanged(object sender, EventArgs e) {
146      if (InvokeRequired)
147        Invoke(new EventHandler(Content_DatastreamChanged), sender, e);
148      else {
149        if (datastreamViewHost.Content != null && Content.Datastream != null &&
150            datastreamViewHost.Content.GetType() != Content.Datastream.GetType())
151          datastreamViewHost.ViewType = null;
152        datastreamViewHost.Content = Content.Datastream;
153      }
154    }
155    #endregion
156
157    #region event handlers
158    private void ensemblesTab_DragDrop(object sender, DragEventArgs e) {
159      if (e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat)) {
160        try {
161          var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
162          if (data is IProxyEnsembleModel) {
163            data = (IProxyEnsembleModel) data;
164            Content.Ensembles.Add((ProxyEnsembleModel) data);
165          }
166        }
167        catch (Exception ex) {
168          MessageBox.Show(ex.Message, "Set ensembles", MessageBoxButtons.OK, MessageBoxIcon.Error);
169        }
170      }
171    }
172
173    private void ensemblesTab_DragEnterOver(object sender, DragEventArgs e) {
174      e.Effect = DragDropEffects.None;
175      if (ReadOnly || !e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat))
176        return;
177
178      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
179
180      if (data is IProxyEnsembleModel) {
181        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
182        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
183        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
184        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
185        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
186      }
187    }
188
189    private void datastreamTab_DragDrop(object sender, DragEventArgs e) {
190      if (e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat)) {
191        try {
192          var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
193          if (data is IValueParameter) data = ((IValueParameter) data).Value;
194          if (data is IRegressionProblemData)
195            Content.Datastream.ProblemData = (RegressionProblemData) data;
196        }
197        catch (Exception ex) {
198          MessageBox.Show(ex.Message, "Set datastream", MessageBoxButtons.OK, MessageBoxIcon.Error);
199        }
200      }
201    }
202
203    private void datastreamTab_DragEnterOver(object sender, DragEventArgs e) {
204      e.Effect = DragDropEffects.None;
205
206      if (ReadOnly || !e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat))
207        return;
208
209      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
210      if (data is IValueParameter) data = ((IValueParameter) data).Value;
211      if (data is IRegressionProblemData) {
212        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
213        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
214        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
215        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
216        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
217      }
218    }
219
220    #endregion event handlers
221  }
222}
Note: See TracBrowser for help on using the repository browser.