Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Views/ExpressionSelectionView.cs @ 14952

Last change on this file since 14952 was 14952, checked in by pkimmesw, 8 years ago

#2665 Added IsNoop to Expression, Made Expressions storable, Fixed Debugger, Fixed and improved problem data and result visualisation, Added custom ErcOption view, Added problem difficulty to problem data name

File size: 4.4 KB
Line 
1using System.Windows.Forms;
2using HeuristicLab.Core.Views;
3using HeuristicLab.MainForm;
4
5
6namespace HeuristicLab.Problems.ProgramSynthesis.Push.Views {
7  using System;
8  using System.Collections.Generic;
9  using System.Linq;
10  using Expressions;
11
12  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
13
14  using Stack;
15
16  [View("Push Expression Selection Editor")]
17  [Content(typeof(IEnabledExpressionsConfiguration), true)]
18  public partial class ExpressionSelectionView : NamedItemView {
19    public ExpressionSelectionView() {
20      InitializeComponent();
21
22      InitEvents();
23      Name = "Push Expression Selection Editor";
24    }
25
26    public new IEnabledExpressionsConfiguration Content
27    {
28      get { return (IEnabledExpressionsConfiguration)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, true, false);
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, false);
60        SetStackNodeText(e.Node.Parent);
61
62        if ((e.Node.Checked && !e.Node.Parent.Checked) ||
63            (!e.Node.Checked && !e.Node.Parent.Nodes.OfType<TreeNode>().Any(n => n.Checked)))
64          e.Node.Parent.Checked = e.Node.Checked;
65      }
66
67      Content.EnabledExpressionsChanged += EnabledExpressionsChanged;
68    }
69
70    protected override void OnContentChanged() {
71      expressionsTreeView.Nodes.Clear();
72      nameTextBox.Text = "Push Expression Selection";
73
74      if (Content == null) {
75        return;
76      }
77
78      Content.EnabledExpressionsChanged += EnabledExpressionsChanged;
79      var treeNodes = GetTreeNodes(Content.EnabledExpressions).ToArray();
80      expressionsTreeView.Nodes.AddRange(treeNodes);
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).ToArray();
92      expressionsTreeView.Nodes.AddRange(treeNodes);
93    }
94
95    private IEnumerable<TreeNode> GetTreeNodes(IList<string> enabledExpressions) {
96      foreach (var stackType in ExpressionTable.StackTypeToNamesTable.Keys) {
97        var stackTreeNode = new TreeNode {
98          Tag = stackType
99        };
100
101        var stackExpressions = ExpressionTable.StackTypeToNamesTable[stackType];
102        var enabledStackExpressions = enabledExpressions.Intersect(stackExpressions).ToList();
103
104        SetStackNodeText(stackTreeNode);
105        stackTreeNode.Checked = enabledStackExpressions.Any();
106
107        var expressionNodes = stackExpressions
108          .OrderBy(name => name)
109          .Select(name => new TreeNode {
110            Text = name,
111            Tag = stackType,
112            Checked = enabledStackExpressions.Contains(name),
113          }).ToArray();
114
115        stackTreeNode.Nodes.AddRange(expressionNodes);
116
117        yield return stackTreeNode;
118      }
119    }
120
121    private void SetStackNodeText(TreeNode node) {
122      var stackType = (StackTypes)node.Tag;
123      var stackExpressions = ExpressionTable.StackTypeToNamesTable[stackType];
124      var enabledStackExpressions = Content.EnabledExpressions.Intersect(stackExpressions).ToList();
125
126      node.Text = string.Format(
127                "{0} ({1}/{2})",
128                Enum.GetName(typeof(StackTypes), stackType),
129                enabledStackExpressions.Count,
130                ExpressionTable.StackTypeToNamesTable[stackType].Count);
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.