Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/3.3/FunctionView.cs @ 2788

Last change on this file since 2788 was 2729, checked in by gkronber, 14 years ago

Fixed problems in persistence and cloning of functions and function libraries. Added test-functionality to editor for function libraries. Fixed bugs in editor for function libraries. #748 (FunctionLibraryView is empty)

File size: 7.9 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.Windows.Forms;
24using HeuristicLab.Core;
25using HeuristicLab.PluginInfrastructure;
26using HeuristicLab.GP.Interfaces;
27using System.Collections.Generic;
28using System.Linq;
29
30namespace HeuristicLab.GP {
31  public partial class FunctionView : ViewBase {
32    private Function function;
33    private const string ALL_SLOTS = "All";
34    private string selectedSlot = ALL_SLOTS;
35
36    public FunctionView() {
37      InitializeComponent();
38    }
39
40    public FunctionView(Function function)
41      : this() {
42      this.function = function;
43      function.Changed += (sender, args) => UpdateControls();
44      Refresh();
45    }
46
47    protected override void UpdateControls() {
48      nameTextBox.Text = function.Name;
49      minSubTreesTextBox.Text = function.MinSubTrees.ToString();
50      maxSubTreesTextBox.Text = function.MaxSubTrees.ToString();
51      ticketsTextBox.Text = function.Tickets.ToString();
52      minTreeHeightTextBox.Text = function.MinTreeHeight.ToString();
53      minTreeSizeTextBox.Text = function.MinTreeSize.ToString();
54      if (function.Initializer != null) {
55        initializerTextBox.Text = function.Initializer.Name;
56      } else {
57        initializerTextBox.Enabled = false;
58        editInitializerButton.Enabled = false;
59      }
60      if (function.Manipulator != null) {
61        manipulatorTextBox.Text = function.Manipulator.Name;
62      } else {
63        manipulatorTextBox.Enabled = false;
64        editManipulatorButton.Enabled = false;
65      }
66
67      argumentComboBox.Items.Clear();
68      argumentComboBox.Items.Add(ALL_SLOTS);
69      for (int i = 0; i < function.MaxSubTrees; i++) {
70        argumentComboBox.Items.Add(i.ToString());
71      }
72
73      UpdateAllowedSubFunctionsList();
74    }
75
76    private void UpdateAllowedSubFunctionsList() {
77      if (function.MaxSubTrees > 0) {
78        subFunctionsListBox.Items.Clear();
79        if (selectedSlot == ALL_SLOTS) {
80          IEnumerable<IFunction> functionSet = function.GetAllowedSubFunctions(0);
81          for (int i = 1; i < function.MaxSubTrees; i++) {
82            functionSet = functionSet.Intersect(function.GetAllowedSubFunctions(i));
83          }
84          foreach (var subFun in functionSet) {
85            subFunctionsListBox.Items.Add(subFun);
86          }
87        } else {
88          int slot = int.Parse(selectedSlot);
89          foreach (var subFun in function.GetAllowedSubFunctions(slot)) {
90            subFunctionsListBox.Items.Add(subFun);
91          }
92        }
93      } else {
94        // no subfunctions allowed
95        subTreesGroupBox.Enabled = false;
96      }
97    }
98
99    private void nameTextBox_TextChanged(object sender, EventArgs e) {
100      string name = nameTextBox.Text;
101      if (!string.IsNullOrEmpty(name)) {
102        function.Name = name;
103        functionPropertiesErrorProvider.SetError(nameTextBox, string.Empty);
104      } else {
105        functionPropertiesErrorProvider.SetError(nameTextBox, "Name can't be empty.");
106      }
107    }
108
109    private void minSubTreesTextBox_TextChanged(object sender, EventArgs e) {
110      int minSubTrees;
111      if (int.TryParse(minSubTreesTextBox.Text, out minSubTrees) && minSubTrees >= 0) {
112        function.MinSubTrees = minSubTrees;
113        functionPropertiesErrorProvider.SetError(minSubTreesTextBox, string.Empty);
114      } else {
115        functionPropertiesErrorProvider.SetError(minSubTreesTextBox, "Min sub-trees must be 0 or a positive integer.");
116      }
117    }
118
119    private void maxSubTreesTextBox_TextChanged(object sender, EventArgs e) {
120      int maxSubTrees;
121      if (int.TryParse(maxSubTreesTextBox.Text, out maxSubTrees) && maxSubTrees >= 0) {
122        function.MaxSubTrees = maxSubTrees;
123        functionPropertiesErrorProvider.SetError(maxSubTreesTextBox, string.Empty);
124      } else {
125        functionPropertiesErrorProvider.SetError(maxSubTreesTextBox, "Max sub-trees must be 0 or a positive integer and larger or equal min sub-trees.");
126      }
127    }
128
129    private void ticketsTextBox_TextChanged(object sender, EventArgs e) {
130      double tickets;
131      if (double.TryParse(ticketsTextBox.Text, out tickets) && tickets >= 0) {
132        function.Tickets = tickets;
133        functionPropertiesErrorProvider.SetError(ticketsTextBox, string.Empty);
134      } else {
135        functionPropertiesErrorProvider.SetError(ticketsTextBox, "Number of tickets must be 0 or a positive real value.");
136      }
137    }
138
139    private void editInitializerButton_Click(object sender, EventArgs e) {
140      ControlManager.Manager.ShowControl(function.Initializer.CreateView());
141    }
142
143    private void editManipulatorButton_Click(object sender, EventArgs e) {
144      ControlManager.Manager.ShowControl(function.Manipulator.CreateView());
145    }
146
147    private void argumentComboBox_SelectedIndexChanged(object sender, EventArgs e) {
148      selectedSlot = argumentComboBox.Text;
149      UpdateAllowedSubFunctionsList();
150    }
151
152    private void subFunctionsListBox_DragEnter(object sender, DragEventArgs e) {
153      e.Effect = DragDropEffects.None;
154      if (e.Data.GetDataPresent("IFunction"))
155        e.Effect = DragDropEffects.Link;
156    }
157    private void subFunctionsListBox_DragOver(object sender, DragEventArgs e) {
158      e.Effect = DragDropEffects.None;
159      if (e.Data.GetDataPresent("IFunction"))
160        e.Effect = DragDropEffects.Link;
161    }
162
163    private void subFunctionsListBox_DragDrop(object sender, DragEventArgs e) {
164      if (e.Effect != DragDropEffects.None) {
165        if (e.Data.GetDataPresent("IFunction")) {
166          IFunction fun = (IFunction)e.Data.GetData("IFunction");
167          try {
168            Cursor = Cursors.WaitCursor;
169            if (selectedSlot == ALL_SLOTS) {
170              for (int slot = 0; slot < function.MaxSubTrees; slot++)
171                function.AddAllowedSubFunction(fun, slot);
172            } else {
173              int slot = int.Parse(selectedSlot);
174              function.AddAllowedSubFunction(fun, slot);
175            }
176          }
177          finally {
178            Cursor = Cursors.Default;
179          }
180        }
181      }
182    }
183
184    private void subFunctionsListBox_KeyUp(object sender, KeyEventArgs e) {
185      try {
186        Cursor = Cursors.WaitCursor;
187        if (subFunctionsListBox.SelectedItems.Count > 0 && e.KeyCode == Keys.Delete) {
188          if (selectedSlot == ALL_SLOTS) {
189            List<IFunction> removedSubFunctions = new List<IFunction>(subFunctionsListBox.SelectedItems.Cast<IFunction>());
190            for (int slot = 0; slot < function.MaxSubTrees; slot++) {
191              foreach (var subFun in removedSubFunctions) {
192                function.RemoveAllowedSubFunction((IFunction)subFun, slot);
193              }
194            }
195          } else {
196            int slot = int.Parse(selectedSlot);
197            List<IFunction> removedSubFunctions = new List<IFunction>(subFunctionsListBox.SelectedItems.Cast<IFunction>());
198            foreach (var subFun in removedSubFunctions) {
199              function.RemoveAllowedSubFunction(subFun, slot);
200            }
201          }
202
203        }
204      }
205      finally {
206        Cursor = Cursors.Default;
207      }
208    }
209  }
210}
Note: See TracBrowser for help on using the repository browser.