Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/03/18 08:14:29 (7 years ago)
Author:
fbaching
Message:

#2911: adding IfThenElse, NOT operator, AVG function
change visibility of util methods

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2911_TSQLSymbolicExpressionFormatter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/TSQLExpressionFormatter.cs

    r15877 r15881  
    3131
    3232namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    33   public static class StringBuilderExtensions {
    34     public static void AppendIndented(this StringBuilder strBuilder, int level, string text) {
    35       strBuilder.Append(new String(' ', level * 2));
     33  internal static class StringBuilderExtensions {
     34    internal static void AppendIndented(this StringBuilder strBuilder, int level, string text) {
     35      strBuilder.Append(new string(' ', level * 2));
    3636      strBuilder.Append(text);
    3737    }
    38     public static void AppendLineIndented(this StringBuilder strBuilder, int level, string text) {
    39       strBuilder.Append(new String(' ', level * 2));
     38    internal static void AppendLineIndented(this StringBuilder strBuilder, int level, string text) {
     39      strBuilder.Append(new string(' ', level * 2));
    4040      strBuilder.AppendLine(text);
    4141    }
     
    104104
    105105      //add variable declaration for convenience
    106       strBuilder.AppendLineIndented(1,$"-- added variable declaration for convenience");
     106      strBuilder.AppendLineIndented(1, $"-- added variable declaration for convenience");
    107107      foreach (var name in sortedVarcharIdentifiers)
    108108        strBuilder.AppendLineIndented(1, $"-- DECLARE {name} NVARCHAR(max) = ''");
     
    114114
    115115    private void FormatRecursively(int level, ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    116       // TODO: adapt to interpreter semantics. The HL interpreter also allows Boolean operations on reals
    117116      if (node.Subtrees.Any()) {
    118117        if (node.Symbol is Addition) {
     
    121120          FormatOperator(level, node, "AND", strBuilder);
    122121        } else if (node.Symbol is Average) {
    123           throw new NotSupportedException($"Symbol {node.Symbol.GetType().Name} not yet supported.");
     122          FormatAverage(level, node, strBuilder);
    124123        } else if (node.Symbol is Cosine) {
    125124          FormatFunction(level, node, "COS", strBuilder);
     
    131130          FormatOperator(level, node, ">", strBuilder);
    132131        } else if (node.Symbol is IfThenElse) {
    133           throw new NotSupportedException($"Symbol {node.Symbol.GetType().Name} not yet supported.");
     132          FormatIfThenElse(level, node, strBuilder);
    134133        } else if (node.Symbol is LessThan) {
    135134          FormatOperator(level, node, "<", strBuilder);
     
    139138          FormatOperator(level, node, "*", strBuilder);
    140139        } else if (node.Symbol is Not) {
    141           throw new NotSupportedException($"Symbol {node.Symbol.GetType().Name} not yet supported.");
     140          FormatOperator(level, node, "NOT LIKE", strBuilder);
    142141        } else if (node.Symbol is Or) {
    143142          FormatOperator(level, node, "OR", strBuilder);
     
    159158          FormatRoot(level, node, strBuilder);
    160159        } else {
    161           throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for C# symbolic expression tree formatter.");
     160          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for TSQL symbolic expression tree formatter.");
    162161        }
    163162      } else {
     
    175174          throw new NotSupportedException($"Symbol {node.Symbol.GetType().Name} not yet supported.");
    176175        } else {
    177           throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for C# symbolic expression tree formatter.");
     176          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for TSQL symbolic expression tree formatter.");
    178177        }
    179178      }
     179    }
     180
     181    private void FormatIfThenElse(int level, ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     182      strBuilder.Append("CASE ISNULL((SELECT 1 WHERE");
     183      FormatRecursively(level,node.GetSubtree(0), strBuilder);
     184      strBuilder.AppendLine("),0)");
     185      strBuilder.AppendIndented(level,"WHEN 1 THEN ");
     186      FormatRecursively(level, node.GetSubtree(1), strBuilder);
     187      strBuilder.AppendLine();
     188      strBuilder.AppendIndented(level, "WHEN 0 THEN ");
     189      FormatRecursively(level, node.GetSubtree(2), strBuilder);
     190      strBuilder.AppendLine();
     191      strBuilder.AppendIndented(level, "END");
     192    }
     193
     194    private void FormatAverage(int level, ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     195      strBuilder.Append("(");
     196      foreach (var child in node.Subtrees) {
     197        FormatRecursively(level, child, strBuilder);
     198        if (child != node.Subtrees.Last())
     199          strBuilder.Append(" + ");
     200      }
     201      strBuilder.Append($") / {node.Subtrees.Count()}");
    180202    }
    181203
     
    204226        FormatRecursively(level++, child, strBuilder);
    205227        if (child != node.Subtrees.Last())
    206           strBuilder.Append( ", ");
     228          strBuilder.Append(", ");
    207229      }
    208230      strBuilder.Append(")");
     
    244266    private void FormatFactor(int level, FactorVariableTreeNode node, StringBuilder strBuilder) {
    245267      strBuilder.AppendLine($"( ");
    246       strBuilder.AppendLineIndented(level+1,$"CASE {VariableName2Identifier(node.VariableName)}");
     268      strBuilder.AppendLineIndented(level + 1, $"CASE {VariableName2Identifier(node.VariableName)}");
    247269      foreach (var name in node.Symbol.GetVariableValues(node.VariableName)) {
    248270        strBuilder.AppendLineIndented(level + 2, $"WHEN '{name}' THEN {node.GetValue(name).ToString(CultureInfo.InvariantCulture)}");
    249271      }
    250       strBuilder.AppendLineIndented(level+1, "ELSE NULL END");
     272      strBuilder.AppendLineIndented(level + 1, "ELSE NULL END");
    251273      strBuilder.AppendIndented(level, ")");
    252274    }
Note: See TracChangeset for help on using the changeset viewer.