1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
|
---|
7 | [StorableClass]
|
---|
8 | public abstract class Symbol : DeepCloneable, IEquatable<Symbol> {
|
---|
9 | [Storable]
|
---|
10 | private readonly int stringRepresentationHash;
|
---|
11 |
|
---|
12 | [Storable]
|
---|
13 | public string StringRepresentation { get; }
|
---|
14 |
|
---|
15 | protected Symbol(string representation) {
|
---|
16 | StringRepresentation = representation;
|
---|
17 | stringRepresentationHash = representation.GetHashCode();
|
---|
18 | }
|
---|
19 |
|
---|
20 | protected Symbol(Symbol original, Cloner cloner) : base(original, cloner) {
|
---|
21 | StringRepresentation = original.StringRepresentation;
|
---|
22 | stringRepresentationHash = original.stringRepresentationHash;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public override string ToString() {
|
---|
26 | return StringRepresentation;
|
---|
27 | }
|
---|
28 |
|
---|
29 | #region IEquatable
|
---|
30 | public static bool operator ==(Symbol s1, Symbol s2) {
|
---|
31 | if (ReferenceEquals(s1, s2)) return true;
|
---|
32 | if (ReferenceEquals(s1, null) || ReferenceEquals(s2, null)) return false;
|
---|
33 | return s1.Equals(s2);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public static bool operator !=(Symbol s1, Symbol s2) {
|
---|
37 | return !(s1 == s2);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public bool Equals(Symbol other) {
|
---|
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;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override bool Equals(object obj) {
|
---|
48 | if (ReferenceEquals(obj, null)) return false;
|
---|
49 | if (ReferenceEquals(obj, this)) return true;
|
---|
50 | if (this.GetType() != obj.GetType()) return false;
|
---|
51 | return Equals((Symbol)obj);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public override int GetHashCode() {
|
---|
55 | return stringRepresentationHash;
|
---|
56 | }
|
---|
57 | #endregion
|
---|
58 | }
|
---|
59 |
|
---|
60 | [StorableClass]
|
---|
61 | public class TerminalSymbol : Symbol {
|
---|
62 | public TerminalSymbol(string representation) : base(representation) { }
|
---|
63 | public TerminalSymbol(TerminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
64 |
|
---|
65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
66 | return new TerminalSymbol(this, cloner);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | [StorableClass]
|
---|
71 | public class VariableTerminalSymbol : TerminalSymbol {
|
---|
72 | public VariableTerminalSymbol(string representation) : base(representation) { }
|
---|
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 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | [StorableClass]
|
---|
81 | public class NonterminalSymbol : Symbol {
|
---|
82 | public NonterminalSymbol(string representation) : base(representation) { }
|
---|
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 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | [StorableClass]
|
---|
91 | public class Production : List<Symbol>, IDeepCloneable {
|
---|
92 |
|
---|
93 | public Production(params Symbol[] symbols) : base(symbols) { }
|
---|
94 |
|
---|
95 | public Production(IEnumerable<Symbol> symbols) : base(symbols) { }
|
---|
96 |
|
---|
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 |
|
---|
109 | public override string ToString() {
|
---|
110 | return string.Join(" ", this);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|