Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Functions/FunctionView.cs @ 4311

Last change on this file since 4311 was 519, checked in by gkronber, 16 years ago

implemented #259 (BakedFunctionTree should provide an appropriate ToString() method)

File size: 9.7 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      funTreeView.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      funTreeView.Nodes.Add(rootNode);
62
63      foreach(IFunctionTree subTree in functionTree.SubTrees) {
64        CreateTree(rootNode, subTree);
65      }
66      funTreeView.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(funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) {
88        IFunctionTree selectedBranch = (IFunctionTree)funTreeView.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(funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) {
123        TreeNode node = funTreeView.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 = funTreeView.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 void copyToClipboardSExpressionToolStripMenuItem_Click(object sender, EventArgs e) {
143      TreeNode node = funTreeView.SelectedNode;
144      if(node == null || node.Tag == null) return;
145
146      string sexp = ((IFunctionTree)node.Tag).ToString();
147      Clipboard.SetText(sexp);
148    }
149
150    private void funTreeView_MouseUp(object sender, MouseEventArgs e) {
151      if(e.Button == MouseButtons.Right) {
152        // Select the clicked node
153        funTreeView.SelectedNode = funTreeView.GetNodeAt(e.X, e.Y);
154      }
155    }
156
157    private class FunctionNameVisitor : IFunctionVisitor {
158      string name;
159      IFunctionTree currentBranch;
160
161      public string Name {
162        get { return name; }
163      }
164
165      public void Visit(IFunctionTree tree) {
166        currentBranch = tree;
167        tree.Function.Accept(this);
168      }
169
170      #region IFunctionVisitor Members
171      public void Visit(IFunction function) {
172        name = function.Name;
173      }
174
175      public void Visit(Addition addition) {
176        name = "+";
177      }
178
179      public void Visit(Constant constant) {
180        name = ((ConstrainedDoubleData)(currentBranch.GetLocalVariable(HeuristicLab.Functions.Constant.VALUE).Value)).Data + "";
181      }
182
183      public void Visit(Cosinus cosinus) {
184        name = "Sin";
185      }
186
187      public void Visit(Differential diff) {
188        string timeOffset = "";
189        int sampleOffset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.OFFSET).Value).Data;
190        int variableIndex = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.INDEX).Value).Data;
191        double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.WEIGHT).Value).Data;
192        if(sampleOffset < 0) {
193          timeOffset = "(t" + sampleOffset + ")";
194        } else if(sampleOffset > 0) {
195          timeOffset = "(t+" + sampleOffset + ")";
196        } else {
197          timeOffset = "";
198        }
199        name = "Diff" + variableIndex + timeOffset + " * " + weight;
200      }
201
202      public void Visit(Division division) {
203        name = "/";
204      }
205
206      public void Visit(Exponential exponential) {
207        name = "Exp";
208      }
209
210      public void Visit(Logarithm logarithm) {
211        name = "Log";
212      }
213
214      public void Visit(Multiplication multiplication) {
215        name = "*";
216      }
217
218      public void Visit(Power power) {
219        name = "Pow";
220      }
221
222      public void Visit(Signum signum) {
223        name = "Sign";
224      }
225
226      public void Visit(Sinus sinus) {
227        name = "Sin";
228      }
229
230      public void Visit(Sqrt sqrt) {
231        name = "Sqrt";
232      }
233
234      public void Visit(Subtraction substraction) {
235        name = "-";
236      }
237
238      public void Visit(Tangens tangens) {
239        name = "Tan";
240      }
241
242      public void Visit(Variable variable) {
243        string timeOffset = "";
244        int sampleOffset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.OFFSET).Value).Data;
245        int variableIndex = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.INDEX).Value).Data;
246        double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.WEIGHT).Value).Data;
247        if(sampleOffset < 0) {
248          timeOffset = "(t" + sampleOffset + ")";
249        } else if(sampleOffset > 0) {
250          timeOffset = "(t+" + sampleOffset + ")";
251        } else {
252          timeOffset = "";
253        }
254        name = "Var" + variableIndex + timeOffset + " * " + weight;
255      }
256
257      public void Visit(And and) {
258        name = "And";
259      }
260
261      public void Visit(Average average) {
262        name = "Avg";
263      }
264
265      public void Visit(IfThenElse ifThenElse) {
266        name = "IFTE";
267      }
268
269      public void Visit(Not not) {
270        name = "Not";
271      }
272
273      public void Visit(Or or) {
274        name = "Or";
275      }
276
277      public void Visit(Xor xor) {
278        name = "Xor";
279      }
280
281      public void Visit(Equal equal) {
282        name = "eq?";
283      }
284
285      public void Visit(LessThan lessThan) {
286        name = "<";
287      }
288
289      public void Visit(GreaterThan greaterThan) {
290        name = ">";
291      }
292      #endregion
293    }
294  }
295}
Note: See TracBrowser for help on using the repository browser.