Changeset 6569 for branches/QAPAlgorithms/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs
- Timestamp:
- 07/17/11 22:51:11 (13 years ago)
- Location:
- branches/QAPAlgorithms
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/QAPAlgorithms
- Property svn:ignore
-
old new 12 12 *.psess 13 13 *.vsp 14 *.docstates
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/QAPAlgorithms/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs
r6233 r6569 21 21 22 22 using System; 23 using System.Linq; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using System.Collections.Generic; 26 28 27 29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 72 74 } 73 75 74 [Storable]75 76 private ProgramRootSymbol programRootSymbol; 76 77 public ProgramRootSymbol ProgramRootSymbol { … … 99 100 } 100 101 101 [Storable]102 102 private Defun defunSymbol; 103 103 protected Defun DefunSymbol { … … 111 111 #endregion 112 112 113 [StorableHook(HookType.AfterDeserialization)] 114 private void AfterDeserialization() { 115 foreach (ISymbol symbol in symbols.Values) 116 RegisterSymbolEvents(symbol); 117 } 113 118 [StorableConstructor] 114 119 protected SymbolicExpressionGrammar(bool deserializing) : base(deserializing) { } 115 120 protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner) 116 121 : base(original, cloner) { 122 foreach (ISymbol symbol in Symbols) 123 RegisterSymbolEvents(symbol); 124 117 125 programRootSymbol = cloner.Clone(original.programRootSymbol); 118 126 startSymbol = cloner.Clone(original.StartSymbol); … … 153 161 } 154 162 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 155 172 public event EventHandler ReadOnlyChanged; 156 173 protected virtual void OnReadOnlyChanged() { … … 166 183 } 167 184 #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 168 238 } 169 239 }
Note: See TracChangeset
for help on using the changeset viewer.