Ejemplo n.º 1
0
        public static IScriptCommand IfEquals <T>(string variable = "{variable}", T value = default(T), IScriptCommand trueCommand = null,
                                                  IScriptCommand otherwiseCommand = null)
        {
            string ifEqualValueProperty = "{IfEquals-Value}";

            return
                (ScriptCommands.Assign(ifEqualValueProperty, value,
                                       IfValue(ComparsionOperator.Equals, variable, ifEqualValueProperty, trueCommand, otherwiseCommand)));
        }
Ejemplo n.º 2
0
        public static IScriptCommand RunICommand(ICommand command,
                                                 string parameterVariable,
                                                 bool throwIfError = false, IScriptCommand nextCommand = null)
        {
            string commandVariable = "{" + string.Format("Command{0}", new Random().Next()) + "}";

            return(ScriptCommands.Assign(commandVariable, command,
                                         ScriptCommands.RunICommand(commandVariable, parameterVariable, throwIfError, nextCommand)));
        }
Ejemplo n.º 3
0
        ///// <summary>
        ///// Add variables (using Expression) to destination.
        ///// </summary>
        ///// <param name="sourceObjectVariable"></param>
        ///// <param name="addValues"></param>
        ///// <param name="destinationVariable"></param>
        ///// <param name="nextCommand"></param>
        ///// <returns></returns>
        //public static IScriptCommand Add(string sourceObjectVariable = "{Source}",
        //    object[] addValues = null,
        //    string destinationVariable = "{Destination}", IScriptCommand nextCommand = null)
        //{
        //    string valueConverterVariable = ParameterDicUtils.CombineVariable(sourceObjectVariable, "Converter");
        //    return AssignValueConverter(ValueConverterType.AddValue, valueConverterVariable,
        //        ScriptCommands.Reassign(sourceObjectVariable, valueConverterVariable,
        //            destinationVariable, false, nextCommand), addValues);
        //}


        /// <summary>
        /// Concat array to destination
        /// </summary>
        /// <param name="sourceObjectVariable"></param>
        /// <param name="addValues"></param>
        /// <param name="destinationVariable"></param>
        /// <param name="nextCommand"></param>
        /// <returns></returns>
        public static IScriptCommand ConcatArray(string sourceObjectVariable = "{Source}",
                                                 object[] addValues          = null,
                                                 string destinationVariable  = "{Destination}", IScriptCommand nextCommand = null)
        {
            string valueConverterVariable = ParameterDicUtils.CombineVariable(sourceObjectVariable, "Converter");

            return(AssignValueConverter(ValueConverterType.ConcatArray, valueConverterVariable,
                                        ScriptCommands.Reassign(sourceObjectVariable, valueConverterVariable,
                                                                destinationVariable, nextCommand), addValues));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Set property of an object in ParameterDic to another object in ParameterDic.
        /// <example>
        /// ScriptCommands.SetProperty("{PSI}", "FileName", "{Value}")
        /// </example>
        /// </summary>
        /// <param name="sourceObjectVariable"></param>
        /// <param name="propertyName"></param>
        /// <param name="valueVariable"></param>
        /// <param name="nextCommand"></param>
        /// <returns></returns>
        public static IScriptCommand SetProperty(string sourceObjectVariable = "{Source}",
                                                 string propertyName         = "Property",
                                                 string valueVariable        = "{Value}", IScriptCommand nextCommand = null)
        {
            string valueConverterVariable = ParameterDicUtils.CombineVariable(sourceObjectVariable, "Converter");

            return(AssignValueConverter(ValueConverterType.SetProperty, valueConverterVariable,
                                        ScriptCommands.Reassign(sourceObjectVariable, valueConverterVariable,
                                                                valueVariable, nextCommand), propertyName));
        }
Ejemplo n.º 5
0
        public static IScriptCommand AddValue <T>(string value1Variable      = "{Value1}",
                                                  T[] value2                 = null,
                                                  string destinationVariable = "{Destination}", IScriptCommand nextCommand = null)
        {
            value2 = value2 ?? new T[] {};
            string value2Variable = ParameterDicUtils.RandomVariable();

            return(ScriptCommands.Assign(value2Variable, value2,
                                         Add(value1Variable, value2Variable, destinationVariable, nextCommand)));
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Serializable, Run IfValue comparsion based on the length of an array in ParameterDic.
 /// </summary>
 /// <param name="op"></param>
 /// <param name="arrayVariable"></param>
 /// <param name="valueVariable"></param>
 /// <param name="trueCommand"></param>
 /// <param name="otherwiseCommand"></param>
 /// <returns></returns>
 public static IScriptCommand IfArrayLength(ComparsionOperator op, string arrayVariable = "{array}",
                                            string valueVariable       = "{value}",
                                            IScriptCommand trueCommand = null, IScriptCommand otherwiseCommand = null)
 {
     return
         (ScriptCommands.IfAssigned(arrayVariable,
                                    ScriptCommands.AssignValueConverter(ValueConverterType.GetProperty, "{GetPropertyConverter}",
                                                                        ScriptCommands.Reassign(arrayVariable, "{GetPropertyConverter}", "{ArrayLength}",
                                                                                                ScriptCommands.PrintLogger(LogLevel.Debug, "Length of array is {ArrayLength}",
                                                                                                                           ScriptCommands.IfValue(op, "{ArrayLength}", valueVariable, trueCommand, otherwiseCommand))), "Length"),
                                    otherwiseCommand));
 }
Ejemplo n.º 7
0
        public override IScriptCommand Execute(IParameterDic pm)
        {
            IScriptCommand command = pm.Get <IScriptCommand>(CommandKey);

            if (command == null && ThrowIfError)
            {
                return(ResultCommand.Error(new ArgumentNullException(CommandKey)));
            }
            command = command ?? ResultCommand.NoError;

            log.Info("Running " + CommandKey);
            return(ScriptCommands.RunQueue(NextCommand, command));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Serializable, do switch...case code by using multiple IfValue.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="variable"></param>
        /// <param name="caseLookup"></param>
        /// <param name="otherwiseCommand"></param>
        /// <returns></returns>
        public static IScriptCommand Switch <T>(string variable = "{variable}",
                                                Dictionary <T, IScriptCommand> caseLookup = null,
                                                IScriptCommand otherwiseCommand           = null, IScriptCommand nextCommnad = null)
        {
            IScriptCommand cmd = null;

            foreach (var key in caseLookup.Keys)
            {
                cmd =
                    cmd == null?IfEquals <T>(variable, key, caseLookup[key], otherwiseCommand) :
                        IfEquals <T>(variable, key, caseLookup[key], cmd);
            }

            return(ScriptCommands.RunSequence(nextCommnad, cmd));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Serializable, shortcut method for [AssignValueConverter], which obtains method result of a property from a variable and assign to another variable.
        /// </summary>
        /// <param name="sourceObjectVariable"></param>
        /// <param name="methodName"></param>
        /// <param name="parameters"></param>
        /// <param name="destinationVariable"></param>
        /// <param name="nextCommand"></param>
        /// <returns></returns>
        public static IScriptCommand ExecuteFunc(string sourceObjectVariable = "{Source}",
                                                 string methodName           = "Method", object[] parameters = null,
                                                 string destinationVariable  = "{Destination}", IScriptCommand nextCommand = null)
        {
            string valueConverterVariable = ParameterDicUtils.CombineVariable(sourceObjectVariable, "Converter");

            List <object> methodParams = new List <object>();

            methodParams.Add(methodName);
            if (parameters != null)
            {
                methodParams.AddRange(parameters);
            }

            return(AssignValueConverter(ValueConverterType.ExecuteMethod, valueConverterVariable,
                                        ScriptCommands.Reassign(sourceObjectVariable, valueConverterVariable,
                                                                destinationVariable, nextCommand), methodParams.ToArray()));
        }
Ejemplo n.º 10
0
        public override async Task <IScriptCommand> ExecuteAsync(IParameterDic pm)
        {
            switch (Mode)
            {
            case RunMode.Parallel:
                await Task.WhenAll(ScriptCommands.Select(cmd => ScriptRunner.RunScriptAsync(pm.Clone(), cmd)));

                break;

            case RunMode.Queue:
                await ScriptRunner.RunScriptAsync(pm, ScriptCommands)
                .ConfigureAwait(this.ContinueOnCaptureContext);

                break;

            case RunMode.Sequence:
                foreach (var cmd in ScriptCommands)
                {
                    await ScriptRunner.RunScriptAsync(pm, cmd)
                    .ConfigureAwait(this.ContinueOnCaptureContext);

                    if (pm.Error() != null)
                    {
                        return(ResultCommand.Error(pm.Error()));
                    }
                }
                break;

            default:
                return(ResultCommand.Error(new NotSupportedException(Mode.ToString())));
            }

            if (pm.Error() != null)
            {
                return(ResultCommand.Error(pm.Error()));
            }
            else
            {
                return(NextCommand);
            }
        }
Ejemplo n.º 11
0
        public override IScriptCommand Execute(IParameterDic pm)
        {
            object value = Value;

            if (ValueFunc != null)
            {
                value = ValueFunc();
            }

            if (!(value is string))
            {
                value = value.ToString();
            }
            else
            {
                value = pm.ReplaceVariableInsideBracketed((string)value);
            }

            return(ScriptCommands.Assign(
                       VariableKey, value, NextCommand).IfExists(SkipIfExists));
        }
Ejemplo n.º 12
0
 public override bool CanExecute(IParameterDic pm)
 {
     return(ScriptCommands.Length == 0 || ScriptCommands.First().CanExecute(pm));
 }
Ejemplo n.º 13
0
 public static IScriptCommand Substring(string sourceObjectVariable, object startIdx,
                                        string destinationVariable = "{Destination}", IScriptCommand nextCommand = null)
 {
     return(ScriptCommands.ExecuteFunc(sourceObjectVariable, "Substring", new object[] { startIdx }, destinationVariable,
                                       nextCommand));
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Assign using multiple expression, variable name based on input of variable name.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="nextCommand"></param>
 /// <param name="memberExpressions"></param>
 /// <example>ScriptCommands.AssignMulti(ResultCommand.OK, () => val1, () => val2), pm));</example>
 /// <returns></returns>
 public static IScriptCommand AssignMulti <T>(IScriptCommand nextCommand,
                                              params Expression <Func <T> >[] memberExpressions)
 {
     return(ScriptCommands.RunSequence(nextCommand, memberExpressions.Select(
                                           (me) => Assign(me, null)).ToArray()));
 }
Ejemplo n.º 15
0
 public static IScriptCommand Reset(string variable, IScriptCommand nextCommand)
 {
     return(ScriptCommands.Assign(variable, null));
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Serializable, remove a variable from ParameterDic.
 /// </summary>
 /// <param name="nextCommand"></param>
 /// <param name="variables"></param>
 /// <returns></returns>
 public static IScriptCommand Reset(IScriptCommand nextCommand = null, params string[] variables)
 {
     return(ScriptCommands.Run(RunMode.Parallel, nextCommand,
                               variables.Select(v => ScriptCommands.Assign(v, null)).ToArray()));
 }