Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6299


Ignore:
Timestamp:
05/26/11 15:11:48 (14 years ago)
Author:
mkommend
Message:

#1479: Corrected handling of GroupSymbols.

Location:
branches/GP.Grammar.Editor
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolView.Designer.cs

    r6109 r6299  
    4747      this.initialFrequencyLabel = new System.Windows.Forms.Label();
    4848      this.initialFrequencyTextBox = new System.Windows.Forms.TextBox();
     49      this.checkBoxEnabled = new System.Windows.Forms.CheckBox();
    4950      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit();
    5051      this.SuspendLayout();
     
    7374      // initialFrequencyTextBox
    7475      //
    75       this.initialFrequencyTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
    76                   | System.Windows.Forms.AnchorStyles.Right)));
     76      this.initialFrequencyTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
     77            | System.Windows.Forms.AnchorStyles.Right)));
    7778      this.errorProvider.SetIconAlignment(this.initialFrequencyTextBox, System.Windows.Forms.ErrorIconAlignment.MiddleLeft);
    7879      this.initialFrequencyTextBox.Location = new System.Drawing.Point(93, 26);
     
    8485      this.initialFrequencyTextBox.Validated += new System.EventHandler(this.initialFrequencyTextBox_Validated);
    8586      //
     87      // checkBoxEnabled
     88      //
     89      this.checkBoxEnabled.AutoSize = true;
     90      this.checkBoxEnabled.Location = new System.Drawing.Point(93, 52);
     91      this.checkBoxEnabled.Name = "checkBoxEnabled";
     92      this.checkBoxEnabled.Size = new System.Drawing.Size(65, 17);
     93      this.checkBoxEnabled.TabIndex = 5;
     94      this.checkBoxEnabled.Text = "Enabled";
     95      this.checkBoxEnabled.UseVisualStyleBackColor = true;
     96      this.checkBoxEnabled.CheckedChanged += new System.EventHandler(this.checkBoxEnabled_CheckedChanged);
     97      //
    8698      // SymbolView
    8799      //
     
    90102      this.Controls.Add(this.initialFrequencyTextBox);
    91103      this.Controls.Add(this.initialFrequencyLabel);
     104      this.Controls.Add(this.checkBoxEnabled);
    92105      this.Name = "SymbolView";
    93       this.Size = new System.Drawing.Size(320, 51);
     106      this.Size = new System.Drawing.Size(320, 75);
     107      this.Controls.SetChildIndex(this.checkBoxEnabled, 0);
    94108      this.Controls.SetChildIndex(this.initialFrequencyLabel, 0);
    95109      this.Controls.SetChildIndex(this.initialFrequencyTextBox, 0);
     
    107121    protected System.Windows.Forms.Label initialFrequencyLabel;
    108122    protected System.Windows.Forms.TextBox initialFrequencyTextBox;
     123    private System.Windows.Forms.CheckBox checkBoxEnabled;
    109124
    110125  }
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolView.cs

    r6233 r6299  
    5757    protected override void SetEnabledStateOfControls() {
    5858      base.SetEnabledStateOfControls();
    59       initialFrequencyTextBox.Enabled = Content != null;
     59      initialFrequencyTextBox.Enabled = Content != null || Locked;
    6060      initialFrequencyTextBox.ReadOnly = ReadOnly;
     61      checkBoxEnabled.Enabled = Content != null || Locked || ReadOnly;
    6162    }
    6263
     
    9394      }
    9495    }
     96
     97    private void checkBoxEnabled_CheckedChanged(object sender, EventArgs e) {
     98      Content.Enabled = checkBoxEnabled.Checked;
     99    }
    95100    #endregion
    96101
     
    99104      if (Content == null) {
    100105        initialFrequencyTextBox.Text = string.Empty;
     106        checkBoxEnabled.Checked = false;
    101107      } else {
    102108        initialFrequencyTextBox.Text = Content.InitialFrequency.ToString();
     109        checkBoxEnabled.Checked = Content.Enabled;
    103110      }
    104111      SetEnabledStateOfControls();
    105112    }
    106113    #endregion
     114
    107115  }
    108116}
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarEditorView.cs

    r6285 r6299  
    100100    private void RebuildSymbolsTreeView() {
    101101      symbolsTreeView.Nodes.Clear();
    102       var start = new TreeNode();
    103       var symbols = new DummySymbol("Symbols");
    104       start.Name = "Symbols";
    105       start.Text = "Symbols";
    106       start.Tag = symbols;
    107       start.Checked = true;
    108       symbolsTreeView.Nodes.Add(start);
     102      var symbols = Content.Symbols.ToList();
     103      var groupSymbols = symbols.OfType<GroupSymbol>().ToList();
     104      var topLevelSymbols = Content.Symbols.Where(s => !groupSymbols.Any(g => g.Symbols.Contains(s)));
     105      AddChildTreeNodes(symbolsTreeView.Nodes, topLevelSymbols);
    109106
    110       foreach (ISymbol symbol in Content.Symbols.Where(s => !(s is IReadOnlySymbol))) {
     107      RebuildImageList();
     108      symbolsTreeView.ExpandAll();
     109    }
     110
     111    private void AddChildTreeNodes(TreeNodeCollection collection, IEnumerable<ISymbol> symbols) {
     112      foreach (ISymbol symbol in symbols) {
    111113        var node = new TreeNode();
    112114        node.Name = symbol.Name;
    113115        node.Text = symbol.Name;
    114116        node.Tag = symbol;
    115         node.Checked = !symbol.InitialFrequency.IsAlmost(0.0);
    116         start.Nodes.Add(node);
     117        node.Checked = symbol.Enabled;
     118        collection.Add(node);
     119        var groupSymbol = symbol as GroupSymbol;
     120        if (groupSymbol != null) AddChildTreeNodes(node.Nodes, groupSymbol.Symbols);
    117121      }
    118       RebuildImageList();
    119       symbolsTreeView.ExpandAll();
    120122    }
    121123
     
    134136      if (e.Action != TreeViewAction.Unknown) {
    135137        DeregisterContentEvents();
    136         foreach (var symbol in IterateTreeNodes(e.Node).Select(n => n.Tag).OfType<ISymbol>()) {
    137           if (e.Node.Checked) symbol.InitialFrequency = 1.0;
    138           else symbol.InitialFrequency = 0.0;
    139         }
     138        var symbol = (ISymbol)e.Node.Tag;
     139        symbol.Enabled = e.Node.Checked;
    140140        RegisterContentEvents();
    141141        Content_Changed(Content, EventArgs.Empty);
     
    176176    protected virtual void RebuildImageList() {
    177177      symbolsTreeView.ImageList.Images.Clear();
    178       foreach (TreeNode treeNode in symbolsTreeView.Nodes) {
     178      foreach (TreeNode treeNode in IterateTreeNodes()) {
    179179        var symbol = (ISymbol)treeNode.Tag;
    180180        symbolsTreeView.ImageList.Images.Add(symbol == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : symbol.ItemImage);
     
    183183    }
    184184  }
     185    #endregion
    185186
    186187  [NonDiscoverableType]
     
    190191    public override IDeepCloneable Clone(Cloner cloner) { return new DummySymbol(this, cloner); }
    191192  }
    192 
    193     #endregion
    194193}
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarView.cs

    r6233 r6299  
    9090          if (!(symbol is IReadOnlySymbol)) {
    9191            symbol.Changed += new EventHandler(symbol_Changed);
    92             symbols.Add(symbol, symbol.InitialFrequency > 0.0);
     92            symbols.Add(symbol, symbol.Enabled);
    9393          }
    9494        }
     
    101101    private void symbol_Changed(object sender, EventArgs e) {
    102102      ISymbol symbol = (ISymbol)sender;
    103       symbols.SetItemCheckedState(symbol, symbol.InitialFrequency > 0.0);
     103      symbols.SetItemCheckedState(symbol, symbol.Enabled);
    104104    }
    105105
    106106    private void symbols_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbol>> e) {
    107107      ICheckedItemList<ISymbol> checkedItemList = (ICheckedItemList<ISymbol>)sender;
    108       foreach (var indexedItem in e.Items) {
    109         if (checkedItemList.ItemChecked(indexedItem.Value)) {
    110           indexedItem.Value.InitialFrequency = 1.0;
    111         } else {
    112           indexedItem.Value.InitialFrequency = 0.0;
    113         }
    114       }
     108      foreach (var indexedItem in e.Items)
     109        indexedItem.Value.Enabled = checkedItemList.ItemChecked(indexedItem.Value);
    115110    }
    116111    private void ClearSymbols() {
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r6296 r6299  
    130130    #region protected grammar manipulation methods
    131131    protected void AddSymbol(ISymbol symbol) {
     132      AddSymbolToDictionaries(symbol);
     133      var groupSymbol = symbol as GroupSymbol;
     134      if (groupSymbol != null) {
     135        foreach (ISymbol s in groupSymbol.Flatten())
     136          AddSymbolToDictionaries(s);
     137      }
     138
     139      ClearCaches();
     140      OnChanged();
     141    }
     142    private void AddSymbolToDictionaries(ISymbol symbol) {
    132143      symbols.Add(symbol.Name, symbol);
    133144      symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0));
    134145      RegisterSymbolEvents(symbol);
    135 
     146    }
     147
     148    protected void RemoveSymbol(ISymbol symbol) {
     149      RemoveSymbolFromDictionaries(symbol);
    136150      var groupSymbol = symbol as GroupSymbol;
    137       if (groupSymbol != null) AddSymbol(groupSymbol);
     151      if (groupSymbol != null) {
     152        foreach (ISymbol s in groupSymbol.Flatten())
     153          RemoveSymbolFromDictionaries(s);
     154      }
    138155
    139156      ClearCaches();
    140157      OnChanged();
    141158    }
    142 
    143     private void AddSymbol(GroupSymbol groupSymbol) {
    144       foreach (ISymbol symbol in groupSymbol.Symbols) {
    145         symbols.Add(symbol.Name, symbol);
    146         symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0));
    147         RegisterSymbolEvents(symbol);
    148 
    149         var childGroup = symbol as GroupSymbol;
    150         if (childGroup != null) AddSymbol(childGroup);
    151       }
    152     }
    153 
    154     protected void RemoveSymbol(ISymbol symbol) {
     159    private void RemoveSymbolFromDictionaries(ISymbol symbol) {
    155160      symbols.Remove(symbol.Name);
    156161      allowedChildSymbols.Remove(symbol.Name);
     
    170175      }
    171176      DeregisterSymbolEvents(symbol);
    172       ClearCaches();
    173       OnChanged();
    174177    }
    175178
     
    181184
    182185    protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
     186      List<ISymbol> parents;
     187      List<ISymbol> childs;
     188      bool changed = false;
     189
     190      var parentGroup = parent as GroupSymbol;
     191      if (parentGroup != null) parents = parentGroup.Flatten().Where(p => !(p is GroupSymbol)).ToList();
     192      else parents = new List<ISymbol>() { parent };
     193      var childGroup = child as GroupSymbol;
     194      if (childGroup != null) childs = childGroup.Flatten().Where(c => !(c is GroupSymbol)).ToList();
     195      else childs = new List<ISymbol>() { child };
     196
     197      foreach (ISymbol p in parents) {
     198        foreach (ISymbol c in childs) {
     199          changed |= AddAllowedChildSymbolToDictionaries(p, c);
     200        }
     201      }
     202
     203      if (changed) {
     204        ClearCaches();
     205        OnChanged();
     206      }
     207    }
     208
     209    private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child) {
     210      List<string> childSymbols;
     211      if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
     212        childSymbols = new List<string>();
     213        allowedChildSymbols.Add(parent.Name, childSymbols);
     214      }
     215      if (childSymbols.Contains(child.Name)) return false;
     216
    183217      suppressEvents = true;
    184218      for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++)
     
    186220      suppressEvents = false;
    187221
    188       List<string> childSymbols;
    189       if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
    190         childSymbols = new List<string>();
    191         allowedChildSymbols.Add(parent.Name, childSymbols);
    192       }
    193 
    194       if (!childSymbols.Contains(child.Name)) {
    195         childSymbols.Add(child.Name);
     222      childSymbols.Add(child.Name);
     223      return true;
     224    }
     225
     226    protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     227      List<ISymbol> parents;
     228      List<ISymbol> childs;
     229      bool changed = false;
     230
     231      var parentGroup = parent as GroupSymbol;
     232      if (parentGroup != null) parents = parentGroup.Flatten().Where(p => !(p is GroupSymbol)).ToList();
     233      else parents = new List<ISymbol>() { parent };
     234      var childGroup = child as GroupSymbol;
     235      if (childGroup != null) childs = childGroup.Flatten().Where(c => !(c is GroupSymbol)).ToList();
     236      else childs = new List<ISymbol>() { child };
     237
     238      foreach (ISymbol p in parents) {
     239        foreach (ISymbol c in childs) {
     240          changed |= AddAllowedChildSymbolToDictionaries(p, c, argumentIndex);
     241        }
     242      }
     243
     244      if (changed) {
    196245        ClearCaches();
    197246        OnChanged();
     
    199248    }
    200249
    201     protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     250
     251    private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child, int argumentIndex) {
    202252      List<string> childSymbols;
    203253      if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols))
    204         if (childSymbols.Contains(child.Name)) return;
     254        if (childSymbols.Contains(child.Name)) return false;
    205255
    206256      var key = Tuple.Create(parent.Name, argumentIndex);
     
    209259        allowedChildSymbolsPerIndex.Add(key, childSymbols);
    210260      }
    211 
    212       if (!childSymbols.Contains(child.Name)) {
    213         childSymbols.Add(child.Name);
    214         ClearCaches();
    215         OnChanged();
    216       }
     261      if (childSymbols.Contains(child.Name)) return false;
     262
     263      childSymbols.Add(child.Name);
     264      return true;
    217265    }
    218266
     
    221269      List<string> childSymbols;
    222270      if (allowedChildSymbols.TryGetValue(child.Name, out childSymbols)) {
    223         if (allowedChildSymbols[parent.Name].Remove(child.Name)) changed = true;
     271        changed |= childSymbols.Remove(child.Name);
    224272      }
    225273
    226274      for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) {
    227275        var key = Tuple.Create(parent.Name, argumentIndex);
    228         if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) {
    229           if (allowedChildSymbolsPerIndex[key].Remove(child.Name)) changed = true;
    230         }
     276        if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols))
     277          changed |= childSymbols.Remove(child.Name);
    231278      }
    232279
     
    240287      bool changed = false;
    241288
     289      suppressEvents = true;
    242290      List<string> childSymbols;
    243291      if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
    244292        if (childSymbols.Remove(child.Name)) {
    245           suppressEvents = true;
    246293          for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
    247294            if (i != argumentIndex) AddAllowedChildSymbol(parent, child, i);
    248295          }
    249           suppressEvents = false;
    250296          changed = true;
    251297        }
    252298      }
     299      suppressEvents = false;
    253300
    254301      var key = Tuple.Create(parent.Name, argumentIndex);
    255       if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) {
    256         if (allowedChildSymbolsPerIndex[key].Remove(child.Name))
    257           changed = true;
    258       }
     302      if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols))
     303        changed |= childSymbols.Remove(child.Name);
    259304
    260305      if (changed) {
     
    265310
    266311    protected void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
     312      var groupSymbol = symbol as GroupSymbol;
     313      if (groupSymbol != null)
     314        foreach (ISymbol s in groupSymbol.Flatten().Where(s => !(s is GroupSymbol)))
     315          SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount);
     316      else
     317        SetSubTreeCountInDictionaries(symbol, minimumSubtreeCount, maximumSubtreeCount);
     318
     319      ClearCaches();
     320      OnChanged();
     321    }
     322
     323    private void SetSubTreeCountInDictionaries(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
    267324      for (int i = GetMaximumSubtreeCount(symbol) - 1; i >= maximumSubtreeCount; i--) {
    268325        var key = Tuple.Create(symbol.Name, i);
     
    271328
    272329      symbolSubtreeCount[symbol.Name] = Tuple.Create(minimumSubtreeCount, maximumSubtreeCount);
    273       ClearCaches();
    274       OnChanged();
    275330    }
    276331    #endregion
     
    291346      List<string> temp;
    292347      if (allowedChildSymbols.TryGetValue(parent.Name, out temp))
    293         if (temp.Contains(child.Name)) return true;
     348        return temp.Contains(child.Name);
    294349      return false;
    295350    }
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/GroupSymbol.cs

    r6296 r6299  
    5959      InitialFrequency = 0.0;
    6060    }
     61
     62    public IEnumerable<ISymbol> Flatten() {
     63      return symbols.Union(symbols.OfType<GroupSymbol>().SelectMany(g => g.Flatten()));
     64    }
    6165  }
    6266}
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Symbol.cs

    r6296 r6299  
    3838        if (value != initialFrequency) {
    3939          initialFrequency = value;
    40           if (initialFrequency.IsAlmost(0.0)) enabled = false;
    4140          OnChanged(EventArgs.Empty);
    4241        }
Note: See TracChangeset for help on using the changeset viewer.