1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
|
---|
27 | [StorableClass]
|
---|
28 | public abstract class Symbol : DeepCloneable, IEquatable<Symbol> {
|
---|
29 | [Storable]
|
---|
30 | private readonly int stringRepresentationHash;
|
---|
31 |
|
---|
32 | [Storable]
|
---|
33 | public string StringRepresentation { get; private set; }
|
---|
34 |
|
---|
35 | protected Symbol(string representation) {
|
---|
36 | StringRepresentation = representation;
|
---|
37 | stringRepresentationHash = representation.GetHashCode();
|
---|
38 | }
|
---|
39 |
|
---|
40 | protected Symbol(Symbol original, Cloner cloner) : base(original, cloner) {
|
---|
41 | StringRepresentation = original.StringRepresentation;
|
---|
42 | stringRepresentationHash = original.stringRepresentationHash;
|
---|
43 | }
|
---|
44 |
|
---|
45 | [StorableConstructor]
|
---|
46 | protected Symbol(bool deserializing) { }
|
---|
47 |
|
---|
48 | public override string ToString() {
|
---|
49 | return StringRepresentation;
|
---|
50 | }
|
---|
51 |
|
---|
52 | #region IEquatable
|
---|
53 | public static bool operator ==(Symbol s1, Symbol s2) {
|
---|
54 | if (ReferenceEquals(s1, s2)) return true;
|
---|
55 | if (ReferenceEquals(s1, null) || ReferenceEquals(s2, null)) return false;
|
---|
56 | return s1.Equals(s2);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static bool operator !=(Symbol s1, Symbol s2) {
|
---|
60 | return !(s1 == s2);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public bool Equals(Symbol other) {
|
---|
64 | if (ReferenceEquals(other, null)) return false;
|
---|
65 | if (ReferenceEquals(other, this)) return true;
|
---|
66 | if (this.GetType() != other.GetType()) return false; // Otherwise, this needs to be reimplemented in derived classes.
|
---|
67 | return StringRepresentation == other.StringRepresentation;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override bool Equals(object obj) {
|
---|
71 | if (ReferenceEquals(obj, null)) return false;
|
---|
72 | if (ReferenceEquals(obj, this)) return true;
|
---|
73 | if (this.GetType() != obj.GetType()) return false;
|
---|
74 | return Equals((Symbol)obj);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public override int GetHashCode() {
|
---|
78 | return stringRepresentationHash;
|
---|
79 | }
|
---|
80 | #endregion
|
---|
81 | }
|
---|
82 |
|
---|
83 | [StorableClass]
|
---|
84 | public class TerminalSymbol : Symbol {
|
---|
85 | public TerminalSymbol(string representation) : base(representation) { }
|
---|
86 | public TerminalSymbol(TerminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
87 |
|
---|
88 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
89 | return new TerminalSymbol(this, cloner);
|
---|
90 | }
|
---|
91 |
|
---|
92 | [StorableConstructor]
|
---|
93 | protected TerminalSymbol(bool deserializing) : base(deserializing) { }
|
---|
94 | }
|
---|
95 |
|
---|
96 | [StorableClass]
|
---|
97 | public class VariableTerminalSymbol : TerminalSymbol {
|
---|
98 | public VariableTerminalSymbol(string representation) : base(representation) { }
|
---|
99 | public VariableTerminalSymbol(VariableTerminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
100 |
|
---|
101 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
102 | return new VariableTerminalSymbol(this, cloner);
|
---|
103 | }
|
---|
104 |
|
---|
105 | [StorableConstructor]
|
---|
106 | protected VariableTerminalSymbol(bool deserializing) : base(deserializing) { }
|
---|
107 | }
|
---|
108 |
|
---|
109 | [StorableClass]
|
---|
110 | public class NonterminalSymbol : Symbol {
|
---|
111 | public NonterminalSymbol(string representation) : base(representation) { }
|
---|
112 | public NonterminalSymbol(NonterminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
113 |
|
---|
114 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
115 | return new NonterminalSymbol(this, cloner);
|
---|
116 | }
|
---|
117 |
|
---|
118 | [StorableConstructor]
|
---|
119 | protected NonterminalSymbol(bool deserializing) : base(deserializing) { }
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | }
|
---|