Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6620 for branches


Ignore:
Timestamp:
08/02/11 10:41:13 (13 years ago)
Author:
mkommend
Message:

#1479: Corrected changes from trunk integration.

Location:
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs

    r6618 r6620  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
     25using HeuristicLab.Collections;
    2426using HeuristicLab.Common;
    2527using HeuristicLab.Core;
    2628using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    27 using System.Collections.Generic;
    2829
    2930namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     
    120121    protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner)
    121122      : base(original, cloner) {
    122       foreach (ISymbol symbol in Symbols)
     123      foreach (ISymbol symbol in symbols.Values)
    123124        RegisterSymbolEvents(symbol);
    124125
     
    161162    }
    162163
    163     protected override void AddSymbol(ISymbol symbol) {
     164    protected override sealed void AddSymbol(ISymbol symbol) {
    164165      base.AddSymbol(symbol);
    165166      RegisterSymbolEvents(symbol);
    166     }
    167     protected override void RemoveSymbol(ISymbol symbol) {
     167      OnChanged();
     168    }
     169    protected override sealed void RemoveSymbol(ISymbol symbol) {
    168170      DeregisterSymbolEvents(symbol);
    169171      base.RemoveSymbol(symbol);
     172      OnChanged();
    170173    }
    171174
     
    231234
    232235    #region symbol events
    233     protected virtual void RegisterSymbolEvents(ISymbol symbol) {
     236    private void RegisterSymbolEvents(ISymbol symbol) {
    234237      symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
    235238      symbol.NameChanged += new EventHandler(Symbol_NameChanged);
    236     }
    237     protected virtual void DeregisterSymbolEvents(ISymbol symbol) {
     239
     240      var groupSymbol = symbol as GroupSymbol;
     241      if (groupSymbol != null) RegisterGroupSymbolEvents(groupSymbol);
     242      else symbol.Changed += new EventHandler(Symbol_Changed);
     243    }
     244    private void DeregisterSymbolEvents(ISymbol symbol) {
    238245      symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
    239246      symbol.NameChanged -= new EventHandler(Symbol_NameChanged);
     247
     248      var groupSymbol = symbol as GroupSymbol;
     249      if (groupSymbol != null) DeregisterGroupSymbolEvents(groupSymbol);
     250      else symbol.Changed -= new EventHandler(Symbol_Changed);
     251    }
     252
     253    private void RegisterGroupSymbolEvents(GroupSymbol groupSymbol) {
     254      groupSymbol.Changed += new EventHandler(GroupSymbol_Changed);
     255      groupSymbol.SymbolsCollection.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded);
     256      groupSymbol.SymbolsCollection.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved);
     257      groupSymbol.SymbolsCollection.CollectionReset += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_CollectionReset);
     258    }
     259    private void DeregisterGroupSymbolEvents(GroupSymbol groupSymbol) {
     260      groupSymbol.Changed -= new EventHandler(GroupSymbol_Changed);
     261      groupSymbol.SymbolsCollection.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded);
     262      groupSymbol.SymbolsCollection.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved);
     263      groupSymbol.SymbolsCollection.CollectionReset -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_CollectionReset);
     264    }
     265
     266    private void Symbol_Changed(object sender, EventArgs e) {
     267      if (suppressEvents) return;
     268      ClearCaches();
     269      OnChanged();
     270    }
     271
     272    private void GroupSymbol_Changed(object sender, EventArgs e) {
     273      if (suppressEvents) return;
     274      suppressEvents = true;
     275      GroupSymbol groupSymbol = (GroupSymbol)sender;
     276      foreach (ISymbol symbol in groupSymbol.Flatten())
     277        symbol.Enabled = groupSymbol.Enabled;
     278      suppressEvents = false;
     279      ClearCaches();
     280      OnChanged();
    240281    }
    241282
     
    280321
    281322      ClearCaches();
     323      OnChanged();
     324    }
     325
     326    private void GroupSymbol_ItemsAdded(object sender, CollectionItemsChangedEventArgs<ISymbol> e) {
     327      suppressEvents = true;
     328      foreach (ISymbol symbol in e.Items)
     329        if (!ContainsSymbol(symbol))
     330          AddSymbol(symbol);
     331      suppressEvents = false;
     332      OnChanged();
     333    }
     334    private void GroupSymbol_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ISymbol> e) {
     335      suppressEvents = true;
     336      foreach (ISymbol symbol in e.Items)
     337        if (ContainsSymbol(symbol))
     338          RemoveSymbol(symbol);
     339      suppressEvents = false;
     340      OnChanged();
     341    }
     342    private void GroupSymbol_CollectionReset(object sender, CollectionItemsChangedEventArgs<ISymbol> e) {
     343      suppressEvents = true;
     344      foreach (ISymbol symbol in e.Items)
     345        if (!ContainsSymbol(symbol))
     346          AddSymbol(symbol);
     347      foreach (ISymbol symbol in e.OldItems)
     348        if (ContainsSymbol(symbol))
     349          RemoveSymbol(symbol);
     350      suppressEvents = false;
     351      OnChanged();
    282352    }
    283353    #endregion
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r6618 r6620  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using HeuristicLab.Collections;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Core;
     
    127126      symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0));
    128127      ClearCaches();
    129       OnChanged();
    130128    }
    131129
     
    265263      var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol));
    266264      if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
    267       if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
     265      if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid maximum subtree count " + maximumSubtreeCount + " for " + symbol);
    268266
    269267      foreach (ISymbol s in symbols)
     
    398396    }
    399397
    400     #region events
    401     private void RegisterSymbolEvents(ISymbol symbol) {
    402       symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
    403       symbol.NameChanged += new EventHandler(Symbol_NameChanged);
    404 
    405       var groupSymbol = symbol as GroupSymbol;
    406       if (groupSymbol != null) RegisterGroupSymbolEvents(groupSymbol);
    407       else symbol.Changed += new EventHandler(Symbol_Changed);
    408     }
    409     private void DeregisterSymbolEvents(ISymbol symbol) {
    410       symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
    411       symbol.NameChanged -= new EventHandler(Symbol_NameChanged);
    412 
    413       var groupSymbol = symbol as GroupSymbol;
    414       if (groupSymbol != null) DeregisterGroupSymbolEvents(groupSymbol);
    415       else symbol.Changed -= new EventHandler(Symbol_Changed);
    416     }
    417 
    418     private void RegisterGroupSymbolEvents(GroupSymbol groupSymbol) {
    419       groupSymbol.Changed += new EventHandler(GroupSymbol_Changed);
    420       groupSymbol.SymbolsCollection.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded);
    421       groupSymbol.SymbolsCollection.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved);
    422       groupSymbol.SymbolsCollection.CollectionReset += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_CollectionReset);
    423     }
    424     private void DeregisterGroupSymbolEvents(GroupSymbol groupSymbol) {
    425       groupSymbol.Changed -= new EventHandler(GroupSymbol_Changed);
    426       groupSymbol.SymbolsCollection.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded);
    427       groupSymbol.SymbolsCollection.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved);
    428       groupSymbol.SymbolsCollection.CollectionReset -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_CollectionReset);
    429     }
    430 
    431     private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) {
    432       if (symbols.ContainsKey(e.Value)) e.Cancel = true;
    433     }
    434     private void Symbol_NameChanged(object sender, EventArgs e) {
    435       ISymbol symbol = (ISymbol)sender;
    436       string oldName = symbols.Where(x => x.Value == symbol).First().Key;
    437       string newName = symbol.Name;
    438 
    439       symbols.Remove(oldName);
    440       symbols.Add(newName, symbol);
    441 
    442       var subtreeCount = symbolSubtreeCount[oldName];
    443       symbolSubtreeCount.Remove(oldName);
    444       symbolSubtreeCount.Add(newName, subtreeCount);
    445 
    446       List<string> allowedChilds;
    447       if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) {
    448         allowedChildSymbols.Remove(oldName);
    449         allowedChildSymbols.Add(newName, allowedChilds);
    450       }
    451 
    452       for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) {
    453         if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) {
    454           allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i));
    455           allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds);
    456         }
    457       }
    458 
    459       foreach (var parent in Symbols) {
    460         if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
    461           if (allowedChilds.Remove(oldName))
    462             allowedChilds.Add(newName);
    463 
    464         for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
    465           if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
    466             if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName);
    467         }
    468       }
    469 
    470       ClearCaches();
    471       OnChanged();
    472     }
    473 
    474     private void Symbol_Changed(object sender, EventArgs e) {
    475       if (suppressEvents) return;
    476       ClearCaches();
    477       OnChanged();
    478     }
    479 
    480     private void GroupSymbol_Changed(object sender, EventArgs e) {
    481       if (suppressEvents) return;
    482       suppressEvents = true;
    483       GroupSymbol groupSymbol = (GroupSymbol)sender;
    484       foreach (ISymbol symbol in groupSymbol.Flatten())
    485         symbol.Enabled = groupSymbol.Enabled;
    486       suppressEvents = false;
    487       ClearCaches();
    488       OnChanged();
    489     }
    490 
    491     private void GroupSymbol_ItemsAdded(object sender, CollectionItemsChangedEventArgs<ISymbol> e) {
    492       suppressEvents = true;
    493       foreach (ISymbol symbol in e.Items)
    494         if (!ContainsSymbol(symbol))
    495           AddSymbol(symbol);
    496       suppressEvents = false;
    497       OnChanged();
    498     }
    499     private void GroupSymbol_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ISymbol> e) {
    500       suppressEvents = true;
    501       foreach (ISymbol symbol in e.Items)
    502         if (ContainsSymbol(symbol))
    503           RemoveSymbol(symbol);
    504       suppressEvents = false;
    505       OnChanged();
    506     }
    507     private void GroupSymbol_CollectionReset(object sender, CollectionItemsChangedEventArgs<ISymbol> e) {
    508       suppressEvents = true;
    509       foreach (ISymbol symbol in e.Items)
    510         if (!ContainsSymbol(symbol))
    511           AddSymbol(symbol);
    512       foreach (ISymbol symbol in e.OldItems)
    513         if (ContainsSymbol(symbol))
    514           RemoveSymbol(symbol);
    515       suppressEvents = false;
    516       OnChanged();
    517     }
    518 
    519398    public event EventHandler Changed;
    520399    protected virtual void OnChanged() {
     
    523402      if (handler != null) Changed(this, EventArgs.Empty);
    524403    }
    525     #endregion
    526404  }
    527405}
Note: See TracChangeset for help on using the changeset viewer.