Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis.Views/ExpressionSelectionView.cs @ 16101

Last change on this file since 16101 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 4.8 KB
Line 
1using System.Windows.Forms;
2using HeuristicLab.Core.Views;
3using HeuristicLab.MainForm;
4
5using System.Collections.Generic;
6using System.Linq;
7
8namespace HeuristicLab.Problems.ProgramSynthesis.Views {
9  [View("Push Expression Selection Editor")]
10  [Content(typeof(IExpressionsConfiguration), true)]
11  public partial class ExpressionSelectionView : ItemView {
12    public ExpressionSelectionView() {
13      InitializeComponent();
14
15      InitEvents();
16      Name = "Push Expression Selection Editor";
17    }
18
19    public new IExpressionsConfiguration Content {
20      get { return (IExpressionsConfiguration)base.Content; }
21      set {
22        if (Content != null) Content.EnabledExpressionsChanged -= EnabledExpressionsChanged;
23        base.Content = value;
24      }
25    }
26
27    protected override void OnReadOnlyChanged() {
28      base.OnReadOnlyChanged();
29
30      expressionsTreeView.Enabled = !ReadOnly;
31    }
32
33    private void InitEvents() {
34      expressionsTreeView.AfterCheck += ExpressionsTreeViewAfterCheck;
35    }
36
37    private void ExpressionsTreeViewAfterCheck(object sender, TreeViewEventArgs e) {
38      if (e.Action == TreeViewAction.Unknown)
39        return;
40
41      Content.EnabledExpressionsChanged -= EnabledExpressionsChanged;
42      var stackType = (StackTypes)e.Node.Tag;
43
44      if (e.Node.Parent == null) {
45        Content.SetStack(stackType, e.Node.Checked);
46        SetStackNodeText(e.Node);
47        foreach (var subNode in e.Node.Nodes.OfType<TreeNode>().Where(n => n.Checked != e.Node.Checked))
48          subNode.Checked = e.Node.Checked;
49      } else {
50        Content.SetExpression(e.Node.Text, e.Node.Checked);
51        SetStackNodeText(e.Node.Parent);
52
53        // enable/disable stack if no or at least one expression is enabled
54        if ((e.Node.Checked && !e.Node.Parent.Checked) ||
55            (!e.Node.Checked && e.Node.Parent.Nodes.OfType<TreeNode>().All(n => !n.Checked))) {
56          e.Node.Parent.Checked = e.Node.Checked;
57        }
58      }
59
60      Content.EnabledExpressionsChanged += EnabledExpressionsChanged;
61    }
62
63    protected override void OnContentChanged() {
64      expressionsTreeView.Nodes.Clear();
65
66      if (Content == null) {
67        return;
68      }
69
70      Content.EnabledExpressionsChanged += EnabledExpressionsChanged;
71      UpdateNodes();
72    }
73
74    private void EnabledExpressionsChanged(object sender, EnabledExpressionsChangedEventArgs e) {
75      if (InvokeRequired) Invoke(new MethodInvoker(UpdateNodes));
76      else UpdateNodes();
77    }
78
79    private void UpdateNodes() {
80      expressionsTreeView.Nodes.Clear();
81
82      var treeNodes = GetTreeNodes(Content.EnabledExpressions)
83        .OrderBy(x => x.Text)
84        .ToArray();
85
86      expressionsTreeView.Nodes.AddRange(treeNodes);
87    }
88
89    private IEnumerable<TreeNode> GetTreeNodes(IReadOnlyList<string> enabledExpressions) {
90      foreach (var stackType in ExpressionTable.StackTypeToNamesTable.Keys) {
91        if (stackType == StackTypes.None)
92          continue;
93
94        var stackTreeNode = new TreeNode {
95          Tag = stackType
96        };
97
98        var stackExpressions = ExpressionTable.StackTypeToNamesTable[stackType];
99        var enabledStackExpressions = enabledExpressions.Intersect(stackExpressions).ToList();
100
101        SetStackNodeText(stackTreeNode);
102        stackTreeNode.Checked = enabledStackExpressions.Any();
103
104        var expressionNodes = stackExpressions
105          .Where(name => {
106            var type = ExpressionTable.NameToTypeTable[name];
107            var attribute = ExpressionTable.TypeToAttributeTable[type];
108
109            return !attribute.IsHidden;
110          })
111          .OrderBy(name => name)
112          .Select(name => new TreeNode {
113            Text = name,
114            Tag = stackType,
115            Checked = enabledStackExpressions.Contains(name),
116          }).ToArray();
117
118        foreach (var node in expressionNodes) {
119          var type = ExpressionTable.NameToTypeTable[node.Text];
120          var attribute = ExpressionTable.TypeToAttributeTable[type];
121
122          node.ToolTipText = attribute.Description;
123        }
124
125        stackTreeNode.Nodes.AddRange(expressionNodes);
126
127        yield return stackTreeNode;
128      }
129    }
130
131    private void SetStackNodeText(TreeNode node) {
132      var stackTypes = (StackTypes)node.Tag;
133      var stackExpressions = ExpressionTable.StackTypeToNamesTable[stackTypes];
134      var enabledStackExpressions = Content.EnabledExpressions.Intersect(stackExpressions).ToList();
135      var stackNames = string.Join(", ", stackTypes.ToValues().Select(type => type.ToString()).OrderBy(x => x));
136
137      node.Text = string.Format(
138                "{0} ({1}/{2})",
139                stackNames,
140                enabledStackExpressions.Count,
141                stackExpressions.Count);
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.