[5686] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[6377] | 25 | using HeuristicLab.Collections;
|
---|
[5686] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace 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 {
|
---|
[6296] | 39 |
|
---|
[5686] | 40 | #region properties for separation between implementation and persistence
|
---|
[5695] | 41 | [Storable(Name = "Symbols")]
|
---|
| 42 | private IEnumerable<ISymbol> StorableSymbols {
|
---|
| 43 | get { return symbols.Values.ToArray(); }
|
---|
| 44 | set { symbols = value.ToDictionary(sym => sym.Name); }
|
---|
| 45 | }
|
---|
[5686] | 46 |
|
---|
[5695] | 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 | }
|
---|
[5686] | 52 |
|
---|
[5695] | 53 | [Storable(Name = "AllowedChildSymbols")]
|
---|
| 54 | private IEnumerable<KeyValuePair<ISymbol, IEnumerable<ISymbol>>> StorableAllowedChildSymbols {
|
---|
[5712] | 55 | get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select(y => GetSymbol(y)).ToArray())).ToArray(); }
|
---|
[5695] | 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 {
|
---|
[5712] | 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(); }
|
---|
[5695] | 62 | set { allowedChildSymbolsPerIndex = value.ToDictionary(x => Tuple.Create(x.Key.Item1.Name, x.Key.Item2), x => x.Value.Select(y => y.Name).ToList()); }
|
---|
| 63 | }
|
---|
[5686] | 64 | #endregion
|
---|
| 65 |
|
---|
[6415] | 66 | private bool suppressEvents;
|
---|
[5686] | 67 | protected Dictionary<string, ISymbol> symbols;
|
---|
[5695] | 68 | protected Dictionary<string, Tuple<int, int>> symbolSubtreeCount;
|
---|
[5686] | 69 | protected Dictionary<string, List<string>> allowedChildSymbols;
|
---|
| 70 | protected Dictionary<Tuple<string, int>, List<string>> allowedChildSymbolsPerIndex;
|
---|
| 71 |
|
---|
[5688] | 72 | public override bool CanChangeName {
|
---|
| 73 | get { return false; }
|
---|
| 74 | }
|
---|
| 75 | public override bool CanChangeDescription {
|
---|
| 76 | get { return false; }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[5686] | 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>();
|
---|
[6296] | 85 |
|
---|
| 86 | suppressEvents = false;
|
---|
[5686] | 87 | }
|
---|
[6233] | 88 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 89 | private void AfterDeserialization() {
|
---|
| 90 | foreach (ISymbol symbol in symbols.Values)
|
---|
| 91 | RegisterSymbolEvents(symbol);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[5686] | 94 | protected SymbolicExpressionGrammarBase(SymbolicExpressionGrammarBase original, Cloner cloner)
|
---|
| 95 | : base(original, cloner) {
|
---|
| 96 | cachedMinExpressionLength = new Dictionary<string, int>();
|
---|
| 97 | cachedMaxExpressionLength = new Dictionary<string, int>();
|
---|
| 98 | cachedMinExpressionDepth = new Dictionary<string, int>();
|
---|
| 99 |
|
---|
[6233] | 100 | symbols = original.symbols.ToDictionary(x => x.Key, y => (ISymbol)cloner.Clone(y.Value));
|
---|
[5695] | 101 | symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubtreeCount);
|
---|
[5686] | 102 |
|
---|
| 103 | allowedChildSymbols = new Dictionary<string, List<string>>();
|
---|
| 104 | foreach (var element in original.allowedChildSymbols)
|
---|
| 105 | allowedChildSymbols.Add(element.Key, new List<string>(element.Value));
|
---|
| 106 |
|
---|
| 107 | allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>();
|
---|
| 108 | foreach (var element in original.allowedChildSymbolsPerIndex)
|
---|
| 109 | allowedChildSymbolsPerIndex.Add(element.Key, new List<string>(element.Value));
|
---|
[6233] | 110 |
|
---|
| 111 | foreach (ISymbol symbol in symbols.Values)
|
---|
| 112 | RegisterSymbolEvents(symbol);
|
---|
[6296] | 113 |
|
---|
| 114 | suppressEvents = false;
|
---|
[5686] | 115 | }
|
---|
| 116 |
|
---|
[5688] | 117 | protected SymbolicExpressionGrammarBase(string name, string description)
|
---|
| 118 | : base(name, description) {
|
---|
[5686] | 119 | cachedMinExpressionLength = new Dictionary<string, int>();
|
---|
| 120 | cachedMaxExpressionLength = new Dictionary<string, int>();
|
---|
| 121 | cachedMinExpressionDepth = new Dictionary<string, int>();
|
---|
| 122 |
|
---|
| 123 | symbols = new Dictionary<string, ISymbol>();
|
---|
[5695] | 124 | symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>();
|
---|
[5686] | 125 | allowedChildSymbols = new Dictionary<string, List<string>>();
|
---|
| 126 | allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>();
|
---|
[6296] | 127 |
|
---|
| 128 | suppressEvents = false;
|
---|
[5686] | 129 | }
|
---|
| 130 |
|
---|
| 131 | #region protected grammar manipulation methods
|
---|
| 132 | protected void AddSymbol(ISymbol symbol) {
|
---|
[6409] | 133 | foreach (ISymbol s in symbol.Flatten())
|
---|
| 134 | AddSymbolToDictionaries(s);
|
---|
[6299] | 135 |
|
---|
| 136 | ClearCaches();
|
---|
| 137 | OnChanged();
|
---|
| 138 | }
|
---|
| 139 | private void AddSymbolToDictionaries(ISymbol symbol) {
|
---|
[5686] | 140 | symbols.Add(symbol.Name, symbol);
|
---|
[6403] | 141 | symbolSubtreeCount.Add(symbol.Name, Tuple.Create(symbol.MinimumArity, symbol.MinimumArity));
|
---|
[6296] | 142 | RegisterSymbolEvents(symbol);
|
---|
[6299] | 143 | }
|
---|
[6296] | 144 |
|
---|
[6299] | 145 | protected void RemoveSymbol(ISymbol symbol) {
|
---|
[6409] | 146 | foreach (ISymbol s in symbol.Flatten())
|
---|
| 147 | RemoveSymbolFromDictionaries(s);
|
---|
[6296] | 148 |
|
---|
[6337] | 149 | foreach (GroupSymbol group in symbols.Values.OfType<GroupSymbol>())
|
---|
[6377] | 150 | group.SymbolsCollection.Remove(symbol);
|
---|
[6337] | 151 |
|
---|
[5686] | 152 | ClearCaches();
|
---|
[6284] | 153 | OnChanged();
|
---|
[5686] | 154 | }
|
---|
[6299] | 155 | private void RemoveSymbolFromDictionaries(ISymbol symbol) {
|
---|
[5686] | 156 | symbols.Remove(symbol.Name);
|
---|
| 157 | allowedChildSymbols.Remove(symbol.Name);
|
---|
| 158 | for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++)
|
---|
| 159 | allowedChildSymbolsPerIndex.Remove(Tuple.Create(symbol.Name, i));
|
---|
[5695] | 160 | symbolSubtreeCount.Remove(symbol.Name);
|
---|
[5686] | 161 |
|
---|
| 162 | foreach (var parent in Symbols) {
|
---|
| 163 | List<string> allowedChilds;
|
---|
| 164 | if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
|
---|
| 165 | allowedChilds.Remove(symbol.Name);
|
---|
| 166 |
|
---|
| 167 | for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
|
---|
| 168 | if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
|
---|
| 169 | allowedChilds.Remove(symbol.Name);
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
[6233] | 172 | DeregisterSymbolEvents(symbol);
|
---|
[5686] | 173 | }
|
---|
| 174 |
|
---|
| 175 | public virtual ISymbol GetSymbol(string symbolName) {
|
---|
| 176 | ISymbol symbol;
|
---|
| 177 | if (symbols.TryGetValue(symbolName, out symbol)) return symbol;
|
---|
| 178 | return null;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
|
---|
[6299] | 182 | bool changed = false;
|
---|
[6296] | 183 |
|
---|
[6409] | 184 | foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol)))
|
---|
| 185 | changed |= AddAllowedChildSymbolToDictionaries(p, child);
|
---|
[6299] | 186 |
|
---|
| 187 | if (changed) {
|
---|
| 188 | ClearCaches();
|
---|
| 189 | OnChanged();
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child) {
|
---|
[5686] | 194 | List<string> childSymbols;
|
---|
| 195 | if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
|
---|
| 196 | childSymbols = new List<string>();
|
---|
| 197 | allowedChildSymbols.Add(parent.Name, childSymbols);
|
---|
| 198 | }
|
---|
[6299] | 199 | if (childSymbols.Contains(child.Name)) return false;
|
---|
[6296] | 200 |
|
---|
[6299] | 201 | suppressEvents = true;
|
---|
| 202 | for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++)
|
---|
| 203 | RemoveAllowedChildSymbol(parent, child, argumentIndex);
|
---|
| 204 | suppressEvents = false;
|
---|
| 205 |
|
---|
| 206 | childSymbols.Add(child.Name);
|
---|
| 207 | return true;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
|
---|
| 211 | bool changed = false;
|
---|
| 212 |
|
---|
[6409] | 213 | foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol)))
|
---|
| 214 | changed |= AddAllowedChildSymbolToDictionaries(p, child, argumentIndex);
|
---|
[6299] | 215 |
|
---|
| 216 | if (changed) {
|
---|
[6296] | 217 | ClearCaches();
|
---|
| 218 | OnChanged();
|
---|
| 219 | }
|
---|
[5686] | 220 | }
|
---|
| 221 |
|
---|
[6299] | 222 |
|
---|
| 223 | private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child, int argumentIndex) {
|
---|
[6296] | 224 | List<string> childSymbols;
|
---|
[6409] | 225 | if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
|
---|
| 226 | childSymbols = new List<string>();
|
---|
| 227 | allowedChildSymbols.Add(parent.Name, childSymbols);
|
---|
| 228 | }
|
---|
| 229 | if (childSymbols.Contains(child.Name)) return false;
|
---|
[6296] | 230 |
|
---|
[6409] | 231 |
|
---|
[5686] | 232 | var key = Tuple.Create(parent.Name, argumentIndex);
|
---|
| 233 | if (!allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) {
|
---|
| 234 | childSymbols = new List<string>();
|
---|
| 235 | allowedChildSymbolsPerIndex.Add(key, childSymbols);
|
---|
| 236 | }
|
---|
[6409] | 237 |
|
---|
[6299] | 238 | if (childSymbols.Contains(child.Name)) return false;
|
---|
[5686] | 239 |
|
---|
[6299] | 240 | childSymbols.Add(child.Name);
|
---|
| 241 | return true;
|
---|
[5686] | 242 | }
|
---|
| 243 |
|
---|
| 244 | protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {
|
---|
[6296] | 245 | bool changed = false;
|
---|
[5792] | 246 | List<string> childSymbols;
|
---|
| 247 | if (allowedChildSymbols.TryGetValue(child.Name, out childSymbols)) {
|
---|
[6299] | 248 | changed |= childSymbols.Remove(child.Name);
|
---|
[6296] | 249 | }
|
---|
| 250 |
|
---|
| 251 | for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) {
|
---|
| 252 | var key = Tuple.Create(parent.Name, argumentIndex);
|
---|
[6299] | 253 | if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols))
|
---|
| 254 | changed |= childSymbols.Remove(child.Name);
|
---|
[5792] | 255 | }
|
---|
[6296] | 256 |
|
---|
| 257 | if (changed) {
|
---|
| 258 | ClearCaches();
|
---|
| 259 | OnChanged();
|
---|
| 260 | }
|
---|
[5686] | 261 | }
|
---|
| 262 |
|
---|
| 263 | protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
|
---|
[6296] | 264 | bool changed = false;
|
---|
| 265 |
|
---|
[6299] | 266 | suppressEvents = true;
|
---|
[5792] | 267 | List<string> childSymbols;
|
---|
[6296] | 268 | if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
|
---|
| 269 | if (childSymbols.Remove(child.Name)) {
|
---|
| 270 | for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
|
---|
| 271 | if (i != argumentIndex) AddAllowedChildSymbol(parent, child, i);
|
---|
| 272 | }
|
---|
| 273 | changed = true;
|
---|
[6284] | 274 | }
|
---|
[5792] | 275 | }
|
---|
[6299] | 276 | suppressEvents = false;
|
---|
[6296] | 277 |
|
---|
| 278 | var key = Tuple.Create(parent.Name, argumentIndex);
|
---|
[6299] | 279 | if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols))
|
---|
| 280 | changed |= childSymbols.Remove(child.Name);
|
---|
[6296] | 281 |
|
---|
| 282 | if (changed) {
|
---|
| 283 | ClearCaches();
|
---|
| 284 | OnChanged();
|
---|
| 285 | }
|
---|
[5686] | 286 | }
|
---|
| 287 |
|
---|
| 288 | protected void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
|
---|
[6409] | 289 | var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol));
|
---|
| 290 | if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
|
---|
| 291 | if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
|
---|
[6299] | 292 |
|
---|
[6409] | 293 | foreach (ISymbol s in symbols)
|
---|
| 294 | SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount);
|
---|
| 295 |
|
---|
[6299] | 296 | ClearCaches();
|
---|
| 297 | OnChanged();
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | private void SetSubTreeCountInDictionaries(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
|
---|
[6403] | 301 | for (int i = maximumSubtreeCount; i < GetMaximumSubtreeCount(symbol); i++) {
|
---|
[5686] | 302 | var key = Tuple.Create(symbol.Name, i);
|
---|
| 303 | allowedChildSymbolsPerIndex.Remove(key);
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[5695] | 306 | symbolSubtreeCount[symbol.Name] = Tuple.Create(minimumSubtreeCount, maximumSubtreeCount);
|
---|
[5686] | 307 | }
|
---|
| 308 | #endregion
|
---|
| 309 |
|
---|
| 310 | public virtual IEnumerable<ISymbol> Symbols {
|
---|
| 311 | get { return symbols.Values; }
|
---|
| 312 | }
|
---|
| 313 | public virtual IEnumerable<ISymbol> AllowedSymbols {
|
---|
[6296] | 314 | get { return Symbols.Where(s => s.Enabled); }
|
---|
[5686] | 315 | }
|
---|
| 316 | public virtual bool ContainsSymbol(ISymbol symbol) {
|
---|
| 317 | return symbols.ContainsKey(symbol.Name);
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
|
---|
[6296] | 321 | if (!child.Enabled) return false;
|
---|
| 322 |
|
---|
[5686] | 323 | List<string> temp;
|
---|
[6409] | 324 | if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) {
|
---|
| 325 | if (temp.Contains(child.Name)) return true;
|
---|
| 326 | if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true;
|
---|
| 327 | }
|
---|
[5686] | 328 | return false;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
|
---|
[6296] | 332 | if (!child.Enabled) return false;
|
---|
[6409] | 333 | if (IsAllowedChildSymbol(parent, child)) return true;
|
---|
[6296] | 334 |
|
---|
[5686] | 335 | List<string> temp;
|
---|
[6409] | 336 | var key = Tuple.Create(parent.Name, argumentIndex);
|
---|
| 337 | if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) {
|
---|
[5686] | 338 | if (temp.Contains(child.Name)) return true;
|
---|
[6409] | 339 | if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true;
|
---|
| 340 | }
|
---|
[5686] | 341 | return false;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
|
---|
[6296] | 345 | List<string> childs;
|
---|
[6409] | 346 | if (!allowedChildSymbols.TryGetValue(parent.Name, out childs))
|
---|
| 347 | return Enumerable.Empty<ISymbol>();
|
---|
| 348 |
|
---|
| 349 | return childs.Select(x => GetSymbol(x)).Where(s => s.Enabled);
|
---|
[5686] | 350 | }
|
---|
| 351 |
|
---|
| 352 | public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) {
|
---|
| 353 | var result = Enumerable.Empty<string>();
|
---|
| 354 |
|
---|
| 355 | List<string> temp;
|
---|
| 356 | if (allowedChildSymbols.TryGetValue(parent.Name, out temp))
|
---|
| 357 | result = result.Union(temp);
|
---|
| 358 | var key = Tuple.Create(parent.Name, argumentIndex);
|
---|
| 359 | if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp))
|
---|
| 360 | result = result.Union(temp);
|
---|
| 361 |
|
---|
[6296] | 362 | return result.Select(x => GetSymbol(x)).Where(s => s.Enabled);
|
---|
[5686] | 363 | }
|
---|
| 364 |
|
---|
| 365 | public virtual int GetMinimumSubtreeCount(ISymbol symbol) {
|
---|
[5695] | 366 | return symbolSubtreeCount[symbol.Name].Item1;
|
---|
[5686] | 367 | }
|
---|
| 368 | public virtual int GetMaximumSubtreeCount(ISymbol symbol) {
|
---|
[5695] | 369 | return symbolSubtreeCount[symbol.Name].Item2;
|
---|
[5686] | 370 | }
|
---|
| 371 |
|
---|
| 372 | private void ClearCaches() {
|
---|
| 373 | cachedMinExpressionLength.Clear();
|
---|
| 374 | cachedMaxExpressionLength.Clear();
|
---|
| 375 | cachedMinExpressionDepth.Clear();
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | private Dictionary<string, int> cachedMinExpressionLength;
|
---|
| 379 | public int GetMinimumExpressionLength(ISymbol symbol) {
|
---|
| 380 | int temp;
|
---|
| 381 | if (!cachedMinExpressionLength.TryGetValue(symbol.Name, out temp)) {
|
---|
| 382 | cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
|
---|
| 383 | long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
|
---|
[6296] | 384 | let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
|
---|
[5686] | 385 | select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min()
|
---|
| 386 | select minForSlot).DefaultIfEmpty(0).Sum();
|
---|
| 387 |
|
---|
| 388 | cachedMinExpressionLength[symbol.Name] = (int)Math.Min(sumOfMinExpressionLengths, int.MaxValue);
|
---|
| 389 | return cachedMinExpressionLength[symbol.Name];
|
---|
| 390 | }
|
---|
| 391 | return temp;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | private Dictionary<string, int> cachedMaxExpressionLength;
|
---|
| 395 | public int GetMaximumExpressionLength(ISymbol symbol) {
|
---|
| 396 | int temp;
|
---|
| 397 | if (!cachedMaxExpressionLength.TryGetValue(symbol.Name, out temp)) {
|
---|
| 398 | cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
|
---|
| 399 | long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol))
|
---|
[6296] | 400 | let maxForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
|
---|
[5686] | 401 | select GetMaximumExpressionLength(s)).DefaultIfEmpty(0).Max()
|
---|
| 402 | select maxForSlot).DefaultIfEmpty(0).Sum();
|
---|
[6009] | 403 | cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, int.MaxValue);
|
---|
[5686] | 404 | return cachedMaxExpressionLength[symbol.Name];
|
---|
| 405 | }
|
---|
| 406 | return temp;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | private Dictionary<string, int> cachedMinExpressionDepth;
|
---|
| 410 | public int GetMinimumExpressionDepth(ISymbol symbol) {
|
---|
| 411 | int temp;
|
---|
| 412 | if (!cachedMinExpressionDepth.TryGetValue(symbol.Name, out temp)) {
|
---|
| 413 | cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
|
---|
[6009] | 414 | long minDepth = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
|
---|
[6296] | 415 | let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
|
---|
[6009] | 416 | select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min()
|
---|
| 417 | select minForSlot).DefaultIfEmpty(0).Max();
|
---|
| 418 | cachedMinExpressionDepth[symbol.Name] = (int)Math.Min(minDepth, int.MaxValue);
|
---|
[5686] | 419 | return cachedMinExpressionDepth[symbol.Name];
|
---|
| 420 | }
|
---|
| 421 | return temp;
|
---|
| 422 | }
|
---|
[6284] | 423 |
|
---|
[6296] | 424 | #region events
|
---|
| 425 | private void RegisterSymbolEvents(ISymbol symbol) {
|
---|
| 426 | symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
|
---|
| 427 | symbol.NameChanged += new EventHandler(Symbol_NameChanged);
|
---|
[6493] | 428 |
|
---|
[6377] | 429 | var groupSymbol = symbol as GroupSymbol;
|
---|
| 430 | if (groupSymbol != null) RegisterGroupSymbolEvents(groupSymbol);
|
---|
[6493] | 431 | else symbol.Changed += new EventHandler(Symbol_Changed);
|
---|
[6296] | 432 | }
|
---|
| 433 | private void DeregisterSymbolEvents(ISymbol symbol) {
|
---|
| 434 | symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
|
---|
| 435 | symbol.NameChanged -= new EventHandler(Symbol_NameChanged);
|
---|
[6493] | 436 |
|
---|
[6377] | 437 | var groupSymbol = symbol as GroupSymbol;
|
---|
| 438 | if (groupSymbol != null) DeregisterGroupSymbolEvents(groupSymbol);
|
---|
[6493] | 439 | else symbol.Changed -= new EventHandler(Symbol_Changed);
|
---|
[6296] | 440 | }
|
---|
| 441 |
|
---|
[6377] | 442 | private void RegisterGroupSymbolEvents(GroupSymbol groupSymbol) {
|
---|
[6493] | 443 | groupSymbol.Changed += new EventHandler(GroupSymbol_Changed);
|
---|
[6377] | 444 | groupSymbol.SymbolsCollection.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded);
|
---|
| 445 | groupSymbol.SymbolsCollection.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved);
|
---|
| 446 | groupSymbol.SymbolsCollection.CollectionReset += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_CollectionReset);
|
---|
| 447 | }
|
---|
| 448 | private void DeregisterGroupSymbolEvents(GroupSymbol groupSymbol) {
|
---|
[6493] | 449 | groupSymbol.Changed -= new EventHandler(GroupSymbol_Changed);
|
---|
[6377] | 450 | groupSymbol.SymbolsCollection.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded);
|
---|
| 451 | groupSymbol.SymbolsCollection.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved);
|
---|
| 452 | groupSymbol.SymbolsCollection.CollectionReset -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_CollectionReset);
|
---|
| 453 | }
|
---|
| 454 |
|
---|
[6296] | 455 | private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) {
|
---|
| 456 | if (symbols.ContainsKey(e.Value)) e.Cancel = true;
|
---|
| 457 | }
|
---|
| 458 | private void Symbol_NameChanged(object sender, EventArgs e) {
|
---|
| 459 | ISymbol symbol = (ISymbol)sender;
|
---|
| 460 | string oldName = symbols.Where(x => x.Value == symbol).First().Key;
|
---|
| 461 | string newName = symbol.Name;
|
---|
| 462 |
|
---|
| 463 | symbols.Remove(oldName);
|
---|
| 464 | symbols.Add(newName, symbol);
|
---|
| 465 |
|
---|
| 466 | var subtreeCount = symbolSubtreeCount[oldName];
|
---|
| 467 | symbolSubtreeCount.Remove(oldName);
|
---|
| 468 | symbolSubtreeCount.Add(newName, subtreeCount);
|
---|
| 469 |
|
---|
| 470 | List<string> allowedChilds;
|
---|
| 471 | if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) {
|
---|
| 472 | allowedChildSymbols.Remove(oldName);
|
---|
| 473 | allowedChildSymbols.Add(newName, allowedChilds);
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) {
|
---|
| 477 | if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) {
|
---|
| 478 | allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i));
|
---|
| 479 | allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds);
|
---|
| 480 | }
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | foreach (var parent in Symbols) {
|
---|
| 484 | if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
|
---|
| 485 | if (allowedChilds.Remove(oldName))
|
---|
| 486 | allowedChilds.Add(newName);
|
---|
| 487 |
|
---|
| 488 | for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
|
---|
| 489 | if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
|
---|
| 490 | if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName);
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 | ClearCaches();
|
---|
| 495 | OnChanged();
|
---|
| 496 | }
|
---|
| 497 |
|
---|
[6493] | 498 | private void Symbol_Changed(object sender, EventArgs e) {
|
---|
| 499 | if (suppressEvents) return;
|
---|
[6296] | 500 | ClearCaches();
|
---|
| 501 | OnChanged();
|
---|
| 502 | }
|
---|
| 503 |
|
---|
[6493] | 504 | private void GroupSymbol_Changed(object sender, EventArgs e) {
|
---|
| 505 | if (suppressEvents) return;
|
---|
| 506 | suppressEvents = true;
|
---|
| 507 | GroupSymbol groupSymbol = (GroupSymbol)sender;
|
---|
| 508 | foreach (ISymbol symbol in groupSymbol.Flatten())
|
---|
| 509 | symbol.Enabled = groupSymbol.Enabled;
|
---|
| 510 | suppressEvents = false;
|
---|
| 511 | ClearCaches();
|
---|
| 512 | OnChanged();
|
---|
| 513 | }
|
---|
| 514 |
|
---|
[6377] | 515 | private void GroupSymbol_ItemsAdded(object sender, CollectionItemsChangedEventArgs<ISymbol> e) {
|
---|
| 516 | suppressEvents = true;
|
---|
| 517 | foreach (ISymbol symbol in e.Items)
|
---|
| 518 | if (!ContainsSymbol(symbol))
|
---|
| 519 | AddSymbol(symbol);
|
---|
| 520 | suppressEvents = false;
|
---|
| 521 | OnChanged();
|
---|
| 522 | }
|
---|
| 523 | private void GroupSymbol_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ISymbol> e) {
|
---|
| 524 | suppressEvents = true;
|
---|
| 525 | foreach (ISymbol symbol in e.Items)
|
---|
| 526 | if (ContainsSymbol(symbol))
|
---|
| 527 | RemoveSymbol(symbol);
|
---|
| 528 | suppressEvents = false;
|
---|
| 529 | OnChanged();
|
---|
| 530 | }
|
---|
| 531 | private void GroupSymbol_CollectionReset(object sender, CollectionItemsChangedEventArgs<ISymbol> e) {
|
---|
| 532 | suppressEvents = true;
|
---|
| 533 | foreach (ISymbol symbol in e.Items)
|
---|
| 534 | if (!ContainsSymbol(symbol))
|
---|
| 535 | AddSymbol(symbol);
|
---|
| 536 | foreach (ISymbol symbol in e.OldItems)
|
---|
| 537 | if (ContainsSymbol(symbol))
|
---|
| 538 | RemoveSymbol(symbol);
|
---|
| 539 | suppressEvents = false;
|
---|
| 540 | OnChanged();
|
---|
| 541 | }
|
---|
| 542 |
|
---|
[6284] | 543 | public event EventHandler Changed;
|
---|
| 544 | protected virtual void OnChanged() {
|
---|
[6296] | 545 | if (suppressEvents) return;
|
---|
[6284] | 546 | var handler = Changed;
|
---|
| 547 | if (handler != null) Changed(this, EventArgs.Empty);
|
---|
| 548 | }
|
---|
[5686] | 549 | #endregion
|
---|
| 550 | }
|
---|
| 551 | }
|
---|