Changeset 14761 for branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
- Timestamp:
- 03/18/17 12:17:13 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r14351 r14761 60 60 61 61 private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 62 if 62 if(node.SubtreeCount > 1) { 63 63 var token = GetToken(node.Symbol); 64 if 64 if(token == "+" || token == "-" || token == "OR" || token == "XOR") { 65 65 strBuilder.Append("("); 66 66 FormatRecursively(node.Subtrees.First(), strBuilder); 67 67 68 foreach 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 74 } else if(token == "*" || token == "/" || token == "AND") { 75 75 strBuilder.Append("("); 76 76 FormatRecursively(node.Subtrees.First(), strBuilder); 77 77 78 foreach 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 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 93 } else if(node.SubtreeCount == 1) { 94 94 var token = GetToken(node.Symbol); 95 if 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 99 } else if(token == "/") { 100 100 strBuilder.Append("1/"); 101 101 FormatRecursively(node.GetSubtree(0), strBuilder); 102 } else if 102 } else if(token == "+" || token == "*") { 103 103 FormatRecursively(node.GetSubtree(0), strBuilder); 104 104 } else { … … 110 110 } else { 111 111 // no subtrees 112 if 112 if(node.Symbol is LaggedVariable) { 113 113 var varNode = node as LaggedVariableTreeNode; 114 if 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 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 128 } else if(node.Symbol is Variable) { 129 129 var varNode = node as VariableTreeNode; 130 if 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 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 140 if(!varNode.Weight.IsAlmost(1.0)) { 141 141 strBuilder.Append(")"); 142 142 } 143 } else if 143 } else if(node.Symbol is FactorVariable) { 144 144 var factorNode = node as FactorVariableTreeNode; 145 if 145 if(factorNode.VariableName.Contains("'")) { 146 146 strBuilder.AppendFormat("\"{0}\"", factorNode.VariableName); 147 147 } else { 148 148 strBuilder.AppendFormat("'{0}'", factorNode.VariableName); 149 149 } 150 } else if (node.Symbol is BinaryFactorVariable) { 150 strBuilder.AppendFormat("[{0}]", 151 string.Join(", ", factorNode.Weights.Select(w => w.ToString(CultureInfo.InvariantCulture)))); 152 } else if(node.Symbol is BinaryFactorVariable) { 151 153 var factorNode = node as BinaryFactorVariableTreeNode; 152 if 154 if(!factorNode.Weight.IsAlmost(1.0)) { 153 155 strBuilder.Append("("); 154 156 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", factorNode.Weight); 155 157 strBuilder.Append("*"); 156 158 } 157 if 159 if(factorNode.VariableName.Contains("'")) { 158 160 strBuilder.AppendFormat("\"{0}={1}\"", factorNode.VariableName, factorNode.VariableValue); 159 161 } else { 160 162 strBuilder.AppendFormat("'{0}={1}'", factorNode.VariableName, factorNode.VariableValue); 161 163 } 162 if 164 if(!factorNode.Weight.IsAlmost(1.0)) { 163 165 strBuilder.Append(")"); 164 166 } 165 167 166 } else if 168 } else if(node.Symbol is Constant) { 167 169 var constNode = node as ConstantTreeNode; 168 if 170 if(constNode.Value >= 0.0) 169 171 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constNode.Value); 170 172 else … … 176 178 private string GetToken(ISymbol symbol) { 177 179 var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).SingleOrDefault(); 178 if 180 if(tok == null) 179 181 throw new ArgumentException(string.Format("Unknown symbol {0} found.", symbol.Name)); 180 182 return tok;
Note: See TracChangeset
for help on using the changeset viewer.