- Timestamp:
- 06/19/11 21:32:06 (13 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs
r6233 r6443 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 { … … 111 113 #endregion 112 114 115 [StorableHook(HookType.AfterDeserialization)] 116 private void AfterDeserialization() { 117 foreach (ISymbol symbol in symbols.Values) 118 RegisterSymbolEvents(symbol); 119 } 113 120 [StorableConstructor] 114 121 protected SymbolicExpressionGrammar(bool deserializing) : base(deserializing) { } 115 122 protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner) 116 123 : base(original, cloner) { 124 foreach (ISymbol symbol in Symbols) 125 RegisterSymbolEvents(symbol); 126 117 127 programRootSymbol = cloner.Clone(original.programRootSymbol); 118 128 startSymbol = cloner.Clone(original.StartSymbol); … … 153 163 } 154 164 165 protected override void AddSymbol(ISymbol symbol) { 166 base.AddSymbol(symbol); 167 RegisterSymbolEvents(symbol); 168 } 169 protected override void RemoveSymbol(ISymbol symbol) { 170 DeregisterSymbolEvents(symbol); 171 base.RemoveSymbol(symbol); 172 } 173 155 174 public event EventHandler ReadOnlyChanged; 156 175 protected virtual void OnReadOnlyChanged() { … … 166 185 } 167 186 #endregion 187 188 #region symbol events 189 protected virtual void RegisterSymbolEvents(ISymbol symbol) { 190 symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 191 symbol.NameChanged += new EventHandler(Symbol_NameChanged); 192 } 193 protected virtual void DeregisterSymbolEvents(ISymbol symbol) { 194 symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 195 symbol.NameChanged -= new EventHandler(Symbol_NameChanged); 196 } 197 198 private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) { 199 if (symbols.ContainsKey(e.Value)) e.Cancel = true; 200 } 201 private void Symbol_NameChanged(object sender, EventArgs e) { 202 ISymbol symbol = (ISymbol)sender; 203 string oldName = symbols.Where(x => x.Value == symbol).First().Key; 204 string newName = symbol.Name; 205 206 symbols.Remove(oldName); 207 symbols.Add(newName, symbol); 208 209 var subtreeCount = symbolSubtreeCount[oldName]; 210 symbolSubtreeCount.Remove(oldName); 211 symbolSubtreeCount.Add(newName, subtreeCount); 212 213 List<string> allowedChilds; 214 if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) { 215 allowedChildSymbols.Remove(oldName); 216 allowedChildSymbols.Add(newName, allowedChilds); 217 } 218 219 for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) { 220 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) { 221 allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i)); 222 allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds); 223 } 224 } 225 226 foreach (var parent in Symbols) { 227 if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds)) 228 if (allowedChilds.Remove(oldName)) 229 allowedChilds.Add(newName); 230 231 for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { 232 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds)) 233 if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName); 234 } 235 } 236 237 ClearCaches(); 238 } 239 #endregion 168 240 } 169 241 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r6233 r6443 81 81 cachedMinExpressionDepth = new Dictionary<string, int>(); 82 82 } 83 [StorableHook(HookType.AfterDeserialization)]84 private void AfterDeserialization() {85 foreach (ISymbol symbol in symbols.Values)86 RegisterSymbolEvents(symbol);87 }88 83 89 84 protected SymbolicExpressionGrammarBase(SymbolicExpressionGrammarBase original, Cloner cloner) … … 93 88 cachedMinExpressionDepth = new Dictionary<string, int>(); 94 89 95 96 90 symbols = original.symbols.ToDictionary(x => x.Key, y => (ISymbol)cloner.Clone(y.Value)); 97 91 symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubtreeCount); … … 104 98 foreach (var element in original.allowedChildSymbolsPerIndex) 105 99 allowedChildSymbolsPerIndex.Add(element.Key, new List<string>(element.Value)); 106 107 foreach (ISymbol symbol in symbols.Values)108 RegisterSymbolEvents(symbol);109 100 } 110 101 … … 122 113 123 114 #region protected grammar manipulation methods 124 protected v oid AddSymbol(ISymbol symbol) {115 protected virtual void AddSymbol(ISymbol symbol) { 125 116 if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined."); 126 RegisterSymbolEvents(symbol);127 117 symbols.Add(symbol.Name, symbol); 128 118 symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0)); … … 130 120 } 131 121 132 private void RegisterSymbolEvents(ISymbol symbol) { 133 symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 134 symbol.NameChanged += new EventHandler(Symbol_NameChanged); 135 } 136 private void DeregisterSymbolEvents(ISymbol symbol) { 137 symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 138 symbol.NameChanged -= new EventHandler(Symbol_NameChanged); 139 } 140 141 private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) { 142 if (symbols.ContainsKey(e.Value)) e.Cancel = true; 143 } 144 private void Symbol_NameChanged(object sender, EventArgs e) { 145 ISymbol symbol = (ISymbol)sender; 146 string oldName = symbols.Where(x => x.Value == symbol).First().Key; 147 string newName = symbol.Name; 148 149 symbols.Remove(oldName); 150 symbols.Add(newName, symbol); 151 152 var subtreeCount = symbolSubtreeCount[oldName]; 153 symbolSubtreeCount.Remove(oldName); 154 symbolSubtreeCount.Add(newName, subtreeCount); 155 156 List<string> allowedChilds; 157 if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) { 158 allowedChildSymbols.Remove(oldName); 159 allowedChildSymbols.Add(newName, allowedChilds); 160 } 161 162 for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) { 163 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) { 164 allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i)); 165 allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds); 166 } 167 } 168 169 foreach (var parent in Symbols) { 170 if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds)) 171 if (allowedChilds.Remove(oldName)) 172 allowedChilds.Add(newName); 173 174 for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { 175 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds)) 176 if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName); 177 } 178 } 179 180 ClearCaches(); 181 } 182 183 protected void RemoveSymbol(ISymbol symbol) { 122 protected virtual void RemoveSymbol(ISymbol symbol) { 184 123 symbols.Remove(symbol.Name); 185 124 allowedChildSymbols.Remove(symbol.Name); … … 198 137 } 199 138 } 200 DeregisterSymbolEvents(symbol);201 139 ClearCaches(); 202 140 } … … 315 253 316 254 317 pr ivatevoid ClearCaches() {255 protected void ClearCaches() { 318 256 cachedMinExpressionLength.Clear(); 319 257 cachedMaxExpressionLength.Clear();
Note: See TracChangeset
for help on using the changeset viewer.