Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Views/ExpressionSelectionView.cs @ 16958

Last change on this file since 16958 was 15273, checked in by pkimmesw, 7 years ago

#2665 Started Plush Encoding, Added Zero Error Individual Count Analyzer

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