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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace 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 | if (!cloner.ClonedObjectRegistered(symbol))
|
---|
40 | cloner.RegisterClonedObject(symbol, symbol);
|
---|
41 | return new SymbolicExpressionTreeGrammar(this, cloner);
|
---|
42 | }
|
---|
43 |
|
---|
44 | [Storable]
|
---|
45 | private ISymbolicExpressionGrammar grammar;
|
---|
46 | public SymbolicExpressionTreeGrammar(ISymbolicExpressionGrammar grammar)
|
---|
47 | : base("SymbolicExpressionTreeGrammar", "A grammar that is used held by symbolic expression trees and allows extensions to the wraped grammar.") {
|
---|
48 | if (grammar == null) throw new ArgumentNullException();
|
---|
49 | this.grammar = grammar;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override IEnumerable<ISymbol> Symbols {
|
---|
53 | get { return grammar.Symbols.Union(base.Symbols); }
|
---|
54 | }
|
---|
55 | public override IEnumerable<ISymbol> AllowedSymbols {
|
---|
56 | get { return base.AllowedSymbols; }
|
---|
57 | }
|
---|
58 | public IEnumerable<ISymbol> ModifyableSymbols {
|
---|
59 | get { return base.symbols.Values; }
|
---|
60 | }
|
---|
61 | public bool IsModifyableSymbol(ISymbol symbol) {
|
---|
62 | return base.symbols.ContainsKey(symbol.Name);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public override bool ContainsSymbol(ISymbol symbol) {
|
---|
66 | return grammar.ContainsSymbol(symbol) || base.ContainsSymbol(symbol);
|
---|
67 | }
|
---|
68 | public override ISymbol GetSymbol(string symbolName) {
|
---|
69 | var symbol = grammar.GetSymbol(symbolName);
|
---|
70 | if (symbol != null) return symbol;
|
---|
71 | symbol = base.GetSymbol(symbolName);
|
---|
72 | if (symbol != null) return symbol;
|
---|
73 | throw new ArgumentException();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
|
---|
77 | return grammar.IsAllowedChildSymbol(parent, child) || base.IsAllowedChildSymbol(parent, child);
|
---|
78 | }
|
---|
79 | public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
|
---|
80 | return grammar.IsAllowedChildSymbol(parent, child, argumentIndex) || base.IsAllowedChildSymbol(parent, child, argumentIndex);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public override int GetMinimumSubtreeCount(ISymbol symbol) {
|
---|
84 | if (grammar.ContainsSymbol(symbol)) return grammar.GetMinimumSubtreeCount(symbol);
|
---|
85 | return base.GetMinimumSubtreeCount(symbol);
|
---|
86 | }
|
---|
87 | public override int GetMaximumSubtreeCount(ISymbol symbol) {
|
---|
88 | if (grammar.ContainsSymbol(symbol)) return grammar.GetMaximumSubtreeCount(symbol);
|
---|
89 | return base.GetMaximumSubtreeCount(symbol);
|
---|
90 | }
|
---|
91 |
|
---|
92 | void ISymbolicExpressionTreeGrammar.AddSymbol(ISymbol symbol) {
|
---|
93 | base.AddSymbol(symbol);
|
---|
94 | }
|
---|
95 | void ISymbolicExpressionTreeGrammar.RemoveSymbol(ISymbol symbol) {
|
---|
96 | if (!IsModifyableSymbol(symbol)) throw new InvalidOperationException();
|
---|
97 | base.RemoveSymbol(symbol);
|
---|
98 | }
|
---|
99 | void ISymbolicExpressionTreeGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
|
---|
100 | base.AddAllowedChildSymbol(parent, child);
|
---|
101 | }
|
---|
102 | void ISymbolicExpressionTreeGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
|
---|
103 | base.AddAllowedChildSymbol(parent, child, argumentIndex);
|
---|
104 | }
|
---|
105 | void ISymbolicExpressionTreeGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {
|
---|
106 | base.RemoveAllowedChildSymbol(parent, child);
|
---|
107 | }
|
---|
108 | void ISymbolicExpressionTreeGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
|
---|
109 | base.RemoveAllowedChildSymbol(parent, child, argumentIndex);
|
---|
110 | }
|
---|
111 |
|
---|
112 | void ISymbolicExpressionTreeGrammar.SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
|
---|
113 | if (!IsModifyableSymbol(symbol)) throw new InvalidOperationException();
|
---|
114 | base.SetSubtreeCount(symbol, minimumSubtreeCount, maximumSubtreeCount);
|
---|
115 | }
|
---|
116 |
|
---|
117 | int ISymbolicExpressionGrammarBase.GetMinimumExpressionDepth(ISymbol symbol) {
|
---|
118 | if (symbols.Count == 0) return grammar.GetMinimumExpressionDepth(symbol);
|
---|
119 | else return base.GetMinimumExpressionDepth(symbol);
|
---|
120 | }
|
---|
121 | int ISymbolicExpressionGrammarBase.GetMinimumExpressionLength(ISymbol symbol) {
|
---|
122 | if (symbols.Count == 0) return grammar.GetMinimumExpressionLength(symbol);
|
---|
123 | else return base.GetMinimumExpressionLength(symbol);
|
---|
124 | }
|
---|
125 | int ISymbolicExpressionGrammarBase.GetMaximumExpressionLength(ISymbol symbol, int maxDepth) {
|
---|
126 | if (symbols.Count == 0) return grammar.GetMaximumExpressionLength(symbol, maxDepth);
|
---|
127 | else return base.GetMaximumExpressionLength(symbol, maxDepth);
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|