Changeset 6620 for branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
- Timestamp:
- 08/02/11 10:41:13 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.