/// <summary> /// Loops over the specified commands, re-evaluating the condition at the start of every loop. /// </summary> /// <param name='condition'> /// A condition which must remain true to continue executing the commands. Must be non-null. /// </param> /// <param name='commands'> /// A list of commands to be executed while condition is true. Must be non-null. /// </param> /// <exception cref="System.ArgumentNullException"></exception> public static CommandDelegate While(CommandCondition condition, params CommandDelegate[] commands) { CheckArgumentNonNull(condition, "condition"); CommandDelegate sequence; if (commands.Length == 0) { sequence = Commands.WaitForFrames(1); } else { sequence = Commands.Sequence(commands); } bool finished = true; return((ref double deltaTime) => { if (!finished) { finished = sequence(ref deltaTime); } while (finished) { if (!condition()) { return true; } finished = sequence(ref deltaTime); } return false; }); }
// <summary> /// Require the specified condition to be true to continue executing the given command. /// </summary> /// <param name='condition'> /// A condition which must remain true to continue executing the commands. Must be non-null. /// </param> /// <param name='command'> /// A deferred command. Must be non-null. This command is deferred, so that if the require condition fails, /// the command can be safely restarted. /// </param> /// <remarks> /// The condition is only re-evaluated on new calls to Update, or after the child command finishes and restarts. /// This means that if the condition suddenly becomes false while the command is executing, it /// won't be immediately escaped. /// </remarks> /// <example> /// <code> /// CommandQueue queue = new CommandQueue(); /// queue.Enqueue( /// Commands.Require( () => someObject != null, /// () => Commands.MoveTo(someObject, somePosition, someDuration) /// ) /// ); /// </code> /// </example> /// <exception cref="System.ArgumentNullException"></exception> public static CommandDelegate Require(CommandCondition condition, CommandFactory deferredCommand) { CheckArgumentNonNull(condition, "condition"); CheckArgumentNonNull(deferredCommand, "deferredCommand"); CommandDelegate command = null; return((ref double deltaTime) => { if (command == null) { command = deferredCommand(); } if (command == null) { return true; } bool finished = condition() ? command(ref deltaTime) : true; if (finished) { command = null; } return finished; }); }
protected override IEnumerable <CommandParameters> Execute(IEnumerable <CommandParameters> inParametersList) { foreach (var inParameters in inParametersList) { this.matchingCondition = ConditionEvaluator.GetFirstMatchingCondition(this.CommandConditions, inParameters.ToDictionary()) as CommandCondition; var outParameters = this.GetCurrentOutParameters(); yield return(outParameters); } }
public void ShouldReturnTrue() { var condition = new CommandCondition(); var result = condition.Analyze(new ScriptContext { Script = "Invoke-WebRequest" }, new Configuration.Condition { Property = "Command", Operator = "equals", Value = "Invoke-WebRequest" }); Assert.True(result); }
/// <summary> /// A Condition command allows branching behaviour. After a condition evaluates to <c>true</c> /// then onTrue will be evaluated until it finishes. Otherise onFalse will be evaluated, (if it /// isn't null). When nested in a Repeat command, conditions will be re-evaluated once for every /// repeat. /// </summary> /// <param name="condition"> /// The condition to evaluate. Must be non-null. /// </param> /// <param name="onTrue"> /// The command to execute if condition evaluates to true. Must be non-null. /// </param> /// <param name="onFalse"> /// The command to execute if condition evaluates to false. /// </param> /// <exception cref="System.ArgumentNullException"></exception> public static CommandDelegate Condition(CommandCondition condition, CommandDelegate onTrue, CommandDelegate onFalse = null) { CheckArgumentNonNull(condition, "condition"); CheckArgumentNonNull(onTrue, "onTrue"); var result = onFalse; return(Sequence( Do(() => { result = onFalse; if (condition()) { result = onTrue; } }), (ref double deltaTime) => result == null || result(ref deltaTime))); }
/// <summary> /// A Condition command allows branching behaviour. After a condition evaluates to <c>true</c> /// then onTrue will be evaluated until it finishes. Otherise onFalse will be evaluated, (if it /// isn't null). When nested in a Repeat command, conditions will be re-evaluated once for every /// repeat. /// </summary> /// <param name="condition"> /// The condition to evaluate. Must be non-null. /// </param> /// <param name="onTrue"> /// The command to execute if condition evaluates to true. Must be non-null. /// </param> /// <param name="onFalse"> /// The command to execute if condition evaluates to false. /// </param> /// <exception cref="System.ArgumentNullException"></exception> public static CommandDelegate Condition(CommandCondition condition, CommandDelegate onTrue, CommandDelegate onFalse = null) { CheckArgumentNonNull(condition, "condition"); CheckArgumentNonNull(onTrue, "onTrue"); CommandDelegate result = onFalse; return Sequence( Commands.Do( () => { result = onFalse; if (condition()) { result = onTrue; } }), (ref double deltaTime) => { if (result != null) { return result(ref deltaTime); } return true; } ); }
internal override void Implies(CommandCondition c) { if (!ShouldVerify) { return; } if (c == CommandCondition.ExactlyOne) { if (_min.GetValueOrDefault(1) != 1 || _max.GetValueOrDefault(1) != 1) { throw SpecFailure.ExactlyOnePlural(); } } else if (c == CommandCondition.NotOneButZeroOrMore) { if (_min.GetValueOrDefault(1) == 1 && _max.GetValueOrDefault(1) == 1) { throw SpecFailure.ExactlyOneSingular(); } } }
internal override void Implies(CommandCondition c) { _inner.Implies(c); }
/// <summary> /// Loops over the specified commands, re-evaluating the condition at the start of every loop. /// </summary> /// <param name='condition'> /// A condition which must remain true to continue executing the commands. Must be non-null. /// </param> /// <param name='commands'> /// A list of commands to be executed while condition is true. Must be non-null. /// </param> /// <exception cref="System.ArgumentNullException"></exception> public static CommandDelegate While(CommandCondition condition, params CommandDelegate[] commands) { CheckArgumentNonNull(condition, "condition"); CommandDelegate sequence; if (commands.Length == 0) { sequence = Commands.WaitForFrames(1); } else { sequence = Commands.Sequence(commands); } bool finished = true; return (ref double deltaTime) => { if (!finished) { finished = sequence(ref deltaTime); } while (finished) { if (!condition()) { return true; } finished = sequence(ref deltaTime); } return false; }; }
// <summary> /// Require the specified condition to be true to continue executing the given command. /// </summary> /// <param name='condition'> /// A condition which must remain true to continue executing the commands. Must be non-null. /// </param> /// <param name='command'> /// A deferred command. Must be non-null. This command is deferred, so that if the require condition fails, /// the command can be safely restarted. /// </param> /// <remarks> /// The condition is only re-evaluated on new calls to Update, or after the child command finishes and restarts. /// This means that if the condition suddenly becomes false while the command is executing, it /// won't be immediately escaped. /// </remarks> /// <example> /// <code> /// CommandQueue queue = new CommandQueue(); /// queue.Enqueue( /// Commands.Require( () => someObject != null, /// () => Commands.MoveTo(someObject, somePosition, someDuration) /// ) /// ); /// </code> /// </example> /// <exception cref="System.ArgumentNullException"></exception> public static CommandDelegate Require(CommandCondition condition, CommandFactory deferredCommand) { CheckArgumentNonNull(condition, "condition"); CheckArgumentNonNull(deferredCommand, "deferredCommand"); CommandDelegate command = null; return (ref double deltaTime) => { if (command == null) { command = deferredCommand(); } if (command == null) { return true; } bool finished = condition() ? command(ref deltaTime) : true; if (finished) { command = null; } return finished; }; }