Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs @ 6233

Last change on this file since 6233 was 6233, checked in by mkommend, 13 years ago

#1532:

  • Enabled renaming of symbols
  • Fixed cloning of grammers
  • Added readonly attribute in grammars to lock grammars during the algorithm run
  • Removed useless clone method in cloner
  • Changed CheckedItemCollectionViews to enable scrolling during the locked state
File size: 5.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
29  [StorableClass]
30  internal sealed class SymbolicExpressionTreeGrammar : SymbolicExpressionGrammarBase, ISymbolicExpressionTreeGrammar {
31    [StorableConstructor]
32    private SymbolicExpressionTreeGrammar(bool deserializing) : base(deserializing) { }
33    private SymbolicExpressionTreeGrammar(SymbolicExpressionTreeGrammar original, Cloner cloner)
34      : base(original, cloner) {
35      this.grammar = original.grammar;
36    }
37    public override IDeepCloneable Clone(Cloner cloner) {
38      foreach (ISymbol symbol in base.Symbols)
39        cloner.RegisterClonedObject(symbol, symbol);
40      return new SymbolicExpressionTreeGrammar(this, cloner);
41    }
42
43    private ISymbolicExpressionGrammar grammar;
44    public SymbolicExpressionTreeGrammar(ISymbolicExpressionGrammar grammar)
45      : base("SymbolicExpressionTreeGrammar", "A grammar that is used held by symbolic expression trees and allows extensions to the wraped grammar.") {
46      if (grammar == null) throw new ArgumentNullException();
47      this.grammar = grammar;
48    }
49
50    public override IEnumerable<ISymbol> Symbols {
51      get { return grammar.Symbols.Union(base.Symbols); }
52    }
53    public override IEnumerable<ISymbol> AllowedSymbols {
54      get { return base.AllowedSymbols; }
55    }
56    public IEnumerable<ISymbol> ModifyableSymbols {
57      get { return base.symbols.Values; }
58    }
59    public bool IsModifyableSymbol(ISymbol symbol) {
60      return base.symbols.ContainsKey(symbol.Name);
61    }
62
63    public override bool ContainsSymbol(ISymbol symbol) {
64      return grammar.ContainsSymbol(symbol) || base.ContainsSymbol(symbol);
65    }
66    public override ISymbol GetSymbol(string symbolName) {
67      var symbol = grammar.GetSymbol(symbolName);
68      if (symbol != null) return symbol;
69      symbol = base.GetSymbol(symbolName);
70      if (symbol != null) return symbol;
71      throw new ArgumentException();
72    }
73
74    public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
75      return grammar.IsAllowedChildSymbol(parent, child) || base.IsAllowedChildSymbol(parent, child);
76    }
77    public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
78      return grammar.IsAllowedChildSymbol(parent, child, argumentIndex) || base.IsAllowedChildSymbol(parent, child, argumentIndex);
79    }
80    public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
81      return grammar.GetAllowedChildSymbols(parent).Union(base.GetAllowedChildSymbols(parent));
82    }
83    public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) {
84      return grammar.GetAllowedChildSymbols(parent, argumentIndex).Union(base.GetAllowedChildSymbols(parent, argumentIndex));
85    }
86
87    public override int GetMinimumSubtreeCount(ISymbol symbol) {
88      if (grammar.ContainsSymbol(symbol)) return grammar.GetMinimumSubtreeCount(symbol);
89      return base.GetMinimumSubtreeCount(symbol);
90    }
91    public override int GetMaximumSubtreeCount(ISymbol symbol) {
92      if (grammar.ContainsSymbol(symbol)) return grammar.GetMaximumSubtreeCount(symbol);
93      return base.GetMaximumSubtreeCount(symbol);
94    }
95
96    void ISymbolicExpressionTreeGrammar.AddSymbol(ISymbol symbol) {
97      base.AddSymbol(symbol);
98    }
99    void ISymbolicExpressionTreeGrammar.RemoveSymbol(ISymbol symbol) {
100      if (!IsModifyableSymbol(symbol)) throw new InvalidOperationException();
101      base.RemoveSymbol(symbol);
102    }
103    void ISymbolicExpressionTreeGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
104      base.AddAllowedChildSymbol(parent, child);
105    }
106    void ISymbolicExpressionTreeGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
107      base.AddAllowedChildSymbol(parent, child, argumentIndex);
108    }
109    void ISymbolicExpressionTreeGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {
110      base.RemoveAllowedChildSymbol(parent, child);
111    }
112    void ISymbolicExpressionTreeGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
113      base.RemoveAllowedChildSymbol(parent, child, argumentIndex);
114    }
115
116    void ISymbolicExpressionTreeGrammar.SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
117      if (!IsModifyableSymbol(symbol)) throw new InvalidOperationException();
118      base.SetSubtreeCount(symbol, minimumSubtreeCount, maximumSubtreeCount);
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.