Changeset 15881 for branches/2911_TSQLSymbolicExpressionFormatter/HeuristicLab.Problems.DataAnalysis.Symbolic
- Timestamp:
- 04/03/18 08:14:29 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2911_TSQLSymbolicExpressionFormatter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/TSQLExpressionFormatter.cs
r15877 r15881 31 31 32 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 33 publicstatic class StringBuilderExtensions {34 publicstatic 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)); 36 36 strBuilder.Append(text); 37 37 } 38 publicstatic 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)); 40 40 strBuilder.AppendLine(text); 41 41 } … … 104 104 105 105 //add variable declaration for convenience 106 strBuilder.AppendLineIndented(1, $"-- added variable declaration for convenience");106 strBuilder.AppendLineIndented(1, $"-- added variable declaration for convenience"); 107 107 foreach (var name in sortedVarcharIdentifiers) 108 108 strBuilder.AppendLineIndented(1, $"-- DECLARE {name} NVARCHAR(max) = ''"); … … 114 114 115 115 private void FormatRecursively(int level, ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 116 // TODO: adapt to interpreter semantics. The HL interpreter also allows Boolean operations on reals117 116 if (node.Subtrees.Any()) { 118 117 if (node.Symbol is Addition) { … … 121 120 FormatOperator(level, node, "AND", strBuilder); 122 121 } else if (node.Symbol is Average) { 123 throw new NotSupportedException($"Symbol {node.Symbol.GetType().Name} not yet supported.");122 FormatAverage(level, node, strBuilder); 124 123 } else if (node.Symbol is Cosine) { 125 124 FormatFunction(level, node, "COS", strBuilder); … … 131 130 FormatOperator(level, node, ">", strBuilder); 132 131 } else if (node.Symbol is IfThenElse) { 133 throw new NotSupportedException($"Symbol {node.Symbol.GetType().Name} not yet supported.");132 FormatIfThenElse(level, node, strBuilder); 134 133 } else if (node.Symbol is LessThan) { 135 134 FormatOperator(level, node, "<", strBuilder); … … 139 138 FormatOperator(level, node, "*", strBuilder); 140 139 } 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); 142 141 } else if (node.Symbol is Or) { 143 142 FormatOperator(level, node, "OR", strBuilder); … … 159 158 FormatRoot(level, node, strBuilder); 160 159 } 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."); 162 161 } 163 162 } else { … … 175 174 throw new NotSupportedException($"Symbol {node.Symbol.GetType().Name} not yet supported."); 176 175 } 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."); 178 177 } 179 178 } 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()}"); 180 202 } 181 203 … … 204 226 FormatRecursively(level++, child, strBuilder); 205 227 if (child != node.Subtrees.Last()) 206 strBuilder.Append( 228 strBuilder.Append(", "); 207 229 } 208 230 strBuilder.Append(")"); … … 244 266 private void FormatFactor(int level, FactorVariableTreeNode node, StringBuilder strBuilder) { 245 267 strBuilder.AppendLine($"( "); 246 strBuilder.AppendLineIndented(level +1,$"CASE {VariableName2Identifier(node.VariableName)}");268 strBuilder.AppendLineIndented(level + 1, $"CASE {VariableName2Identifier(node.VariableName)}"); 247 269 foreach (var name in node.Symbol.GetVariableValues(node.VariableName)) { 248 270 strBuilder.AppendLineIndented(level + 2, $"WHEN '{name}' THEN {node.GetValue(name).ToString(CultureInfo.InvariantCulture)}"); 249 271 } 250 strBuilder.AppendLineIndented(level +1, "ELSE NULL END");272 strBuilder.AppendLineIndented(level + 1, "ELSE NULL END"); 251 273 strBuilder.AppendIndented(level, ")"); 252 274 }
Note: See TracChangeset
for help on using the changeset viewer.