Changeset 18203
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r18172 r18203 39 39 if (token == "+" || token == "-" || token == "OR" || token == "XOR") { 40 40 var parenthesisRequired = false; 41 if (node.Parent != null && node.Parent.SubtreeCount > 1) {41 if (node.Parent != null && IsOperator(node.Parent.Symbol)) { 42 42 var parentOp = GetToken(node.Parent.Symbol); 43 43 if (parentOp != "+" && parentOp != "-" && parentOp != "OR" && parentOp != "XOR") … … 154 154 } 155 155 } 156 } 157 158 private static bool IsOperator(ISymbol sy) { 159 return sy is Addition || 160 sy is Subtraction || 161 sy is Multiplication || 162 sy is Division || 163 sy is And || 164 sy is Or || 165 sy is Xor; 156 166 } 157 167 -
trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/InfixExpressionParserTest.cs
r18171 r18203 22 22 23 23 using System; 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 24 25 using Microsoft.VisualStudio.TestTools.UnitTesting; 25 26 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests { … … 148 149 // nested functions 149 150 Assert.AreEqual("SIN(SIN(SIN('X1')))", formatter.Format(parser.Parse("SIN(SIN(SIN(X1)))"))); 151 152 { 153 // a tree with single-arity multiplication and addition 154 // ... 155 // * 156 // | 157 // + 158 // / \ 159 // v1 v2 160 // 161 // is still formatted as (v1 + v2) even though it is not strictly necessary 162 var root = new ProgramRootSymbol().CreateTreeNode(); 163 var start = new StartSymbol().CreateTreeNode(); 164 var mul = new Multiplication().CreateTreeNode(); 165 var add = new Addition().CreateTreeNode(); 166 var var1 = (VariableTreeNode)new Variable().CreateTreeNode(); var1.VariableName = "x1"; var1.Weight = 1.0; 167 var var2 = (VariableTreeNode)new Variable().CreateTreeNode(); var2.VariableName = "x2"; var2.Weight = 1.0; 168 add.AddSubtree(var1); 169 add.AddSubtree(var2); 170 mul.AddSubtree(add); 171 start.AddSubtree(mul); 172 root.AddSubtree(start); 173 var t = new SymbolicExpressionTree(root); 174 175 Assert.AreEqual("('x1' + 'x2')", formatter.Format(t)); // TODO parenthesis not strictly required here 176 } 177 { 178 // * 179 // |\ 180 // * v3 181 // | 182 // + 183 // / \ 184 // v1 v2 185 // 186 // is still formatted as (v1 + v2) even though it is not strictly necessary 187 var root = new ProgramRootSymbol().CreateTreeNode(); 188 var start = new StartSymbol().CreateTreeNode(); 189 var mul1 = new Multiplication().CreateTreeNode(); 190 var mul2 = new Multiplication().CreateTreeNode(); 191 var add = new Addition().CreateTreeNode(); 192 var var1 = (VariableTreeNode)new Variable().CreateTreeNode(); var1.VariableName = "x1"; var1.Weight = 1.0; 193 var var2 = (VariableTreeNode)new Variable().CreateTreeNode(); var2.VariableName = "x2"; var2.Weight = 1.0; 194 var var3 = (VariableTreeNode)new Variable().CreateTreeNode(); var3.VariableName = "x3"; var3.Weight = 1.0; 195 add.AddSubtree(var1); 196 add.AddSubtree(var2); 197 mul2.AddSubtree(add); 198 mul1.AddSubtree(mul2); 199 mul1.AddSubtree(var3); 200 start.AddSubtree(mul1); 201 root.AddSubtree(start); 202 var t = new SymbolicExpressionTree(root); 203 204 Assert.AreEqual("('x1' + 'x2') * 'x3'", formatter.Format(t)); 205 } 206 207 { 208 // sin 209 // | 210 // * 211 // | 212 // + 213 // / \ 214 // v1 v2 215 // 216 // is still formatted as (v1 + v2) even though it is not strictly necessary 217 var root = new ProgramRootSymbol().CreateTreeNode(); 218 var start = new StartSymbol().CreateTreeNode(); 219 var sin = new Sine().CreateTreeNode(); 220 var mul = new Multiplication().CreateTreeNode(); 221 var add = new Addition().CreateTreeNode(); 222 var var1 = (VariableTreeNode)new Variable().CreateTreeNode(); var1.VariableName = "x1"; var1.Weight = 1.0; 223 var var2 = (VariableTreeNode)new Variable().CreateTreeNode(); var2.VariableName = "x2"; var2.Weight = 1.0; 224 add.AddSubtree(var1); 225 add.AddSubtree(var2); 226 mul.AddSubtree(add); 227 sin.AddSubtree(mul); 228 start.AddSubtree(sin); 229 root.AddSubtree(start); 230 var t = new SymbolicExpressionTree(root); 231 232 Assert.AreEqual("SIN(('x1' + 'x2'))", formatter.Format(t)); // TODO would be better to prevent double parenthesis here 233 } 150 234 } 151 235 }
Note: See TracChangeset
for help on using the changeset viewer.