Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode/HeuristicLab.Problems.Robocode/Symbols/Logical Expressions/LogicalExpression.cs @ 9593

Last change on this file since 9593 was 9565, checked in by melkaref, 11 years ago

Robocode Plugin code without Mutation Operators

File size: 2.4 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4
5namespace HeuristicLab.Problems.Robocode
6{
7    [StorableClass]
8    public class LogicalExpression : CodeNode
9    {
10        public override int MinimumArity { get { return 1; } }
11        public override int MaximumArity { get { return 1; } }
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 LogicalExpression(bool deserializing) : base(deserializing) { }
21        private LogicalExpression(LogicalExpression original, Cloner cloner)
22            : base(original, cloner)
23        {
24            this.Prefix = "";
25            this.Suffix = "";
26        }
27
28        public LogicalExpression()
29            : base("LogicalExpression", "A LogicalExpression.")
30        {
31            this.Prefix = "";
32            this.Suffix = "";
33        }
34
35        public override IDeepCloneable Clone(Cloner cloner)
36        {
37            return new LogicalExpression(this, cloner);
38        }
39
40        public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children)
41        {
42            var enumerator = children.GetEnumerator();
43
44            if (!enumerator.MoveNext()) throw new System.Exception("LogicalExpression was not given a child.");
45            var symbol = enumerator.Current.Symbol;
46
47            if(!(symbol is LogicalValue || symbol is LogicalComparison ||
48                symbol is NumericalComparison || symbol is Negation))
49                throw new System.Exception("LogicalExpression was given a child of type " + symbol.GetType().ToString() +
50                    ". The expected child must be of type " + typeof(LogicalValue).ToString() + " or " +
51                    typeof(LogicalComparison).ToString() + " or " + typeof(NumericalComparison).ToString() + " or " +
52                    typeof(Negation).ToString() + ".");
53
54            string result = ((CodeNode)symbol).Interpret(enumerator.Current, enumerator.Current.Subtrees);
55            if (enumerator.MoveNext()) throw new System.Exception("LogicalExpression was given more than one child.");
56
57            return this.Prefix + result + this.Suffix;
58        }
59    }
60}
Note: See TracBrowser for help on using the repository browser.