[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 |
|
---|
[15974] | 12 | [Storable]
|
---|
| 13 | public string StringRepresentation { get; private set; }
|
---|
[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 |
|
---|
[15963] | 25 | [StorableConstructor]
|
---|
| 26 | protected Symbol(bool deserializing) { }
|
---|
| 27 |
|
---|
[15712] | 28 | public override string ToString() {
|
---|
[15714] | 29 | return StringRepresentation;
|
---|
[15712] | 30 | }
|
---|
[15828] | 31 |
|
---|
| 32 | #region IEquatable
|
---|
| 33 | public static bool operator ==(Symbol s1, Symbol s2) {
|
---|
[15832] | 34 | if (ReferenceEquals(s1, s2)) return true;
|
---|
| 35 | if (ReferenceEquals(s1, null) || ReferenceEquals(s2, null)) return false;
|
---|
| 36 | return s1.Equals(s2);
|
---|
[15828] | 37 | }
|
---|
| 38 |
|
---|
| 39 | public static bool operator !=(Symbol s1, Symbol s2) {
|
---|
| 40 | return !(s1 == s2);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public bool Equals(Symbol other) {
|
---|
[15832] | 44 | if (ReferenceEquals(other, null)) return false;
|
---|
| 45 | if (ReferenceEquals(other, this)) return true;
|
---|
| 46 | if (this.GetType() != other.GetType()) return false; // Otherwise, this needs to be reimplemented in derived classes.
|
---|
| 47 | return StringRepresentation == other.StringRepresentation;
|
---|
[15828] | 48 | }
|
---|
| 49 |
|
---|
| 50 | public override bool Equals(object obj) {
|
---|
[15832] | 51 | if (ReferenceEquals(obj, null)) return false;
|
---|
| 52 | if (ReferenceEquals(obj, this)) return true;
|
---|
| 53 | if (this.GetType() != obj.GetType()) return false;
|
---|
[15828] | 54 | return Equals((Symbol)obj);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public override int GetHashCode() {
|
---|
| 58 | return stringRepresentationHash;
|
---|
| 59 | }
|
---|
| 60 | #endregion
|
---|
[15712] | 61 | }
|
---|
| 62 |
|
---|
[15960] | 63 | [StorableClass]
|
---|
[15712] | 64 | public class TerminalSymbol : Symbol {
|
---|
[15960] | 65 | public TerminalSymbol(string representation) : base(representation) { }
|
---|
| 66 | public TerminalSymbol(TerminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
[15812] | 67 |
|
---|
[15960] | 68 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 69 | return new TerminalSymbol(this, cloner);
|
---|
| 70 | }
|
---|
[15963] | 71 |
|
---|
| 72 | [StorableConstructor]
|
---|
| 73 | protected TerminalSymbol(bool deserializing) : base(deserializing) { }
|
---|
[15712] | 74 | }
|
---|
| 75 |
|
---|
[15960] | 76 | [StorableClass]
|
---|
[15832] | 77 | public class VariableTerminalSymbol : TerminalSymbol {
|
---|
| 78 | public VariableTerminalSymbol(string representation) : base(representation) { }
|
---|
[15960] | 79 | public VariableTerminalSymbol(VariableTerminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
| 80 |
|
---|
| 81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 82 | return new VariableTerminalSymbol(this, cloner);
|
---|
| 83 | }
|
---|
[15963] | 84 |
|
---|
| 85 | [StorableConstructor]
|
---|
| 86 | protected VariableTerminalSymbol(bool deserializing) : base(deserializing) { }
|
---|
[15832] | 87 | }
|
---|
| 88 |
|
---|
[15960] | 89 | [StorableClass]
|
---|
[15712] | 90 | public class NonterminalSymbol : Symbol {
|
---|
[15834] | 91 | public NonterminalSymbol(string representation) : base(representation) { }
|
---|
[15960] | 92 | public NonterminalSymbol(NonterminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
| 93 |
|
---|
| 94 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 95 | return new NonterminalSymbol(this, cloner);
|
---|
| 96 | }
|
---|
[15963] | 97 |
|
---|
| 98 | [StorableConstructor]
|
---|
| 99 | protected NonterminalSymbol(bool deserializing) : base(deserializing) { }
|
---|
[15712] | 100 | }
|
---|
| 101 |
|
---|
[15960] | 102 | [StorableClass]
|
---|
| 103 | public class Production : List<Symbol>, IDeepCloneable {
|
---|
[15712] | 104 |
|
---|
| 105 | public Production(params Symbol[] symbols) : base(symbols) { }
|
---|
| 106 |
|
---|
| 107 | public Production(IEnumerable<Symbol> symbols) : base(symbols) { }
|
---|
| 108 |
|
---|
[15963] | 109 | [StorableConstructor]
|
---|
| 110 | protected Production(bool deserializing) { }
|
---|
| 111 |
|
---|
[15960] | 112 | protected Production(Production original, Cloner cloner) : this(original) {
|
---|
| 113 | cloner.RegisterClonedObject(original, this);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
| 117 | return new Production(this, cloner);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | public object Clone() {
|
---|
| 121 | return Clone(new Cloner());
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[15712] | 124 | public override string ToString() {
|
---|
| 125 | return string.Join(" ", this);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|