Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymbolicDataAnalysisGenealogyGraphView.cs @ 11817

Last change on this file since 11817 was 11817, checked in by bburlacu, 8 years ago

#1772: Refactored genealogy graph views. Added an additional class specializing GenealogyGraphView<ISymbolicExpressionTree> for the sole purpose of making the SymbolicDataAnalysisGenealogyGraphView work with the designer. Added property to expose the SymbolicExpressionTreeChart in the GraphicalSymbolicExpressionTreeView in order to be able to wire mouse click events.

File size: 7.2 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.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
29using HeuristicLab.EvolutionTracking;
30using HeuristicLab.EvolutionTracking.Views;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
34  [View("SymbolicDataAnalysisGenealogyGraphView")]
35  [Content(typeof(IGenealogyGraph<ISymbolicExpressionTree>), IsDefaultView = true)]
36  public partial class SymbolicDataAnalysisGenealogyGraphView : SymbolicDataAnalysisGenealogyGraphViewDesignable {
37    private readonly ISymbolicExpressionTreeNodeSimilarityComparer comparer;
38
39    public SymbolicDataAnalysisGenealogyGraphView() {
40      InitializeComponent();
41      comparer = new SymbolicExpressionTreeNodeSimilarityComparer();
42      viewHost.ViewType = typeof(GraphicalSymbolicExpressionTreeView);
43    }
44    #region event handlers
45    protected override void OnContentChanged() {
46      base.OnContentChanged();
47      if (Content != null) { genealogyGraphChart.GenealogyGraph = Content; }
48    }
49    #endregion
50
51    public SymbolicExpressionTreeChart SymbolicExpressionTreeChart {
52      get {
53        var view = (GraphicalSymbolicExpressionTreeView)base.viewHost.ActiveView;
54        return view == null ? null : view.SymbolicExpressionTreeChart;
55      }
56    }
57
58    protected override void RegisterContentEvents() {
59      base.RegisterContentEvents();
60      if (SymbolicExpressionTreeChart != null)
61        SymbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked += treeChart_SymbolicExpressionTreeNodeClicked;
62    }
63
64    protected override void DeregisterContentEvents() {
65      if (SymbolicExpressionTreeChart != null)
66        SymbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked -= treeChart_SymbolicExpressionTreeNodeClicked;
67      base.DeregisterContentEvents();
68    }
69
70    public override void graphChart_GenealogyGraphNodeClicked(object sender, MouseEventArgs args) {
71      base.graphChart_GenealogyGraphNodeClicked(sender, args);
72      var visualNode = (VisualGenealogyGraphNode)sender;
73      var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)visualNode.Data;
74      if (graphNode.InArcs.Any()) {
75        var fragment = graphNode.InArcs.Last().Data as IFragment<ISymbolicExpressionTreeNode>;
76        if (fragment != null) {
77          treeChart_HighlightSubtree(graphNode.Data.NodeAt(fragment.Index1));
78        }
79      }
80      if (graphNode.OutArcs.Any()) {
81        var td = graphNode.OutArcs.Last().Data as Tuple<int, int, int, int>;
82        if (td != null) {
83          var s = graphNode.Data.NodeAt(td.Item3); // subtree index
84          var f = graphNode.Data.NodeAt(td.Item4); // fragment index
85          treeChart_HighlightSubtree(s, Color.Orange);
86          treeChart_HighlightSubtree(f, Color.RoyalBlue);
87        }
88      }
89    }
90
91    public void treeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs args) {
92      var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
93      var subtree = visualNode.Content;
94
95      // highlight the selected subtree inside the displayed tree on the right hand side
96      treeChart_ClearColors();
97      treeChart_HighlightSubtree(subtree);
98
99      bool trace = genealogyGraphChart.TraceFragments; // check whether we are in 'trace' or 'match' mode
100
101      if (trace) {
102        // perform fragment tracing
103        var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
104        var subtreeIndex = graphNode.Data.IterateNodesPrefix().ToList().IndexOf(subtree);
105        //        var fragmentGraph = SymbolicDataAnalysisExpressionTracing.TraceSubtree(graphNode, subtreeIndex);
106        var traceGraph = TraceCalculator.TraceSubtree(graphNode, subtreeIndex);
107
108        if (traceGraph.Vertices.Any()) {
109          var genealogyNodes = traceGraph.Vertices.Select(v => Content.GetByContent(v.Data));
110          genealogyGraphChart.HighlightNodes(genealogyNodes);
111          foreach (var a in traceGraph.Arcs) {
112            genealogyGraphChart.HighlightArc(Content.GetByContent(a.Target.Data), Content.GetByContent(a.Source.Data));
113          }
114          MainFormManager.MainForm.ShowContent(traceGraph); // display the fragment graph on the screen
115        }
116      } else {
117        // perform matching like it was done before
118        // currently there is no possibility to specify the subtree matching criteria
119        var trees = Content.Vertices.Select(x => x.Data);
120        var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer));
121
122        var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x));
123        graphChart_highlightMatchingVertices(matchingVertices);
124      }
125    }
126
127    private void graphChart_highlightMatchingVertices(IEnumerable<IGenealogyGraphNode> vertices) {
128      genealogyGraphChart.Chart.UpdateEnabled = false;
129      genealogyGraphChart.ClearPrimitives();
130      genealogyGraphChart.HighlightNodes(vertices);
131      genealogyGraphChart.Chart.UpdateEnabled = true;
132      genealogyGraphChart.Chart.EnforceUpdate();
133    }
134
135    private void treeChart_ClearColors() {
136      foreach (var node in SymbolicExpressionTreeChart.Tree.IterateNodesPrefix()) {
137        var visualNode = SymbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
138        if (visualNode != null) {
139          visualNode.LineColor = Color.Black;
140          visualNode.FillColor = Color.Transparent;
141        }
142      }
143      SymbolicExpressionTreeChart.RepaintNodes();
144    }
145
146    private void treeChart_HighlightSubtree(ISymbolicExpressionTreeNode subtree, Color? color = null) {
147      Color myColor = color ?? Color.RoyalBlue;
148      foreach (var s in subtree.IterateNodesPrefix()) {
149        var visualNode = SymbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(s);
150        visualNode.LineColor = myColor;
151        //        visualNode.FillColor = myColor;
152
153        foreach (var c in s.Subtrees) {
154          var visualArc = SymbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNodeConnection(s, c);
155          visualArc.LineColor = myColor;
156        }
157      }
158      SymbolicExpressionTreeChart.RepaintNodes();
159    }
160  }
161
162  public class SymbolicDataAnalysisGenealogyGraphViewDesignable : GenealogyGraphView<ISymbolicExpressionTree> { }
163}
Note: See TracBrowser for help on using the repository browser.