Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/28/18 14:44:25 (7 years ago)
Author:
lkammere
Message:

#2886 Move code for visualization and logging of sentences to separate classes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs

    r15817 r15821  
    4545
    4646    #region HL Symbols for Parsing ExpressionTrees
    47     private TypeCoherentExpressionGrammar symbolicExpressionGrammar;
    4847
    4948    private ISymbol constSy;
     
    5756    private ISymbol sinSy;
    5857    private ISymbol cosSy;
    59    
     58
    6059    private ISymbol rootSy;
    6160    private ISymbol startSy;
     61
     62    private InfixExpressionFormatter infixExpressionFormatter;
    6263    #endregion
    6364
     
    7374      SinFactor = new NonterminalSymbol("SinFactor");
    7475      CosFactor = new NonterminalSymbol("CosFactor");
    75          
     76
    7677      SimpleExpr = new NonterminalSymbol("SimpleExpr");
    7778      SimpleTerm = new NonterminalSymbol("SimpleTerm");
     
    127128
    128129      #region Parsing to SymbolicExpressionTree
    129       symbolicExpressionGrammar = new TypeCoherentExpressionGrammar();
     130      var symbolicExpressionGrammar = new TypeCoherentExpressionGrammar();
    130131      symbolicExpressionGrammar.ConfigureAsDefaultRegressionGrammar();
    131132
     
    143144      startSy = symbolicExpressionGrammar.Symbols.OfType<StartSymbol>().First();
    144145
     146      infixExpressionFormatter = new InfixExpressionFormatter();
    145147      #endregion
    146148    }
     
    274276
    275277    #region Parse to SymbolicExpressionTree
     278
     279    public string ToInfixString(SymbolString sentence) {
     280      Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!");
     281      Debug.Assert(sentence.All(s => s is TerminalSymbol), "Trying to evaluate symbol sequence with nonterminalsymbols!");
     282
     283      return infixExpressionFormatter.Format(ParseSymbolicExpressionTree(sentence));
     284    }
     285
    276286    public SymbolicExpressionTree ParseSymbolicExpressionTree(SymbolString sentence) {
    277287      Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!");
    278288      Debug.Assert(sentence.All(s => s is TerminalSymbol), "Trying to evaluate symbol sequence with nonterminalsymbols!");
    279 
    280       symbolicExpressionGrammar.ConfigureAsDefaultRegressionGrammar();     // TODO: not necessary to call this for each sentence
    281289
    282290      var rootNode = rootSy.CreateTreeNode();
     
    339347    }
    340348    #endregion
    341 
    342     #region Parse to Infix string
    343 
    344     public SymbolString PostfixToInfixParser(SymbolString phrase) {
    345       Stack<Symbol> parseStack = new Stack<Symbol>(phrase);
    346 
    347       return PostfixToInfixSubtreeParser(parseStack);
    348     }
    349 
    350     private SymbolString PostfixToInfixSubtreeParser(Stack<Symbol> parseStack) {
    351       Symbol head = parseStack.Pop();
    352 
    353       SymbolString result = new SymbolString(parseStack.Count);
    354 
    355       if (ReferenceEquals(head, Addition) || ReferenceEquals(head, Multiplication)) {
    356         // right part
    357         SymbolString rightPart = PostfixToInfixSubtreeParser(parseStack);
    358         SymbolString leftPart = PostfixToInfixSubtreeParser(parseStack);
    359 
    360         result.AddRange(leftPart);
    361         result.Add(head);
    362         result.AddRange(rightPart);
    363 
    364       } else if (ReferenceEquals(head, Log) || ReferenceEquals(head, Exp)
    365               || ReferenceEquals(head, Sin) || ReferenceEquals(head, Cos)
    366               || ReferenceEquals(head, Inv)) {
    367         result.Add(head);
    368         result.Add(OpeningBracket);
    369         result.AddRange(PostfixToInfixSubtreeParser(parseStack));
    370         result.Add(ClosingBracket);
    371 
    372       } else {
    373         result.Add(head);
    374       }
    375       return result;
    376     }
    377 
    378     #endregion
    379349  }
    380350}
Note: See TracChangeset for help on using the changeset viewer.