Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/17/13 12:07:20 (11 years ago)
Author:
ascheibe
Message:

#2069 simplified Interpret methods of symbols

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Statements/IfThenElseStat.cs

    r10043 r10046  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    5152      ISymbolicExpressionTreeNode condition = null, truePart = null, falsePart = null;
    5253      string[] parts = new string[3];
    53       var enumerator = children.GetEnumerator();
    54       int childCount = 0;
    55       while (enumerator.MoveNext()) {
    56         childCount++;
    57         switch (childCount) {
    58           case 1: condition = enumerator.Current; break;
    59           case 2: truePart = enumerator.Current; break;
    60           case 3: falsePart = enumerator.Current; break;
    61           default: throw new ArgumentException("Unexpected number of children. Expected a maximum of 3 children.");
    62         }
    63       }
    64       if (childCount < 2) throw new ArgumentException("Unexpected number of children. Expected at least 2 children.");
     54
     55      if (children.Count() < 2 || children.Count() > 3)
     56        throw new Exception("Unexpected number of children. Expected 2 or 3 children.");
     57
     58      var childLst = children.ToList();
     59      condition = childLst[0];
     60      truePart = childLst[1];
     61      if (childLst.Count == 3)
     62        falsePart = childLst[2];
    6563
    6664      parts[0] = ((CodeNode)condition.Symbol).Interpret(condition, condition.Subtrees);
    6765      parts[1] = ((CodeNode)truePart.Symbol).Interpret(truePart, truePart.Subtrees);
    68       if (childCount == 3) parts[2] = ((CodeNode)falsePart.Symbol).Interpret(falsePart, falsePart.Subtrees);
     66      if (childLst.Count == 3) parts[2] = ((CodeNode)falsePart.Symbol).Interpret(falsePart, falsePart.Subtrees);
    6967
    7068      string result = "if (" + parts[0] + ") {" + parts[1] + "}";
Note: See TracChangeset for help on using the changeset viewer.