Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/10/15 12:02:20 (9 years ago)
Author:
mkommend
Message:

#2320: Merged r12422, r12423, r12424, r12480, r12481 and r12482 into stable.

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding

  • stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/SymbolicExpressionGrammarBase.cs

    r12702 r12706  
    4141    private IEnumerable<ISymbol> StorableSymbols {
    4242      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); }
    4444    }
    4545
     
    4747    private IEnumerable<KeyValuePair<ISymbol, Tuple<int, int>>> StorableSymbolSubtreeCount {
    4848      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); }
    5050    }
    5151
     
    5353    private IEnumerable<KeyValuePair<ISymbol, IEnumerable<ISymbol>>> StorableAllowedChildSymbols {
    5454      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()); }
    5656    }
    5757
    5858    [Storable(Name = "AllowedChildSymbolsPerIndex")]
    5959    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      }
    6265    }
    6366    #endregion
    6467
    6568    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;
    7073
    7174    public override bool CanChangeName {
     
    8790      cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();
    8891
     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
    8997      suppressEvents = false;
    9098    }
     
    133141
    134142    #region protected grammar manipulation methods
    135     protected virtual void AddSymbol(ISymbol symbol) {
     143    public virtual void AddSymbol(ISymbol symbol) {
    136144      if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
    137145      foreach (var s in symbol.Flatten()) {
     
    143151    }
    144152
    145     protected virtual void RemoveSymbol(ISymbol symbol) {
     153    public virtual void RemoveSymbol(ISymbol symbol) {
    146154      foreach (var s in symbol.Flatten()) {
    147155        symbols.Remove(s.Name);
     
    175183    }
    176184
    177     protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
     185    public virtual void AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
    178186      bool changed = false;
    179187
     
    204212    }
    205213
    206     protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     214    public virtual void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
    207215      bool changed = false;
    208216
     
    238246    }
    239247
    240     protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {
     248    public virtual void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {
    241249      bool changed = false;
    242250      List<string> childSymbols;
     
    257265    }
    258266
    259     protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     267    public virtual void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
    260268      bool changed = false;
    261269
     
    282290    }
    283291
    284     protected void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
     292    public virtual void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
    285293      var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol));
    286294      if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
     
    308316    }
    309317    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); }
    311319    }
    312320    public virtual bool ContainsSymbol(ISymbol symbol) {
     
    518526      if (suppressEvents) return;
    519527      var handler = Changed;
    520       if (handler != null) Changed(this, EventArgs.Empty);
     528      if (handler != null) handler(this, EventArgs.Empty);
    521529    }
    522530  }
Note: See TracChangeset for help on using the changeset viewer.