Changeset 17685
- Timestamp:
- 07/18/20 19:57:08 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r17581 r17685 29 29 using HeuristicLab.Core; 30 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 31 using Microsoft.SqlServer.Server; 31 32 32 33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 83 84 if (!varNode.Weight.IsAlmost(1.0)) { 84 85 strBuilder.Append("("); 85 if (constants != null) { 86 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight); 87 } else { 88 strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); 89 } 86 AppendConstant(strBuilder, constants, varNode.Weight, formatString, numberFormat); 90 87 strBuilder.Append("*"); 91 88 } 92 89 93 90 strBuilder.Append("LAG("); 94 if (varNode.VariableName.Contains("'")) 95 strBuilder.AppendFormat("\"{0}\"", varNode.VariableName); 96 else 97 strBuilder.AppendFormat("'{0}'", varNode.VariableName); 98 91 AppendVariableName(strBuilder, varNode.VariableName); 99 92 strBuilder.Append(", ") 100 93 .AppendFormat(numberFormat, "{0}", varNode.Lag) 101 94 .Append(")"); 95 if (!varNode.Weight.IsAlmost(1.0)) strBuilder.Append(")"); 102 96 } else if (node.Symbol is Variable) { 103 97 var varNode = node as VariableTreeNode; 104 98 if (!varNode.Weight.IsAlmost(1.0)) { 105 99 strBuilder.Append("("); 106 if (constants != null) { 107 string constantKey = $"c_{constants.Count}"; 108 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constantKey); 109 constants.Add(new KeyValuePair<string, double>(constantKey, varNode.Weight)); 110 } else { 111 strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); 112 } 113 100 AppendConstant(strBuilder, constants, varNode.Weight, formatString, numberFormat); 114 101 strBuilder.Append("*"); 115 102 } 116 103 117 if (varNode.VariableName.Contains("'")) 118 strBuilder.AppendFormat("\"{0}\"", varNode.VariableName); 119 else 120 strBuilder.AppendFormat("'{0}'", varNode.VariableName); 104 AppendVariableName(strBuilder, varNode.VariableName); 121 105 122 106 if (!varNode.Weight.IsAlmost(1.0)) strBuilder.Append(")"); 123 107 } else if (node.Symbol is FactorVariable) { 124 108 var factorNode = node as FactorVariableTreeNode; 125 if (factorNode.VariableName.Contains("'")) 126 strBuilder.AppendFormat("\"{0}\"", factorNode.VariableName); 127 else 128 strBuilder.AppendFormat("'{0}'", factorNode.VariableName); 129 130 strBuilder.AppendFormat("[{0}]", 131 string.Join(", ", factorNode.Weights.Select(w => w.ToString(formatString, numberFormat)))); 109 AppendVariableName(strBuilder, factorNode.VariableName); 110 111 strBuilder.Append("["); 112 for (int i = 0; i < factorNode.Weights.Length; i++) { 113 if (i > 0) strBuilder.Append(", "); 114 AppendConstant(strBuilder, constants, factorNode.Weights[i], formatString, numberFormat); 115 } 116 strBuilder.Append("]"); 132 117 } else if (node.Symbol is BinaryFactorVariable) { 133 118 var factorNode = node as BinaryFactorVariableTreeNode; 134 119 if (!factorNode.Weight.IsAlmost(1.0)) { 135 120 strBuilder.Append("("); 136 if (constants != null) { 137 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", factorNode.Weight); 138 } else { 139 strBuilder.Append(factorNode.Weight.ToString(formatString, numberFormat)); 140 } 121 AppendConstant(strBuilder, constants, factorNode.Weight, formatString, numberFormat); 141 122 142 123 strBuilder.Append("*"); 143 124 } 144 125 145 if (factorNode.VariableName.Contains("'")) 146 strBuilder.AppendFormat("\"{0}\"", factorNode.VariableName); 147 else 148 strBuilder.AppendFormat("'{0}'", factorNode.VariableName); 149 126 AppendVariableName(strBuilder, factorNode.VariableName); 150 127 strBuilder.Append(" = "); 151 if (factorNode.VariableValue.Contains("'")) 152 strBuilder.AppendFormat("\"{0}\"", factorNode.VariableValue); 153 else 154 strBuilder.AppendFormat("'{0}'", factorNode.VariableValue); 128 AppendVariableName(strBuilder, factorNode.VariableValue); 155 129 156 130 if (!factorNode.Weight.IsAlmost(1.0)) strBuilder.Append(")"); 157 131 } else if (node.Symbol is Constant) { 158 132 var constNode = node as ConstantTreeNode; 159 if (constants != null) { 160 string constantKey = $"c_{constants.Count}"; 161 162 strBuilder.AppendFormat(CultureInfo.InvariantCulture, constantKey); 163 constants.Add(new KeyValuePair<string, double>(constantKey, constNode.Value)); 133 if(constants==null && constNode.Value < 0) { 134 strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat)) 135 .Append(")"); // (-1 164 136 } else { 165 if (constNode.Value >= 0.0) 166 strBuilder.Append(constNode.Value.ToString(formatString, numberFormat)); 167 else 168 strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat)) 169 .Append(")"); // (-1 137 AppendConstant(strBuilder, constants, constNode.Value, formatString, numberFormat); 170 138 } 171 139 } 172 140 } 141 } 142 143 private static void AppendConstant(StringBuilder strBuilder, List<KeyValuePair<string, double>> constants, double value, string formatString, NumberFormatInfo numberFormat) { 144 if (constants != null) { 145 string constantKey = $"c_{constants.Count}"; 146 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constantKey); 147 constants.Add(new KeyValuePair<string, double>(constantKey, value)); 148 } else { 149 strBuilder.Append(value.ToString(formatString, numberFormat)); 150 } 151 } 152 153 private static void AppendVariableName(StringBuilder strBuilder, string name) { 154 if (name.Contains("'")) 155 strBuilder.AppendFormat("\"{0}\"", name); 156 else 157 strBuilder.AppendFormat("'{0}'", name); 173 158 } 174 159
Note: See TracChangeset
for help on using the changeset viewer.