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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Data;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using System.Windows.Forms;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.PluginInfrastructure;
|
---|
32 | using HeuristicLab.Data;
|
---|
33 |
|
---|
34 | namespace 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 | ModelAnalyzerExportVisitor visitor = new ModelAnalyzerExportVisitor();
|
---|
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 | #endregion
|
---|
260 | }
|
---|
261 |
|
---|
262 | private class ModelAnalyzerExportVisitor : IFunctionVisitor {
|
---|
263 | private string prefix;
|
---|
264 | private string currentIndend = "";
|
---|
265 | private IFunctionTree currentBranch;
|
---|
266 | public string ModelAnalyzerPrefix {
|
---|
267 | get { return prefix; }
|
---|
268 | }
|
---|
269 | public void Reset() {
|
---|
270 | prefix = "";
|
---|
271 | }
|
---|
272 |
|
---|
273 | private void VisitFunction(string name, IFunction f) {
|
---|
274 | prefix += currentIndend + "[F]" + name + "(\n";
|
---|
275 | }
|
---|
276 |
|
---|
277 | #region IFunctionVisitor Members
|
---|
278 |
|
---|
279 | public void Visit(IFunction function) {
|
---|
280 | prefix += function.Name;
|
---|
281 | }
|
---|
282 |
|
---|
283 | public void Visit(Addition addition) {
|
---|
284 | VisitFunction("Addition[0]", addition);
|
---|
285 | }
|
---|
286 |
|
---|
287 | public void Visit(Constant constant) {
|
---|
288 | double value = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Constant.VALUE).Value).Data;
|
---|
289 | prefix += currentIndend + "[T]Constant(" + value + ";0;0)";
|
---|
290 | }
|
---|
291 |
|
---|
292 | public void Visit(Cosinus cosinus) {
|
---|
293 | VisitFunction("Trigonometrics[1]", cosinus);
|
---|
294 | }
|
---|
295 |
|
---|
296 | public void Visit(Division division) {
|
---|
297 | VisitFunction("Division[0]", division);
|
---|
298 | }
|
---|
299 |
|
---|
300 | public void Visit(Exponential exponential) {
|
---|
301 | VisitFunction("Exponential[0]", exponential);
|
---|
302 | }
|
---|
303 |
|
---|
304 | public void Visit(Logarithm logarithm) {
|
---|
305 | VisitFunction("Logarithm[0]", logarithm);
|
---|
306 | }
|
---|
307 |
|
---|
308 | public void Visit(Multiplication multiplication) {
|
---|
309 | VisitFunction("Multiplication[0]", multiplication);
|
---|
310 | }
|
---|
311 |
|
---|
312 | public void Visit(Power power) {
|
---|
313 | VisitFunction("Power[0]", power);
|
---|
314 | }
|
---|
315 |
|
---|
316 | public void Visit(Signum signum) {
|
---|
317 | VisitFunction("Signum[0]", signum);
|
---|
318 | }
|
---|
319 |
|
---|
320 | public void Visit(Sinus sinus) {
|
---|
321 | VisitFunction("Trigonometrics[0]", sinus);
|
---|
322 | }
|
---|
323 |
|
---|
324 | public void Visit(Sqrt sqrt) {
|
---|
325 | VisitFunction("Sqrt[0]", sqrt);
|
---|
326 | }
|
---|
327 |
|
---|
328 | public void Visit(Substraction substraction) {
|
---|
329 | VisitFunction("Substraction[0]", substraction);
|
---|
330 | }
|
---|
331 |
|
---|
332 | public void Visit(Tangens tangens) {
|
---|
333 | VisitFunction("Trigonometrics[2]", tangens);
|
---|
334 | }
|
---|
335 |
|
---|
336 | public void Visit(HeuristicLab.Functions.Variable variable) {
|
---|
337 | double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.WEIGHT).Value).Data;
|
---|
338 | double index = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.INDEX).Value).Data;
|
---|
339 | double offset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.OFFSET).Value).Data;
|
---|
340 |
|
---|
341 | prefix += currentIndend + "[T]Variable(" + weight + ";" + index + ";" + -offset + ")";
|
---|
342 | }
|
---|
343 |
|
---|
344 | public void Visit(And and) {
|
---|
345 | VisitFunction("Logical[0]", and);
|
---|
346 | }
|
---|
347 |
|
---|
348 | public void Visit(Average average) {
|
---|
349 | VisitFunction("N/A (average)", average);
|
---|
350 | }
|
---|
351 |
|
---|
352 | public void Visit(IfThenElse ifThenElse) {
|
---|
353 | VisitFunction("Conditional[0]", ifThenElse);
|
---|
354 | }
|
---|
355 |
|
---|
356 | public void Visit(Not not) {
|
---|
357 | VisitFunction("Logical[2]", not);
|
---|
358 | }
|
---|
359 |
|
---|
360 | public void Visit(Or or) {
|
---|
361 | VisitFunction("Logical[1]", or);
|
---|
362 | }
|
---|
363 |
|
---|
364 | public void Visit(Xor xor) {
|
---|
365 | VisitFunction("N/A (xor)", xor);
|
---|
366 | }
|
---|
367 |
|
---|
368 | public void Visit(Equal equal) {
|
---|
369 | VisitFunction("Boolean[2]", equal);
|
---|
370 | }
|
---|
371 |
|
---|
372 | public void Visit(LessThan lessThan) {
|
---|
373 | VisitFunction("Boolean[0]", lessThan);
|
---|
374 | }
|
---|
375 | #endregion
|
---|
376 |
|
---|
377 | public void Visit(IFunctionTree functionTree) {
|
---|
378 | currentBranch = functionTree;
|
---|
379 | functionTree.Function.Accept(this);
|
---|
380 | currentIndend += " ";
|
---|
381 | foreach(IFunctionTree subTree in functionTree.SubTrees) {
|
---|
382 | Visit(subTree);
|
---|
383 | prefix += ";\n";
|
---|
384 | }
|
---|
385 | prefix = prefix.TrimEnd(';', '\n');
|
---|
386 | prefix += ")";
|
---|
387 | currentIndend = currentIndend.Remove(0, 2);
|
---|
388 | }
|
---|
389 | }
|
---|
390 | }
|
---|
391 | }
|
---|