Changeset 14823 for branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
- Timestamp:
- 04/04/17 16:53:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r14761 r14823 60 60 61 61 private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 62 if (node.SubtreeCount > 1) {62 if (node.SubtreeCount > 1) { 63 63 var token = GetToken(node.Symbol); 64 if (token == "+" || token == "-" || token == "OR" || token == "XOR") {64 if (token == "+" || token == "-" || token == "OR" || token == "XOR") { 65 65 strBuilder.Append("("); 66 66 FormatRecursively(node.Subtrees.First(), strBuilder); 67 67 68 foreach (var subtree in node.Subtrees.Skip(1)) {68 foreach (var subtree in node.Subtrees.Skip(1)) { 69 69 strBuilder.Append(" ").Append(token).Append(" "); 70 70 FormatRecursively(subtree, strBuilder); … … 72 72 strBuilder.Append(")"); 73 73 74 } else if (token == "*" || token == "/" || token == "AND") {74 } else if (token == "*" || token == "/" || token == "AND") { 75 75 strBuilder.Append("("); 76 76 FormatRecursively(node.Subtrees.First(), strBuilder); 77 77 78 foreach (var subtree in node.Subtrees.Skip(1)) {78 foreach (var subtree in node.Subtrees.Skip(1)) { 79 79 strBuilder.Append(" ").Append(token).Append(" "); 80 80 FormatRecursively(subtree, strBuilder); … … 85 85 strBuilder.Append(token).Append("("); 86 86 FormatRecursively(node.Subtrees.First(), strBuilder); 87 foreach (var subtree in node.Subtrees.Skip(1)) {87 foreach (var subtree in node.Subtrees.Skip(1)) { 88 88 strBuilder.Append(", "); 89 89 FormatRecursively(subtree, strBuilder); … … 91 91 strBuilder.Append(")"); 92 92 } 93 } else if (node.SubtreeCount == 1) {93 } else if (node.SubtreeCount == 1) { 94 94 var token = GetToken(node.Symbol); 95 if (token == "-" || token == "NOT") {95 if (token == "-" || token == "NOT") { 96 96 strBuilder.Append("(").Append(token).Append("("); 97 97 FormatRecursively(node.GetSubtree(0), strBuilder); 98 98 strBuilder.Append("))"); 99 } else if (token == "/") {99 } else if (token == "/") { 100 100 strBuilder.Append("1/"); 101 101 FormatRecursively(node.GetSubtree(0), strBuilder); 102 } else if (token == "+" || token == "*") {102 } else if (token == "+" || token == "*") { 103 103 FormatRecursively(node.GetSubtree(0), strBuilder); 104 104 } else { … … 110 110 } else { 111 111 // no subtrees 112 if (node.Symbol is LaggedVariable) {112 if (node.Symbol is LaggedVariable) { 113 113 var varNode = node as LaggedVariableTreeNode; 114 if (!varNode.Weight.IsAlmost(1.0)) {114 if (!varNode.Weight.IsAlmost(1.0)) { 115 115 strBuilder.Append("("); 116 116 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight); … … 118 118 } 119 119 strBuilder.Append("LAG("); 120 if (varNode.VariableName.Contains("'")) {120 if (varNode.VariableName.Contains("'")) { 121 121 strBuilder.AppendFormat("\"{0}\"", varNode.VariableName); 122 122 } else { … … 126 126 .AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Lag) 127 127 .Append(")"); 128 } else if (node.Symbol is Variable) {128 } else if (node.Symbol is Variable) { 129 129 var varNode = node as VariableTreeNode; 130 if (!varNode.Weight.IsAlmost(1.0)) {130 if (!varNode.Weight.IsAlmost(1.0)) { 131 131 strBuilder.Append("("); 132 132 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight); 133 133 strBuilder.Append("*"); 134 134 } 135 if (varNode.VariableName.Contains("'")) {135 if (varNode.VariableName.Contains("'")) { 136 136 strBuilder.AppendFormat("\"{0}\"", varNode.VariableName); 137 137 } else { 138 138 strBuilder.AppendFormat("'{0}'", varNode.VariableName); 139 139 } 140 if (!varNode.Weight.IsAlmost(1.0)) {140 if (!varNode.Weight.IsAlmost(1.0)) { 141 141 strBuilder.Append(")"); 142 142 } 143 } else if (node.Symbol is FactorVariable) {143 } else if (node.Symbol is FactorVariable) { 144 144 var factorNode = node as FactorVariableTreeNode; 145 if (factorNode.VariableName.Contains("'")) {145 if (factorNode.VariableName.Contains("'")) { 146 146 strBuilder.AppendFormat("\"{0}\"", factorNode.VariableName); 147 147 } else { … … 150 150 strBuilder.AppendFormat("[{0}]", 151 151 string.Join(", ", factorNode.Weights.Select(w => w.ToString(CultureInfo.InvariantCulture)))); 152 } else if (node.Symbol is BinaryFactorVariable) {152 } else if (node.Symbol is BinaryFactorVariable) { 153 153 var factorNode = node as BinaryFactorVariableTreeNode; 154 if (!factorNode.Weight.IsAlmost(1.0)) {154 if (!factorNode.Weight.IsAlmost(1.0)) { 155 155 strBuilder.Append("("); 156 156 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", factorNode.Weight); 157 157 strBuilder.Append("*"); 158 158 } 159 if (factorNode.VariableName.Contains("'")) {160 strBuilder.AppendFormat("\"{0} ={1}\"", factorNode.VariableName, factorNode.VariableValue);159 if (factorNode.VariableName.Contains("'")) { 160 strBuilder.AppendFormat("\"{0}\"", factorNode.VariableName); 161 161 } else { 162 strBuilder.AppendFormat("'{0} ={1}'", factorNode.VariableName, factorNode.VariableValue);162 strBuilder.AppendFormat("'{0}'", factorNode.VariableName); 163 163 } 164 if(!factorNode.Weight.IsAlmost(1.0)) { 164 strBuilder.Append(" = "); 165 if (factorNode.VariableValue.Contains("'")) { 166 strBuilder.AppendFormat("\"{0}\"", factorNode.VariableValue); 167 } else { 168 strBuilder.AppendFormat("'{0}'", factorNode.VariableValue); 169 } 170 171 if (!factorNode.Weight.IsAlmost(1.0)) { 165 172 strBuilder.Append(")"); 166 173 } 167 174 168 } else if (node.Symbol is Constant) {175 } else if (node.Symbol is Constant) { 169 176 var constNode = node as ConstantTreeNode; 170 if (constNode.Value >= 0.0)177 if (constNode.Value >= 0.0) 171 178 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constNode.Value); 172 179 else … … 178 185 private string GetToken(ISymbol symbol) { 179 186 var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).SingleOrDefault(); 180 if (tok == null)187 if (tok == null) 181 188 throw new ArgumentException(string.Format("Unknown symbol {0} found.", symbol.Name)); 182 189 return tok;
Note: See TracChangeset
for help on using the changeset viewer.