- Timestamp:
- 08/02/11 10:41:13 (13 years ago)
- 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 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 25 using HeuristicLab.Collections; 24 26 using HeuristicLab.Common; 25 27 using HeuristicLab.Core; 26 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using System.Collections.Generic;28 29 29 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 120 121 protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner) 121 122 : base(original, cloner) { 122 foreach (ISymbol symbol in Symbols)123 foreach (ISymbol symbol in symbols.Values) 123 124 RegisterSymbolEvents(symbol); 124 125 … … 161 162 } 162 163 163 protected override void AddSymbol(ISymbol symbol) {164 protected override sealed void AddSymbol(ISymbol symbol) { 164 165 base.AddSymbol(symbol); 165 166 RegisterSymbolEvents(symbol); 166 } 167 protected override void RemoveSymbol(ISymbol symbol) { 167 OnChanged(); 168 } 169 protected override sealed void RemoveSymbol(ISymbol symbol) { 168 170 DeregisterSymbolEvents(symbol); 169 171 base.RemoveSymbol(symbol); 172 OnChanged(); 170 173 } 171 174 … … 231 234 232 235 #region symbol events 233 pr otected virtualvoid RegisterSymbolEvents(ISymbol symbol) {236 private void RegisterSymbolEvents(ISymbol symbol) { 234 237 symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 235 238 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) { 238 245 symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 239 246 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(); 240 281 } 241 282 … … 280 321 281 322 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(); 282 352 } 283 353 #endregion -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r6618 r6620 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.Collections;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; … … 127 126 symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0)); 128 127 ClearCaches(); 129 OnChanged();130 128 } 131 129 … … 265 263 var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol)); 266 264 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 m inimum subtree count " + minimumSubtreeCount + " for " + symbol);265 if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid maximum subtree count " + maximumSubtreeCount + " for " + symbol); 268 266 269 267 foreach (ISymbol s in symbols) … … 398 396 } 399 397 400 #region events401 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 519 398 public event EventHandler Changed; 520 399 protected virtual void OnChanged() { … … 523 402 if (handler != null) Changed(this, EventArgs.Empty); 524 403 } 525 #endregion526 404 } 527 405 }
Note: See TracChangeset
for help on using the changeset viewer.