Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Ported the rest of the changes to the DirectedGraph and Vertex to the GenealogyGraph and GenealogyGraphNode. Adapted tracking operators, analyzers and views.

File size: 7.0 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.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core.Views;
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("SymboldDataAnalysisGenealogyView")]
35  [Content(typeof(IGenealogyGraph<ISymbolicExpressionTree>), IsDefaultView = true)]
36  public partial class SymboldDataAnalysisGenealogyView : ItemView {
37    private readonly ISymbolicExpressionTreeNodeSimilarityComparer comparer;
38
39    public SymboldDataAnalysisGenealogyView() {
40      InitializeComponent();
41
42      comparer = new SymbolicExpressionTreeNodeSimilarityComparer();
43    }
44
45    public new IGenealogyGraph<ISymbolicExpressionTree> Content {
46      get { return (IGenealogyGraph<ISymbolicExpressionTree>)base.Content; }
47      set { base.Content = value; }
48    }
49
50    #region event handlers
51
52    protected override void OnContentChanged() {
53      base.OnContentChanged();
54      if (Content != null) {
55        genealogyGraphChart.GenealogyGraph = Content;
56      }
57    }
58
59    #endregion
60
61    protected override void RegisterContentEvents() {
62      genealogyGraphChart.GenealogyGraphNodeClicked += graphChart_GenealogyGraphNodeClicked;
63      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked += treeChart_SymbolicExpressionNodeClicked;
64      base.RegisterContentEvents();
65    }
66
67    protected override void DeregisterContentEvents() {
68      base.DeregisterContentEvents();
69      genealogyGraphChart.GenealogyGraphNodeClicked -= graphChart_GenealogyGraphNodeClicked;
70      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked -= treeChart_SymbolicExpressionNodeClicked;
71    }
72
73    public void graphChart_GenealogyGraphNodeClicked(object sender, MouseEventArgs args) {
74      var visualNode = (VisualGenealogyGraphNode)sender;
75      var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)visualNode.Data;
76      var tree = graphNode.Data;
77      symbolicExpressionTreeChart.Tree = tree;
78
79      if (graphNode.InArcs.Any()) {
80        var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data;
81        if (fragment != null) {
82          treeChart_HighlightSubtree(graphNode.Data.NodeAt(fragment.Index1));
83        }
84      }
85    }
86
87    public void treeChart_SymbolicExpressionNodeClicked(object sender, MouseEventArgs args) {
88      var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
89      var subtree = visualNode.Content;
90
91      // highlight the selected subtree inside the displayed tree on the right hand side
92      treeChart_ClearColors();
93      treeChart_HighlightSubtree(subtree);
94
95      bool trace = genealogyGraphChart.TraceFragments; // check whether the mode is 'trace' or 'match'
96
97      if (trace) {
98        // perform fragment tracing
99        var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
100        var subtreeIndex = graphNode.Data.IterateNodesPrefix().ToList().IndexOf(subtree);
101        var fragmentGraph = SymbolicDataAnalysisExpressionTracing.TraceSubtree(graphNode, subtreeIndex);
102        if (fragmentGraph.Vertices.Any()) {
103          MainFormManager.MainForm.ShowContent(fragmentGraph); // display the fragment graph on the screen
104        }
105      } else {
106        // perform matching like it was done before
107        // currently there is no possibility to specify the subtree matching criteria
108        var trees = Content.Vertices.Select(x => x.Data);
109        var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer));
110
111        var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x)).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>();
112        graphChart_highlightMatchingVertices(matchingVertices);
113      }
114    }
115
116    private void graphChart_highlightMatchingVertices(IEnumerable<IGenealogyGraphNode> vertices) {
117      genealogyGraphChart.Chart.UpdateEnabled = false;
118      genealogyGraphChart.ClearPrimitives();
119      genealogyGraphChart.HighlightNodes(vertices);
120      genealogyGraphChart.Chart.UpdateEnabled = true;
121      genealogyGraphChart.Chart.EnforceUpdate();
122    }
123
124    private void treeChart_ClearColors() {
125      foreach (var node in symbolicExpressionTreeChart.Tree.IterateNodesPrefix()) {
126        var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
127        if (visualNode != null) {
128          visualNode.LineColor = Color.Black;
129          visualNode.FillColor = Color.Transparent;
130        }
131      }
132      symbolicExpressionTreeChart.RepaintNodes();
133    }
134
135    private void treeChart_HighlightSubtree(ISymbolicExpressionTreeNode subtree) {
136      foreach (var s in subtree.IterateNodesPrefix()) {
137        var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(s);
138        visualNode.LineColor = Color.RoyalBlue;
139        visualNode.FillColor = Color.LightBlue;
140
141        foreach (var c in s.Subtrees) {
142          var visualArc = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNodeConnection(s, c);
143          visualArc.LineColor = Color.RoyalBlue;
144        }
145      }
146      symbolicExpressionTreeChart.RepaintNodes();
147    }
148
149    #region events for configuring the behavior of the genealogy chart (trace/match, simple lineages, etc)
150    private void trace_checkBox_CheckedChanged(object sender, System.EventArgs e) {
151      genealogyGraphChart.TraceFragments = trace_checkBox.Checked;
152    }
153
154    private void simpleLineages_checkBox_CheckedChanged(object sender, System.EventArgs e) {
155      genealogyGraphChart.SimpleLineages = simpleLineages_checkBox.Checked;
156    }
157
158    private void lockGraph_checkBox_CheckedChanged(object sender, System.EventArgs e) {
159      genealogyGraphChart.LockGenealogy = lockGraph_checkBox.Checked;
160    }
161    #endregion
162
163    private void hotPaths_button_Click(object sender, System.EventArgs e) {
164      genealogyGraphChart.HighlightHotPaths();
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.