Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 1.8 KB
RevLine 
[15714]1using System.Collections.Generic;
[15712]2
3namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
4
5  public abstract class Symbol {
[15800]6    public string StringRepresentation { get; }
[15712]7
8    protected Symbol(string representation) {
[15714]9      StringRepresentation = representation;
[15712]10    }
11
12    public override string ToString() {
[15714]13      return StringRepresentation;
[15712]14    }
15  }
16
17  public class TerminalSymbol : Symbol {
[15812]18    public readonly bool IsVariable;
19
20    public TerminalSymbol(string representation, bool isVariable = false) : base(representation) {
21      IsVariable = isVariable;
22    }
[15712]23  }
24
25  public class NonterminalSymbol : Symbol {
[15800]26    public List<Production> Alternatives { get; }
[15712]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
[15800]38    public IEnumerable<TerminalSymbol> VariableTerminalSymbols { get; }
[15714]39
[15712]40    public VariableSymbol(string representation, IEnumerable<string> variableNames) : base(representation) {
[15714]41      List<TerminalSymbol> createdSymbols = new List<TerminalSymbol>();
42      VariableTerminalSymbols = createdSymbols;
43
[15712]44      foreach (string variableName in variableNames) {
[15812]45        TerminalSymbol s = new TerminalSymbol(variableName, isVariable: true);
[15714]46        createdSymbols.Add(s);
47        AddProduction(s);
[15712]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.