using HeuristicLab.Common; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.Robocode { [StorableClass] public class LogicalComparison : CodeNode { public override int MinimumArity { get { return 3; } } public override int MaximumArity { get { return 3; } } [Storable] public override string Prefix { get; set; } [Storable] public override string Suffix { get; set; } [StorableConstructor] private LogicalComparison(bool deserializing) : base(deserializing) { } private LogicalComparison(LogicalComparison original, Cloner cloner) : base(original, cloner) { } public LogicalComparison() : base("LogicalComparison", "A LogicalComparison.") { this.Prefix = "("; this.Suffix = ")"; } public override IDeepCloneable Clone(Cloner cloner) { return new LogicalComparison(this, cloner); } public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable children) { ISymbolicExpressionTreeNode condition = null, lhs = null, rhs = null; var enumerator = children.GetEnumerator(); int childCount = 0; while (enumerator.MoveNext()) { childCount++; switch (childCount) { case 1: condition = enumerator.Current; break; case 2: lhs = enumerator.Current; break; case 3: rhs = enumerator.Current; break; default: throw new System.Exception("Unexpected number of children. Expected 3 children."); } } if (childCount < 3) throw new System.Exception("Unexpected number of children. Expected 3 children."); if (!(condition.Symbol is Conjunction || condition.Symbol is Disjunction)) throw new System.Exception("Unexpected first child for LogicalComparison of type: " + condition.GetType().ToString() + ". Expected " + typeof(Conjunction).ToString() + " or " + typeof(Disjunction).ToString() + "."); // TODO Check children 2 & 3 for correct types string[] result = new string[] { ((CodeNode)condition.Symbol).Interpret(condition, condition.Subtrees), ((CodeNode)lhs.Symbol).Interpret(lhs, lhs.Subtrees), ((CodeNode)rhs.Symbol).Interpret(rhs, rhs.Subtrees) }; return this.Prefix + " " + result[1] + " " + result[0] + " " + result[2] + " " + this.Suffix; } } }