Changeset 9630 for branches/Robocode/HeuristicLab.Problems.Robocode/Symbols/Numerical Expressions/NumericalOperation.cs
- Timestamp:
- 06/14/13 11:03:13 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Robocode/HeuristicLab.Problems.Robocode/Symbols/Numerical Expressions/NumericalOperation.cs
r9565 r9630 3 3 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 4 4 5 namespace HeuristicLab.Problems.Robocode 6 { 7 [StorableClass] 8 public class NumericalOperation : CodeNode 9 { 10 public override int MinimumArity { get { return 3; } } 11 public override int MaximumArity { get { return 3; } } 5 namespace HeuristicLab.Problems.Robocode { 6 [StorableClass] 7 public class NumericalOperation : CodeNode { 8 public override int MinimumArity { get { return 3; } } 9 public override int MaximumArity { get { return 3; } } 12 10 13 14 11 [Storable] 12 public override string Prefix { get; set; } 15 13 16 17 14 [Storable] 15 public override string Suffix { get; set; } 18 16 19 [StorableConstructor] 20 private NumericalOperation(bool deserializing) : base(deserializing) { } 21 private NumericalOperation(NumericalOperation original, Cloner cloner) 22 : base(original, cloner) 23 { 24 this.Prefix = "("; 25 this.Suffix = ")"; 17 [StorableConstructor] 18 private NumericalOperation(bool deserializing) : base(deserializing) { } 19 private NumericalOperation(NumericalOperation original, Cloner cloner) 20 : base(original, cloner) { 21 } 22 23 public NumericalOperation() 24 : base("NumericalOperation", "An expression consisting of two numbers and an operator.") { 25 this.Prefix = "("; 26 this.Suffix = ")"; 27 } 28 29 public override IDeepCloneable Clone(Cloner cloner) { 30 return new NumericalOperation(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."); 26 44 } 45 } 46 if (childCount < 3) throw new System.Exception("Unexpected number of children. Expected 3 children."); 27 47 28 public NumericalOperation() 29 : base("NumericalOperation", "An expression consisting of two numbers and an operator.") 30 { 31 this.Prefix = "("; 32 this.Suffix = ")"; 33 } 48 if (!(condition.Symbol is Addition || condition.Symbol is Division || 49 condition.Symbol is Modulus || condition.Symbol is Multiplication || condition.Symbol is Subtraction)) 50 throw new System.Exception("Unexpected first child for NumericalOperation of type: " + 51 condition.GetType().ToString() + ". Expected " + typeof(Addition).ToString() + 52 " or " + typeof(Division).ToString() + 53 " or " + typeof(Modulus).ToString() + 54 " or " + typeof(Multiplication).ToString() + 55 " or " + typeof(Subtraction).ToString() + "."); 34 56 35 public override IDeepCloneable Clone(Cloner cloner) 36 { 37 return new NumericalOperation(this, cloner); 38 } 57 // TODO Check children 2 & 3 for correct types 39 58 40 public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) 41 { 42 ISymbolicExpressionTreeNode condition = null, lhs = null, rhs = null; 43 var enumerator = children.GetEnumerator(); 44 int childCount = 0; 45 while (enumerator.MoveNext()) 46 { 47 childCount++; 48 switch (childCount) 49 { 50 case 1: condition = enumerator.Current; break; 51 case 2: lhs = enumerator.Current; break; 52 case 3: rhs = enumerator.Current; break; 53 default: throw new System.Exception("Unexpected number of children. Expected 3 children."); 54 } 55 } 56 if (childCount < 3) throw new System.Exception("Unexpected number of children. Expected 3 children."); 57 58 if (!(condition.Symbol is Addition || condition.Symbol is Division || 59 condition.Symbol is Modulus || condition.Symbol is Multiplication || condition.Symbol is Subtraction)) 60 throw new System.Exception("Unexpected first child for NumericalOperation of type: " + 61 condition.GetType().ToString() + ". Expected " + typeof(Addition).ToString() + 62 " or " + typeof(Division).ToString() + 63 " or " + typeof(Modulus).ToString() + 64 " or " + typeof(Multiplication).ToString() + 65 " or " + typeof(Subtraction).ToString() + "."); 66 67 // TODO Check children 2 & 3 for correct types 68 69 string[] result = new string[] 59 string[] result = new string[] 70 60 { 71 61 ((CodeNode)condition.Symbol).Interpret(condition, condition.Subtrees), … … 74 64 }; 75 65 76 return this.Prefix + " " + result[1] + " " + result[0] + " " + result[2] + " " + this.Suffix; 77 } 66 return this.Prefix + " " + result[1] + " " + result[0] + " " + result[2] + " " + this.Suffix; 78 67 } 68 } 79 69 }
Note: See TracChangeset
for help on using the changeset viewer.