Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/FunctionTreeView.cs @ 2210

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

GP Refactoring #713

  • introduced a plugin for GP interfaces
  • created a new interface IGeneticProgrammingModel which represents GP models in HL scopes instead of IFunctionTree
  • changed interfaces IFunction and IFunctionTree
  • moved some files to new directories (general housekeeping)
  • changed all GP operators and engines to work with IGeneticProgrammingModels
  • removed parameters TreeSize and TreeHeight in all GP operators
  • changed parameter OperatorLibrary to FunctionLibrary in all GP operators
File size: 7.6 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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Core;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Data;
33using HeuristicLab.GP.Interfaces;
34
35namespace HeuristicLab.GP {
36  public partial class FunctionTreeView : ViewBase {
37    private IFunctionTree functionTree;
38
39    private IFunctionTree selectedBranch;
40    private IVariable selectedVariable;
41    private IFunctionTreeNameGenerator nameGenerator;
42    private IFunctionTreeNameGenerator[] allNameGenerators;
43    private MenuItem representationsMenu;
44
45    public FunctionTreeView() {
46      nameGenerator = new DefaultFunctionTreeNameGenerator();
47      InitializeComponent();
48
49      DiscoveryService discoveryService = new DiscoveryService();
50      allNameGenerators = discoveryService.GetInstances<IFunctionTreeNameGenerator>();
51      representationsMenu = new MenuItem();
52      representationsMenu.Text = "Tree representation";
53      representationsMenu.Name = "Tree representation";
54      foreach (IFunctionTreeNameGenerator generator in allNameGenerators) {
55        MenuItem mi = new MenuItem(generator.Name, MakeNameGeneratorDelegate(generator), Shortcut.None);
56        if (generator is DefaultFunctionTreeNameGenerator) mi.Checked = true;
57        else mi.Checked = false;
58        mi.Tag = generator;
59        representationsMenu.MenuItems.Add(mi);
60      }
61    }
62
63    public FunctionTreeView(IFunctionTree functionTree)
64      : this() {
65      this.functionTree = functionTree;
66      Refresh();
67    }
68
69    protected override void UpdateControls() {
70      funTreeView.Nodes.Clear();
71      TreeNode rootNode = new TreeNode();
72      rootNode.Name = functionTree.Function.Name;
73      rootNode.Text = nameGenerator.GetName(functionTree);
74      rootNode.Tag = functionTree;
75      treeNodeContextMenu.MenuItems.Clear();
76      treeNodeContextMenu.MenuItems.Add(representationsMenu);
77      DiscoveryService discoveryService = new DiscoveryService();
78      IFunctionTreeExporter[] exporters = discoveryService.GetInstances<IFunctionTreeExporter>();
79      foreach (IFunctionTreeExporter exporter in exporters) {
80        string result;
81        // register a menu item for the exporter
82        MenuItem item = new MenuItem("Copy to clip-board (" + exporter.Name + ")",
83          MakeExporterDelegate(exporter), Shortcut.None);
84        // try to export the whole tree
85        if (exporter.TryExport(functionTree, out result)) {
86          // if it worked enable the context-menu item
87          item.Enabled = true;
88        } else {
89          item.Enabled = false;
90        }
91        treeNodeContextMenu.MenuItems.Add(item);
92      }
93      rootNode.ContextMenu = treeNodeContextMenu;
94      funTreeView.Nodes.Add(rootNode);
95
96      foreach (IFunctionTree subTree in functionTree.SubTrees) {
97        CreateTree(rootNode, subTree);
98      }
99      funTreeView.ExpandAll();
100    }
101
102    private EventHandler MakeNameGeneratorDelegate(IFunctionTreeNameGenerator generator) {
103      return delegate(object source, EventArgs args) {
104        IFunctionTreeNameGenerator g = (IFunctionTreeNameGenerator)((MenuItem)source).Tag;
105        this.nameGenerator = g;
106        foreach (MenuItem otherMenuItem in representationsMenu.MenuItems) otherMenuItem.Checked = false;
107        ((MenuItem)source).Checked = true;
108        UpdateControls();
109      };
110    }
111
112    private EventHandler MakeExporterDelegate(IFunctionTreeExporter exporter) {
113      return delegate(object source, EventArgs args) {
114        TreeNode node = funTreeView.SelectedNode;
115        if (node == null || node.Tag == null) return;
116        Clipboard.SetText(exporter.Export((IFunctionTree)node.Tag));
117      };
118    }
119
120    private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) {
121      TreeNode node = new TreeNode();
122      node.Name = functionTree.Function.Name;
123      node.Text = nameGenerator.GetName(functionTree);
124      node.Tag = functionTree;
125      node.ContextMenu = treeNodeContextMenu;
126      rootNode.Nodes.Add(node);
127      foreach (IFunctionTree subTree in functionTree.SubTrees) {
128        CreateTree(node, subTree);
129      }
130    }
131
132    private void functionTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
133      variablesListBox.Items.Clear();
134      variablesSplitContainer.Panel2.Controls.Clear();
135      templateTextBox.Clear();
136      editButton.Enabled = false;
137      if (funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) {
138        IFunctionTree selectedBranch = (IFunctionTree)funTreeView.SelectedNode.Tag;
139//      UpdateVariablesList(selectedBranch);
140        templateTextBox.Text = nameGenerator.GetName(selectedBranch);
141        this.selectedBranch = selectedBranch;
142        editButton.Enabled = true;
143      }
144    }
145
146    //private void UpdateVariablesList(IFunctionTree functionTree) {
147    //  foreach (IVariable variable in functionTree.LocalVariables) {
148    //    variablesListBox.Items.Add(variable.Name);
149    //  }
150    //}
151
152    private void variablesListBox_SelectedIndexChanged(object sender, EventArgs e) {
153      //// in case we had an event-handler registered for a different variable => unregister the event-handler
154      //if (selectedVariable != null) {
155      //  selectedVariable.Value.Changed -= new EventHandler(selectedVariable_ValueChanged);
156      //}
157      //if (variablesListBox.SelectedItem != null) {
158      //  string selectedVariableName = (string)variablesListBox.SelectedItem;
159      //  selectedVariable = selectedBranch.GetLocalVariable(selectedVariableName);
160      //  variablesSplitContainer.Panel2.Controls.Clear();
161      //  Control editor = (Control)selectedVariable.CreateView();
162      //  variablesSplitContainer.Panel2.Controls.Add(editor);
163      //  editor.Dock = DockStyle.Fill;
164      //  // register an event handler that updates the treenode when the value of the variable is changed by the user
165      //  selectedVariable.Value.Changed += new EventHandler(selectedVariable_ValueChanged);
166      //} else {
167      //  variablesSplitContainer.Panel2.Controls.Clear();
168      //}
169    }
170
171    void selectedVariable_ValueChanged(object sender, EventArgs e) {
172      if (funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) {
173        TreeNode node = funTreeView.SelectedNode;
174        node.Text = nameGenerator.GetName(functionTree);
175      }
176    }
177
178    protected virtual void editButton_Click(object sender, EventArgs e) {
179      PluginManager.ControlManager.ShowControl(selectedBranch.Function.CreateView());
180    }
181
182    private void funTreeView_MouseUp(object sender, MouseEventArgs e) {
183      if (e.Button == MouseButtons.Right) {
184        // Select the clicked node
185        funTreeView.SelectedNode = funTreeView.GetNodeAt(e.X, e.Y);
186      }
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.