#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.Collections; using System.ComponentModel; using System.Linq; using System.Windows.Forms; using HeuristicLab.Common; using HeuristicLab.Common.Resources; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.Optimization; using HeuristicLab.Optimization.Views; namespace HeuristicLab.OptimizationExpertSystem.Views { [View("Expert-system Optimizer View")] [Content(typeof(ExpertSystem), IsDefaultView = true)] public partial class ExpertSystemView : NamedItemView { protected TypeSelectorDialog problemTypeSelectorDialog; protected virtual bool SuppressEvents { get; set; } private bool okbDownloadInProgress; public new ExpertSystem Content { get { return (ExpertSystem)base.Content; } set { base.Content = value; } } public ExpertSystemView() { InitializeComponent(); // brings progress panel to front (it is not visible by default, but obstructs other elements in designer) this.Controls.SetChildIndex(this.progressPanel, 0); algorithmStartButton.Text = string.Empty; algorithmStartButton.Image = VSImageLibrary.Play; refreshMapButton.Text = string.Empty; refreshMapButton.Image = VSImageLibrary.Refresh; } 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 -= ContentOnPropertyChanged; Content.SuggestedInstances.CollectionReset -= SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsAdded -= SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsMoved -= SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsRemoved -= SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsReplaced -= SuggestedInstancesOnChanged; if (Content.Problem != null) Content.Problem.ProblemChanged -= ContentOnProblemChanged; base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.PropertyChanged += ContentOnPropertyChanged; Content.SuggestedInstances.CollectionReset += SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsAdded += SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsMoved += SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsRemoved += SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsReplaced += SuggestedInstancesOnChanged; if (Content.Problem != null) Content.Problem.ProblemChanged += ContentOnProblemChanged; } protected override void OnContentChanged() { base.OnContentChanged(); SuppressEvents = true; okbDownloadInProgress = false; try { if (Content == null) { maxEvaluationsTextBox.Text = String.Empty; problemViewHost.Content = null; algorithmViewHost.Content = null; runsView.Content = null; kbViewHost.Content = null; problemInstancesView.Content = null; } else { maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); problemViewHost.Content = Content.Problem; runsView.Content = Content.Runs; kbViewHost.ViewType = typeof(RunCollectionRLDView); kbViewHost.Content = Content.KnowledgeBase; problemInstancesView.Content = Content.ProblemInstances; } } finally { SuppressEvents = false; } UpdateSuggestedInstancesCombobox(); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked; problemViewHost.Enabled = Content != null && !ReadOnly && !Locked && !okbDownloadInProgress; suggestedInstancesComboBox.Enabled = Content != null && !ReadOnly && !Locked && !okbDownloadInProgress; algorithmStartButton.Enabled = Content != null && !ReadOnly && !Locked && suggestedInstancesComboBox.SelectedIndex >= 0; algorithmViewHost.Enabled = Content != null && !ReadOnly && !Locked; runsView.Enabled = Content != null; kbViewHost.Enabled = Content != null && !okbDownloadInProgress; problemInstancesView.Enabled = Content != null && !okbDownloadInProgress; refreshMapButton.Enabled = Content != null; okbDownloadButton.Enabled = Content != null && Content.Problem != null && Content.Problem.ProblemId >= 0 && !ReadOnly && !Locked && !okbDownloadInProgress; } private void UpdateSuggestedInstancesCombobox() { 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; } } #region Event Handlers #region Content events private void ContentOnProblemChanged(object sender, EventArgs eventArgs) { UpdateSuggestedInstancesCombobox(); SetEnabledStateOfControls(); } private void ContentOnPropertyChanged(object sender, PropertyChangedEventArgs e) { if (InvokeRequired) { Invoke((Action)ContentOnPropertyChanged, sender, e); return; } SuppressEvents = true; try { switch (e.PropertyName) { case "KnowledgeBase": kbViewHost.Content = Content.KnowledgeBase; break; case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break; case "Problem": problemViewHost.Content = Content.Problem; Content.Problem.ProblemChanged += ContentOnProblemChanged; break; case "ProblemInstances": problemInstancesView.Content = Content.ProblemInstances; break; } } finally { SuppressEvents = false; } SetEnabledStateOfControls(); } private void SuggestedInstancesOnChanged(object sender, EventArgs e) { UpdateSuggestedInstancesCombobox(); } #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); } } #endregion private bool validDragOperation; private void instancesDropPanel_DragEnter(object sender, DragEventArgs e) { validDragOperation = false; if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun)) { validDragOperation = true; } else if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) { validDragOperation = true; var items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat); foreach (object item in items) validDragOperation = validDragOperation && (item is IRun); } } private void instancesDropPanel_DragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.None; if (!validDragOperation) return; 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 instancesDropPanel_DragDrop(object sender, DragEventArgs e) { if (e.Effect == DragDropEffects.None) return; if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun) { var item = (IRun)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat); Content.ProblemInstances.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (IRun)item.Clone() : item); } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) { var items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast(); if (e.Effect.HasFlag(DragDropEffects.Copy)) { var cloner = new Cloner(); items = items.Select(x => cloner.Clone(x)); } Content.ProblemInstances.AddRange(items); } } #endregion private void refreshMapButton_Click(object sender, EventArgs e) { Content.UpdateInstanceMap(); } private void okbDownloadButton_Click(object sender, EventArgs e) { if (Content.Problem.ProblemId < 0) { MessageBox.Show("Please select a problem instance first."); return; } var progress = new Progress(); progress.ProgressStateChanged += OkbDownloadProgressOnStateChanged; Content.UpdateKnowledgeBaseAsync(progress); MainFormManager.GetMainForm().AddOperationProgressToView(progressPanel, progress); progressPanel.Visible = true; SetEnabledStateOfControls(); } private void OkbDownloadProgressOnStateChanged(object sender, EventArgs eventArgs) { var progress = (IProgress)sender; okbDownloadInProgress = progress.ProgressState == ProgressState.Started; SetEnabledStateOfControls(); if (!okbDownloadInProgress) { progressPanel.Visible = false; progress.ProgressStateChanged -= OkbDownloadProgressOnStateChanged; } } private void algorithmStartButton_Click(object sender, EventArgs e) { var selectedInstance = (IAlgorithm)suggestedInstancesComboBox.SelectedItem; var clone = (IAlgorithm)selectedInstance.Clone(); clone.Prepare(true); clone.Start(); algorithmViewHost.Content = clone.Results; } } }