1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
|
---|
9 | [StorableClass]
|
---|
10 | public abstract class Symbol : DeepCloneable, IEquatable<Symbol> {
|
---|
11 | [Storable]
|
---|
12 | private readonly int stringRepresentationHash;
|
---|
13 |
|
---|
14 | [Storable]
|
---|
15 | public string StringRepresentation { get; private set; }
|
---|
16 |
|
---|
17 | protected Symbol(string representation) {
|
---|
18 | StringRepresentation = representation;
|
---|
19 | stringRepresentationHash = representation.GetHashCode();
|
---|
20 | }
|
---|
21 |
|
---|
22 | protected Symbol(Symbol original, Cloner cloner) : base(original, cloner) {
|
---|
23 | StringRepresentation = original.StringRepresentation;
|
---|
24 | stringRepresentationHash = original.stringRepresentationHash;
|
---|
25 | }
|
---|
26 |
|
---|
27 | [StorableConstructor]
|
---|
28 | protected Symbol(bool deserializing) { }
|
---|
29 |
|
---|
30 | public override string ToString() {
|
---|
31 | return StringRepresentation;
|
---|
32 | }
|
---|
33 |
|
---|
34 | #region IEquatable
|
---|
35 | public static bool operator ==(Symbol s1, Symbol s2) {
|
---|
36 | if (ReferenceEquals(s1, s2)) return true;
|
---|
37 | if (ReferenceEquals(s1, null) || ReferenceEquals(s2, null)) return false;
|
---|
38 | return s1.Equals(s2);
|
---|
39 | }
|
---|
40 |
|
---|
41 | public static bool operator !=(Symbol s1, Symbol s2) {
|
---|
42 | return !(s1 == s2);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public bool Equals(Symbol other) {
|
---|
46 | if (ReferenceEquals(other, null)) return false;
|
---|
47 | if (ReferenceEquals(other, this)) return true;
|
---|
48 | if (this.GetType() != other.GetType()) return false; // Otherwise, this needs to be reimplemented in derived classes.
|
---|
49 | return StringRepresentation == other.StringRepresentation;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override bool Equals(object obj) {
|
---|
53 | if (ReferenceEquals(obj, null)) return false;
|
---|
54 | if (ReferenceEquals(obj, this)) return true;
|
---|
55 | if (this.GetType() != obj.GetType()) return false;
|
---|
56 | return Equals((Symbol)obj);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override int GetHashCode() {
|
---|
60 | return stringRepresentationHash;
|
---|
61 | }
|
---|
62 | #endregion
|
---|
63 | }
|
---|
64 |
|
---|
65 | [StorableClass]
|
---|
66 | public class TerminalSymbol : Symbol {
|
---|
67 | public TerminalSymbol(string representation) : base(representation) { }
|
---|
68 | public TerminalSymbol(TerminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
69 |
|
---|
70 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
71 | return new TerminalSymbol(this, cloner);
|
---|
72 | }
|
---|
73 |
|
---|
74 | [StorableConstructor]
|
---|
75 | protected TerminalSymbol(bool deserializing) : base(deserializing) { }
|
---|
76 | }
|
---|
77 |
|
---|
78 | [StorableClass]
|
---|
79 | public class VariableTerminalSymbol : TerminalSymbol {
|
---|
80 | public VariableTerminalSymbol(string representation) : base(representation) { }
|
---|
81 | public VariableTerminalSymbol(VariableTerminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
82 |
|
---|
83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
84 | return new VariableTerminalSymbol(this, cloner);
|
---|
85 | }
|
---|
86 |
|
---|
87 | [StorableConstructor]
|
---|
88 | protected VariableTerminalSymbol(bool deserializing) : base(deserializing) { }
|
---|
89 | }
|
---|
90 |
|
---|
91 | [StorableClass]
|
---|
92 | public class NonterminalSymbol : Symbol {
|
---|
93 | public NonterminalSymbol(string representation) : base(representation) { }
|
---|
94 | public NonterminalSymbol(NonterminalSymbol original, Cloner cloner) : base(original, cloner) { }
|
---|
95 |
|
---|
96 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
97 | return new NonterminalSymbol(this, cloner);
|
---|
98 | }
|
---|
99 |
|
---|
100 | [StorableConstructor]
|
---|
101 | protected NonterminalSymbol(bool deserializing) : base(deserializing) { }
|
---|
102 | }
|
---|
103 |
|
---|
104 | [StorableClass]
|
---|
105 | public class Production : DeepCloneable, IList<Symbol> {
|
---|
106 | [Storable]
|
---|
107 | private List<Symbol> symbols;
|
---|
108 |
|
---|
109 | public int Count { get { return symbols.Count; } }
|
---|
110 |
|
---|
111 | public bool IsReadOnly {
|
---|
112 | get { return false; }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public Symbol this[int index] {
|
---|
116 | get { return symbols[index]; }
|
---|
117 | set { symbols[index] = value; }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public Production(params Symbol[] symbols) {
|
---|
121 | this.symbols = symbols.ToList();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public Production(IEnumerable<Symbol> symbols) {
|
---|
125 | this.symbols = symbols.ToList();
|
---|
126 | }
|
---|
127 |
|
---|
128 | [StorableConstructor]
|
---|
129 | protected Production(bool deserializing) { }
|
---|
130 |
|
---|
131 | protected Production(Production original, Cloner cloner) {
|
---|
132 | symbols = original.symbols.Select(cloner.Clone).ToList();
|
---|
133 | }
|
---|
134 |
|
---|
135 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
136 | return new Production(this, cloner);
|
---|
137 | }
|
---|
138 |
|
---|
139 | public override string ToString() {
|
---|
140 | return string.Join(" ", this);
|
---|
141 | }
|
---|
142 |
|
---|
143 | #region IList<Symbol> methods
|
---|
144 | public IEnumerator<Symbol> GetEnumerator() {
|
---|
145 | return symbols.GetEnumerator();
|
---|
146 | }
|
---|
147 |
|
---|
148 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
149 | return GetEnumerator();
|
---|
150 | }
|
---|
151 |
|
---|
152 | public int IndexOf(Symbol item) {
|
---|
153 | return symbols.IndexOf(item);
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void Insert(int index, Symbol item) {
|
---|
157 | symbols.Insert(index, item);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public void RemoveAt(int index) {
|
---|
161 | symbols.RemoveAt(index);
|
---|
162 | }
|
---|
163 |
|
---|
164 | public void Add(Symbol item) {
|
---|
165 | symbols.Add(item);
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void Clear() {
|
---|
169 | symbols.Clear();
|
---|
170 | }
|
---|
171 |
|
---|
172 | public bool Contains(Symbol item) {
|
---|
173 | return symbols.Contains(item);
|
---|
174 | }
|
---|
175 |
|
---|
176 | public void CopyTo(Symbol[] array, int arrayIndex) {
|
---|
177 | symbols.CopyTo(array, arrayIndex);
|
---|
178 | }
|
---|
179 |
|
---|
180 | public void CopyTo(int index, Symbol[] array, int arrayIndex, int count) {
|
---|
181 | symbols.CopyTo(index, array, arrayIndex, count);
|
---|
182 | }
|
---|
183 |
|
---|
184 | public bool Remove(Symbol item) {
|
---|
185 | return symbols.Remove(item);
|
---|
186 | }
|
---|
187 | #endregion
|
---|
188 | }
|
---|
189 | }
|
---|