[15828] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[15960] | 3 | using HeuristicLab.Common;
|
---|
| 4 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[15712] | 5 |
|
---|
| 6 | namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
|
---|
[15960] | 7 | [StorableClass]
|
---|
| 8 | public abstract class Symbol : DeepCloneable, IEquatable<Symbol> {
|
---|
| 9 | [Storable]
|
---|
[15828] | 10 | private readonly int stringRepresentationHash;
|
---|
| 11 |
|
---|
[15960] | 12 | [Storable]
|
---|
[15800] | 13 | public string StringRepresentation { get; }
|
---|
[15712] | 14 |
|
---|
| 15 | protected Symbol(string representation) {
|
---|
[15714] | 16 | StringRepresentation = representation;
|
---|
[15828] | 17 | stringRepresentationHash = representation.GetHashCode();
|
---|
[15712] | 18 | }
|
---|
| 19 |
|
---|
[15960] | 20 | protected Symbol(Symbol original, Cloner cloner) : base(original, cloner) {
|
---|
| 21 | StringRepresentation = original.StringRepresentation;
|
---|
| 22 | stringRepresentationHash = original.stringRepresentationHash;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[15712] | 25 | public override string ToString() {
|
---|
[15714] | 26 | return StringRepresentation;
|
---|
[15712] | 27 | }
|
---|
[15828] | 28 |
|
---|
| 29 | #region IEquatable
|
---|
| 30 | public static bool operator ==(Symbol s1, Symbol s2) {
|
---|
[15832] | 31 | if (ReferenceEquals(s1, s2)) return true;
|
---|
| 32 | if (ReferenceEquals(s1, null) || ReferenceEquals(s2, null)) return false;
|
---|
| 33 | return s1.Equals(s2);
|
---|
[15828] | 34 | }
|
---|
| 35 |
|
---|
| 36 | public static bool operator !=(Symbol s1, Symbol s2) {
|
---|
| 37 | return !(s1 == s2);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | public bool Equals(Symbol other) {
|
---|
[15832] | 41 | if (ReferenceEquals(other, null)) return false;
|
---|
| 42 | if (ReferenceEquals(other, this)) return true;
|
---|
| 43 | if (this.GetType() != other.GetType()) return false; // Otherwise, this needs to be reimplemented in derived classes.
|
---|
| 44 | return StringRepresentation == other.StringRepresentation;
|
---|
[15828] | 45 | }
|
---|
| 46 |
|
---|
| 47 | public override bool Equals(object obj) {
|
---|
[15832] | 48 | if (ReferenceEquals(obj, null)) return false;
|
---|
| 49 | if (ReferenceEquals(obj, this)) return true;
|
---|
| 50 | if (this.GetType() != obj.GetType()) return false;
|
---|
[15828] | 51 | return Equals((Symbol)obj);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public override int GetHashCode() {
|
---|
| 55 | return stringRepresentationHash;
|
---|
| 56 | }
|
---|
| 57 | #endregion
|
---|
[15712] | 58 | }
|
---|
| 59 |
|
---|
[15960] | 60 | [StorableClass]
|
---|
[15712] | 61 | public class TerminalSymbol : Symbol {
|
---|
[15960] | 62 | public TerminalSymbol(string representation) : base(representation) { }
|
---|
| 63 | public TerminalSymbol(TerminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
[15812] | 64 |
|
---|
[15960] | 65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 66 | return new TerminalSymbol(this, cloner);
|
---|
| 67 | }
|
---|
[15712] | 68 | }
|
---|
| 69 |
|
---|
[15960] | 70 | [StorableClass]
|
---|
[15832] | 71 | public class VariableTerminalSymbol : TerminalSymbol {
|
---|
| 72 | public VariableTerminalSymbol(string representation) : base(representation) { }
|
---|
[15960] | 73 | public VariableTerminalSymbol(VariableTerminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
| 74 |
|
---|
| 75 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 76 | return new VariableTerminalSymbol(this, cloner);
|
---|
| 77 | }
|
---|
[15832] | 78 | }
|
---|
| 79 |
|
---|
[15960] | 80 | [StorableClass]
|
---|
[15712] | 81 | public class NonterminalSymbol : Symbol {
|
---|
[15834] | 82 | public NonterminalSymbol(string representation) : base(representation) { }
|
---|
[15960] | 83 | public NonterminalSymbol(NonterminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
| 84 |
|
---|
| 85 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 86 | return new NonterminalSymbol(this, cloner);
|
---|
| 87 | }
|
---|
[15712] | 88 | }
|
---|
| 89 |
|
---|
[15960] | 90 | [StorableClass]
|
---|
| 91 | public class Production : List<Symbol>, IDeepCloneable {
|
---|
[15712] | 92 |
|
---|
| 93 | public Production(params Symbol[] symbols) : base(symbols) { }
|
---|
| 94 |
|
---|
| 95 | public Production(IEnumerable<Symbol> symbols) : base(symbols) { }
|
---|
| 96 |
|
---|
[15960] | 97 | protected Production(Production original, Cloner cloner) : this(original) {
|
---|
| 98 | cloner.RegisterClonedObject(original, this);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
| 102 | return new Production(this, cloner);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | public object Clone() {
|
---|
| 106 | return Clone(new Cloner());
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[15712] | 109 | public override string ToString() {
|
---|
| 110 | return string.Join(" ", this);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|