Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15812 was 15812, checked in by lkammere, 6 years ago

#2886: Performance Improvements - Only store hash of archived phrases and reduce number of enumerators.

File size: 1.8 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
4
5  public abstract class Symbol {
6    public string StringRepresentation { get; }
7
8    protected Symbol(string representation) {
9      StringRepresentation = representation;
10    }
11
12    public override string ToString() {
13      return StringRepresentation;
14    }
15  }
16
17  public class TerminalSymbol : Symbol {
18    public readonly bool IsVariable;
19
20    public TerminalSymbol(string representation, bool isVariable = false) : base(representation) {
21      IsVariable = isVariable;
22    }
23  }
24
25  public class NonterminalSymbol : Symbol {
26    public List<Production> Alternatives { get; }
27
28    public NonterminalSymbol(string representation) : base(representation) {
29      Alternatives = new List<Production>();
30    }
31
32    public void AddProduction(params Symbol[] production) {
33      Alternatives.Add(new Production(production));
34    }
35  }
36
37  public class VariableSymbol : NonterminalSymbol { // Convenience class
38    public IEnumerable<TerminalSymbol> VariableTerminalSymbols { get; }
39
40    public VariableSymbol(string representation, IEnumerable<string> variableNames) : base(representation) {
41      List<TerminalSymbol> createdSymbols = new List<TerminalSymbol>();
42      VariableTerminalSymbols = createdSymbols;
43
44      foreach (string variableName in variableNames) {
45        TerminalSymbol s = new TerminalSymbol(variableName, isVariable: true);
46        createdSymbols.Add(s);
47        AddProduction(s);
48      }
49    }
50  }
51
52  public class Production : List<Symbol> {
53
54    public Production(params Symbol[] symbols) : base(symbols) { }
55
56    public Production(IEnumerable<Symbol> symbols) : base(symbols) { }
57
58    public override string ToString() {
59      return string.Join(" ", this);
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.