Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode/HeuristicLab.Problems.Robocode/Symbols/Logical Expressions/NumericalComparison.cs @ 9882

Last change on this file since 9882 was 9630, checked in by ascheibe, 11 years ago

#2069 fixed cloning constructors and formatting

File size: 2.9 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4
5namespace HeuristicLab.Problems.Robocode {
6  [StorableClass]
7  public class NumericalComparison : CodeNode {
8    public override int MinimumArity { get { return 3; } }
9    public override int MaximumArity { get { return 3; } }
10
11    [Storable]
12    public override string Prefix { get; set; }
13
14    [Storable]
15    public override string Suffix { get; set; }
16
17    [StorableConstructor]
18    private NumericalComparison(bool deserializing) : base(deserializing) { }
19    private NumericalComparison(NumericalComparison original, Cloner cloner)
20      : base(original, cloner) {
21    }
22
23    public NumericalComparison()
24      : base("NumericalComparison", "A NumericalComparison.") {
25      this.Prefix = "(";
26      this.Suffix = ")";
27    }
28
29    public override IDeepCloneable Clone(Cloner cloner) {
30      return new NumericalComparison(this, cloner);
31    }
32
33    public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) {
34      ISymbolicExpressionTreeNode condition = null, lhs = null, rhs = null;
35      var enumerator = children.GetEnumerator();
36      int childCount = 0;
37      while (enumerator.MoveNext()) {
38        childCount++;
39        switch (childCount) {
40          case 1: condition = enumerator.Current; break;
41          case 2: lhs = enumerator.Current; break;
42          case 3: rhs = enumerator.Current; break;
43          default: throw new System.Exception("Unexpected number of children. Expected 3 children.");
44        }
45      }
46      if (childCount < 3) throw new System.Exception("Unexpected number of children. Expected 3 children.");
47
48      if (!(condition.Symbol is Equal || condition.Symbol is GreaterThan ||
49          condition.Symbol is GreaterThanOrEqual || condition.Symbol is LessThan || condition.Symbol is LessThanOrEqual))
50        throw new System.Exception("Unexpected first child for NumericalComparison of type: " +
51            condition.GetType().ToString() + ". Expected " + typeof(Equal).ToString() +
52            " or " + typeof(GreaterThan).ToString() +
53            " or " + typeof(GreaterThanOrEqual).ToString() +
54            " or " + typeof(LessThan).ToString() +
55            " or " + typeof(LessThanOrEqual).ToString() + ".");
56
57      // TODO Check children 2 & 3 for correct types
58
59      string[] result = new string[]
60            {
61                ((CodeNode)condition.Symbol).Interpret(condition, condition.Subtrees),
62                ((CodeNode)lhs.Symbol).Interpret(lhs, lhs.Subtrees),
63                ((CodeNode)rhs.Symbol).Interpret(rhs, rhs.Subtrees)
64            };
65
66      return this.Prefix + " " + result[1] + " " + result[0] + " " + result[2] + " " + this.Suffix;
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.