Changeset 12706 for stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/SymbolicExpressionGrammarBase.cs
- Timestamp:
- 07/10/15 12:02:20 (9 years ago)
- Location:
- stable
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 12422,12424,12480-12482
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/SymbolicExpressionGrammarBase.cs
r12702 r12706 41 41 private IEnumerable<ISymbol> StorableSymbols { 42 42 get { return symbols.Values.ToArray(); } 43 set { symbols = value.ToDictionary(sym => sym.Name); }43 set { foreach (var s in value) symbols.Add(s.Name, s); } 44 44 } 45 45 … … 47 47 private IEnumerable<KeyValuePair<ISymbol, Tuple<int, int>>> StorableSymbolSubtreeCount { 48 48 get { return symbolSubtreeCount.Select(x => new KeyValuePair<ISymbol, Tuple<int, int>>(GetSymbol(x.Key), x.Value)).ToArray(); } 49 set { symbolSubtreeCount = value.ToDictionary(x => x.Key.Name, x => x.Value); }49 set { foreach (var pair in value) symbolSubtreeCount.Add(pair.Key.Name, pair.Value); } 50 50 } 51 51 … … 53 53 private IEnumerable<KeyValuePair<ISymbol, IEnumerable<ISymbol>>> StorableAllowedChildSymbols { 54 54 get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select(GetSymbol).ToArray())).ToArray(); } 55 set { allowedChildSymbols = value.ToDictionary(x => x.Key.Name, x => x.Value.Select(y => y.Name).ToList()); }55 set { foreach (var pair in value) allowedChildSymbols.Add(pair.Key.Name, pair.Value.Select(y => y.Name).ToList()); } 56 56 } 57 57 58 58 [Storable(Name = "AllowedChildSymbolsPerIndex")] 59 59 private IEnumerable<KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>> StorableAllowedChildSymbolsPerIndex { 60 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(); } 61 set { allowedChildSymbolsPerIndex = value.ToDictionary(x => Tuple.Create(x.Key.Item1.Name, x.Key.Item2), x => x.Value.Select(y => y.Name).ToList()); } 60 get { return allowedChildSymbolsPerIndex.Select(x => new KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>(Tuple.Create(GetSymbol(x.Key.Item1), x.Key.Item2), x.Value.Select(GetSymbol).ToArray())).ToArray(); } 61 set { 62 foreach (var pair in value) 63 allowedChildSymbolsPerIndex.Add(Tuple.Create(pair.Key.Item1.Name, pair.Key.Item2), pair.Value.Select(y => y.Name).ToList()); 64 } 62 65 } 63 66 #endregion 64 67 65 68 private bool suppressEvents; 66 protected Dictionary<string, ISymbol> symbols;67 protected Dictionary<string, Tuple<int, int>> symbolSubtreeCount;68 protected Dictionary<string, List<string>> allowedChildSymbols;69 protected Dictionary<Tuple<string, int>, List<string>> allowedChildSymbolsPerIndex;69 protected readonly Dictionary<string, ISymbol> symbols; 70 protected readonly Dictionary<string, Tuple<int, int>> symbolSubtreeCount; 71 protected readonly Dictionary<string, List<string>> allowedChildSymbols; 72 protected readonly Dictionary<Tuple<string, int>, List<string>> allowedChildSymbolsPerIndex; 70 73 71 74 public override bool CanChangeName { … … 87 90 cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>(); 88 91 92 symbols = new Dictionary<string, ISymbol>(); 93 symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(); 94 allowedChildSymbols = new Dictionary<string, List<string>>(); 95 allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>(); 96 89 97 suppressEvents = false; 90 98 } … … 133 141 134 142 #region protected grammar manipulation methods 135 p rotectedvirtual void AddSymbol(ISymbol symbol) {143 public virtual void AddSymbol(ISymbol symbol) { 136 144 if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined."); 137 145 foreach (var s in symbol.Flatten()) { … … 143 151 } 144 152 145 p rotectedvirtual void RemoveSymbol(ISymbol symbol) {153 public virtual void RemoveSymbol(ISymbol symbol) { 146 154 foreach (var s in symbol.Flatten()) { 147 155 symbols.Remove(s.Name); … … 175 183 } 176 184 177 p rotectedvoid AddAllowedChildSymbol(ISymbol parent, ISymbol child) {185 public virtual void AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 178 186 bool changed = false; 179 187 … … 204 212 } 205 213 206 p rotectedvoid AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {214 public virtual void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 207 215 bool changed = false; 208 216 … … 238 246 } 239 247 240 p rotectedvoid RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {248 public virtual void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) { 241 249 bool changed = false; 242 250 List<string> childSymbols; … … 257 265 } 258 266 259 p rotectedvoid RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {267 public virtual void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 260 268 bool changed = false; 261 269 … … 282 290 } 283 291 284 p rotectedvoid SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {292 public virtual void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 285 293 var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol)); 286 294 if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol); … … 308 316 } 309 317 public virtual IEnumerable<ISymbol> AllowedSymbols { 310 get { foreach (var s in Symbols) if (s.Enabled) yield return s; }318 get { return Symbols.Where(s => s.Enabled); } 311 319 } 312 320 public virtual bool ContainsSymbol(ISymbol symbol) { … … 518 526 if (suppressEvents) return; 519 527 var handler = Changed; 520 if (handler != null) Changed(this, EventArgs.Empty);528 if (handler != null) handler(this, EventArgs.Empty); 521 529 } 522 530 }
Note: See TracChangeset
for help on using the changeset viewer.