Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 111 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 10.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.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;
32
33namespace HeuristicLab.Functions {
34  public partial class FunctionView : ViewBase {
35    private IFunction function;
36
37    private IFunction selectedFunction;
38    private IVariable selectedVariable;
39
40    private FunctionNameVisitor functionNameVisitor;
41    public FunctionView() {
42      InitializeComponent();
43      functionNameVisitor = new FunctionNameVisitor();
44    }
45
46    public FunctionView(IFunction function)
47      : this() {
48      this.function = function;
49      Refresh();
50    }
51
52    protected override void UpdateControls() {
53      functionTreeView.Nodes.Clear();
54      function.Accept(functionNameVisitor);
55      TreeNode rootNode = new TreeNode();
56      rootNode.Name = function.Name;
57      rootNode.Text = functionNameVisitor.Name;
58      rootNode.Tag = function;
59      rootNode.ContextMenuStrip = treeNodeContextMenu;
60      functionTreeView.Nodes.Add(rootNode);
61
62      foreach(IFunction subFunction in function.SubFunctions) {
63        CreateTree(rootNode, subFunction);
64      }
65      functionTreeView.ExpandAll();
66    }
67
68    private void CreateTree(TreeNode rootNode, IFunction function) {
69      TreeNode node = new TreeNode();
70      function.Accept(functionNameVisitor);
71      node.Tag = function;
72      node.Name = function.Name;
73      node.Text = functionNameVisitor.Name;
74      node.ContextMenuStrip = treeNodeContextMenu;
75      rootNode.Nodes.Add(node);
76      foreach(IFunction subFunction in function.SubFunctions) {
77        CreateTree(node, subFunction);
78      }
79    }
80
81    private void functionTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
82      variablesListBox.Items.Clear();
83      variablesSplitContainer.Panel2.Controls.Clear();
84      templateTextBox.Clear();
85      editButton.Enabled = false;
86      if(functionTreeView.SelectedNode != null && functionTreeView.SelectedNode.Tag != null) {
87        IFunction selectedFunction = (IFunction)functionTreeView.SelectedNode.Tag;
88        UpdateVariablesList(selectedFunction);
89        templateTextBox.Text = selectedFunction.MetaObject.Name;
90        this.selectedFunction = selectedFunction;
91        editButton.Enabled = true;
92      }
93    }
94
95    private void UpdateVariablesList(IFunction function) {
96      foreach(IVariable variable in function.LocalVariables) {
97        variablesListBox.Items.Add(variable.Name);
98      }
99    }
100
101    private void variablesListBox_SelectedIndexChanged(object sender, EventArgs e) {
102      // in case we had an event-handler registered for a different variable => unregister the event-handler
103      if(selectedVariable != null) {
104        selectedVariable.Value.Changed -= new EventHandler(selectedVariable_ValueChanged);
105      }
106      if(variablesListBox.SelectedItem != null) {
107        string selectedVariableName = (string)variablesListBox.SelectedItem;
108        selectedVariable = selectedFunction.GetVariable(selectedVariableName);
109        variablesSplitContainer.Panel2.Controls.Clear();
110        Control editor = (Control)selectedVariable.CreateView();
111        variablesSplitContainer.Panel2.Controls.Add(editor);
112        editor.Dock = DockStyle.Fill;
113        // register an event handler that updates the treenode when the value of the variable is changed by the user
114        selectedVariable.Value.Changed += new EventHandler(selectedVariable_ValueChanged);
115      } else {
116        variablesSplitContainer.Panel2.Controls.Clear();
117      }
118    }
119
120    void selectedVariable_ValueChanged(object sender, EventArgs e) {
121      if(functionTreeView.SelectedNode != null && functionTreeView.SelectedNode.Tag != null) {
122        TreeNode node = functionTreeView.SelectedNode;
123        selectedFunction.Accept(functionNameVisitor);
124        node.Text = functionNameVisitor.Name;
125      }
126    }
127
128    private void editButton_Click(object sender, EventArgs e) {
129      OperatorBaseView operatorView = new OperatorBaseView(selectedFunction.MetaObject);
130      PluginManager.ControlManager.ShowControl(operatorView);
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      ModelAnalyzerExportVisitor visitor = new ModelAnalyzerExportVisitor();
138      ((IFunction)node.Tag).Accept(visitor);
139      Clipboard.SetText(visitor.ModelAnalyzerPrefix);
140    }
141
142    private class FunctionNameVisitor : IFunctionVisitor {
143      string name;
144
145      public string Name {
146        get { return name; }
147      }
148
149      #region IFunctionVisitor Members
150
151      public void Visit(IFunction function) {
152        name = function.Name;
153      }
154
155      public void Visit(Addition addition) {
156        name = "+";
157      }
158
159      public void Visit(Constant constant) {
160        name = constant.Value + "";
161      }
162
163      public void Visit(Cosinus cosinus) {
164        name = "Sin";
165      }
166
167      public void Visit(Division division) {
168        name = "/";
169      }
170
171      public void Visit(Exponential exponential) {
172        name = "Exp";
173      }
174
175      public void Visit(Logarithm logarithm) {
176        name = "Log";
177      }
178
179      public void Visit(Multiplication multiplication) {
180        name = "*";
181      }
182
183      public void Visit(Power power) {
184        name = "Pow";
185      }
186
187      public void Visit(Signum signum) {
188        name = "Sign";
189      }
190
191      public void Visit(Sinus sinus) {
192        name = "Sin";
193      }
194
195      public void Visit(Sqrt sqrt) {
196        name = "Sqrt";
197      }
198
199      public void Visit(Substraction substraction) {
200        name = "-";
201      }
202
203      public void Visit(Tangens tangens) {
204        name = "Tan";
205      }
206
207      public void Visit(Variable variable) {
208        string timeOffset = "";
209        if(variable.SampleOffset < 0) {
210          timeOffset = "(t" + variable.SampleOffset + ")";
211        } else if(variable.SampleOffset > 0) {
212          timeOffset = "(t+" + variable.SampleOffset + ")";
213        } else {
214          timeOffset = "";
215        }
216        name = "Var" + variable.VariableIndex + timeOffset + " * " + variable.Weight;
217      }
218
219      public void Visit(And and) {
220        name = "And";
221      }
222
223      public void Visit(Average average) {
224        name = "Avg";
225      }
226
227      public void Visit(IfThenElse ifThenElse) {
228        name = "IFTE";
229      }
230
231      public void Visit(Not not) {
232        name = "Not";
233      }
234
235      public void Visit(Or or) {
236        name = "Or";
237      }
238
239      public void Visit(Xor xor) {
240        name = "Xor";
241      }
242
243      public void Visit(Equal equal) {
244        name = "eq?";
245      }
246
247      public void Visit(LessThan lessThan) {
248        name = "<";
249      }
250
251      #endregion
252    }
253
254    private class ModelAnalyzerExportVisitor : IFunctionVisitor {
255      private string prefix;
256      private string currentIndend = "";
257      public string ModelAnalyzerPrefix {
258        get { return prefix; }
259      }
260      public void Reset() {
261        prefix = "";
262      }
263
264      private void VisitFunction(string name, IFunction f) {
265        prefix += currentIndend + "[F]"+name+"(\n";
266        currentIndend += "  ";
267        foreach(IFunction subFunction in f.SubFunctions) {
268          subFunction.Accept(this);
269          prefix += ";\n";
270        }
271        prefix = prefix.TrimEnd(';','\n');
272        prefix += ")";
273        currentIndend = currentIndend.Remove(0, 2);
274      }
275
276      #region IFunctionVisitor Members
277
278      public void Visit(IFunction function) {
279        prefix += function.Name;
280      }
281
282      public void Visit(Addition addition) {
283        VisitFunction("Addition[0]", addition);
284      }
285
286      public void Visit(Constant constant) {
287        prefix += currentIndend + "[T]Constant(" + constant.Value.Data.ToString() + ";0;0)";
288      }
289
290      public void Visit(Cosinus cosinus) {
291        VisitFunction("Trigonometrics[1]", cosinus);
292      }
293
294      public void Visit(Division division) {
295        VisitFunction("Division[0]", division);
296      }
297
298      public void Visit(Exponential exponential) {
299        VisitFunction("Exponential[0]", exponential);
300      }
301
302      public void Visit(Logarithm logarithm) {
303        VisitFunction("Logarithm[0]", logarithm);
304      }
305
306      public void Visit(Multiplication multiplication) {
307        VisitFunction("Multiplication[0]", multiplication);
308      }
309
310      public void Visit(Power power) {
311        VisitFunction("Power[0]", power);
312      }
313
314      public void Visit(Signum signum) {
315        VisitFunction("Signum[0]", signum);
316      }
317
318      public void Visit(Sinus sinus) {
319        VisitFunction("Trigonometrics[0]", sinus);
320      }
321
322      public void Visit(Sqrt sqrt) {
323        VisitFunction("Sqrt[0]", sqrt);
324      }
325
326      public void Visit(Substraction substraction) {
327        VisitFunction("Substraction[0]", substraction);
328      }
329
330      public void Visit(Tangens tangens) {
331        VisitFunction("Trigonometrics[2]", tangens);
332      }
333
334      public void Visit(HeuristicLab.Functions.Variable variable) {
335        prefix += currentIndend + "[T]Variable(" + variable.Weight + ";" + variable.VariableIndex + ";" + -variable.SampleOffset + ")";
336      }
337
338      public void Visit(And and) {
339        VisitFunction("Logical[0]", and);
340      }
341
342      public void Visit(Average average) {
343        VisitFunction("N/A (average)", average);
344      }
345
346      public void Visit(IfThenElse ifThenElse) {
347        VisitFunction("Conditional[0]", ifThenElse);
348      }
349
350      public void Visit(Not not) {
351        VisitFunction("Logical[2]", not);
352      }
353
354      public void Visit(Or or) {
355        VisitFunction("Logical[1]", or);
356      }
357
358      public void Visit(Xor xor) {
359        VisitFunction("N/A (xor)", xor);
360      }
361
362      public void Visit(Equal equal) {
363        VisitFunction("Boolean[2]", equal);
364      }
365
366      public void Visit(LessThan lessThan) {
367        VisitFunction("Boolean[0]", lessThan);
368      }
369
370      #endregion
371    }
372
373  }
374}
Note: See TracBrowser for help on using the repository browser.