/// Author:	Guy Spronck
        /// Date:	19-06-2015
        /// <summary>
        /// for use as base.execute()
        /// Adds references to all the varibles of the scope of the parent to the scope of the Compisite being executed.
        /// </summary>
        /// <param name="parent">Parent of the Compisite you are executing</param>
        public override bool execute(Composite parent)
        {
            BackgroundWorker bw = new BackgroundWorker ();

            bw.WorkerReportsProgress = true;

            bw.DoWork += (object sender, DoWorkEventArgs e) => {
                Robot.bg = sender as BackgroundWorker;
                Thread.Sleep(GlobalSupport.GameSpeed);
                foreach (ICodeBlock codeBlock in children) {
                    codeBlock.execute (this);
                    Thread.Sleep (GlobalSupport.GameSpeed);
                }
            };

            bw.ProgressChanged += new ProgressChangedEventHandler (delegate(object o, ProgressChangedEventArgs args) {
                // Update progress, redraw robot
                Robot instance = Robot.Instance;
                instance.UpdateGUI(args.ProgressPercentage);
            });

            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler (delegate(object o, RunWorkerCompletedEventArgs args) {
                    // Thread finished -- Check if correct
                    Robot.checkGoalsEvent(this, args);
            });

            bw.RunWorkerAsync (); // Start the worker
            return false;
        }
        /// Author: Bert van Montfort
        /// <summary>
        /// Executes the while loop until the conditions set are no longer true, or until the while loop has completed 300 loops,
        /// at which point it will assume the loop is infinite and the loop breaks.
        /// 
        /// Returns false if the while loop is infinite, true if not.
        /// </summary>
        public override bool execute(Composite parent)
        {
            base.execute (parent); //adds parents variables to own variables

            if (Robot.bg != null) {
                Robot.bg.ReportProgress (lineNumber);
            }
            int count = 0;
            while (conditions.solve(this)) {
                foreach (ICodeBlock codeBlock in children) {
                    if (codeBlock.execute(this)) {
                        Thread.Sleep (GlobalSupport.GameSpeed);
                    }
                }
                if (count > 300) {
                    return false;
                }
                count++;

                if (Robot.bg != null) {
                    Robot.bg.ReportProgress (lineNumber);
                    Thread.Sleep (GlobalSupport.GameSpeed/2);
                }
            }
            return true;
        }
 /// Author: Bert van Montfort
 /// <summary>
 /// Executes all children of this functionblock 
 /// </summary>
 /// <param name="parent">Parent of the Composite you are executing</param>
 public override bool execute(Composite parent)
 {
     base.execute (parent);
     foreach (ICodeBlock codeBlock in children) {
         codeBlock.execute (parent);
         Thread.Sleep (GlobalSupport.GameSpeed);
     }
     return false;
 }
 /// Author: Bert van Montfort
 /// <summary>
 /// for use as base.execute()
 /// Adds references to all the varibles of the scope of the parent to the scope of the Compisite being executed.
 /// </summary>
 /// <param name="parent">Parent of the Compisite you are executing</param>
 public virtual bool execute(Composite parent)
 {
     if (parent != null) {
     foreach (KeyValuePair<string, Variable> varPair in parent.variables) {
         this.variables[varPair.Key] = varPair.Value;
         }
     }
     return true;
 }
 /// Author: Bert van Montfort
 /// <summary>
 /// Executes a turn right command to the robot.
 /// </summary>
 /// <param name="parent">Parent.</param>
 public override bool execute(Composite parent)
 {
     Robot robot = Robot.Instance;
     if (Robot.bg != null) {
         Robot.bg.ReportProgress (lineNumber);
     }
     robot.Moves++;
     robot.RotateRight (lineNumber);
     return true;
 }
        /// Author: Bert van Montfort
        /// <summary>
        /// Solves the VariableCombo.
        /// </summary>
        /// <param name="parent">Composite Parent.</param>
        public override Variable solve(Composite parent)
        {
            Variable leftValue = leftSolver.solve (parent);
            Variable rightValue = rightSolver.solve (parent);
            Variable returnValue = null;

            try {
                if (leftValue.Type == EVariableType.Int && rightValue.Type == EVariableType.Int) {
                    switch (mathOperator) {
                    case EMathOperator.Multiply:
                        returnValue = new Variable ((leftValue.Value as int?) * (rightValue.Value as int?), EVariableType.Int);
                        break;
                    case EMathOperator.Divide:
                        //TODO cant divide by 0
                        returnValue = new Variable ((leftValue.Value as int?) / (rightValue.Value as int?), EVariableType.Int);
                        break;
                    case EMathOperator.Add:
                        returnValue = new Variable ((leftValue.Value as int?) + (rightValue.Value as int?), EVariableType.Int);
                        break;
                    case EMathOperator.Subtract:
                        returnValue = new Variable ((leftValue.Value as int?) - (rightValue.Value as int?), EVariableType.Int);
                        break;
                    default:
                        //TODO throw exception
                        break;
                    }
                } else {
                    switch (mathOperator) {
                    case EMathOperator.Multiply:
                        returnValue = new Variable (leftValue.Value * rightValue.Value, EVariableType.String);
                        break;
                    case EMathOperator.Divide:
                        //TODO cant divide by 0
                        returnValue = new Variable (leftValue.Value / rightValue.Value, EVariableType.String);
                        break;
                    case EMathOperator.Add:
                        returnValue = new Variable (leftValue.Value + rightValue.Value, EVariableType.String);
                        break;
                    case EMathOperator.Subtract:
                        returnValue = new Variable (leftValue.Value - rightValue.Value, EVariableType.String);
                        break;
                    default:
                        //TODO throw exception
                        break;
                    }
                }

            } catch (DivideByZeroException){
                throw new RunTimeException ("Cannot divide by zero.");
            }
            catch (Exception) {
                throw new RunTimeException ("Unsupported Operation.");
            }
            return returnValue;
        }
 /// Author: Bert van Montfort
 /// <summary>
 /// Executes the CmdDefineVariable
 /// 
 /// Creates a new Variable using Definevariable varValue.
 /// If the variable already exists in the parent of this command the value will be overwritten
 /// If the varibale does not yet exist it will be added instead. 
 /// </summary>
 /// <param name="parent">The parent holing the Dictionary of variables</param>
 public override bool execute(Composite parent)
 {
     Variable collectedVar = varValue.solve (parent);
     variables = parent.variables;
     if (variables.ContainsKey (variableName)) {
         Variable oldVariable = variables [variableName];
         oldVariable.Value = collectedVar.Value;
     } else {
         variables.Add (variableName, collectedVar);
     }
     return true;
 }
 /// Author: Bert van Montfort
 /// <summary>
 /// Solve the specified parent.
 /// </summary>
 /// <param name="parent">Composite Parent.</param>
 public override Variable solve(Composite parent)
 {
     // If the variable is defined, no lookup is required because this is a literal.
     if (myVariable != null) {
         return myVariable;
     } else {
         if (parent.variables.ContainsKey (variableName)) {
             return parent.variables [variableName];
         } else {
             throw new RunTimeException (String.Format ("Error At Line [{0}]: Use of undefined variable '{1}'", lineNumber, variableName));
         }
     }
 }
 /// Author: Bert van Montfort
 /// <summary>
 /// Executes a walk 1 step forward command for the robot.
 /// </summary>
 /// <param name="parent">Parent.</param>
 public override bool execute(Composite parent)
 {
     try{
         Robot robot= Robot.Instance;
         if (Robot.bg != null) {
             Robot.bg.ReportProgress (lineNumber);
         }
         robot.Moves++;
         robot.Forward(lineNumber);
     return true;
     } catch (RobotException ex){
         throw ex;
     }
 }
        /// Author: Bert van Montfort
        /// <summary>
        /// Solve the specified parent.
        /// </summary>
        /// <param name="parent">Parent.</param>
        public override bool solve(Composite parent)
        {
            Variable leftVar = leftSolver.solve (parent);
            Variable rightVar = rightSolver.solve (parent);
            bool result;

            switch (comparisonOperator) {
            case EComparisonOperator.ValueEqualTo:
                if (leftVar.Value == rightVar.Value) {
                    result = true;
                } else
                    result = false;
                break;
            case EComparisonOperator.ValueGreaterThan:
                if (leftVar.Value > rightVar.Value) {
                    result = true;
                } else
                    result = false;
                break;
            case EComparisonOperator.ValueGreaterThanOrEqualTo:
                if (leftVar.Value >= rightVar.Value) {
                    result = true;
                } else
                    result = false;
                break;
            case EComparisonOperator.ValueLessThan:
                if (leftVar.Value < rightVar.Value) {
                    result = true;
                } else
                    result = false;
                break;
            case EComparisonOperator.ValueLessThanOrEqualTo:
                if (leftVar.Value <= rightVar.Value) {
                    result = true;
                } else
                    result = false;
                break;
            case EComparisonOperator.ValueNotEqualTo:
                if (leftVar.Value != rightVar.Value) {
                    result = true;
                } else
                    result = false;
                break;
            default:
                result = false;
                break;
            }
            return result;
        }
 public override bool execute(Composite parent)
 {
     try{
         Robot robot = Robot.Instance;
         if (Robot.bg != null) {
             Robot.bg.ReportProgress (lineNumber);
         }
         robot.PickUp(lineNumber, ObjectToPickUP);
     return false;
     }
     catch(RobotException ex){
         throw ex;
     }
     catch(MapException ex){
         throw ex;
     }
 }
        /// Author: Bert van Montfort
        /// <summary>
        /// Executes the For loop
        /// The steps are:
        /// 1. the defineVariable is executed
        /// 2. the Solver is checked
        /// 3. if Solver.solve() is true all the for loop children will be executed, else the loop ends
        /// 4. incrementCommand is executed
        /// 5. back to step 2.
        /// 
        /// after 300 loops the loop will break and assume the loop is infinite. 
        /// </summary>
        /// <param name="parent">Parent of the Compisite you are executing</param>
        public override bool execute(Composite parent)
        {
            base.execute (parent);
            int count = 0;

            declareVariable.execute (this);
            while (solver.solve(this)) {
                foreach (ICodeBlock child in children) {
                    child.execute (this);
                    Thread.Sleep (GlobalSupport.GameSpeed);
                }
                incrementCommand.execute (this);
                count++;
                if (count >= 100) {
                    return false;
                }
            }
            return true;
        }
        /// Author: Bert van Montfort
        /// <summary>
        /// Solve this instance.
        /// </summary>
        public override bool solve(Composite parent)
        {
            bool leftValue = leftSolver.solve(parent);
            bool rightValue = (rightSolver != null) ? rightSolver.solve (parent) : false;

            switch (logicOperator) {
            case ELogicOperators.And:
                if (leftValue && rightValue) {
                    return true;
                }
                break;
            case ELogicOperators.Not:
                return !leftValue;
            case ELogicOperators.Or:
                if (leftValue || rightValue) {
                    return true;
                }
                break;
            default:
                break;
            }
            return false;
        }
 public abstract Variable solve(Composite parent);
 public abstract bool execute(Composite parent);
 /// Author: Bert van Montfort
 /// <summary>
 /// Execute the specified parent.
 /// </summary>
 /// <param name="parent">Composite Parent of this Command</param>
 public override bool execute(Composite parent)
 {
     return executeCommand (parent);
 }
 public abstract bool solve(Composite parent);
 private bool executeCommand(Composite parent)
 {
     FunctionBlockList.getFunction (functionName, lineNumber).execute(parent);
     return true;
 }
 /// Author: Bert van Montfort
 /// <summary>
 /// Tells the hold Variablesolver to Solve and returns it's value
 /// </summary>
 /// <param name="parent">Composite Parent.</param>
 public override Variable solve(Composite parent)
 {
     return assignment.solve (parent);
 }
 /// Author: Bert van Montfort
 /// <summary>
 /// 
 /// </summary>
 /// <param name="parent">Parent.</param>
 public override bool solve(Composite parent)
 {
     return solvePiece(instruction);
 }