Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs @ 11813

Last change on this file since 11813 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Common;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
28  [StorableClass]
29  internal sealed class SymbolicExpressionTreeGrammar : SymbolicExpressionGrammarBase, ISymbolicExpressionTreeGrammar {
30    [StorableConstructor]
31    private SymbolicExpressionTreeGrammar(bool deserializing) : base(deserializing) { }
32    private SymbolicExpressionTreeGrammar(SymbolicExpressionTreeGrammar original, Cloner cloner)
33      : base(original, cloner) {
34      this.grammar = original.grammar;
35    }
36    public override IDeepCloneable Clone(Cloner cloner) {
37      foreach (ISymbol symbol in base.Symbols)
38        if (!cloner.ClonedObjectRegistered(symbol))
39          cloner.RegisterClonedObject(symbol, symbol);
40      return new SymbolicExpressionTreeGrammar(this, cloner);
41    }
42
43    [Storable]
44    private ISymbolicExpressionGrammar grammar;
45    public SymbolicExpressionTreeGrammar(ISymbolicExpressionGrammar grammar)
46      : base("SymbolicExpressionTreeGrammar", "A grammar that is used held by symbolic expression trees and allows extensions to the wrapped grammar.") {
47      if (grammar == null) throw new ArgumentNullException();
48      this.grammar = grammar;
49    }
50
51    public override IEnumerable<ISymbol> Symbols {
52      get {
53        foreach (var s in grammar.Symbols) yield return s;
54        foreach (var s in base.symbols.Values) yield return s;
55      }
56    }
57    public override IEnumerable<ISymbol> AllowedSymbols {
58      get { return base.AllowedSymbols; }
59    }
60    public IEnumerable<ISymbol> ModifyableSymbols {
61      get { return base.symbols.Values; }
62    }
63    public bool IsModifyableSymbol(ISymbol symbol) {
64      return base.symbols.ContainsKey(symbol.Name);
65    }
66
67    public override bool ContainsSymbol(ISymbol symbol) {
68      return grammar.ContainsSymbol(symbol) || base.ContainsSymbol(symbol);
69    }
70    public override ISymbol GetSymbol(string symbolName) {
71      var symbol = grammar.GetSymbol(symbolName);
72      if (symbol != null) return symbol;
73      symbol = base.GetSymbol(symbolName);
74      if (symbol != null) return symbol;
75      throw new ArgumentException();
76    }
77
78    public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
79      return grammar.IsAllowedChildSymbol(parent, child) || base.IsAllowedChildSymbol(parent, child);
80    }
81    public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
82      return grammar.IsAllowedChildSymbol(parent, child, argumentIndex) || base.IsAllowedChildSymbol(parent, child, 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    }
118
119    int ISymbolicExpressionGrammarBase.GetMinimumExpressionDepth(ISymbol symbol) {
120      if (symbols.Count == 0) return grammar.GetMinimumExpressionDepth(symbol);
121      else return base.GetMinimumExpressionDepth(symbol);
122    }
123    int ISymbolicExpressionGrammarBase.GetMaximumExpressionDepth(ISymbol symbol) {
124      if (symbols.Count == 0) return grammar.GetMaximumExpressionDepth(symbol);
125      else return base.GetMaximumExpressionDepth(symbol);
126    }
127    int ISymbolicExpressionGrammarBase.GetMinimumExpressionLength(ISymbol symbol) {
128      if (symbols.Count == 0) return grammar.GetMinimumExpressionLength(symbol);
129      else return base.GetMinimumExpressionLength(symbol);
130    }
131    int ISymbolicExpressionGrammarBase.GetMaximumExpressionLength(ISymbol symbol, int maxDepth) {
132      if (symbols.Count == 0) return grammar.GetMaximumExpressionLength(symbol, maxDepth);
133      else return base.GetMaximumExpressionLength(symbol, maxDepth);
134    }
135
136  }
137}
Note: See TracBrowser for help on using the repository browser.