Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/17/10 00:04:06 (14 years ago)
Author:
swinkler
Message:

Implemented first version of Smalltalk representation for most symbols. (#1270)

Location:
branches/SmalltalkExport/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/SmalltalkExport/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters/3.3/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters.csproj

    r4822 r4823  
    1 <?xml version="1.0" encoding="utf-8"?>
     1<?xml version="1.0" encoding="utf-8"?>
    22<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    33  <PropertyGroup>
     
    5959      <HintPath>C:\Program Files\HeuristicLab 3.3\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath>
    6060    </Reference>
     61    <Reference Include="HeuristicLab.Problems.DataAnalysis-3.3">
     62      <HintPath>C:\Program Files\HeuristicLab 3.3\HeuristicLab.Problems.DataAnalysis-3.3.dll</HintPath>
     63    </Reference>
    6164    <Reference Include="Microsoft.Build.Conversion.v3.5" />
    6265    <Reference Include="System" />
  • branches/SmalltalkExport/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters/3.3/SymbolicExpressionTreeSmalltalkStringFormatter.cs

    r4821 r4823  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
     27using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
    2628
    2729namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters {
     
    3133  public class SymbolicExpressionTreeSmalltalkStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
    3234
    33     public bool Indent { get; set; }
    34 
    3535    [StorableConstructor]
    3636    protected SymbolicExpressionTreeSmalltalkStringFormatter(bool deserializing) : base(deserializing) { }
    3737    protected SymbolicExpressionTreeSmalltalkStringFormatter(SymbolicExpressionTreeSmalltalkStringFormatter original, Cloner cloner) : base(original, cloner) { }
    38     public SymbolicExpressionTreeSmalltalkStringFormatter()
    39       : base() {
    40       Indent = true;
     38    public SymbolicExpressionTreeSmalltalkStringFormatter() : base() { }
     39
     40    public string Format(SymbolicExpressionTree symbolicExpressionTree) {
     41      return FormatRecursively(symbolicExpressionTree.Root);
    4142    }
    4243
    43     public string Format(SymbolicExpressionTree symbolicExpressionTree) {
    44       return FormatRecursively(symbolicExpressionTree.Root, 0);
    45     }
     44    private string FormatRecursively(SymbolicExpressionTreeNode node) {
     45      StringBuilder stringBuilder = new StringBuilder();
    4646
    47     private string FormatRecursively(SymbolicExpressionTreeNode node, int indentLength) {
    48       StringBuilder strBuilder = new StringBuilder();
    49       if (Indent) strBuilder.Append(' ', indentLength);
    50       strBuilder.Append(".(");
    51       // internal nodes or leaf nodes?
    52       if (node.SubTrees.Count > 0) {
    53         // symbol on same line as '('
    54         strBuilder.AppendLine(node.ToString());
    55         // each subtree expression on a new line
    56         // and closing ')' also on new line
    57         foreach (var subtree in node.SubTrees) {
    58           strBuilder.AppendLine(FormatRecursively(subtree, indentLength + 2));
     47      Symbol symbol = node.Symbol;
     48      if (symbol is Addition) {
     49        for (int i = 0; i < node.SubTrees.Count; i++) {
     50          if (i > 0) stringBuilder.Append("+");
     51          stringBuilder.Append("(");
     52          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
     53          stringBuilder.Append(")");
    5954        }
    60         if (Indent) strBuilder.Append(' ', indentLength);
    61         strBuilder.Append(")");
    62       } else {
    63         // symbol in the same line with as '(' and ')'
    64         strBuilder.Append(node.ToString());
    65         strBuilder.Append(")");
     55      } else if (symbol is And) {
     56        for (int i = 0; i < node.SubTrees.Count; i++) {
     57          if (i > 0) stringBuilder.Append("&");
     58          stringBuilder.Append("(");
     59          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
     60          stringBuilder.Append(")");
     61        }
     62      } else if (symbol is Average) {
     63        stringBuilder.Append("(1/");
     64        stringBuilder.Append(node.SubTrees.Count);
     65        stringBuilder.Append(")*(");
     66        for (int i = 0; i < node.SubTrees.Count; i++) {
     67          if (i > 0) stringBuilder.Append("+");
     68          stringBuilder.Append("(");
     69          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
     70          stringBuilder.Append(")");
     71        }
     72        stringBuilder.Append(")");
     73      } else if (symbol is Constant) {
     74        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
     75        stringBuilder.Append(constantTreeNode.Value);
     76      } else if (symbol is Cosine) {
     77        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     78        stringBuilder.Append("cos");
     79      } else if (symbol is Division) {
     80        for (int i = 0; i < node.SubTrees.Count; i++) {
     81          if (i > 0) stringBuilder.Append("/");
     82          stringBuilder.Append("(");
     83          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
     84          stringBuilder.Append(")");
     85        }
     86      } else if (symbol is Exponential) {
     87        stringBuilder.Append("not yet implemented");
     88      } else if (symbol is GreaterThan) {
     89        stringBuilder.Append("(");
     90        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     91        stringBuilder.Append(">");
     92        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
     93        stringBuilder.Append(")");
     94      } else if (symbol is IfThenElse) {
     95        stringBuilder.Append("(");
     96        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     97        stringBuilder.Append(" ifTrue: [");
     98        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
     99        stringBuilder.Append("] ifFalse: [");
     100        stringBuilder.Append(FormatRecursively(node.SubTrees[2]));
     101        stringBuilder.Append("] )");
     102      } else if (symbol is LaggedVariable) {
     103        stringBuilder.Append("not implemented");
     104      } else if (symbol is LessThan) {
     105        stringBuilder.Append("(");
     106        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     107        stringBuilder.Append("<");
     108        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
     109        stringBuilder.Append(")");
     110      } else if (symbol is Logarithm) {
     111        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     112        stringBuilder.Append("log");
     113      } else if (symbol is Multiplication) {
     114        for (int i = 0; i < node.SubTrees.Count; i++) {
     115          if (i > 0) stringBuilder.Append("*");
     116          stringBuilder.Append("(");
     117          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
     118          stringBuilder.Append(")");
     119        }
     120      } else if (symbol is Not) {
     121        stringBuilder.Append("!");
     122        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     123      } else if (symbol is Or) {
     124        for (int i = 0; i < node.SubTrees.Count; i++) {
     125          if (i > 0) stringBuilder.Append("|");
     126          stringBuilder.Append("(");
     127          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
     128          stringBuilder.Append(")");
     129        }
     130      } else if (symbol is Sine) {
     131        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     132        stringBuilder.Append("sin");
     133      } else if (symbol is Subtraction) {
     134        for (int i = 0; i < node.SubTrees.Count; i++) {
     135          if (i > 0) stringBuilder.Append("-");
     136          stringBuilder.Append("(");
     137          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
     138          stringBuilder.Append(")");
     139        }
     140      } else if (symbol is Tangent) {
     141        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     142        stringBuilder.Append("tan");
     143      } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) {
     144        stringBuilder.Append("not yet implemented");
    66145      }
    67       return strBuilder.ToString();
     146
     147      else {
     148        stringBuilder.Append("ERROR");
     149      }
     150
     151      return stringBuilder.ToString();
    68152    }
    69153
Note: See TracChangeset for help on using the changeset viewer.