Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.GP/3.3/FunctionLibraryEditor.cs @ 3758

Last change on this file since 3758 was 2222, checked in by gkronber, 15 years ago

Merged changes from GP-refactoring branch back into the trunk #713.

File size: 4.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core;
27using HeuristicLab.GP.Interfaces;
28
29namespace HeuristicLab.GP {
30  public partial class FunctionLibraryEditor : EditorBase {
31    private ChooseItemDialog chooseFunctionDialog;
32    public FunctionLibrary FunctionLibrary {
33      get { return (FunctionLibrary)Item; }
34      set { base.Item = value; }
35    }
36
37    public FunctionLibraryEditor(FunctionLibrary library)
38      : base() {
39      InitializeComponent();
40      FunctionLibrary = library;
41    }
42
43    protected override void AddItemEvents() {
44      base.AddItemEvents();
45      base.Item.Changed += (sender, args) => UpdateControls();
46    }
47
48    protected override void UpdateControls() {
49      base.UpdateControls();
50      foreach (IFunction fun in FunctionLibrary.Functions) {
51        if (fun.Manipulator != null) {
52          mutationListView.Items.Add(CreateListViewItem(fun));
53        }
54        if (fun.Initializer != null) {
55          initListView.Items.Add(CreateListViewItem(fun));
56        }
57      }
58    }
59
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      }
68    }
69
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      }
78    }
79
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    }
88
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    }
97
98    private void functionsListView_SelectedIndexChanged(object sender, EventArgs e) {
99      removeButton.Enabled = functionsListView.SelectedIndices.Count > 0;
100    }
101
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;
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.