Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/14/08 14:42:29 (16 years ago)
Author:
gkronber
Message:

worked on exporters and display of function-names in the functionView (#177)

Location:
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification
Files:
26 edited

Legend:

Unmodified
Added
Removed
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Addition.cs

    r645 r654  
    4747      AddConstraint(new SubOperatorTypeConstraint(2));
    4848    }
    49 
    50     public override void Accept(IFunctionVisitor visitor) {
    51       visitor.Visit(this);
    52     }
    5349  }
    5450}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/And.cs

    r645 r654  
    4343      AddConstraint(new SubOperatorTypeConstraint(2));
    4444    }
    45 
    46     public override void Accept(IFunctionVisitor visitor) {
    47       visitor.Visit(this);
    48     }
    4945  }
    5046}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Average.cs

    r645 r654  
    4141      AddConstraint(new SubOperatorTypeConstraint(2));
    4242    }
    43 
    44     public override void Accept(IFunctionVisitor visitor) {
    45       visitor.Visit(this);
    46     }
    4743  }
    4844}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Constant.cs

    r645 r654  
    9090      AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp));
    9191    }
    92 
    93     public override void Accept(IFunctionVisitor visitor) {
    94       visitor.Visit(this);
    95     }
    9692  }
    9793}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Cosinus.cs

    r645 r654  
    4040      AddConstraint(new SubOperatorTypeConstraint(0));
    4141    }
    42 
    43     public override void Accept(IFunctionVisitor visitor) {
    44       visitor.Visit(this);
    45     }
    4642  }
    4743}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Differential.cs

    r645 r654  
    4343      : base() {
    4444    }
    45 
    46     public override void Accept(IFunctionVisitor visitor) {
    47       visitor.Visit(this);
    48     }
    4945  }
    5046}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Division.cs

    r645 r654  
    5454      AddConstraint(new SubOperatorTypeConstraint(2));
    5555    }
    56 
    57     public override void Accept(IFunctionVisitor visitor) {
    58       visitor.Visit(this);
    59     }
    6056  }
    6157}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Equal.cs

    r645 r654  
    4141      AddConstraint(new SubOperatorTypeConstraint(1));
    4242    }
    43 
    44     public override void Accept(IFunctionVisitor visitor) {
    45       visitor.Visit(this);
    46     }
    4743  }
    4844}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Exponential.cs

    r645 r654  
    4040      AddConstraint(new SubOperatorTypeConstraint(0));
    4141    }
    42 
    43     public override void Accept(IFunctionVisitor visitor) {
    44       visitor.Visit(this);
    45     }
    4642  }
    4743}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/GreaterThan.cs

    r645 r654  
    4040      AddConstraint(new SubOperatorTypeConstraint(1));
    4141    }
    42     public override void Accept(IFunctionVisitor visitor) {
    43       visitor.Visit(this);
    44     }
    4542  }
    4643}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/IfThenElse.cs

    r645 r654  
    4343      AddConstraint(new SubOperatorTypeConstraint(2));
    4444    }
    45 
    46     public override void Accept(IFunctionVisitor visitor) {
    47       visitor.Visit(this);
    48     }
    4945  }
    5046}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/LessThan.cs

    r645 r654  
    4141      AddConstraint(new SubOperatorTypeConstraint(1));
    4242    }
    43 
    44     public override void Accept(IFunctionVisitor visitor) {
    45       visitor.Visit(this);
    46     }
    4743  }
    4844}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Logarithm.cs

    r645 r654  
    4040      AddConstraint(new SubOperatorTypeConstraint(0));
    4141    }
    42 
    43     public override void Accept(IFunctionVisitor visitor) {
    44       visitor.Visit(this);
    45     }
    4642  }
    4743}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/ModelAnalyzerExporter.cs

    r645 r654  
    2727
    2828namespace HeuristicLab.GP.StructureIdentification {
    29   public class ModelAnalyzerExporter : IFunctionVisitor {
    30     private string prefix;
    31     private string currentIndent = "";
    32     private IFunctionTree currentBranch;
    33     public string ModelAnalyzerPrefix {
    34       get { return prefix; }
    35     }
    36     public void Reset() {
    37       prefix = "";
    38     }
    39 
    40     private void VisitFunction(string name, IFunction f) {
    41       prefix += currentIndent + "[F]" + name + "(\n";
    42     }
    43 
    44     #region IFunctionVisitor Members
    45 
    46     public void Visit(IFunction function) {
    47       prefix += function.Name;
    48     }
    49 
    50     public void Visit(Addition addition) {
    51       VisitFunction("Addition[0]", addition);
    52     }
    53 
    54     public void Visit(Constant constant) {
    55       double value = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(Constant.VALUE).Value).Data;
    56       prefix += currentIndent + "[T]Constant(" + value.ToString("r") + ";0;0)";
    57     }
    58 
    59     public void Visit(Cosinus cosinus) {
    60       VisitFunction("Trigonometrics[1]", cosinus);
    61     }
    62 
    63     public void Visit(Differential differential) {
    64       double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(Differential.WEIGHT).Value).Data;
    65       double index = ((ConstrainedIntData)currentBranch.GetLocalVariable(Differential.INDEX).Value).Data;
    66       double offset = ((ConstrainedIntData)currentBranch.GetLocalVariable(Differential.OFFSET).Value).Data;
    67 
    68       prefix += currentIndent + "[T]Differential(" + weight.ToString("r") + ";" + index + ";" + -offset + ")";
    69     }
    70 
    71     public void Visit(Division division) {
    72       VisitFunction("Division[0]", division);
    73     }
    74 
    75     public void Visit(Exponential exponential) {
    76       VisitFunction("Exponential[0]", exponential);
    77     }
    78 
    79     public void Visit(Logarithm logarithm) {
    80       VisitFunction("Logarithm[0]", logarithm);
    81     }
    82 
    83     public void Visit(Multiplication multiplication) {
    84       VisitFunction("Multiplication[0]", multiplication);
    85     }
    86 
    87     public void Visit(Power power) {
    88       VisitFunction("Power[0]", power);
    89     }
    90 
    91     public void Visit(Signum signum) {
    92       VisitFunction("Signum[0]", signum);
    93     }
    94 
    95     public void Visit(Sinus sinus) {
    96       VisitFunction("Trigonometrics[0]", sinus);
    97     }
    98 
    99     public void Visit(Sqrt sqrt) {
    100       VisitFunction("Sqrt[0]", sqrt);
    101     }
    102 
    103     public void Visit(Subtraction substraction) {
    104       VisitFunction("Subtraction[0]", substraction);
    105     }
    106 
    107     public void Visit(Tangens tangens) {
    108       VisitFunction("Trigonometrics[2]", tangens);
    109     }
    110 
    111     public void Visit(Variable variable) {
    112       double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(Variable.WEIGHT).Value).Data;
    113       double index = ((ConstrainedIntData)currentBranch.GetLocalVariable(Variable.INDEX).Value).Data;
    114       double offset = ((ConstrainedIntData)currentBranch.GetLocalVariable(Variable.OFFSET).Value).Data;
    115 
    116       prefix += currentIndent + "[T]Variable(" + weight.ToString("r") + ";" + index + ";" + -offset + ")";
    117     }
    118 
    119     public void Visit(And and) {
    120       VisitFunction("Logical[0]", and);
    121     }
    122 
    123     public void Visit(Average average) {
    124       VisitFunction("N/A (average)", average);
    125     }
    126 
    127     public void Visit(IfThenElse ifThenElse) {
    128       VisitFunction("Conditional[0]", ifThenElse);
    129     }
    130 
    131     public void Visit(Not not) {
    132       VisitFunction("Logical[2]", not);
    133     }
    134 
    135     public void Visit(Or or) {
    136       VisitFunction("Logical[1]", or);
    137     }
    138 
    139     public void Visit(Xor xor) {
    140       VisitFunction("N/A (xor)", xor);
    141     }
    142 
    143     public void Visit(Equal equal) {
    144       VisitFunction("Boolean[2]", equal);
    145     }
    146 
    147     public void Visit(LessThan lessThan) {
    148       VisitFunction("Boolean[0]", lessThan);
    149     }
    150 
    151     public void Visit(GreaterThan greaterThan) {
    152       VisitFunction("Boolean[4]", greaterThan);
    153     }
     29  public class ModelAnalyzerExporter : IFunctionTreeExporter, IFunctionTreeNameGenerator {
     30    #region IFunctionTreeExporter Members
     31
     32    public string Name {
     33      get {
     34        return "HL2 ModelAnalyzer Exporter";
     35      }
     36    }
     37
     38    public bool TryExport(IFunctionTree tree, out string exported) {
     39      try {
     40        exported = Export(tree);
     41        return true;
     42      } catch(UnknownFunctionException) {
     43        exported = "";
     44        return false;
     45      }
     46    }
     47
     48    public string Export(IFunctionTree tree) {
     49      string result = ExportFunction(tree.Function, tree);
     50      result += "(\n";
     51      foreach(IFunctionTree subTree in tree.SubTrees) {
     52        result += Export(subTree);
     53        result += ";\n";
     54      }
     55      result = result.TrimEnd(';', '\n');
     56      if(tree.SubTrees.Count > 0) result += ")";
     57      return result;
     58    }
     59
    15460    #endregion
    15561
    156     public void Visit(IFunctionTree functionTree) {
    157       currentBranch = functionTree;
    158       functionTree.Function.Accept(this);
    159       currentIndent += "  ";
    160       foreach(IFunctionTree subTree in functionTree.SubTrees) {
    161         Visit(subTree);
    162         prefix += ";\n";
    163       }
    164       prefix = prefix.TrimEnd(';', '\n');
    165       if(functionTree.SubTrees.Count>0) prefix += ")";
    166       currentIndent = currentIndent.Remove(0, 2);
     62    private string ExportFunction(IFunction function, IFunctionTree tree) {
     63      // this is smelly, if there is a cleaner way to have a 'dynamic' visitor
     64      // please let me know! (gkronber 14.10.2008)
     65      if(function is Addition) return ((Addition)function).ExportToHL2(tree);
     66      if(function is And) return ((And)function).ExportToHL2(tree);
     67      if(function is Average) return ((Average)function).ExportToHL2(tree);
     68      if(function is Constant) return ((Constant)function).ExportToHL2(tree);
     69      if(function is Cosinus) return ((Cosinus)function).ExportToHL2(tree);
     70      if(function is Differential) return ((Differential)function).ExportToHL2(tree);
     71      if(function is Division) return ((Division)function).ExportToHL2(tree);
     72      if(function is Equal) return ((Equal)function).ExportToHL2(tree);
     73      if(function is Exponential) return ((Exponential)function).ExportToHL2(tree);
     74      if(function is GreaterThan) return ((GreaterThan)function).ExportToHL2(tree);
     75      if(function is IfThenElse) return ((IfThenElse)function).ExportToHL2(tree);
     76      if(function is LessThan) return ((LessThan)function).ExportToHL2(tree);
     77      if(function is Logarithm) return ((Logarithm)function).ExportToHL2(tree);
     78      if(function is Multiplication) return ((Multiplication)function).ExportToHL2(tree);
     79      if(function is Not) return ((Not)function).ExportToHL2(tree);
     80      if(function is Or) return ((Or)function).ExportToHL2(tree);
     81      if(function is Power) return ((Power)function).ExportToHL2(tree);
     82      if(function is Signum) return ((Signum)function).ExportToHL2(tree);
     83      if(function is Sinus) return ((Sinus)function).ExportToHL2(tree);
     84      if(function is Sqrt) return ((Sqrt)function).ExportToHL2(tree);
     85      if(function is Subtraction) return ((Subtraction)function).ExportToHL2(tree);
     86      if(function is Tangens) return ((Tangens)function).ExportToHL2(tree);
     87      if(function is Variable) return ((Variable)function).ExportToHL2(tree);
     88      if(function is Xor) return ((Xor)function).ExportToHL2(tree);
     89      throw new UnknownFunctionException(function.Name);
     90    }
     91
     92    #region IFunctionTreeNameGenerator Members
     93
     94    string IFunctionTreeNameGenerator.Name {
     95      get { return "HL2 Representation"; }
     96    }
     97
     98    public string GetName(IFunctionTree tree) {
     99      string name = "";
     100      try {
     101        name = ExportFunction(tree.Function, tree);
     102      } catch(UnknownFunctionException) {
     103        name = "N/A";
     104      }
     105      return name;
     106    }
     107
     108    #endregion
     109  }
     110
     111  internal static class HL2ExporterExtensions {
     112    private static string GetHL2FunctionName(string name) {
     113      return "[F]" + name ;
     114    }
     115
     116    public static string ExportToHL2(this Addition addition, IFunctionTree tree) {
     117      return GetHL2FunctionName("Addition[0]");
     118    }
     119
     120    public static string ExportToHL2(this Constant constant, IFunctionTree tree) {
     121      double value = ((ConstrainedDoubleData)tree.GetLocalVariable(Constant.VALUE).Value).Data;
     122      return "[T]Constant(" + value.ToString("r") + ";0;0)";
     123    }
     124
     125    public static string ExportToHL2(this Cosinus cosinus, IFunctionTree tree) {
     126      return GetHL2FunctionName("Trigonometrics[1]");
     127    }
     128
     129    public static string ExportToHL2(this Differential differential, IFunctionTree tree) {
     130      double weight = ((ConstrainedDoubleData)tree.GetLocalVariable(Differential.WEIGHT).Value).Data;
     131      double index = ((ConstrainedIntData)tree.GetLocalVariable(Differential.INDEX).Value).Data;
     132      double offset = ((ConstrainedIntData)tree.GetLocalVariable(Differential.OFFSET).Value).Data;
     133
     134      return "[T]Differential(" + weight.ToString("r") + ";" + index + ";" + -offset + ")";
     135    }
     136
     137    public static string ExportToHL2(this Division division, IFunctionTree tree) {
     138      return GetHL2FunctionName("Division[0]");
     139    }
     140
     141    public static string ExportToHL2(this Exponential exponential, IFunctionTree tree) {
     142      return GetHL2FunctionName("Exponential[0]");
     143    }
     144
     145    public static string ExportToHL2(this Logarithm logarithm, IFunctionTree tree) {
     146      return GetHL2FunctionName("Logarithm[0]");
     147    }
     148
     149    public static string ExportToHL2(this Multiplication multiplication, IFunctionTree tree) {
     150      return GetHL2FunctionName("Multiplication[0]");
     151    }
     152
     153    public static string ExportToHL2(this Power power, IFunctionTree tree) {
     154      return GetHL2FunctionName("Power[0]");
     155    }
     156
     157    public static string ExportToHL2(this Signum signum, IFunctionTree tree) {
     158      return GetHL2FunctionName("Signum[0]");
     159    }
     160
     161    public static string ExportToHL2(this Sinus sinus, IFunctionTree tree) {
     162      return GetHL2FunctionName("Trigonometrics[0]");
     163    }
     164
     165    public static string ExportToHL2(this Sqrt sqrt, IFunctionTree tree) {
     166      return GetHL2FunctionName("Sqrt[0]");
     167    }
     168
     169    public static string ExportToHL2(this Subtraction substraction, IFunctionTree tree) {
     170      return GetHL2FunctionName("Subtraction[0]");
     171    }
     172
     173    public static string ExportToHL2(this Tangens tangens, IFunctionTree tree) {
     174      return GetHL2FunctionName("Trigonometrics[2]");
     175    }
     176
     177    public static string ExportToHL2(this Variable variable, IFunctionTree tree) {
     178      double weight = ((ConstrainedDoubleData)tree.GetLocalVariable(Variable.WEIGHT).Value).Data;
     179      double index = ((ConstrainedIntData)tree.GetLocalVariable(Variable.INDEX).Value).Data;
     180      double offset = ((ConstrainedIntData)tree.GetLocalVariable(Variable.OFFSET).Value).Data;
     181
     182      return "[T]Variable(" + weight.ToString("r") + ";" + index + ";" + -offset + ")";
     183    }
     184
     185    public static string ExportToHL2(this And and, IFunctionTree tree) {
     186      return GetHL2FunctionName("Logical[0]");
     187    }
     188
     189    public static string ExportToHL2(this Average average, IFunctionTree tree) {
     190      return GetHL2FunctionName("Average[0]");
     191    }
     192
     193    public static string ExportToHL2(this IfThenElse ifThenElse, IFunctionTree tree) {
     194      return GetHL2FunctionName("Conditional[0]");
     195    }
     196
     197    public static string ExportToHL2(this Not not, IFunctionTree tree) {
     198      return GetHL2FunctionName("Logical[2]");
     199    }
     200
     201    public static string ExportToHL2(this Or or, IFunctionTree tree) {
     202      return GetHL2FunctionName("Logical[1]");
     203    }
     204
     205    public static string ExportToHL2(this Xor xor, IFunctionTree tree) {
     206      return GetHL2FunctionName("Logical[3]");
     207    }
     208
     209    public static string ExportToHL2(this Equal equal, IFunctionTree tree) {
     210      return GetHL2FunctionName("Boolean[2]");
     211    }
     212
     213    public static string ExportToHL2(this LessThan lessThan, IFunctionTree tree) {
     214      return GetHL2FunctionName("Boolean[0]");
     215    }
     216
     217    public static string ExportToHL2(this GreaterThan greaterThan, IFunctionTree tree) {
     218      return GetHL2FunctionName("Boolean[4]");
    167219    }
    168220  }
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Multiplication.cs

    r645 r654  
    4848      AddConstraint(new SubOperatorTypeConstraint(2));
    4949    }
    50 
    51     public override void Accept(IFunctionVisitor visitor) {
    52       visitor.Visit(this);
    53     }
    5450  }
    5551}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Not.cs

    r645 r654  
    4040      AddConstraint(new SubOperatorTypeConstraint(0));
    4141    }
    42 
    43     public override void Accept(IFunctionVisitor visitor) {
    44       visitor.Visit(this);
    45     }
    4642  }
    4743}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Or.cs

    r645 r654  
    4343      AddConstraint(new SubOperatorTypeConstraint(2));
    4444    }
    45 
    46     public override void Accept(IFunctionVisitor visitor) {
    47       visitor.Visit(this);
    48     }
    4945  }
    5046}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Power.cs

    r645 r654  
    4141      AddConstraint(new SubOperatorTypeConstraint(1));
    4242    }
    43 
    44     public override void Accept(IFunctionVisitor visitor) {
    45       visitor.Visit(this);
    46     }
    4743  }
    4844}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Signum.cs

    r645 r654  
    4040      AddConstraint(new SubOperatorTypeConstraint(0));
    4141    }
    42 
    43     public override void Accept(IFunctionVisitor visitor) {
    44       visitor.Visit(this);
    45     }
    4642  }
    4743}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Sinus.cs

    r645 r654  
    4040      AddConstraint(new SubOperatorTypeConstraint(0));
    4141    }
    42 
    43     public override void Accept(IFunctionVisitor visitor) {
    44       visitor.Visit(this);
    45     }
    4642  }
    4743}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Sqrt.cs

    r645 r654  
    4040      AddConstraint(new SubOperatorTypeConstraint(0));
    4141    }
    42 
    43     public override void Accept(IFunctionVisitor visitor) {
    44       visitor.Visit(this);
    45     }
    4642  }
    4743}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Subtraction.cs

    r645 r654  
    4848      AddConstraint(new SubOperatorTypeConstraint(2));
    4949    }
    50 
    51     public override void Accept(IFunctionVisitor visitor) {
    52       visitor.Visit(this);
    53     }
    5450  }
    5551}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/SymbolicExpressionExporter.cs

    r645 r654  
    2727
    2828namespace HeuristicLab.GP.StructureIdentification {
    29   public class SymbolicExpressionExporter : IFunctionVisitor {
    30     private IFunctionTree currentBranch;
     29  public class SymbolicExpressionExporter : IFunctionTreeExporter, IFunctionTreeNameGenerator {
    3130    private StringBuilder builder;
    3231    private string currentIndent;
    33     public SymbolicExpressionExporter() {
    34       Reset();
    35     }
    36 
    37     public void Reset() {
     32
     33    #region IFunctionTreeExporter Members
     34
     35    public string Name {
     36      get { return "Symbolic Expression Exporter"; }
     37    }
     38
     39
     40    public string Export(IFunctionTree tree) {
     41      builder = new StringBuilder();
    3842      currentIndent = "";
    39       builder = new StringBuilder();
    40     }
    41 
    42     public string GetStringRepresentation() {
     43      BuildExportString(tree);
    4344      return builder.ToString();
    4445    }
    4546
    46     private void VisitFunction(string name, IFunction f) {
     47    public bool TryExport(IFunctionTree tree, out string exported) {
     48      try {
     49        exported = Export(tree);
     50        return true;
     51      } catch(UnknownFunctionException) {
     52        exported = "";
     53        return false;
     54      }
     55    }
     56
     57    private void BuildExportString(IFunctionTree tree) {
    4758      builder.Append(currentIndent);
    48       builder.Append("(" + name + " ");
    49     }
    50 
    51     #region IFunctionVisitor Members
    52 
    53     public void Visit(IFunction function) {
    54       builder.Append(function.Name);
    55     }
    56 
    57     public void Visit(Addition addition) {
    58       VisitFunction("+", addition);
    59     }
    60 
    61     public void Visit(Constant constant) {
    62       builder.Append(currentIndent+currentBranch.GetLocalVariable(Constant.VALUE).Value);
    63     }
    64 
    65     public void Visit(Cosinus cosinus) {
    66       VisitFunction("cos", cosinus);
    67     }
    68 
    69     public void Visit(Division division) {
    70       VisitFunction("/", division);
    71     }
    72 
    73     public void Visit(Exponential exponential) {
    74       VisitFunction("exp", exponential);
    75     }
    76 
    77     public void Visit(Logarithm logarithm) {
    78       VisitFunction("log", logarithm);
    79     }
    80 
    81     public void Visit(Multiplication multiplication) {
    82       VisitFunction("*", multiplication);
    83     }
    84 
    85     public void Visit(Power power) {
    86       VisitFunction("expt", power);
    87     }
    88 
    89     public void Visit(Signum signum) {
    90       VisitFunction("sign", signum);
    91     }
    92 
    93     public void Visit(Sinus sinus) {
    94       VisitFunction("sin", sinus);
    95     }
    96 
    97     public void Visit(Sqrt sqrt) {
    98       VisitFunction("sqrt", sqrt);
    99     }
    100 
    101     public void Visit(Subtraction subtraction) {
    102       VisitFunction("-", subtraction);
    103     }
    104 
    105     public void Visit(Tangens tangens) {
    106       VisitFunction("tan", tangens);
    107     }
    108 
    109     public void Visit(Variable variable) {
    110       builder.Append(currentIndent + "(variable " + currentBranch.GetLocalVariable(Variable.WEIGHT).Value + " " +
    111         currentBranch.GetLocalVariable(Variable.INDEX).Value + " " + currentBranch.GetLocalVariable(Variable.OFFSET).Value + ")");
    112     }
    113     public void Visit(Differential differential) {
    114       builder.Append(currentIndent+"(differential " + currentBranch.GetLocalVariable(Differential.WEIGHT).Value + " "+
    115         currentBranch.GetLocalVariable(Differential.INDEX).Value+ " " + currentBranch.GetLocalVariable(Differential.OFFSET).Value+")");
    116     }
    117 
    118     public void Visit(And and) {
    119       VisitFunction("and", and);
    120     }
    121 
    122     public void Visit(Average average) {
    123       VisitFunction("mean", average);
    124     }
    125 
    126     public void Visit(IfThenElse ifThenElse) {
    127       VisitFunction("if", ifThenElse);
    128     }
    129 
    130     public void Visit(Not not) {
    131       VisitFunction("not", not);
    132     }
    133 
    134     public void Visit(Or or) {
    135       VisitFunction("or", or);
    136     }
    137 
    138     public void Visit(Xor xor) {
    139       VisitFunction("xor", xor);
    140     }
    141 
    142     public void Visit(Equal equal) {
    143       VisitFunction("equ", equal);
    144     }
    145 
    146     public void Visit(LessThan lessThan) {
    147       VisitFunction("<", lessThan);
    148     }
    149 
    150     public void Visit(GreaterThan greaterThan) {
    151       VisitFunction(">", greaterThan);
    152     }
     59      builder.Append("(" + ExportFunction(tree.Function, tree) + " ");
     60      currentIndent += "  ";
     61      foreach(IFunctionTree subTree in tree.SubTrees) {
     62        builder.Append("\n");
     63        BuildExportString(subTree);
     64      }
     65      if(tree.SubTrees.Count > 0) builder.Append(")");
     66      currentIndent = currentIndent.Remove(0, 2);
     67    }
     68
     69    private string ExportFunction(IFunction function, IFunctionTree tree) {
     70      // this is smelly, if there is a cleaner way to have a 'dynamic' visitor
     71      // please let me know! (gkronber 14.10.2008)
     72      if(function is Addition) return ((Addition)function).ExportToScheme();
     73      if(function is And) return ((And)function).ExportToScheme();
     74      if(function is Average) return ((Average)function).ExportToScheme();
     75      if(function is Constant) return ((Constant)function).ExportToScheme(tree);
     76      if(function is Cosinus) return ((Cosinus)function).ExportToScheme();
     77      if(function is Differential) return ((Differential)function).ExportToScheme(tree);
     78      if(function is Division) return ((Division)function).ExportToScheme();
     79      if(function is Equal) return ((Equal)function).ExportToScheme();
     80      if(function is Exponential) return ((Exponential)function).ExportToScheme();
     81      if(function is GreaterThan) return ((GreaterThan)function).ExportToScheme();
     82      if(function is IfThenElse) return ((IfThenElse)function).ExportToScheme();
     83      if(function is LessThan) return ((LessThan)function).ExportToScheme();
     84      if(function is Logarithm) return ((Logarithm)function).ExportToScheme();
     85      if(function is Multiplication) return ((Multiplication)function).ExportToScheme();
     86      if(function is Not) return ((Not)function).ExportToScheme();
     87      if(function is Or) return ((Or)function).ExportToScheme();
     88      if(function is Power) return ((Power)function).ExportToScheme();
     89      if(function is Signum) return ((Signum)function).ExportToScheme();
     90      if(function is Sinus) return ((Sinus)function).ExportToScheme();
     91      if(function is Sqrt) return ((Sqrt)function).ExportToScheme();
     92      if(function is Subtraction) return ((Subtraction)function).ExportToScheme();
     93      if(function is Tangens) return ((Tangens)function).ExportToScheme();
     94      if(function is Variable) return ((Variable)function).ExportToScheme(tree);
     95      if(function is Xor) return ((Xor)function).ExportToScheme();
     96      throw new UnknownFunctionException(function.Name);
     97    }
     98
     99
    153100    #endregion
    154101
    155     public void Visit(IFunctionTree functionTree) {
    156       currentBranch = functionTree;
    157       functionTree.Function.Accept(this);
    158       currentIndent += "  ";
    159 
    160       foreach(IFunctionTree subTree in functionTree.SubTrees) {
    161         builder.Append("\n");
    162         Visit(subTree);
     102    #region IFunctionTreeNameGenerator Members
     103
     104    string IFunctionTreeNameGenerator.Name {
     105      get { return "Symbolic Expression"; }
     106    }
     107
     108    public string GetName(IFunctionTree tree) {
     109      string name = "";
     110      try {
     111        name = ExportFunction(tree.Function, tree);
     112      } catch(UnknownFunctionException) {
     113        name = "N/A";
    163114      }
    164       if(functionTree.SubTrees.Count > 0) builder.Append(")");
    165       currentIndent = currentIndent.Remove(0, 2);
     115      return name;
     116    }
     117
     118    #endregion
     119  }
     120
     121  internal static class SchemeExporterExtensions {
     122    public static string ExportToScheme(this Addition addition) {
     123      return "+";
     124    }
     125
     126    public static string ExportToScheme(this Constant constant, IFunctionTree tree) {
     127      return tree.GetLocalVariable(Constant.VALUE).Value.ToString();
     128    }
     129
     130    public static string ExportToScheme(this Cosinus cosinus) {
     131      return "cos";
     132    }
     133
     134    public static string ExportToScheme(this Division division) {
     135      return "/";
     136    }
     137
     138    public static string ExportToScheme(this Exponential exponential) {
     139      return "exp";
     140    }
     141
     142    public static string ExportToScheme(this Logarithm logarithm) {
     143      return "log";
     144    }
     145
     146    public static string ExportToScheme(this Multiplication multiplication) {
     147      return "*";
     148    }
     149
     150    public static string ExportToScheme(this Power power) {
     151      return "expt";
     152    }
     153
     154    public static string ExportToScheme(this Signum signum) {
     155      return "sign";
     156    }
     157
     158    public static string ExportToScheme(this Sinus sinus) {
     159      return "sin";
     160    }
     161
     162    public static string ExportToScheme(this Sqrt sqrt) {
     163      return "sqrt";
     164    }
     165
     166    public static string ExportToScheme(this Subtraction subtraction) {
     167      return "-";
     168    }
     169
     170    public static string ExportToScheme(this Tangens tangens) {
     171      return "tan";
     172    }
     173
     174    public static string ExportToScheme(this Variable variable, IFunctionTree tree) {
     175      return "(variable " + tree.GetLocalVariable(Variable.WEIGHT).Value + " " +
     176        tree.GetLocalVariable(Variable.INDEX).Value + " " + tree.GetLocalVariable(Variable.OFFSET).Value + ")";
     177    }
     178    public static string ExportToScheme(this Differential differential, IFunctionTree tree) {
     179      return "(differential " + tree.GetLocalVariable(Differential.WEIGHT).Value + " " +
     180        tree.GetLocalVariable(Differential.INDEX).Value + " " + tree.GetLocalVariable(Differential.OFFSET).Value + ")";
     181    }
     182
     183    public static string ExportToScheme(this And and) {
     184      return "and";
     185    }
     186
     187    public static string ExportToScheme(this Average average) {
     188      return "mean";
     189    }
     190
     191    public static string ExportToScheme(this IfThenElse ifThenElse) {
     192      return "if";
     193    }
     194
     195    public static string ExportToScheme(this Not not) {
     196      return "not";
     197    }
     198
     199    public static string ExportToScheme(this Or or) {
     200      return "or";
     201    }
     202
     203    public static string ExportToScheme(this Xor xor) {
     204      return "xor";
     205    }
     206
     207    public static string ExportToScheme(this Equal equal) {
     208      return "equ";
     209    }
     210
     211    public static string ExportToScheme(this LessThan lessThan) {
     212      return "<";
     213    }
     214
     215    public static string ExportToScheme(this GreaterThan greaterThan) {
     216      return ">";
    166217    }
    167218  }
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Tangens.cs

    r645 r654  
    4040      AddConstraint(new SubOperatorTypeConstraint(0));
    4141    }
    42 
    43     public override void Accept(IFunctionVisitor visitor) {
    44       visitor.Visit(this);
    45     }
    4642  }
    4743}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Variable.cs

    r645 r654  
    207207      SetupManipulation();
    208208    }
    209 
    210     public override void Accept(IFunctionVisitor visitor) {
    211       visitor.Visit(this);
    212     }
    213209  }
    214210}
  • branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Xor.cs

    r645 r654  
    4141      AddConstraint(new SubOperatorTypeConstraint(1));
    4242    }
    43 
    44     public override void Accept(IFunctionVisitor visitor) {
    45       visitor.Visit(this);
    46     }
    4743  }
    4844}
Note: See TracChangeset for help on using the changeset viewer.