Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs @ 6618

Last change on this file since 6618 was 6618, checked in by mkommend, 13 years ago

#1479: Integrated trunk changes.

File size: 22.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
31  /// <summary>
32  /// The default symbolic expression grammar stores symbols and syntactic constraints for symbols.
33  /// Symbols are treated as equvivalent if they have the same name.
34  /// Syntactic constraints limit the number of allowed sub trees for a node with a symbol and which symbols are allowed
35  /// in the sub-trees of a symbol (can be specified for each sub-tree index separately).
36  /// </summary>
37  [StorableClass]
38  public abstract class SymbolicExpressionGrammarBase : NamedItem, ISymbolicExpressionGrammarBase {
39
40    #region properties for separation between implementation and persistence
41    [Storable(Name = "Symbols")]
42    private IEnumerable<ISymbol> StorableSymbols {
43      get { return symbols.Values.ToArray(); }
44      set { symbols = value.ToDictionary(sym => sym.Name); }
45    }
46
47    [Storable(Name = "SymbolSubtreeCount")]
48    private IEnumerable<KeyValuePair<ISymbol, Tuple<int, int>>> StorableSymbolSubtreeCount {
49      get { return symbolSubtreeCount.Select(x => new KeyValuePair<ISymbol, Tuple<int, int>>(GetSymbol(x.Key), x.Value)).ToArray(); }
50      set { symbolSubtreeCount = value.ToDictionary(x => x.Key.Name, x => x.Value); }
51    }
52
53    [Storable(Name = "AllowedChildSymbols")]
54    private IEnumerable<KeyValuePair<ISymbol, IEnumerable<ISymbol>>> StorableAllowedChildSymbols {
55      get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select(y => GetSymbol(y)).ToArray())).ToArray(); }
56      set { allowedChildSymbols = value.ToDictionary(x => x.Key.Name, x => x.Value.Select(y => y.Name).ToList()); }
57    }
58
59    [Storable(Name = "AllowedChildSymbolsPerIndex")]
60    private IEnumerable<KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>> StorableAllowedChildSymbolsPerIndex {
61      get { return allowedChildSymbolsPerIndex.Select(x => new KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>(Tuple.Create<ISymbol, int>(GetSymbol(x.Key.Item1), x.Key.Item2), x.Value.Select(y => GetSymbol(y)).ToArray())).ToArray(); }
62      set { allowedChildSymbolsPerIndex = value.ToDictionary(x => Tuple.Create(x.Key.Item1.Name, x.Key.Item2), x => x.Value.Select(y => y.Name).ToList()); }
63    }
64    #endregion
65
66    private bool suppressEvents;
67    protected Dictionary<string, ISymbol> symbols;
68    protected Dictionary<string, Tuple<int, int>> symbolSubtreeCount;
69    protected Dictionary<string, List<string>> allowedChildSymbols;
70    protected Dictionary<Tuple<string, int>, List<string>> allowedChildSymbolsPerIndex;
71
72    public override bool CanChangeName {
73      get { return false; }
74    }
75    public override bool CanChangeDescription {
76      get { return false; }
77    }
78
79    [StorableConstructor]
80    protected SymbolicExpressionGrammarBase(bool deserializing)
81      : base(deserializing) {
82      cachedMinExpressionLength = new Dictionary<string, int>();
83      cachedMaxExpressionLength = new Dictionary<string, int>();
84      cachedMinExpressionDepth = new Dictionary<string, int>();
85
86      suppressEvents = false;
87    }
88
89    protected SymbolicExpressionGrammarBase(SymbolicExpressionGrammarBase original, Cloner cloner)
90      : base(original, cloner) {
91      cachedMinExpressionLength = new Dictionary<string, int>();
92      cachedMaxExpressionLength = new Dictionary<string, int>();
93      cachedMinExpressionDepth = new Dictionary<string, int>();
94
95      symbols = original.symbols.ToDictionary(x => x.Key, y => (ISymbol)cloner.Clone(y.Value));
96      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubtreeCount);
97
98      allowedChildSymbols = new Dictionary<string, List<string>>();
99      foreach (var element in original.allowedChildSymbols)
100        allowedChildSymbols.Add(element.Key, new List<string>(element.Value));
101
102      allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>();
103      foreach (var element in original.allowedChildSymbolsPerIndex)
104        allowedChildSymbolsPerIndex.Add(element.Key, new List<string>(element.Value));
105
106      suppressEvents = false;
107    }
108
109    protected SymbolicExpressionGrammarBase(string name, string description)
110      : base(name, description) {
111      cachedMinExpressionLength = new Dictionary<string, int>();
112      cachedMaxExpressionLength = new Dictionary<string, int>();
113      cachedMinExpressionDepth = new Dictionary<string, int>();
114
115      symbols = new Dictionary<string, ISymbol>();
116      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>();
117      allowedChildSymbols = new Dictionary<string, List<string>>();
118      allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>();
119
120      suppressEvents = false;
121    }
122
123    #region protected grammar manipulation methods
124    protected virtual void AddSymbol(ISymbol symbol) {
125      if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
126      symbols.Add(symbol.Name, symbol);
127      symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0));
128      ClearCaches();
129      OnChanged();
130    }
131
132    protected virtual void RemoveSymbol(ISymbol symbol) {
133      symbols.Remove(symbol.Name);
134      allowedChildSymbols.Remove(symbol.Name);
135      for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++)
136        allowedChildSymbolsPerIndex.Remove(Tuple.Create(symbol.Name, i));
137      symbolSubtreeCount.Remove(symbol.Name);
138
139      foreach (var parent in Symbols) {
140        List<string> allowedChilds;
141        if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
142          allowedChilds.Remove(symbol.Name);
143
144        for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
145          if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
146            allowedChilds.Remove(symbol.Name);
147        }
148      }
149    }
150
151    public virtual ISymbol GetSymbol(string symbolName) {
152      ISymbol symbol;
153      if (symbols.TryGetValue(symbolName, out symbol)) return symbol;
154      return null;
155    }
156
157    protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
158      bool changed = false;
159
160      foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol)))
161        changed |= AddAllowedChildSymbolToDictionaries(p, child);
162
163      if (changed) {
164        ClearCaches();
165        OnChanged();
166      }
167    }
168
169    private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child) {
170      List<string> childSymbols;
171      if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
172        childSymbols = new List<string>();
173        allowedChildSymbols.Add(parent.Name, childSymbols);
174      }
175      if (childSymbols.Contains(child.Name)) return false;
176
177      suppressEvents = true;
178      for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++)
179        RemoveAllowedChildSymbol(parent, child, argumentIndex);
180      suppressEvents = false;
181
182      childSymbols.Add(child.Name);
183      return true;
184    }
185
186    protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
187      bool changed = false;
188
189      foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol)))
190        changed |= AddAllowedChildSymbolToDictionaries(p, child, argumentIndex);
191
192      if (changed) {
193        ClearCaches();
194        OnChanged();
195      }
196    }
197
198
199    private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child, int argumentIndex) {
200      List<string> childSymbols;
201      if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
202        childSymbols = new List<string>();
203        allowedChildSymbols.Add(parent.Name, childSymbols);
204      }
205      if (childSymbols.Contains(child.Name)) return false;
206
207
208      var key = Tuple.Create(parent.Name, argumentIndex);
209      if (!allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) {
210        childSymbols = new List<string>();
211        allowedChildSymbolsPerIndex.Add(key, childSymbols);
212      }
213
214      if (childSymbols.Contains(child.Name)) return false;
215
216      childSymbols.Add(child.Name);
217      return true;
218    }
219
220    protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {
221      bool changed = false;
222      List<string> childSymbols;
223      if (allowedChildSymbols.TryGetValue(child.Name, out childSymbols)) {
224        changed |= childSymbols.Remove(child.Name);
225      }
226
227      for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) {
228        var key = Tuple.Create(parent.Name, argumentIndex);
229        if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols))
230          changed |= childSymbols.Remove(child.Name);
231      }
232
233      if (changed) {
234        ClearCaches();
235        OnChanged();
236      }
237    }
238
239    protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
240      bool changed = false;
241
242      suppressEvents = true;
243      List<string> childSymbols;
244      if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
245        if (childSymbols.Remove(child.Name)) {
246          for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
247            if (i != argumentIndex) AddAllowedChildSymbol(parent, child, i);
248          }
249          changed = true;
250        }
251      }
252      suppressEvents = false;
253
254      var key = Tuple.Create(parent.Name, argumentIndex);
255      if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols))
256        changed |= childSymbols.Remove(child.Name);
257
258      if (changed) {
259        ClearCaches();
260        OnChanged();
261      }
262    }
263
264    protected void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
265      var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol));
266      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 minimum subtree count " + minimumSubtreeCount + " for " + symbol);
268
269      foreach (ISymbol s in symbols)
270        SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount);
271
272      ClearCaches();
273      OnChanged();
274    }
275
276    private void SetSubTreeCountInDictionaries(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
277      for (int i = maximumSubtreeCount; i < GetMaximumSubtreeCount(symbol); i++) {
278        var key = Tuple.Create(symbol.Name, i);
279        allowedChildSymbolsPerIndex.Remove(key);
280      }
281
282      symbolSubtreeCount[symbol.Name] = Tuple.Create(minimumSubtreeCount, maximumSubtreeCount);
283    }
284    #endregion
285
286    public virtual IEnumerable<ISymbol> Symbols {
287      get { return symbols.Values; }
288    }
289    public virtual IEnumerable<ISymbol> AllowedSymbols {
290      get { return Symbols.Where(s => s.Enabled); }
291    }
292    public virtual bool ContainsSymbol(ISymbol symbol) {
293      return symbols.ContainsKey(symbol.Name);
294    }
295
296    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
297      if (!child.Enabled) return false;
298
299      List<string> temp;
300      if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) {
301        if (temp.Contains(child.Name)) return true;
302        if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true;
303      }
304      return false;
305    }
306
307    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
308      if (!child.Enabled) return false;
309      if (IsAllowedChildSymbol(parent, child)) return true;
310
311      List<string> temp;
312      var key = Tuple.Create(parent.Name, argumentIndex);
313      if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) {
314        if (temp.Contains(child.Name)) return true;
315        if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true;
316      }
317      return false;
318    }
319
320    public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
321      List<string> childs;
322      if (!allowedChildSymbols.TryGetValue(parent.Name, out childs))
323        return Enumerable.Empty<ISymbol>();
324
325      return childs.Select(x => GetSymbol(x)).Where(s => s.Enabled);
326    }
327
328    public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) {
329      var result = Enumerable.Empty<string>();
330
331      List<string> temp;
332      if (allowedChildSymbols.TryGetValue(parent.Name, out temp))
333        result = result.Union(temp);
334      var key = Tuple.Create(parent.Name, argumentIndex);
335      if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp))
336        result = result.Union(temp);
337
338      return result.Select(x => GetSymbol(x)).Where(s => s.Enabled);
339    }
340
341    public virtual int GetMinimumSubtreeCount(ISymbol symbol) {
342      return symbolSubtreeCount[symbol.Name].Item1;
343    }
344    public virtual int GetMaximumSubtreeCount(ISymbol symbol) {
345      return symbolSubtreeCount[symbol.Name].Item2;
346    }
347
348    protected void ClearCaches() {
349      cachedMinExpressionLength.Clear();
350      cachedMaxExpressionLength.Clear();
351      cachedMinExpressionDepth.Clear();
352    }
353
354    private Dictionary<string, int> cachedMinExpressionLength;
355    public int GetMinimumExpressionLength(ISymbol symbol) {
356      int temp;
357      if (!cachedMinExpressionLength.TryGetValue(symbol.Name, out temp)) {
358        cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
359        long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
360                                              let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
361                                                                      select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min()
362                                              select minForSlot).DefaultIfEmpty(0).Sum();
363
364        cachedMinExpressionLength[symbol.Name] = (int)Math.Min(sumOfMinExpressionLengths, int.MaxValue);
365        return cachedMinExpressionLength[symbol.Name];
366      }
367      return temp;
368    }
369
370    private Dictionary<string, int> cachedMaxExpressionLength;
371    public int GetMaximumExpressionLength(ISymbol symbol) {
372      int temp;
373      if (!cachedMaxExpressionLength.TryGetValue(symbol.Name, out temp)) {
374        cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
375        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol))
376                                  let maxForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
377                                                          select GetMaximumExpressionLength(s)).DefaultIfEmpty(0).Max()
378                                  select maxForSlot).DefaultIfEmpty(0).Sum();
379        cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, int.MaxValue);
380        return cachedMaxExpressionLength[symbol.Name];
381      }
382      return temp;
383    }
384
385    private Dictionary<string, int> cachedMinExpressionDepth;
386    public int GetMinimumExpressionDepth(ISymbol symbol) {
387      int temp;
388      if (!cachedMinExpressionDepth.TryGetValue(symbol.Name, out temp)) {
389        cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
390        long minDepth = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
391                             let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
392                                                     select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min()
393                             select minForSlot).DefaultIfEmpty(0).Max();
394        cachedMinExpressionDepth[symbol.Name] = (int)Math.Min(minDepth, int.MaxValue);
395        return cachedMinExpressionDepth[symbol.Name];
396      }
397      return temp;
398    }
399
400    #region events
401    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    public event EventHandler Changed;
520    protected virtual void OnChanged() {
521      if (suppressEvents) return;
522      var handler = Changed;
523      if (handler != null) Changed(this, EventArgs.Empty);
524    }
525    #endregion
526  }
527}
Note: See TracBrowser for help on using the repository browser.