using HeuristicLab.Common; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.Robocode { [StorableClass] public class NumericalOperation : 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 NumericalOperation(bool deserializing) : base(deserializing) { } private NumericalOperation(NumericalOperation original, Cloner cloner) : base(original, cloner) { } public NumericalOperation() : base("NumericalOperation", "An expression consisting of two numbers and an operator.") { this.Prefix = "("; this.Suffix = ")"; } public override IDeepCloneable Clone(Cloner cloner) { return new NumericalOperation(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 Addition || condition.Symbol is Division || condition.Symbol is Modulus || condition.Symbol is Multiplication || condition.Symbol is Subtraction)) throw new System.Exception("Unexpected first child for NumericalOperation of type: " + condition.GetType().ToString() + ". Expected " + typeof(Addition).ToString() + " or " + typeof(Division).ToString() + " or " + typeof(Modulus).ToString() + " or " + typeof(Multiplication).ToString() + " or " + typeof(Subtraction).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; } } }