Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Collections/sources/HeuristicLab.Functions/FunctionView.cs @ 630

Last change on this file since 630 was 365, checked in by gkronber, 16 years ago

implemented #195

File size: 9.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.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(Differential diff) {
173        string timeOffset = "";
174        int sampleOffset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.OFFSET).Value).Data;
175        int variableIndex = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.INDEX).Value).Data;
176        double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.WEIGHT).Value).Data;
177        if(sampleOffset < 0) {
178          timeOffset = "(t" + sampleOffset + ")";
179        } else if(sampleOffset > 0) {
180          timeOffset = "(t+" + sampleOffset + ")";
181        } else {
182          timeOffset = "";
183        }
184        name = "Diff" + variableIndex + timeOffset + " * " + weight;
185      }
186
187      public void Visit(Division division) {
188        name = "/";
189      }
190
191      public void Visit(Exponential exponential) {
192        name = "Exp";
193      }
194
195      public void Visit(Logarithm logarithm) {
196        name = "Log";
197      }
198
199      public void Visit(Multiplication multiplication) {
200        name = "*";
201      }
202
203      public void Visit(Power power) {
204        name = "Pow";
205      }
206
207      public void Visit(Signum signum) {
208        name = "Sign";
209      }
210
211      public void Visit(Sinus sinus) {
212        name = "Sin";
213      }
214
215      public void Visit(Sqrt sqrt) {
216        name = "Sqrt";
217      }
218
219      public void Visit(Subtraction substraction) {
220        name = "-";
221      }
222
223      public void Visit(Tangens tangens) {
224        name = "Tan";
225      }
226
227      public void Visit(Variable variable) {
228        string timeOffset = "";
229        int sampleOffset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.OFFSET).Value).Data;
230        int variableIndex = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.INDEX).Value).Data;
231        double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.WEIGHT).Value).Data;
232        if(sampleOffset < 0) {
233          timeOffset = "(t" + sampleOffset + ")";
234        } else if(sampleOffset > 0) {
235          timeOffset = "(t+" + sampleOffset + ")";
236        } else {
237          timeOffset = "";
238        }
239        name = "Var" + variableIndex + timeOffset + " * " + weight;
240      }
241
242      public void Visit(And and) {
243        name = "And";
244      }
245
246      public void Visit(Average average) {
247        name = "Avg";
248      }
249
250      public void Visit(IfThenElse ifThenElse) {
251        name = "IFTE";
252      }
253
254      public void Visit(Not not) {
255        name = "Not";
256      }
257
258      public void Visit(Or or) {
259        name = "Or";
260      }
261
262      public void Visit(Xor xor) {
263        name = "Xor";
264      }
265
266      public void Visit(Equal equal) {
267        name = "eq?";
268      }
269
270      public void Visit(LessThan lessThan) {
271        name = "<";
272      }
273
274      public void Visit(GreaterThan greaterThan) {
275        name = ">";
276      }
277      #endregion
278    }
279  }
280}
Note: See TracBrowser for help on using the repository browser.