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 IfStatement : CodeNode
|
---|
9 | {
|
---|
10 | public override int MinimumArity { get { return 2; } }
|
---|
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 IfStatement(bool deserializing) : base(deserializing) { }
|
---|
21 | private IfStatement(IfStatement original, Cloner cloner)
|
---|
22 | : base(original, cloner)
|
---|
23 | {
|
---|
24 | this.Prefix = "if(";
|
---|
25 | this.Suffix = ")";
|
---|
26 | }
|
---|
27 |
|
---|
28 | public IfStatement()
|
---|
29 | : base("IfStatement", "An IfStatement.")
|
---|
30 | {
|
---|
31 | this.Prefix = "if(";
|
---|
32 | this.Suffix = ")";
|
---|
33 | }
|
---|
34 |
|
---|
35 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
36 | {
|
---|
37 | return new IfStatement(this, cloner);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children)
|
---|
41 | {
|
---|
42 | string condition = "", ifTrue = "", ifElse = "";
|
---|
43 | foreach (ISymbolicExpressionTreeNode c in children)
|
---|
44 | {
|
---|
45 | if(c.Symbol is LogicalExpression)
|
---|
46 | condition = ((CodeNode)c.Symbol).Interpret(c, c.Subtrees);
|
---|
47 | else if(c.Symbol is Block)
|
---|
48 | ifTrue = ((CodeNode)c.Symbol).Interpret(c, c.Subtrees);
|
---|
49 | else if(c.Symbol is ElseStatement || c.Symbol is DoNothing)
|
---|
50 | ifElse = ((CodeNode)c.Symbol).Interpret(c, c.Subtrees);
|
---|
51 | else
|
---|
52 | throw new System.Exception("Unexpected Child node.");
|
---|
53 | }
|
---|
54 | return this.Prefix + " " + condition + " " + this.Suffix +
|
---|
55 | "\r\n" + ifTrue + "\r\n" + ifElse + "\r\n";
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|