Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/27/13 14:21:20 (11 years ago)
Author:
gkronber
Message:

#1270 adapted smalltalk exporter to match interpreter semantics

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicExpressionTreeSmalltalkStringFormatter.cs

    r9649 r9794  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2020#endregion
    2121
     22using System.Globalization;
    2223using System.Text;
    2324using HeuristicLab.Core;
     
    4142    }
    4243
     44    // returns the smalltalk expression corresponding to the node
     45    // smalltalk expressions are always surrounded by parantheses "(<expr>)"
    4346    private string FormatRecursively(ISymbolicExpressionTreeNode node) {
    4447
     
    4851        return FormatRecursively(node.GetSubtree(0));
    4952
    50       StringBuilder stringBuilder = new StringBuilder();
     53      StringBuilder stringBuilder = new StringBuilder(20);
    5154
    5255      stringBuilder.Append("(");
     
    6164        for (int i = 0; i < node.SubtreeCount; i++) {
    6265          if (i > 0) stringBuilder.Append("&");
    63           stringBuilder.Append("((");
     66          stringBuilder.Append("(");
    6467          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    65           stringBuilder.Append(")>=0)");
     68          stringBuilder.Append(" > 0)");
    6669        }
    6770        stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
     
    7275        for (int i = 0; i < node.SubtreeCount; i++) {
    7376          if (i > 0) stringBuilder.Append("+");
    74           stringBuilder.Append("(");
    7577          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    76           stringBuilder.Append(")");
    7778        }
    7879        stringBuilder.Append(")");
    7980      } else if (symbol is Constant) {
    8081        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
    81         stringBuilder.Append(constantTreeNode.Value.ToString().Replace(",", "."));
     82        stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
    8283      } else if (symbol is Cosine) {
    8384        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     
    114115        stringBuilder.Append("]");
    115116      } else if (symbol is LaggedVariable) {
    116         stringBuilder.Append("not implemented");
     117        stringBuilder.Append("lagged variable not implemented");
    117118      } else if (symbol is LessThan) {
    118119        stringBuilder.Append("(");
     
    130131        }
    131132      } else if (symbol is Not) {
    132         stringBuilder.Append("-1*");
     133        stringBuilder.Append("(");
    133134        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     135        stringBuilder.Append(">0) ifTrue: [-1] ifFalse: [1.0]");
    134136      } else if (symbol is Or) {
    135137        stringBuilder.Append("(");
    136138        for (int i = 0; i < node.SubtreeCount; i++) {
    137139          if (i > 0) stringBuilder.Append("|");
    138           stringBuilder.Append("((");
     140          stringBuilder.Append("(");
    139141          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    140           stringBuilder.Append(")>=0)");
     142          stringBuilder.Append(">0)");
    141143        }
    142144        stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
     
    151153          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    152154          for (int i = 1; i < node.SubtreeCount; i++) {
    153             stringBuilder.Append("-");
     155            stringBuilder.Append(" - ");
    154156            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    155157          }
     
    157159      } else if (symbol is Tangent) {
    158160        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    159         stringBuilder.Append("tan");
     161        stringBuilder.Append(" tan");
    160162      } else if (symbol is Variable) {
    161163        VariableTreeNode variableTreeNode = node as VariableTreeNode;
    162         stringBuilder.Append(variableTreeNode.Weight.ToString().Replace(",", "."));
     164        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
    163165        stringBuilder.Append("*");
    164166        stringBuilder.Append(variableTreeNode.VariableName);
    165167      } else {
    166         stringBuilder.Append("ERROR");
     168        stringBuilder.Append("(");
     169        for (int i = 0; i < node.SubtreeCount; i++) {
     170          if (i > 0) stringBuilder.Append(", ");
     171          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
     172        }
     173        stringBuilder.AppendFormat(" {0} [Not Supported] )", node.Symbol.Name);
    167174      }
    168175
Note: See TracChangeset for help on using the changeset viewer.