#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.ComponentModel; using System.Globalization; using System.Linq; using System.Windows.Forms; using HeuristicLab.Analysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.Optimization; using HeuristicLab.Optimization.Views; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.OptimizationExpertSystem.Views { [View("Expert-system Optimizer View")] [Content(typeof(ExpertSystemOptimizer), IsDefaultView = true)] public partial class ExpertSystemOptimizerView : IOptimizerView { protected TypeSelectorDialog problemTypeSelectorDialog; protected virtual bool SuppressEvents { get; set; } public new ExpertSystemOptimizer Content { get { return (ExpertSystemOptimizer)base.Content; } set { base.Content = value; } } public ExpertSystemOptimizerView() { InitializeComponent(); } protected override void Dispose(bool disposing) { if (disposing) { if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose(); if (components != null) components.Dispose(); } base.Dispose(disposing); } protected override void DeregisterContentEvents() { Content.PropertyChanged -= Content_PropertyChanged; Content.SuggestedInstances.CollectionReset -= SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsAdded -= SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsMoved -= SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsRemoved -= SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsReplaced -= SuggestedInstancesOnChanged; base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.PropertyChanged += Content_PropertyChanged; Content.SuggestedInstances.CollectionReset += SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsAdded += SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsMoved += SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsRemoved += SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsReplaced += SuggestedInstancesOnChanged; } protected override void OnContentChanged() { base.OnContentChanged(); SuppressEvents = true; try { if (Content == null) { maxEvaluationsTextBox.Text = String.Empty; problemViewHost.Content = null; algorithmViewHost.Content = null; runsView.Content = null; okbRunCollectionView.Content = null; } else { maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); problemViewHost.Content = Content.Problem; runsView.Content = Content.Runs; okbRunCollectionView.Content = Content.KnowledgeBase; } UpdateSuggestedInstancesCombobox(); } finally { SuppressEvents = false; } } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked; newProblemButton.Enabled = Content != null && !ReadOnly && !Locked; openProblemButton.Enabled = Content != null && !ReadOnly && !Locked; problemViewHost.Enabled = Content != null && !ReadOnly && !Locked; suggestedInstancesComboBox.Enabled = Content != null && !ReadOnly && !Locked; algorithmViewHost.Enabled = Content != null && !ReadOnly && !Locked; runsView.Enabled = Content != null; okbRunCollectionView.Enabled = Content != null; } protected override void OnClosed(FormClosedEventArgs e) { if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) { //The content must be stopped if no other view showing the content is available var optimizers = MainFormManager.MainForm.Views.OfType().Where(v => v != this).Select(v => v.Content).OfType(); if (!optimizers.Contains(Content)) { var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers); if (!nestedOptimizers.Contains(Content)) Content.Stop(); } } base.OnClosed(e); } private void UpdateSuggestedInstancesCombobox() { SuspendRepaint(); try { var prevSelection = (IAlgorithm)suggestedInstancesComboBox.SelectedItem; var prevNewIndex = -1; suggestedInstancesComboBox.Items.Clear(); if (Content == null) return; for (var i = 0; i < Content.SuggestedInstances.Count; i++) { suggestedInstancesComboBox.Items.Add(Content.SuggestedInstances[i]); if (prevSelection == null || Content.SuggestedInstances[i].Name == prevSelection.Name) prevNewIndex = prevSelection == null ? 0 : i; } if (prevNewIndex >= 0) { suggestedInstancesComboBox.SelectedIndex = prevNewIndex; } } finally { ResumeRepaint(true); } } #region Event Handlers #region Content events private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (InvokeRequired) { Invoke((Action)Content_PropertyChanged, sender, e); return; } SuppressEvents = true; try { switch (e.PropertyName) { case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break; case "Runs": runsView.Content = Content.Runs; break; case "KnowledgeBase": okbRunCollectionView.Content = Content.KnowledgeBase; break; case "Problem": problemViewHost.Content = Content.Problem; break; } } finally { SuppressEvents = false; } } private void SuggestedInstancesOnChanged(object sender, EventArgs e) { SuppressEvents = true; try { UpdateSuggestedInstancesCombobox(); } finally { SuppressEvents = false; } } #endregion #region Control events private void maxEvaluationsTextBox_Validating(object sender, CancelEventArgs e) { if (SuppressEvents) return; if (InvokeRequired) { Invoke((Action)maxEvaluationsTextBox_Validating, sender, e); return; } int value; if (!int.TryParse(maxEvaluationsTextBox.Text, out value)) { e.Cancel = !maxEvaluationsTextBox.ReadOnly && maxEvaluationsTextBox.Enabled; errorProvider.SetError(maxEvaluationsTextBox, "Please enter a valid integer number."); } else { Content.MaximumEvaluations = value; e.Cancel = false; errorProvider.SetError(maxEvaluationsTextBox, null); } } private void newProblemButton_Click(object sender, EventArgs e) { if (problemTypeSelectorDialog == null) { problemTypeSelectorDialog = new TypeSelectorDialog { Caption = "Select Problem" }; problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems"; problemTypeSelectorDialog.TypeSelector.Configure(typeof(IProblem) , showNotInstantiableTypes: false, showGenericTypes: false); } if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) { try { Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType(); } catch (Exception ex) { ErrorHandling.ShowErrorDialog(this, ex); } } } private void openProblemButton_Click(object sender, EventArgs e) { openFileDialog.Title = "Open Problem"; if (openFileDialog.ShowDialog(this) == DialogResult.OK) { newProblemButton.Enabled = openProblemButton.Enabled = false; problemViewHost.Enabled = false; ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) { try { if (error != null) throw error; var problem = content as IProblem; if (problem == null) { var algorithm = content as IAlgorithm; if (algorithm == null || algorithm.Problem == null) MessageBox.Show(this, "The selected file is not a problem, nor an algorithm with a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error); else Content.Problem = algorithm.Problem; } else Content.Problem = problem; } catch (Exception ex) { ErrorHandling.ShowErrorDialog(this, ex); } finally { Invoke(new Action(delegate() { problemViewHost.Enabled = true; newProblemButton.Enabled = openProblemButton.Enabled = true; })); } }); } } private void problemTabPage_DragEnterOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.None; var prob = e.Data.GetData(Constants.DragDropDataFormat) as IProblem; if (!ReadOnly && prob != null) { if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy; else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move; else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link; } } private void problemTabPage_DragDrop(object sender, DragEventArgs e) { if (e.Effect != DragDropEffects.None) { var prob = e.Data.GetData(Constants.DragDropDataFormat) as IProblem; if (e.Effect.HasFlag(DragDropEffects.Copy)) prob = (IProblem)prob.Clone(); Content.Problem = prob; } } private void suggestedInstancesComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (SuppressEvents) return; if (InvokeRequired) { Invoke((Action)suggestedInstancesComboBox_SelectedIndexChanged, sender, e); return; } if (suggestedInstancesComboBox.SelectedIndex >= 0) algorithmViewHost.Content = (IAlgorithm)suggestedInstancesComboBox.SelectedItem; else algorithmViewHost.Content = null; } #endregion #endregion } }