Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymboldDataAnalysisGenealogyView.cs @ 11506

Last change on this file since 11506 was 11506, checked in by bburlacu, 9 years ago

#1772: Updated views (fixed position and anchors).

File size: 8.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
30using HeuristicLab.EvolutionTracking;
31using HeuristicLab.EvolutionTracking.Views;
32using HeuristicLab.MainForm;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
35  [View("SymboldDataAnalysisGenealogyView")]
36  [Content(typeof(IGenealogyGraph<ISymbolicExpressionTree>), IsDefaultView = true)]
37  public partial class SymboldDataAnalysisGenealogyView : ItemView {
38    private readonly ISymbolicExpressionTreeNodeSimilarityComparer comparer;
39
40    public SymboldDataAnalysisGenealogyView() {
41      InitializeComponent();
42
43      comparer = new SymbolicExpressionTreeNodeSimilarityComparer();
44    }
45
46    public new IGenealogyGraph<ISymbolicExpressionTree> Content {
47      get { return (IGenealogyGraph<ISymbolicExpressionTree>)base.Content; }
48      set { base.Content = value; }
49    }
50
51    #region event handlers
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55      if (Content != null) {
56        genealogyGraphChart.GenealogyGraph = Content;
57      }
58    }
59
60    #endregion
61
62    protected override void RegisterContentEvents() {
63      genealogyGraphChart.GenealogyGraphNodeClicked += graphChart_GenealogyGraphNodeClicked;
64      genealogyGraphChart.GenealogyGraphNodeDoubleClicked += graphChart_GenealogyGraphNodeDoubleClicked;
65      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked += treeChart_SymbolicExpressionNodeClicked;
66      base.RegisterContentEvents();
67    }
68
69    protected override void DeregisterContentEvents() {
70      base.DeregisterContentEvents();
71      genealogyGraphChart.GenealogyGraphNodeClicked -= graphChart_GenealogyGraphNodeClicked;
72      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked -= treeChart_SymbolicExpressionNodeClicked;
73    }
74
75    public void graphChart_GenealogyGraphNodeClicked(object sender, MouseEventArgs args) {
76      var visualNode = (VisualGenealogyGraphNode)sender;
77      var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)visualNode.Data;
78      var tree = graphNode.Data;
79      symbolicExpressionTreeChart.Tree = tree;
80      if (graphNode.InArcs.Any()) {
81        var fragment = graphNode.InArcs.Last().Data as IFragment<ISymbolicExpressionTreeNode>;
82        if (fragment != null) {
83          treeChart_HighlightSubtree(graphNode.Data.NodeAt(fragment.Index1));
84        }
85      }
86      if (graphNode.OutArcs.Any()) {
87        var td = graphNode.OutArcs.Last().Data as Tuple<int, int, int, int>;
88        if (td != null) {
89          var s = graphNode.Data.NodeAt(td.Item3); // subtree index
90          var f = graphNode.Data.NodeAt(td.Item4); // fragment index
91          treeChart_HighlightSubtree(s, Color.Orange);
92          treeChart_HighlightSubtree(f, Color.RoyalBlue);
93        }
94      }
95    }
96
97    public void graphChart_GenealogyGraphNodeDoubleClicked(object sender, MouseEventArgs arcs) {
98      var visualNode = (VisualGenealogyGraphNode)sender;
99      var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)visualNode.Data;
100      //      var tree = graphNode.Data;
101      //      var model = new RegressionModel();
102      //      var solution = new RegressionSolution()
103      //      MainFormManager.MainForm.ShowContent(tree);
104    }
105
106    public void treeChart_SymbolicExpressionNodeClicked(object sender, MouseEventArgs args) {
107      var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
108      var subtree = visualNode.Content;
109
110      // highlight the selected subtree inside the displayed tree on the right hand side
111      treeChart_ClearColors();
112      treeChart_HighlightSubtree(subtree);
113
114      bool trace = genealogyGraphChart.TraceFragments; // check whether the mode is 'trace' or 'match'
115
116      if (trace) {
117        // perform fragment tracing
118        var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
119        var subtreeIndex = graphNode.Data.IterateNodesPrefix().ToList().IndexOf(subtree);
120        var fragmentGraph = SymbolicDataAnalysisExpressionTracing.TraceSubtree(graphNode, subtreeIndex);
121
122        if (fragmentGraph.Vertices.Any()) {
123          var genealogyNodes = fragmentGraph.Vertices.Select(v => Content.GetByContent(v.Data));
124          genealogyGraphChart.HighlightNodes(genealogyNodes);
125          foreach (var a in fragmentGraph.Arcs) {
126            genealogyGraphChart.HighlightArc(Content.GetByContent(a.Target.Data), Content.GetByContent(a.Source.Data));
127          }
128          MainFormManager.MainForm.ShowContent(fragmentGraph); // display the fragment graph on the screen
129        }
130      } else {
131        // perform matching like it was done before
132        // currently there is no possibility to specify the subtree matching criteria
133        var trees = Content.Vertices.Select(x => x.Data);
134        var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer));
135
136        var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x));
137        graphChart_highlightMatchingVertices(matchingVertices);
138      }
139    }
140
141    private void graphChart_highlightMatchingVertices(IEnumerable<IGenealogyGraphNode> vertices) {
142      genealogyGraphChart.Chart.UpdateEnabled = false;
143      genealogyGraphChart.ClearPrimitives();
144      genealogyGraphChart.HighlightNodes(vertices);
145      genealogyGraphChart.Chart.UpdateEnabled = true;
146      genealogyGraphChart.Chart.EnforceUpdate();
147    }
148
149    private void treeChart_ClearColors() {
150      foreach (var node in symbolicExpressionTreeChart.Tree.IterateNodesPrefix()) {
151        var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
152        if (visualNode != null) {
153          visualNode.LineColor = Color.Black;
154          visualNode.FillColor = Color.Transparent;
155        }
156      }
157      symbolicExpressionTreeChart.RepaintNodes();
158    }
159
160    private void treeChart_HighlightSubtree(ISymbolicExpressionTreeNode subtree, Color? color = null) {
161      Color myColor = color == null ? Color.RoyalBlue : (Color)color;
162      foreach (var s in subtree.IterateNodesPrefix()) {
163        var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(s);
164        visualNode.LineColor = myColor;
165        //        visualNode.FillColor = myColor;
166
167        foreach (var c in s.Subtrees) {
168          var visualArc = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNodeConnection(s, c);
169          visualArc.LineColor = myColor;
170        }
171      }
172      symbolicExpressionTreeChart.RepaintNodes();
173    }
174
175    #region events for configuring the behavior of the genealogy chart (trace/match, simple lineages, etc)
176    private void trace_checkBox_CheckedChanged(object sender, System.EventArgs e) {
177      genealogyGraphChart.TraceFragments = trace_checkBox.Checked;
178    }
179
180    private void simpleLineages_checkBox_CheckedChanged(object sender, System.EventArgs e) {
181      genealogyGraphChart.SimpleLineages = simpleLineages_checkBox.Checked;
182    }
183
184    private void lockGraph_checkBox_CheckedChanged(object sender, System.EventArgs e) {
185      genealogyGraphChart.LockGenealogy = lockGraph_checkBox.Checked;
186    }
187    #endregion
188
189    private void hotPaths_button_Click(object sender, System.EventArgs e) {
190      genealogyGraphChart.HighlightHotPaths();
191    }
192  }
193}
Note: See TracBrowser for help on using the repository browser.