Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Added some useful functionality to the GenealogyGraphView.

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