Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/15/11 13:34:38 (13 years ago)
Author:
mkommend
Message:

#1418: Finally added results from the grammar refactoring.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs

    r5499 r5686  
    2020#endregion
    2121
     22using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2225using HeuristicLab.Common;
    2326using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2427
    2528namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    26   public sealed class SymbolicExpressionTreeGrammar : DefaultSymbolicExpressionGrammar {
    27     public SymbolicExpressionTreeGrammar(ISymbolicExpressionTreeGrammar grammar)
    28       : base(grammar) {
    29     }
     29  [StorableClass]
     30  internal sealed class SymbolicExpressionTreeGrammar : SymbolicExpressionGrammarBase, ISymbolicExpressionTreeGrammar {
    3031    [StorableConstructor]
    3132    private SymbolicExpressionTreeGrammar(bool deserializing) : base(deserializing) { }
    32     // don't call storable ctor of base class to prevent full cloning
    33     // instead use storable ctor to initialize an empty grammar and fill with InizializeShallowClone
    3433    private SymbolicExpressionTreeGrammar(SymbolicExpressionTreeGrammar original, Cloner cloner)
    35       : base(false) {
    36       cloner.RegisterClonedObject(original, this);
    37       InitializeShallowClone(original);
     34      : base(original, cloner) {
     35      this.grammar = original.grammar;
    3836    }
    39     private SymbolicExpressionTreeGrammar() : base() { }
    40 
    4137    public override IDeepCloneable Clone(Cloner cloner) {
    4238      return new SymbolicExpressionTreeGrammar(this, cloner);
    4339    }
     40
     41    private ISymbolicExpressionGrammar grammar;
     42    public SymbolicExpressionTreeGrammar(ISymbolicExpressionGrammar grammar)
     43      : base() {
     44      if (grammar == null) throw new ArgumentNullException();
     45      this.grammar = grammar;
     46    }
     47
     48    public override IEnumerable<ISymbol> Symbols {
     49      get { return grammar.Symbols.Union(base.Symbols); }
     50    }
     51    public override IEnumerable<ISymbol> AllowedSymbols {
     52      get { return base.AllowedSymbols; }
     53    }
     54    public IEnumerable<ISymbol> ModifyableSymbols {
     55      get { return base.symbols.Values; }
     56    }
     57    public bool IsModifyableSymbol(ISymbol symbol) {
     58      return base.symbols.ContainsKey(symbol.Name);
     59    }
     60
     61    public override bool ContainsSymbol(ISymbol symbol) {
     62      return grammar.ContainsSymbol(symbol) || base.ContainsSymbol(symbol);
     63    }
     64    public override ISymbol GetSymbol(string symbolName) {
     65      var symbol = grammar.GetSymbol(symbolName);
     66      if (symbol != null) return symbol;
     67      symbol = base.GetSymbol(symbolName);
     68      if (symbol != null) return symbol;
     69      throw new ArgumentException();
     70    }
     71
     72    public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
     73      return grammar.IsAllowedChildSymbol(parent, child) || base.IsAllowedChildSymbol(parent, child);
     74    }
     75    public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     76      return grammar.IsAllowedChildSymbol(parent, child, argumentIndex) || base.IsAllowedChildSymbol(parent, child, argumentIndex);
     77    }
     78    public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
     79      return grammar.GetAllowedChildSymbols(parent).Union(base.GetAllowedChildSymbols(parent));
     80    }
     81    public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) {
     82      return grammar.GetAllowedChildSymbols(parent, argumentIndex).Union(base.GetAllowedChildSymbols(parent, argumentIndex));
     83    }
     84
     85    public override int GetMinimumSubtreeCount(ISymbol symbol) {
     86      if (grammar.ContainsSymbol(symbol)) return grammar.GetMinimumSubtreeCount(symbol);
     87      return base.GetMinimumSubtreeCount(symbol);
     88    }
     89    public override int GetMaximumSubtreeCount(ISymbol symbol) {
     90      if (grammar.ContainsSymbol(symbol)) return grammar.GetMaximumSubtreeCount(symbol);
     91      return base.GetMaximumSubtreeCount(symbol);
     92    }
     93
     94    void ISymbolicExpressionTreeGrammar.AddSymbol(ISymbol symbol) {
     95      base.AddSymbol(symbol);
     96    }
     97    void ISymbolicExpressionTreeGrammar.RemoveSymbol(ISymbol symbol) {
     98      if (!IsModifyableSymbol(symbol)) throw new InvalidOperationException();
     99      base.RemoveSymbol(symbol);
     100    }
     101    void ISymbolicExpressionTreeGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
     102      base.AddAllowedChildSymbol(parent, child);
     103    }
     104    void ISymbolicExpressionTreeGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     105      base.AddAllowedChildSymbol(parent, child, argumentIndex);
     106    }
     107    void ISymbolicExpressionTreeGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {
     108      base.RemoveAllowedChildSymbol(parent, child);
     109    }
     110    void ISymbolicExpressionTreeGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     111      base.RemoveAllowedChildSymbol(parent, child, argumentIndex);
     112    }
     113
     114    void ISymbolicExpressionTreeGrammar.SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
     115      if (!IsModifyableSymbol(symbol)) throw new InvalidOperationException();
     116      base.SetSubtreeCount(symbol, minimumSubtreeCount, maximumSubtreeCount);
     117    }
    44118  }
    45119}
Note: See TracChangeset for help on using the changeset viewer.