[4249] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4249] | 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 |
|
---|
[5686] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[4249] | 24 | using HeuristicLab.Common;
|
---|
[16565] | 25 | using HEAL.Attic;
|
---|
[4249] | 26 |
|
---|
| 27 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[16565] | 28 | [StorableType("ADB1DF1C-BE39-49F7-BADD-978F495EEA4D")]
|
---|
[5686] | 29 | internal sealed class SymbolicExpressionTreeGrammar : SymbolicExpressionGrammarBase, ISymbolicExpressionTreeGrammar {
|
---|
[4249] | 30 | [StorableConstructor]
|
---|
[16565] | 31 | private SymbolicExpressionTreeGrammar(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 32 | private SymbolicExpressionTreeGrammar(SymbolicExpressionTreeGrammar original, Cloner cloner)
|
---|
[5686] | 33 | : base(original, cloner) {
|
---|
| 34 | this.grammar = original.grammar;
|
---|
[4722] | 35 | }
|
---|
[4249] | 36 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[6233] | 37 | foreach (ISymbol symbol in base.Symbols)
|
---|
[6237] | 38 | if (!cloner.ClonedObjectRegistered(symbol))
|
---|
| 39 | cloner.RegisterClonedObject(symbol, symbol);
|
---|
[4722] | 40 | return new SymbolicExpressionTreeGrammar(this, cloner);
|
---|
[4249] | 41 | }
|
---|
[5686] | 42 |
|
---|
[6378] | 43 | [Storable]
|
---|
[5686] | 44 | private ISymbolicExpressionGrammar grammar;
|
---|
| 45 | public SymbolicExpressionTreeGrammar(ISymbolicExpressionGrammar grammar)
|
---|
[7236] | 46 | : base("SymbolicExpressionTreeGrammar", "A grammar that is used held by symbolic expression trees and allows extensions to the wrapped grammar.") {
|
---|
[5686] | 47 | if (grammar == null) throw new ArgumentNullException();
|
---|
| 48 | this.grammar = grammar;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public override IEnumerable<ISymbol> Symbols {
|
---|
[7656] | 52 | get {
|
---|
[7660] | 53 | foreach (var s in grammar.Symbols) yield return s;
|
---|
[7656] | 54 | foreach (var s in base.symbols.Values) yield return s;
|
---|
| 55 | }
|
---|
[5686] | 56 | }
|
---|
| 57 | public IEnumerable<ISymbol> ModifyableSymbols {
|
---|
| 58 | get { return base.symbols.Values; }
|
---|
| 59 | }
|
---|
| 60 | public bool IsModifyableSymbol(ISymbol symbol) {
|
---|
| 61 | return base.symbols.ContainsKey(symbol.Name);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public override bool ContainsSymbol(ISymbol symbol) {
|
---|
| 65 | return grammar.ContainsSymbol(symbol) || base.ContainsSymbol(symbol);
|
---|
| 66 | }
|
---|
| 67 | public override ISymbol GetSymbol(string symbolName) {
|
---|
| 68 | var symbol = grammar.GetSymbol(symbolName);
|
---|
| 69 | if (symbol != null) return symbol;
|
---|
| 70 | symbol = base.GetSymbol(symbolName);
|
---|
| 71 | if (symbol != null) return symbol;
|
---|
| 72 | throw new ArgumentException();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[12422] | 75 | public override void RemoveSymbol(ISymbol symbol) {
|
---|
| 76 | if (!IsModifyableSymbol(symbol)) throw new InvalidOperationException();
|
---|
| 77 | base.RemoveSymbol(symbol);
|
---|
[5686] | 78 | }
|
---|
| 79 |
|
---|
| 80 | public override int GetMinimumSubtreeCount(ISymbol symbol) {
|
---|
| 81 | if (grammar.ContainsSymbol(symbol)) return grammar.GetMinimumSubtreeCount(symbol);
|
---|
| 82 | return base.GetMinimumSubtreeCount(symbol);
|
---|
| 83 | }
|
---|
| 84 | public override int GetMaximumSubtreeCount(ISymbol symbol) {
|
---|
| 85 | if (grammar.ContainsSymbol(symbol)) return grammar.GetMaximumSubtreeCount(symbol);
|
---|
| 86 | return base.GetMaximumSubtreeCount(symbol);
|
---|
| 87 | }
|
---|
[12422] | 88 | public override void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
|
---|
[5686] | 89 | if (!IsModifyableSymbol(symbol)) throw new InvalidOperationException();
|
---|
| 90 | base.SetSubtreeCount(symbol, minimumSubtreeCount, maximumSubtreeCount);
|
---|
| 91 | }
|
---|
[6911] | 92 |
|
---|
[12422] | 93 | public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
|
---|
| 94 | return grammar.IsAllowedChildSymbol(parent, child) || base.IsAllowedChildSymbol(parent, child);
|
---|
[6911] | 95 | }
|
---|
[12422] | 96 | public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
|
---|
| 97 | return grammar.IsAllowedChildSymbol(parent, child, argumentIndex) || base.IsAllowedChildSymbol(parent, child, argumentIndex);
|
---|
[7079] | 98 | }
|
---|
[4249] | 99 | }
|
---|
| 100 | }
|
---|