Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11503 was 11503, checked in by bburlacu, 10 years ago

#1772: Added handling of double click event in the genealogy graph (opens a new view of the selected symbolic expression tree). Added highlighting of trace data. Very minor cosmetic changes to the TraceCalculator. Removed resx file from project.

File size: 7.8 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
81      if (graphNode.InArcs.Any()) {
82        var fragment = graphNode.InArcs.Last().Data as IFragment<ISymbolicExpressionTreeNode>;
83        if (fragment != null) {
84          treeChart_HighlightSubtree(graphNode.Data.NodeAt(fragment.Index1));
85        } else {
86          var td = graphNode.InArcs.Last().Data as Tuple<int, int, int, int>;
87          if (td != null) {
88            var s = graphNode.Data.NodeAt(td.Item1);
89            var f = graphNode.Data.NodeAt(td.Item2);
90            treeChart_HighlightSubtree(s, Color.Orange);
91            treeChart_HighlightSubtree(f, Color.RoyalBlue);
92          }
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      MainFormManager.MainForm.ShowContent(tree);
102    }
103
104    public void treeChart_SymbolicExpressionNodeClicked(object sender, MouseEventArgs args) {
105      var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
106      var subtree = visualNode.Content;
107
108      // highlight the selected subtree inside the displayed tree on the right hand side
109      treeChart_ClearColors();
110      treeChart_HighlightSubtree(subtree);
111
112      bool trace = genealogyGraphChart.TraceFragments; // check whether the mode is 'trace' or 'match'
113
114      if (trace) {
115        // perform fragment tracing
116        var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
117        var subtreeIndex = graphNode.Data.IterateNodesPrefix().ToList().IndexOf(subtree);
118        var fragmentGraph = SymbolicDataAnalysisExpressionTracing.TraceSubtree(graphNode, subtreeIndex);
119        if (fragmentGraph.Vertices.Any()) {
120          MainFormManager.MainForm.ShowContent(fragmentGraph); // display the fragment graph on the screen
121        }
122      } else {
123        // perform matching like it was done before
124        // currently there is no possibility to specify the subtree matching criteria
125        var trees = Content.Vertices.Select(x => x.Data);
126        var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer));
127
128        var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x));
129        graphChart_highlightMatchingVertices(matchingVertices);
130      }
131    }
132
133    private void graphChart_highlightMatchingVertices(IEnumerable<IGenealogyGraphNode> vertices) {
134      genealogyGraphChart.Chart.UpdateEnabled = false;
135      genealogyGraphChart.ClearPrimitives();
136      genealogyGraphChart.HighlightNodes(vertices);
137      genealogyGraphChart.Chart.UpdateEnabled = true;
138      genealogyGraphChart.Chart.EnforceUpdate();
139    }
140
141    private void treeChart_ClearColors() {
142      foreach (var node in symbolicExpressionTreeChart.Tree.IterateNodesPrefix()) {
143        var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
144        if (visualNode != null) {
145          visualNode.LineColor = Color.Black;
146          visualNode.FillColor = Color.Transparent;
147        }
148      }
149      symbolicExpressionTreeChart.RepaintNodes();
150    }
151
152    private void treeChart_HighlightSubtree(ISymbolicExpressionTreeNode subtree, Color? color = null) {
153      Color myColor = color == null ? Color.RoyalBlue : (Color)color;
154      foreach (var s in subtree.IterateNodesPrefix()) {
155        var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(s);
156        visualNode.LineColor = myColor;
157        //        visualNode.FillColor = myColor;
158
159        foreach (var c in s.Subtrees) {
160          var visualArc = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNodeConnection(s, c);
161          visualArc.LineColor = myColor;
162        }
163      }
164      symbolicExpressionTreeChart.RepaintNodes();
165    }
166
167    #region events for configuring the behavior of the genealogy chart (trace/match, simple lineages, etc)
168    private void trace_checkBox_CheckedChanged(object sender, System.EventArgs e) {
169      genealogyGraphChart.TraceFragments = trace_checkBox.Checked;
170    }
171
172    private void simpleLineages_checkBox_CheckedChanged(object sender, System.EventArgs e) {
173      genealogyGraphChart.SimpleLineages = simpleLineages_checkBox.Checked;
174    }
175
176    private void lockGraph_checkBox_CheckedChanged(object sender, System.EventArgs e) {
177      genealogyGraphChart.LockGenealogy = lockGraph_checkBox.Checked;
178    }
179    #endregion
180
181    private void hotPaths_button_Click(object sender, System.EventArgs e) {
182      genealogyGraphChart.HighlightHotPaths();
183    }
184  }
185}
Note: See TracBrowser for help on using the repository browser.