using HeuristicLab.Common; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.Robocode { [StorableClass] public class IfStatement : CodeNode { public override int MinimumArity { get { return 2; } } public override int MaximumArity { get { return 3; } } [Storable] public override string Prefix { get; set; } [Storable] public override string Suffix { get; set; } [StorableConstructor] private IfStatement(bool deserializing) : base(deserializing) { } private IfStatement(IfStatement original, Cloner cloner) : base(original, cloner) { } public IfStatement() : base("IfStatement", "An IfStatement.") { this.Prefix = "if("; this.Suffix = ")"; } public override IDeepCloneable Clone(Cloner cloner) { return new IfStatement(this, cloner); } public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable children) { string condition = "", ifTrue = "", ifElse = ""; foreach (ISymbolicExpressionTreeNode c in children) { if (c.Symbol is LogicalExpression) condition = ((CodeNode)c.Symbol).Interpret(c, c.Subtrees); else if (c.Symbol is Block) ifTrue = ((CodeNode)c.Symbol).Interpret(c, c.Subtrees); else if (c.Symbol is ElseStatement || c.Symbol is DoNothing) ifElse = ((CodeNode)c.Symbol).Interpret(c, c.Subtrees); else throw new System.Exception("Unexpected Child node."); } return this.Prefix + " " + condition + " " + this.Suffix + "\r\n" + ifTrue + "\r\n" + ifElse + "\r\n"; } } }