Changeset 6803 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs
- Timestamp:
- 09/20/11 11:12:10 (13 years ago)
- Location:
- trunk/sources
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources
- Property svn:ignore
-
old new 13 13 *.vsp 14 14 *.docstates 15 bin 16 HeuristicLab 3.3.6.0.ReSharper.user
-
- Property svn:mergeinfo changed
/branches/GP.Grammar.Editor (added) merged: 6284-6285,6296,6299,6335,6337,6377,6379,6387,6403,6409,6415,6493-6494,6497,6618,6620,6622,6626,6647,6675,6782,6784,6786,6795
- Property svn:ignore
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
-
Property
svn:ignore
set to
bin
-
Property
svn:ignore
set to
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs
r6532 r6803 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 … … 177 180 } 178 181 179 #region IStatefulItem 182 #region IStatefulItem methods 180 183 void IStatefulItem.InitializeState() { } 181 184 void IStatefulItem.ClearState() { … … 184 187 #endregion 185 188 189 #region ISymbolicExpressionGrammar methods 190 void ISymbolicExpressionGrammar.AddSymbol(ISymbol symbol) { 191 if (ReadOnly) throw new InvalidOperationException(); 192 AddSymbol(symbol); 193 } 194 void ISymbolicExpressionGrammar.RemoveSymbol(ISymbol symbol) { 195 if (ReadOnly) throw new InvalidOperationException(); 196 RemoveSymbol(symbol); 197 } 198 199 void ISymbolicExpressionGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 200 if (ReadOnly) throw new InvalidOperationException(); 201 base.AddAllowedChildSymbol(parent, child); 202 } 203 void ISymbolicExpressionGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 204 if (ReadOnly) throw new InvalidOperationException(); 205 base.AddAllowedChildSymbol(parent, child, argumentIndex); 206 } 207 void ISymbolicExpressionGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) { 208 if (ReadOnly) throw new InvalidOperationException(); 209 base.RemoveAllowedChildSymbol(parent, child); 210 } 211 void ISymbolicExpressionGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 212 if (ReadOnly) throw new InvalidOperationException(); 213 base.RemoveAllowedChildSymbol(parent, child, argumentIndex); 214 } 215 216 void ISymbolicExpressionGrammar.SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 217 if (ReadOnly) throw new InvalidOperationException(); 218 base.SetSubtreeCount(symbol, minimumSubtreeCount, maximumSubtreeCount); 219 } 220 221 private bool suppressEvents = false; 222 void ISymbolicExpressionGrammar.StartGrammarManipulation() { 223 suppressEvents = true; 224 } 225 void ISymbolicExpressionGrammar.FinishedGrammarManipulation() { 226 suppressEvents = false; 227 OnChanged(); 228 } 229 230 protected override void OnChanged() { 231 if (suppressEvents) return; 232 base.OnChanged(); 233 } 234 #endregion 235 186 236 #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); 237 private void RegisterSymbolEvents(ISymbol symbol) { 238 foreach (var s in symbol.Flatten()) { 239 s.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 240 s.NameChanged += new EventHandler(Symbol_NameChanged); 241 242 var groupSymbol = s as GroupSymbol; 243 if (groupSymbol != null) RegisterGroupSymbolEvents(groupSymbol); 244 else symbol.Changed += new EventHandler(Symbol_Changed); 245 } 246 } 247 private void DeregisterSymbolEvents(ISymbol symbol) { 248 foreach (var s in symbol.Flatten()) { 249 s.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 250 s.NameChanged -= new EventHandler(Symbol_NameChanged); 251 252 var groupSymbol = s as GroupSymbol; 253 if (groupSymbol != null) DeregisterGroupSymbolEvents(groupSymbol); 254 else symbol.Changed -= new EventHandler(Symbol_Changed); 255 } 256 } 257 258 private void RegisterGroupSymbolEvents(GroupSymbol groupSymbol) { 259 groupSymbol.Changed += new EventHandler(GroupSymbol_Changed); 260 groupSymbol.SymbolsCollection.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded); 261 groupSymbol.SymbolsCollection.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved); 262 groupSymbol.SymbolsCollection.CollectionReset += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_CollectionReset); 263 } 264 private void DeregisterGroupSymbolEvents(GroupSymbol groupSymbol) { 265 groupSymbol.Changed -= new EventHandler(GroupSymbol_Changed); 266 groupSymbol.SymbolsCollection.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded); 267 groupSymbol.SymbolsCollection.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved); 268 groupSymbol.SymbolsCollection.CollectionReset -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_CollectionReset); 269 } 270 271 private void Symbol_Changed(object sender, EventArgs e) { 272 ClearCaches(); 273 OnChanged(); 274 } 275 276 private void GroupSymbol_Changed(object sender, EventArgs e) { 277 GroupSymbol groupSymbol = (GroupSymbol)sender; 278 foreach (ISymbol symbol in groupSymbol.Flatten()) 279 symbol.Enabled = groupSymbol.Enabled; 280 281 ClearCaches(); 282 OnChanged(); 194 283 } 195 284 … … 234 323 235 324 ClearCaches(); 325 OnChanged(); 326 } 327 328 private void GroupSymbol_ItemsAdded(object sender, CollectionItemsChangedEventArgs<ISymbol> e) { 329 foreach (ISymbol symbol in e.Items) 330 if (!ContainsSymbol(symbol)) 331 AddSymbol(symbol); 332 OnChanged(); 333 } 334 private void GroupSymbol_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ISymbol> e) { 335 foreach (ISymbol symbol in e.Items) 336 if (ContainsSymbol(symbol)) 337 RemoveSymbol(symbol); 338 OnChanged(); 339 } 340 private void GroupSymbol_CollectionReset(object sender, CollectionItemsChangedEventArgs<ISymbol> e) { 341 foreach (ISymbol symbol in e.Items) 342 if (!ContainsSymbol(symbol)) 343 AddSymbol(symbol); 344 foreach (ISymbol symbol in e.OldItems) 345 if (ContainsSymbol(symbol)) 346 RemoveSymbol(symbol); 347 OnChanged(); 236 348 } 237 349 #endregion
Note: See TracChangeset
for help on using the changeset viewer.