Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14814 for branches


Ignore:
Timestamp:
03/31/17 15:47:55 (8 years ago)
Author:
mkommend
Message:

#2650: Removed misleading comment from C# formatter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs

    r14720 r14814  
    5656    }
    5757
    58     private string VariableName2Identifier(string name) {
    59       // tries to convert a variable name to a valid C# identifier.
    60       // the following code would work for all possible variable names
    61       // return "_" + string.Join("_", Encoding.UTF8.GetBytes(name));
    62 
     58    private string VariableName2Identifier(string name) {     
    6359      /*
    6460       * identifier-start-character:
     
    10096    private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    10197      // TODO: adapt to interpreter semantics. The HL interpreter also allows Boolean operations on reals
    102       if(node.Subtrees.Any()) {
    103         if(node.Symbol is Addition) {
     98      if (node.Subtrees.Any()) {
     99        if (node.Symbol is Addition) {
    104100          FormatOperator(node, "+", strBuilder);
    105         } else if(node.Symbol is And) {
     101        } else if (node.Symbol is And) {
    106102          FormatOperator(node, "&&", strBuilder);
    107         } else if(node.Symbol is Average) {
     103        } else if (node.Symbol is Average) {
    108104          FormatFunction(node, "Average", strBuilder);
    109         } else if(node.Symbol is Cosine) {
     105        } else if (node.Symbol is Cosine) {
    110106          FormatFunction(node, "Math.Cos", strBuilder);
    111         } else if(node.Symbol is Division) {
     107        } else if (node.Symbol is Division) {
    112108          FormatDivision(node, strBuilder);
    113         } else if(node.Symbol is Exponential) {
     109        } else if (node.Symbol is Exponential) {
    114110          FormatFunction(node, "Math.Exp", strBuilder);
    115         } else if(node.Symbol is GreaterThan) {
     111        } else if (node.Symbol is GreaterThan) {
    116112          FormatOperator(node, ">", strBuilder);
    117         } else if(node.Symbol is IfThenElse) {
     113        } else if (node.Symbol is IfThenElse) {
    118114          FormatFunction(node, "EvaluateIf", strBuilder);
    119         } else if(node.Symbol is LessThan) {
     115        } else if (node.Symbol is LessThan) {
    120116          FormatOperator(node, "<", strBuilder);
    121         } else if(node.Symbol is Logarithm) {
     117        } else if (node.Symbol is Logarithm) {
    122118          FormatFunction(node, "Math.Log", strBuilder);
    123         } else if(node.Symbol is Multiplication) {
     119        } else if (node.Symbol is Multiplication) {
    124120          FormatOperator(node, "*", strBuilder);
    125         } else if(node.Symbol is Not) {
     121        } else if (node.Symbol is Not) {
    126122          FormatOperator(node, "!", strBuilder);
    127         } else if(node.Symbol is Or) {
     123        } else if (node.Symbol is Or) {
    128124          FormatOperator(node, "||", strBuilder);
    129         } else if(node.Symbol is Xor) {
     125        } else if (node.Symbol is Xor) {
    130126          FormatOperator(node, "^", strBuilder);
    131         } else if(node.Symbol is Sine) {
     127        } else if (node.Symbol is Sine) {
    132128          FormatFunction(node, "Math.Sin", strBuilder);
    133         } else if(node.Symbol is Subtraction) {
     129        } else if (node.Symbol is Subtraction) {
    134130          FormatSubtraction(node, strBuilder);
    135         } else if(node.Symbol is Tangent) {
     131        } else if (node.Symbol is Tangent) {
    136132          FormatFunction(node, "Math.Tan", strBuilder);
    137         } else if(node.Symbol is Square) {
     133        } else if (node.Symbol is Square) {
    138134          FormatSquare(node, strBuilder);
    139         } else if(node.Symbol is SquareRoot) {
     135        } else if (node.Symbol is SquareRoot) {
    140136          FormatFunction(node, "Math.Sqrt", strBuilder);
    141         } else if(node.Symbol is Power) {
     137        } else if (node.Symbol is Power) {
    142138          FormatFunction(node, "Math.Pow", strBuilder);
    143         } else if(node.Symbol is Root) {
     139        } else if (node.Symbol is Root) {
    144140          FormatRoot(node, strBuilder);
    145141        } else {
     
    147143        }
    148144      } else {
    149         if(node is VariableTreeNode) {
     145        if (node is VariableTreeNode) {
    150146          var varNode = node as VariableTreeNode;
    151147          strBuilder.AppendFormat("{0} * {1}", VariableName2Identifier(varNode.VariableName), varNode.Weight.ToString("g17", CultureInfo.InvariantCulture));
    152         } else if(node is ConstantTreeNode) {
     148        } else if (node is ConstantTreeNode) {
    153149          var constNode = node as ConstantTreeNode;
    154150          strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture));
    155         } else if(node.Symbol is FactorVariable) {
     151        } else if (node.Symbol is FactorVariable) {
    156152          var factorNode = node as FactorVariableTreeNode;
    157153          FormatFactor(factorNode, strBuilder);
    158         } else if(node.Symbol is BinaryFactorVariable) {
     154        } else if (node.Symbol is BinaryFactorVariable) {
    159155          var binFactorNode = node as BinaryFactorVariableTreeNode;
    160156          FormatBinaryFactor(binFactorNode, strBuilder);
     
    189185
    190186    private void FormatDivision(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    191       if(node.SubtreeCount == 1) {
     187      if (node.SubtreeCount == 1) {
    192188        strBuilder.Append("1.0 / ");
    193189        FormatRecursively(node.GetSubtree(0), strBuilder);
     
    195191        FormatRecursively(node.GetSubtree(0), strBuilder);
    196192        strBuilder.Append("/ (");
    197         for(int i = 1; i < node.SubtreeCount; i++) {
    198           if(i > 1) strBuilder.Append(" * ");
     193        for (int i = 1; i < node.SubtreeCount; i++) {
     194          if (i > 1) strBuilder.Append(" * ");
    199195          FormatRecursively(node.GetSubtree(i), strBuilder);
    200196        }
     
    204200
    205201    private void FormatSubtraction(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    206       if(node.SubtreeCount == 1) {
     202      if (node.SubtreeCount == 1) {
    207203        strBuilder.Append("-");
    208204        FormatRecursively(node.GetSubtree(0), strBuilder);
     
    215211    private void FormatOperator(ISymbolicExpressionTreeNode node, string symbol, StringBuilder strBuilder) {
    216212      strBuilder.Append("(");
    217       foreach(var child in node.Subtrees) {
     213      foreach (var child in node.Subtrees) {
    218214        FormatRecursively(child, strBuilder);
    219         if(child != node.Subtrees.Last())
     215        if (child != node.Subtrees.Last())
    220216          strBuilder.Append(" " + symbol + " ");
    221217      }
     
    225221    private void FormatFunction(ISymbolicExpressionTreeNode node, string function, StringBuilder strBuilder) {
    226222      strBuilder.Append(function + "(");
    227       foreach(var child in node.Subtrees) {
     223      foreach (var child in node.Subtrees) {
    228224        FormatRecursively(child, strBuilder);
    229         if(child != node.Subtrees.Last())
     225        if (child != node.Subtrees.Last())
    230226          strBuilder.Append(", ");
    231227      }
     
    246242      // here we don't have access to problemData to determine the type for each variable (double/string) therefore we must distinguish based on the symbol type
    247243      HashSet<string> doubleVarNames = new HashSet<string>();
    248       foreach(var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is VariableTreeNode || x is VariableConditionTreeNode)) {
     244      foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is VariableTreeNode || x is VariableConditionTreeNode)) {
    249245        doubleVarNames.Add(((IVariableTreeNode)node).VariableName);
    250246      }
    251247
    252248      HashSet<string> stringVarNames = new HashSet<string>();
    253       foreach(var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is BinaryFactorVariableTreeNode || x is FactorVariableTreeNode)) {
     249      foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is BinaryFactorVariableTreeNode || x is FactorVariableTreeNode)) {
    254250        stringVarNames.Add(((IVariableTreeNode)node).VariableName);
    255251      }
     
    258254      strBuilder.Append(string.Join(", ", orderedNames));
    259255
    260       if(stringVarNames.Any() && doubleVarNames.Any())
     256      if (stringVarNames.Any() && doubleVarNames.Any())
    261257        strBuilder.AppendLine(",");
    262258      orderedNames = doubleVarNames.OrderBy(n => n, new NaturalStringComparer()).Select(n => "double " + VariableName2Identifier(n) + " /* " + n + " */");
Note: See TracChangeset for help on using the changeset viewer.