using HeuristicLab.Common; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.Robocode { [StorableClass] public class NumericalComparison : CodeNode { public override int MinimumArity { get { return 3; } } public override int MaximumArity { get { return 3; } } [Storable] public override string Prefix { get; set; } [Storable] public override string Suffix { get; set; } [StorableConstructor] private NumericalComparison(bool deserializing) : base(deserializing) { } private NumericalComparison(NumericalComparison original, Cloner cloner) : base(original, cloner) { } public NumericalComparison() : base("NumericalComparison", "A NumericalComparison.") { this.Prefix = "("; this.Suffix = ")"; } public override IDeepCloneable Clone(Cloner cloner) { return new NumericalComparison(this, cloner); } public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable children) { ISymbolicExpressionTreeNode condition = null, lhs = null, rhs = null; var enumerator = children.GetEnumerator(); int childCount = 0; while (enumerator.MoveNext()) { childCount++; switch (childCount) { case 1: condition = enumerator.Current; break; case 2: lhs = enumerator.Current; break; case 3: rhs = enumerator.Current; break; default: throw new System.Exception("Unexpected number of children. Expected 3 children."); } } if (childCount < 3) throw new System.Exception("Unexpected number of children. Expected 3 children."); if (!(condition.Symbol is Equal || condition.Symbol is GreaterThan || condition.Symbol is GreaterThanOrEqual || condition.Symbol is LessThan || condition.Symbol is LessThanOrEqual)) throw new System.Exception("Unexpected first child for NumericalComparison of type: " + condition.GetType().ToString() + ". Expected " + typeof(Equal).ToString() + " or " + typeof(GreaterThan).ToString() + " or " + typeof(GreaterThanOrEqual).ToString() + " or " + typeof(LessThan).ToString() + " or " + typeof(LessThanOrEqual).ToString() + "."); // TODO Check children 2 & 3 for correct types string[] result = new string[] { ((CodeNode)condition.Symbol).Interpret(condition, condition.Subtrees), ((CodeNode)lhs.Symbol).Interpret(lhs, lhs.Subtrees), ((CodeNode)rhs.Symbol).Interpret(rhs, rhs.Subtrees) }; return this.Prefix + " " + result[1] + " " + result[0] + " " + result[2] + " " + this.Suffix; } } }