Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs @ 15963

Last change on this file since 15963 was 15963, checked in by bburlacu, 6 years ago

#2886: Add missing storable constructors

File size: 4.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Common;
4using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5
6namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
7  [StorableClass]
8  public abstract class Symbol : DeepCloneable, IEquatable<Symbol> {
9    [Storable]
10    private readonly int stringRepresentationHash;
11
12    [Storable(AllowOneWay = true)]
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    [StorableConstructor]
26    protected Symbol(bool deserializing) { }
27
28    public override string ToString() {
29      return StringRepresentation;
30    }
31
32    #region IEquatable
33    public static bool operator ==(Symbol s1, Symbol s2) {
34      if (ReferenceEquals(s1, s2)) return true;
35      if (ReferenceEquals(s1, null) || ReferenceEquals(s2, null)) return false;
36      return s1.Equals(s2);
37    }
38
39    public static bool operator !=(Symbol s1, Symbol s2) {
40      return !(s1 == s2);
41    }
42
43    public bool Equals(Symbol other) {
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;
48    }
49
50    public override bool Equals(object obj) {
51      if (ReferenceEquals(obj, null)) return false;
52      if (ReferenceEquals(obj, this)) return true;
53      if (this.GetType() != obj.GetType()) return false;
54      return Equals((Symbol)obj);
55    }
56
57    public override int GetHashCode() {
58      return stringRepresentationHash;
59    }
60    #endregion
61  }
62
63  [StorableClass]
64  public class TerminalSymbol : Symbol {
65    public TerminalSymbol(string representation) : base(representation) { }
66    public TerminalSymbol(TerminalSymbol original, Cloner cloner) : base(original, cloner) { }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new TerminalSymbol(this, cloner);
70    }
71
72    [StorableConstructor]
73    protected TerminalSymbol(bool deserializing) : base(deserializing) { }
74  }
75
76  [StorableClass]
77  public class VariableTerminalSymbol : TerminalSymbol {
78    public VariableTerminalSymbol(string representation) : base(representation) { }
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    }
84
85    [StorableConstructor]
86    protected VariableTerminalSymbol(bool deserializing) : base(deserializing) { }
87  }
88
89  [StorableClass]
90  public class NonterminalSymbol : Symbol {
91    public NonterminalSymbol(string representation) : base(representation) { }
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    }
97
98    [StorableConstructor]
99    protected NonterminalSymbol(bool deserializing) : base(deserializing) { }
100  }
101
102  [StorableClass]
103  public class Production : List<Symbol>, IDeepCloneable {
104
105    public Production(params Symbol[] symbols) : base(symbols) { }
106
107    public Production(IEnumerable<Symbol> symbols) : base(symbols) { }
108
109    [StorableConstructor]
110    protected Production(bool deserializing) { }
111
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
124    public override string ToString() {
125      return string.Join(" ", this);
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.