Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/01/13 12:08:25 (11 years ago)
Author:
jkarder
Message:

#2069:

  • refactored grammar and symbols
  • fixed cloning and storable ctors
  • fixed plugin dependencies
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Logical Expressions/Logical Comparators/Equal.cs

    r9790 r10011  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    2627namespace HeuristicLab.Problems.Robocode {
    2728  [StorableClass]
    28   public class Equal : CodeNode {
    29     public override int MinimumArity { get { return 0; } }
    30     public override int MaximumArity { get { return 0; } }
     29  public class Equal : CodeNode, ILogicalComparator {
     30    public override int MinimumArity { get { return 2; } }
     31    public override int MaximumArity { get { return 2; } }
    3132
    3233    [Storable]
     
    3738
    3839    [StorableConstructor]
    39     private Equal(bool deserializing) : base(deserializing) { }
    40     private Equal(Equal original, Cloner cloner)
    41       : base(original, cloner) {
    42     }
     40    protected Equal(bool deserializing) : base(deserializing) { }
     41    protected Equal(Equal original, Cloner cloner) : base(original, cloner) { }
    4342
    44     public Equal()
    45       : base("Equal", "Equal comparator.") {
    46       this.Prefix = "==";
    47       this.Suffix = "";
    48     }
     43    public Equal() : base("Equal", "Equal comparator.") { }
    4944
    5045    public override IDeepCloneable Clone(Cloner cloner) {
     
    5247    }
    5348
    54     public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) {
    55       return this.Prefix + " " + this.Suffix;
     49    public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {
     50      ISymbolicExpressionTreeNode lhs = null, rhs = null;
     51      var enumerator = children.GetEnumerator();
     52      int childCount = 0;
     53      while (enumerator.MoveNext()) {
     54        childCount++;
     55        switch (childCount) {
     56          case 1: lhs = enumerator.Current; break;
     57          case 2: rhs = enumerator.Current; break;
     58          default: throw new System.Exception("Unexpected number of children. Expected 2 children.");
     59        }
     60      }
     61      if (childCount < 2) throw new System.Exception("Unexpected number of children. Expected 2 children.");
     62
     63      var result = new[] {
     64        ((CodeNode)lhs.Symbol).Interpret(lhs, lhs.Subtrees),
     65        ((CodeNode)rhs.Symbol).Interpret(rhs, rhs.Subtrees)
     66      };
     67
     68      return result[0] + " == " + result[1];
    5669    }
    5770  }
Note: See TracChangeset for help on using the changeset viewer.