Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/17/11 22:51:11 (13 years ago)
Author:
abeham
Message:

#1541

  • updated to latest trunk version
Location:
branches/QAPAlgorithms
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/QAPAlgorithms

  • branches/QAPAlgorithms/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs

    r6233 r6569  
    2121
    2222using System;
     23using System.Linq;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    2526using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     27using System.Collections.Generic;
    2628
    2729namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     
    7274    }
    7375
    74     [Storable]
    7576    private ProgramRootSymbol programRootSymbol;
    7677    public ProgramRootSymbol ProgramRootSymbol {
     
    99100    }
    100101
    101     [Storable]
    102102    private Defun defunSymbol;
    103103    protected Defun DefunSymbol {
     
    111111    #endregion
    112112
     113    [StorableHook(HookType.AfterDeserialization)]
     114    private void AfterDeserialization() {
     115      foreach (ISymbol symbol in symbols.Values)
     116        RegisterSymbolEvents(symbol);
     117    }
    113118    [StorableConstructor]
    114119    protected SymbolicExpressionGrammar(bool deserializing) : base(deserializing) { }
    115120    protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner)
    116121      : base(original, cloner) {
     122      foreach (ISymbol symbol in Symbols)
     123        RegisterSymbolEvents(symbol);
     124
    117125      programRootSymbol = cloner.Clone(original.programRootSymbol);
    118126      startSymbol = cloner.Clone(original.StartSymbol);
     
    153161    }
    154162
     163    protected override void AddSymbol(ISymbol symbol) {
     164      base.AddSymbol(symbol);
     165      RegisterSymbolEvents(symbol);
     166    }
     167    protected override void RemoveSymbol(ISymbol symbol) {
     168      DeregisterSymbolEvents(symbol);
     169      base.RemoveSymbol(symbol);
     170    }
     171
    155172    public event EventHandler ReadOnlyChanged;
    156173    protected virtual void OnReadOnlyChanged() {
     
    166183    }
    167184    #endregion
     185
     186    #region symbol events
     187    protected virtual void RegisterSymbolEvents(ISymbol symbol) {
     188      symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
     189      symbol.NameChanged += new EventHandler(Symbol_NameChanged);
     190    }
     191    protected virtual void DeregisterSymbolEvents(ISymbol symbol) {
     192      symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
     193      symbol.NameChanged -= new EventHandler(Symbol_NameChanged);
     194    }
     195
     196    private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) {
     197      if (symbols.ContainsKey(e.Value)) e.Cancel = true;
     198    }
     199    private void Symbol_NameChanged(object sender, EventArgs e) {
     200      ISymbol symbol = (ISymbol)sender;
     201      string oldName = symbols.Where(x => x.Value == symbol).First().Key;
     202      string newName = symbol.Name;
     203
     204      symbols.Remove(oldName);
     205      symbols.Add(newName, symbol);
     206
     207      var subtreeCount = symbolSubtreeCount[oldName];
     208      symbolSubtreeCount.Remove(oldName);
     209      symbolSubtreeCount.Add(newName, subtreeCount);
     210
     211      List<string> allowedChilds;
     212      if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) {
     213        allowedChildSymbols.Remove(oldName);
     214        allowedChildSymbols.Add(newName, allowedChilds);
     215      }
     216
     217      for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) {
     218        if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) {
     219          allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i));
     220          allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds);
     221        }
     222      }
     223
     224      foreach (var parent in Symbols) {
     225        if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
     226          if (allowedChilds.Remove(oldName))
     227            allowedChilds.Add(newName);
     228
     229        for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
     230          if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
     231            if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName);
     232        }
     233      }
     234
     235      ClearCaches();
     236    }
     237    #endregion
    168238  }
    169239}
Note: See TracChangeset for help on using the changeset viewer.