1 | using HeuristicLab.Common;
|
---|
2 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
3 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Problems.Robocode
|
---|
6 | {
|
---|
7 | [StorableClass]
|
---|
8 | public class LogicalComparison : CodeNode
|
---|
9 | {
|
---|
10 | public override int MinimumArity { get { return 3; } }
|
---|
11 | public override int MaximumArity { get { return 3; } }
|
---|
12 |
|
---|
13 | [Storable]
|
---|
14 | public override string Prefix { get; set; }
|
---|
15 |
|
---|
16 | [Storable]
|
---|
17 | public override string Suffix { get; set; }
|
---|
18 |
|
---|
19 | [StorableConstructor]
|
---|
20 | private LogicalComparison(bool deserializing) : base(deserializing) { }
|
---|
21 | private LogicalComparison(LogicalComparison original, Cloner cloner)
|
---|
22 | : base(original, cloner)
|
---|
23 | {
|
---|
24 | this.Prefix = "(";
|
---|
25 | this.Suffix = ")";
|
---|
26 | }
|
---|
27 |
|
---|
28 | public LogicalComparison()
|
---|
29 | : base("LogicalComparison", "A LogicalComparison.")
|
---|
30 | {
|
---|
31 | this.Prefix = "(";
|
---|
32 | this.Suffix = ")";
|
---|
33 | }
|
---|
34 |
|
---|
35 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
36 | {
|
---|
37 | return new LogicalComparison(this, cloner);
|
---|
38 | }
|
---|
39 |
|
---|
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 Conjunction || condition.Symbol is Disjunction))
|
---|
59 | throw new System.Exception("Unexpected first child for LogicalComparison of type: " +
|
---|
60 | condition.GetType().ToString() + ". Expected " + typeof(Conjunction).ToString() +
|
---|
61 | " or " + typeof(Disjunction).ToString() + ".");
|
---|
62 |
|
---|
63 | // TODO Check children 2 & 3 for correct types
|
---|
64 |
|
---|
65 | string[] result = new string[]
|
---|
66 | {
|
---|
67 | ((CodeNode)condition.Symbol).Interpret(condition, condition.Subtrees),
|
---|
68 | ((CodeNode)lhs.Symbol).Interpret(lhs, lhs.Subtrees),
|
---|
69 | ((CodeNode)rhs.Symbol).Interpret(rhs, rhs.Subtrees)
|
---|
70 | };
|
---|
71 |
|
---|
72 | return this.Prefix + " " + result[1] + " " + result[0] + " " + result[2] + " " + this.Suffix;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|