[645] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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 System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[2210] | 27 | using HeuristicLab.GP.Interfaces;
|
---|
[645] | 28 |
|
---|
| 29 | namespace HeuristicLab.GP {
|
---|
[2202] | 30 | public partial class FunctionLibraryEditor : EditorBase {
|
---|
[2212] | 31 | private ChooseItemDialog chooseFunctionDialog;
|
---|
[2202] | 32 | public FunctionLibrary FunctionLibrary {
|
---|
| 33 | get { return (FunctionLibrary)Item; }
|
---|
[645] | 34 | set { base.Item = value; }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[2202] | 37 | public FunctionLibraryEditor(FunctionLibrary library)
|
---|
[645] | 38 | : base() {
|
---|
| 39 | InitializeComponent();
|
---|
[2202] | 40 | FunctionLibrary = library;
|
---|
[2212] | 41 | }
|
---|
[645] | 42 |
|
---|
[2212] | 43 | protected override void AddItemEvents() {
|
---|
| 44 | base.AddItemEvents();
|
---|
| 45 | base.Item.Changed += (sender, args) => UpdateControls();
|
---|
| 46 | }
|
---|
[645] | 47 |
|
---|
[2212] | 48 | protected override void UpdateControls() {
|
---|
| 49 | base.UpdateControls();
|
---|
| 50 | foreach (IFunction fun in FunctionLibrary.Functions) {
|
---|
[2202] | 51 | if (fun.Manipulator != null) {
|
---|
[2212] | 52 | mutationListView.Items.Add(CreateListViewItem(fun));
|
---|
[645] | 53 | }
|
---|
[2202] | 54 | if (fun.Initializer != null) {
|
---|
[2212] | 55 | initListView.Items.Add(CreateListViewItem(fun));
|
---|
[645] | 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[2212] | 60 | private void mutationListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 61 | if (mutationListView.SelectedItems.Count > 0 && mutationListView.SelectedItems[0].Tag != null) {
|
---|
| 62 | IOperator manipulator = ((IFunction)mutationListView.SelectedItems[0].Tag).Manipulator;
|
---|
| 63 | mutationVariableView.Enabled = true;
|
---|
| 64 | mutationVariableView.Variable = new Variable("Manipulator", manipulator);
|
---|
| 65 | } else {
|
---|
| 66 | mutationVariableView.Enabled = false;
|
---|
| 67 | }
|
---|
[2202] | 68 | }
|
---|
[645] | 69 |
|
---|
[2212] | 70 | private void initListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 71 | if (initListView.SelectedItems.Count > 0 && initListView.SelectedItems[0].Tag != null) {
|
---|
| 72 | IOperator initializer = ((IFunction)initListView.SelectedItems[0].Tag).Initializer;
|
---|
| 73 | initVariableView.Enabled = true;
|
---|
| 74 | initVariableView.Variable = new Variable("Initializer", initializer);
|
---|
| 75 | } else {
|
---|
| 76 | initVariableView.Enabled = false;
|
---|
| 77 | }
|
---|
[2202] | 78 | }
|
---|
[645] | 79 |
|
---|
[2212] | 80 | private void addButton_Click(object sender, EventArgs e) {
|
---|
| 81 | if (chooseFunctionDialog == null) chooseFunctionDialog = new ChooseItemDialog(typeof(IFunction));
|
---|
| 82 | if (chooseFunctionDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 83 | FunctionLibrary.AddFunction((IFunction)chooseFunctionDialog.Item);
|
---|
| 84 | functionsListView.Items.Add(CreateListViewItem((IFunction)chooseFunctionDialog.Item));
|
---|
| 85 | functionsListView.Sort();
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
[645] | 88 |
|
---|
[2212] | 89 | private void removeButton_Click(object sender, EventArgs e) {
|
---|
| 90 | // delete from the end of the list
|
---|
| 91 | IEnumerable<int> removeIndices = functionsListView.SelectedIndices.OfType<int>().OrderBy(x => 1.0 / x);
|
---|
| 92 | foreach (int selectedIndex in removeIndices) {
|
---|
| 93 | FunctionLibrary.RemoveFunction((IFunction)functionsListView.Items[selectedIndex].Tag);
|
---|
| 94 | functionsListView.Items.RemoveAt(selectedIndex);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
[645] | 97 |
|
---|
[2212] | 98 | private void functionsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 99 | removeButton.Enabled = functionsListView.SelectedIndices.Count > 0;
|
---|
[645] | 100 | }
|
---|
| 101 |
|
---|
[2212] | 102 | private ListViewItem CreateListViewItem(IFunction function) {
|
---|
| 103 | ListViewItem item = new ListViewItem();
|
---|
| 104 | item.Name = function.Name;
|
---|
| 105 | item.Text = function.Name;
|
---|
| 106 | item.Tag = function;
|
---|
| 107 | return item;
|
---|
[645] | 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|