Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Functions/FunctionView.cs @ 192

Last change on this file since 192 was 185, checked in by gkronber, 17 years ago

fixed #114 (code-cleanup)

File size: 8.4 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;
33
34namespace HeuristicLab.Functions {
35  public partial class FunctionTreeView : ViewBase {
36    private IFunctionTree functionTree;
37
38    private IFunctionTree selectedBranch;
39    private IVariable selectedVariable;
40
41    private FunctionNameVisitor functionNameVisitor;
42    public FunctionTreeView() {
43      InitializeComponent();
44      functionNameVisitor = new FunctionNameVisitor();
45    }
46
47    public FunctionTreeView(IFunctionTree functionTree)
48      : this() {
49      this.functionTree = functionTree;
50      Refresh();
51    }
52
53    protected override void UpdateControls() {
54      functionTreeView.Nodes.Clear();
55      functionNameVisitor.Visit(functionTree);
56      TreeNode rootNode = new TreeNode();
57      rootNode.Name = functionTree.Function.Name;
58      rootNode.Text = functionNameVisitor.Name;
59      rootNode.Tag = functionTree;
60      rootNode.ContextMenuStrip = treeNodeContextMenu;
61      functionTreeView.Nodes.Add(rootNode);
62
63      foreach(IFunctionTree subTree in functionTree.SubTrees) {
64        CreateTree(rootNode, subTree);
65      }
66      functionTreeView.ExpandAll();
67    }
68
69    private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) {
70      TreeNode node = new TreeNode();
71      functionNameVisitor.Visit(functionTree);
72      node.Name = functionTree.Function.Name;
73      node.Text = functionNameVisitor.Name;
74      node.Tag = functionTree;
75      node.ContextMenuStrip = treeNodeContextMenu;
76      rootNode.Nodes.Add(node);
77      foreach(IFunctionTree subTree in functionTree.SubTrees) {
78        CreateTree(node, subTree);
79      }
80    }
81
82    private void functionTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
83      variablesListBox.Items.Clear();
84      variablesSplitContainer.Panel2.Controls.Clear();
85      templateTextBox.Clear();
86      editButton.Enabled = false;
87      if(functionTreeView.SelectedNode != null && functionTreeView.SelectedNode.Tag != null) {
88        IFunctionTree selectedBranch = (IFunctionTree)functionTreeView.SelectedNode.Tag;
89        UpdateVariablesList(selectedBranch);
90        templateTextBox.Text = selectedBranch.Function.Name;
91        this.selectedBranch = selectedBranch;
92        editButton.Enabled = true;
93      }
94    }
95
96    private void UpdateVariablesList(IFunctionTree functionTree) {
97      foreach(IVariable variable in functionTree.LocalVariables) {
98        variablesListBox.Items.Add(variable.Name);
99      }
100    }
101
102    private void variablesListBox_SelectedIndexChanged(object sender, EventArgs e) {
103      // in case we had an event-handler registered for a different variable => unregister the event-handler
104      if(selectedVariable != null) {
105        selectedVariable.Value.Changed -= new EventHandler(selectedVariable_ValueChanged);
106      }
107      if(variablesListBox.SelectedItem != null) {
108        string selectedVariableName = (string)variablesListBox.SelectedItem;
109        selectedVariable = selectedBranch.GetLocalVariable(selectedVariableName);
110        variablesSplitContainer.Panel2.Controls.Clear();
111        Control editor = (Control)selectedVariable.CreateView();
112        variablesSplitContainer.Panel2.Controls.Add(editor);
113        editor.Dock = DockStyle.Fill;
114        // register an event handler that updates the treenode when the value of the variable is changed by the user
115        selectedVariable.Value.Changed += new EventHandler(selectedVariable_ValueChanged);
116      } else {
117        variablesSplitContainer.Panel2.Controls.Clear();
118      }
119    }
120
121    void selectedVariable_ValueChanged(object sender, EventArgs e) {
122      if(functionTreeView.SelectedNode != null && functionTreeView.SelectedNode.Tag != null) {
123        TreeNode node = functionTreeView.SelectedNode;
124        functionNameVisitor.Visit(selectedBranch);
125        node.Text = functionNameVisitor.Name;
126      }
127    }
128
129    protected virtual void editButton_Click(object sender, EventArgs e) {
130      PluginManager.ControlManager.ShowControl(selectedBranch.Function.CreateView());
131    }
132
133    private void copyToClipboardMenuItem_Click(object sender, EventArgs e) {
134      TreeNode node = functionTreeView.SelectedNode;
135      if(node == null || node.Tag == null) return;
136
137      ModelAnalyzerExporter visitor = new ModelAnalyzerExporter();
138      visitor.Visit((IFunctionTree)node.Tag);
139      Clipboard.SetText(visitor.ModelAnalyzerPrefix);
140    }
141
142    private class FunctionNameVisitor : IFunctionVisitor {
143      string name;
144      IFunctionTree currentBranch;
145
146      public string Name {
147        get { return name; }
148      }
149
150      public void Visit(IFunctionTree tree) {
151        currentBranch = tree;
152        tree.Function.Accept(this);
153      }
154
155      #region IFunctionVisitor Members
156      public void Visit(IFunction function) {
157        name = function.Name;
158      }
159
160      public void Visit(Addition addition) {
161        name = "+";
162      }
163
164      public void Visit(Constant constant) {
165        name = ((ConstrainedDoubleData)(currentBranch.GetLocalVariable(HeuristicLab.Functions.Constant.VALUE).Value)).Data + "";
166      }
167
168      public void Visit(Cosinus cosinus) {
169        name = "Sin";
170      }
171
172      public void Visit(Division division) {
173        name = "/";
174      }
175
176      public void Visit(Exponential exponential) {
177        name = "Exp";
178      }
179
180      public void Visit(Logarithm logarithm) {
181        name = "Log";
182      }
183
184      public void Visit(Multiplication multiplication) {
185        name = "*";
186      }
187
188      public void Visit(Power power) {
189        name = "Pow";
190      }
191
192      public void Visit(Signum signum) {
193        name = "Sign";
194      }
195
196      public void Visit(Sinus sinus) {
197        name = "Sin";
198      }
199
200      public void Visit(Sqrt sqrt) {
201        name = "Sqrt";
202      }
203
204      public void Visit(Substraction substraction) {
205        name = "-";
206      }
207
208      public void Visit(Tangens tangens) {
209        name = "Tan";
210      }
211
212      public void Visit(Variable variable) {
213        string timeOffset = "";
214        int sampleOffset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.OFFSET).Value).Data;
215        int variableIndex = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.INDEX).Value).Data;
216        double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.WEIGHT).Value).Data;
217        if(sampleOffset < 0) {
218          timeOffset = "(t" + sampleOffset + ")";
219        } else if(sampleOffset > 0) {
220          timeOffset = "(t+" + sampleOffset + ")";
221        } else {
222          timeOffset = "";
223        }
224        name = "Var" + variableIndex + timeOffset + " * " + weight;
225      }
226
227      public void Visit(And and) {
228        name = "And";
229      }
230
231      public void Visit(Average average) {
232        name = "Avg";
233      }
234
235      public void Visit(IfThenElse ifThenElse) {
236        name = "IFTE";
237      }
238
239      public void Visit(Not not) {
240        name = "Not";
241      }
242
243      public void Visit(Or or) {
244        name = "Or";
245      }
246
247      public void Visit(Xor xor) {
248        name = "Xor";
249      }
250
251      public void Visit(Equal equal) {
252        name = "eq?";
253      }
254
255      public void Visit(LessThan lessThan) {
256        name = "<";
257      }
258
259      public void Visit(GreaterThan greaterThan) {
260        name = ">";
261      }
262      #endregion
263    }
264  }
265}
Note: See TracBrowser for help on using the repository browser.