[9565] | 1 | using HeuristicLab.Common;
|
---|
| 2 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 3 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 4 |
|
---|
[9630] | 5 | namespace HeuristicLab.Problems.Robocode {
|
---|
| 6 | [StorableClass]
|
---|
| 7 | public class LogicalComparison : CodeNode {
|
---|
| 8 | public override int MinimumArity { get { return 3; } }
|
---|
| 9 | public override int MaximumArity { get { return 3; } }
|
---|
[9565] | 10 |
|
---|
[9630] | 11 | [Storable]
|
---|
| 12 | public override string Prefix { get; set; }
|
---|
[9565] | 13 |
|
---|
[9630] | 14 | [Storable]
|
---|
| 15 | public override string Suffix { get; set; }
|
---|
[9565] | 16 |
|
---|
[9630] | 17 | [StorableConstructor]
|
---|
| 18 | private LogicalComparison(bool deserializing) : base(deserializing) { }
|
---|
| 19 | private LogicalComparison(LogicalComparison original, Cloner cloner)
|
---|
| 20 | : base(original, cloner) {
|
---|
| 21 | }
|
---|
[9565] | 22 |
|
---|
[9630] | 23 | public LogicalComparison()
|
---|
| 24 | : base("LogicalComparison", "A LogicalComparison.") {
|
---|
| 25 | this.Prefix = "(";
|
---|
| 26 | this.Suffix = ")";
|
---|
| 27 | }
|
---|
[9565] | 28 |
|
---|
[9630] | 29 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 30 | return new LogicalComparison(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.");
|
---|
[9565] | 44 | }
|
---|
[9630] | 45 | }
|
---|
| 46 | if (childCount < 3) throw new System.Exception("Unexpected number of children. Expected 3 children.");
|
---|
[9565] | 47 |
|
---|
[9630] | 48 | if (!(condition.Symbol is Conjunction || condition.Symbol is Disjunction))
|
---|
| 49 | throw new System.Exception("Unexpected first child for LogicalComparison of type: " +
|
---|
| 50 | condition.GetType().ToString() + ". Expected " + typeof(Conjunction).ToString() +
|
---|
| 51 | " or " + typeof(Disjunction).ToString() + ".");
|
---|
[9565] | 52 |
|
---|
[9630] | 53 | // TODO Check children 2 & 3 for correct types
|
---|
[9565] | 54 |
|
---|
[9630] | 55 | string[] result = new string[]
|
---|
[9565] | 56 | {
|
---|
| 57 | ((CodeNode)condition.Symbol).Interpret(condition, condition.Subtrees),
|
---|
| 58 | ((CodeNode)lhs.Symbol).Interpret(lhs, lhs.Subtrees),
|
---|
| 59 | ((CodeNode)rhs.Symbol).Interpret(rhs, rhs.Subtrees)
|
---|
| 60 | };
|
---|
| 61 |
|
---|
[9630] | 62 | return this.Prefix + " " + result[1] + " " + result[0] + " " + result[2] + " " + this.Suffix;
|
---|
[9565] | 63 | }
|
---|
[9630] | 64 | }
|
---|
[9565] | 65 | }
|
---|