public void ExecuteWhileCommand() { IExpression incrementX = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new ConstantExpression(1), new VariableExpression("a")); IExpression decrementY = new ArithmeticBinaryExpression(ArithmeticOperator.Subtract, new VariableExpression("b"), new ConstantExpression(1)); ICommand setX = new SetVariableCommand("a", incrementX); ICommand setY = new SetVariableCommand("b", decrementY); List <ICommand> commands = new List <ICommand>(); commands.Add(setX); commands.Add(setY); ICommand command = new CompositeCommand(commands); IExpression yexpr = new VariableExpression("b"); WhileCommand whilecmd = new WhileCommand(yexpr, command); Context context = new Context(); context.SetValue("a", 0); context.SetValue("b", 5); whilecmd.Execute(context); Assert.AreEqual(0, context.GetValue("b")); Assert.AreEqual(5, context.GetValue("a")); }
public void ExecuteWhileCommandWithNullCondition() { Context context = new Context(); IExpression cond = new ConstantExpression(null); ICommand cmd = new ExpressionCommand(new AssignExpression("a", new ConstantExpression(42))); WhileCommand wcmd = new WhileCommand(cond, cmd); wcmd.Execute(context); Assert.IsNull(context.GetValue("a")); }
public static void Main() { MachineComposite dublin = ExampleMachine.Dublin(); Term sp = new Constant((Machine)dublin.Find("StarPress:1401")); Term ub = new Constant((Machine)dublin.Find("UnloadBuffer:1501")); WhileCommand wc = new WhileCommand( new HasMaterial(sp), new CarryCommand(sp, ub)); wc.Execute(); }
public void ExecuteWhileCommandWithTrueCondition() { Context context = new Context(); IExpression cond = new NameExpression("a"); ICommand cmd = new ExpressionCommand(new AssignExpression("a", new ConstantExpression(false))); WhileCommand wcmd = new WhileCommand(cond, cmd); context.SetValue("a", true); wcmd.Execute(context); Assert.AreEqual(false, context.GetValue("a")); }
public static void Main() { MachineComposite dublin = ExampleMachine.Dublin(); //Machine starPress = (Machine) dublin.Find("StarPress:1401"); // uncomment to add bins //starPress.Load(new Bin(42));// and see it work //starPress.Load(new Bin(84));// Term sp = new Constant((Machine)dublin.Find("StarPress:1401")); Term ub = new Constant((Machine)dublin.Find("UnloadBuffer:1501")); WhileCommand wc = new WhileCommand( new HasMaterial(sp), new CarryCommand(sp, ub)); wc.Execute(); }
public void CreateAndEvaluateSimpleWhileCommand() { BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("a", 1); IExpression condition = new CompareExpression(ComparisonOperator.Less, new NameExpression("a"), new ConstantExpression(10)); ICommand body = new SetCommand("a", new BinaryOperatorExpression(new NameExpression("a"), new ConstantExpression(1), BinaryOperator.Add)); WhileCommand command = new WhileCommand(condition, body); command.Execute(environment); Assert.AreEqual(condition, command.Condition); Assert.AreEqual(body, command.Command); Assert.AreEqual(10, environment.GetValue("a")); }
public void EvaluateWhileCommandUsingDecrement() { BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("a", 2); IExpression condition = new VariableExpression("a"); ICommand command = new SetCommand("a", new BinaryArithmeticExpression(new VariableExpression("a"), new ConstantExpression(1), ArithmeticOperator.Subtract)); WhileCommand wcommand = new WhileCommand(condition, command); Assert.AreEqual(command, wcommand.Command); Assert.AreEqual(condition, wcommand.Condition); wcommand.Execute(environment); Assert.AreEqual(0, environment.GetValue("a")); }
public void WhileWithAddExpression() { var expression = new CompareExpression(ComparisonOperator.Less, new VariableExpression("a"), new ConstantExpression(10)); var inccommand = new SetVariableCommand("k", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("k"), new ConstantExpression(1))); var addcommand = new SetVariableCommand("a", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("k"), new VariableExpression("a"))); var command = new CompositeCommand(new ICommand[] { inccommand, addcommand }); var whilecommand = new WhileCommand(expression, command); Context context = new Context(); context.SetValue("a", 0); context.SetValue("k", 0); var result = whilecommand.Execute(context); Assert.IsNull(result); Assert.IsNotNull(whilecommand.Condition); Assert.IsNotNull(whilecommand.Command); Assert.AreEqual(4, context.GetValue("k")); Assert.AreEqual(10, context.GetValue("a")); }