Exemple #1
0
        public bool Set <T>(string variableKey, T value = default(T), bool skipIfExists = false)
        {
            string variable = ParameterDicUtils.GetVariable(variableKey);

            string[] variableSplit = variable.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            if (variableSplit.Length > 1)
            {
                //If have more than one hierarchy (e.g. {Code.Directory.Temp})
                //Remove last node, and construct as variable key (e.g. {Code.Directory}))
                string parentKey    = "{" + String.Join(".", variableSplit.Take(variableSplit.Length - 1)) + "}";
                string childPath    = variableSplit.Last();
                string childKey     = "{" + childPath + "}";
                object parentObject = Get(parentKey);

                //If ParentObject not found, create as ParameterDic.
                if (parentObject == null)
                {
                    parentObject = new ParameterDic();
                    Set <ParameterDic>(parentKey, (ParameterDic)parentObject, false);
                }

                if (parentObject is IParameterDic)
                {
                    (parentObject as IParameterDic).Set <T>(childKey, value, skipIfExists);
                    return(true);
                }
                else
                {
                    TypeInfoUtils.SetProperty(parentObject, childPath, value);
                    return(true);
                }
            }
            else
            if (_store.ContainsKey(variable))
            {
                if (!skipIfExists)
                {
                    if (!(_store[variable] is T) || !(_store[variable].Equals(value)))
                    {
                        _store[variable] = value;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                _store.Add(variable, value);
                return(true);
            }
        }
Exemple #2
0
        /// <summary>
        /// Given an array filter using IfValue, and store matched item to destination variable.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceArrayVariable"></param>
        /// <param name="property"></param>
        /// <param name="op"></param>
        /// <param name="value"></param>
        /// <param name="destinationArrayVariable"></param>
        /// <param name="nextCommand"></param>
        /// <returns></returns>
        public static IScriptCommand FilterArray <T>(string sourceArrayVariable      = "{SourceArray}", string property   = "Property",
                                                     ComparsionOperator op           = ComparsionOperator.Equals, T value = default(T),
                                                     string destinationArrayVariable = "{DestinationArray}", IScriptCommand nextCommand = null)
        {
            string valueVariable = ParameterDicUtils.CombineVariable(sourceArrayVariable, "ValueCompare");

            return(Assign(valueVariable, value,
                          FilterArray(sourceArrayVariable, property, op, valueVariable, destinationArrayVariable, nextCommand)));
        }
Exemple #3
0
        /// <summary>
        /// Serializable, shortcut method for [AssignValueConverter], which a specific item from an array from a variable and assign to another variable.
        /// </summary>
        /// <param name="arrayVariable"></param>
        /// <param name="id"></param>
        /// <param name="destinationVariable"></param>
        /// <param name="nextCommand"></param>
        /// <returns></returns>
        public static IScriptCommand GetArrayItem(string arrayVariable       = "{Array}", int id = 0,
                                                  string destinationVariable = "{Destination}", IScriptCommand nextCommand = null)
        {
            string valueConverterVariable = ParameterDicUtils.CombineVariable(arrayVariable, "Converter");

            return(AssignValueConverter(ValueConverterType.GetArrayItem, valueConverterVariable,
                                        ScriptCommands.Reassign(arrayVariable, valueConverterVariable,
                                                                destinationVariable, nextCommand), id));
        }
Exemple #4
0
        /// <summary>
        /// Iterate an IEnumeration and return true if anyone's property equals to the value.
        /// <example>
        /// IScriptCommand iterateCommand2 =
        ///      ScriptCommands.ForEachIfAnyValue<DateTime>("{Items}", null, ComparsionOperator.Equals, DateTime.Today,
        ///	    ScriptCommands.PrintDebug("True"), ScriptCommands.PrintDebug("False"));
        /// </example>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="itemsVariable"></param>
        /// <param name="property"></param>
        /// <param name="op"></param>
        /// <param name="value"></param>
        /// <param name="nextCommand"></param>
        /// <param name="otherwiseCommand"></param>
        /// <returns></returns>
        public static IScriptCommand ForEachIfAnyValue <T>(string itemsVariable       = "{Items}", string property            = null,
                                                           ComparsionOperator op      = ComparsionOperator.Equals, T value    = default(T),
                                                           IScriptCommand nextCommand = null, IScriptCommand otherwiseCommand = null)
        {
            string compareVariable = ParameterDicUtils.CombineVariable(itemsVariable.Replace(".", ""), "Compare");

            return(Assign(compareVariable, value,
                          ForEachIfAnyValue(itemsVariable, property, op, compareVariable, nextCommand, otherwiseCommand)));
        }
Exemple #5
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));
        }
Exemple #6
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));
        }
        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)));
        }
Exemple #8
0
        /// <summary>
        /// Serializable, Use Reassign to obtain a property of a vaiable in ParameterDic and use IfEquals to compare, and run different command based on result.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="variable"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <param name="trueCommand"></param>
        /// <param name="otherwiseCommand"></param>
        /// <returns></returns>
        public static IScriptCommand IfPropertyEquals <T>(string variable                 = "{variable}", string propertyName = "Property",
                                                          T value                         = default(T),
                                                          IScriptCommand trueCommand      = null,
                                                          IScriptCommand otherwiseCommand = null)
        {
            string variableProperty = ParameterDicUtils.CombineVariable(variable, propertyName);
            string valueProperty    = "{IfPropertyEquals-Value}";

            return(GetProperty(variable, propertyName, valueProperty,
                               IfEquals <T>(valueProperty, value, trueCommand, otherwiseCommand)));
        }
Exemple #9
0
 public void Remove(params string[] variableKeys)
 {
     foreach (string variableKey in variableKeys)
     {
         string variable = ParameterDicUtils.GetVariable(variableKey);
         if (_store.ContainsKey(variable))
         {
             _store.Remove(variable);
         }
     }
 }
Exemple #10
0
        /// <summary>
        /// Given an array filter using IfValue, and store matched item to destination variable.
        /// </summary>
        /// <param name="sourceArrayVariable"></param>
        /// <param name="property"></param>
        /// <param name="op"></param>
        /// <param name="valueVariable"></param>
        /// <param name="destinationArrayVariable"></param>
        /// <param name="nextCommand"></param>
        /// <returns></returns>
        public static IScriptCommand FilterArray(string sourceArrayVariable      = "{SourceArray}", string property                 = "Property",
                                                 ComparsionOperator op           = ComparsionOperator.Equals, string valueVariable  = "{Value}",
                                                 string destinationArrayVariable = "{DestinationArray}", IScriptCommand nextCommand = null)
        {
            string currentItemVariable     = ParameterDicUtils.CombineVariable(sourceArrayVariable, "Current");
            string currentPropertyVariable = ParameterDicUtils.CombineVariable(sourceArrayVariable, "Current" +
                                                                               (property == null ? "" : "." + property));
            string tempArrayVariable = ParameterDicUtils.CombineVariable(sourceArrayVariable, "Temp");

            return(ForEach(sourceArrayVariable, currentItemVariable,
                           IfValue(op, currentPropertyVariable, valueVariable,
                                   ConcatArray(tempArrayVariable, new object[] { currentItemVariable }, tempArrayVariable)),
                           Assign(destinationArrayVariable, tempArrayVariable, nextCommand)));
        }
Exemple #11
0
        /// <summary>
        /// Iterate an IEnumeration and return true if anyone's property equals to the specified variable.
        /// </summary>
        /// <example>
        /// IScriptCommand iterateCommand2 =
        ///    ScriptCommands.ForEachIfAnyValue<DateTime>("{Items}", "Day", ComparsionOperator.Equals, "Today.Day",
        ///	   ScriptCommands.PrintDebug("True"), ScriptCommands.PrintDebug("False"));
        /// </example>
        /// <param name="itemsVariable"></param>
        /// <param name="property"></param>
        /// <param name="op"></param>
        /// <param name="compareVariable"></param>
        /// <param name="nextCommand"></param>
        /// <param name="otherwiseCommand"></param>
        /// <returns></returns>
        public static IScriptCommand ForEachIfAnyValue(string itemsVariable       = "{Items}", string property = null,
                                                       ComparsionOperator op      = ComparsionOperator.Equals, string compareVariable = "{Variable}",
                                                       IScriptCommand nextCommand = null, IScriptCommand otherwiseCommand             = null)
        {
            string currentItemVariable     = ParameterDicUtils.CombineVariable(itemsVariable.Replace(".", ""), "Current");
            string currentPropertyVariable = ParameterDicUtils.CombineVariable(itemsVariable.Replace(".", ""), "Current" +
                                                                               ((property == null) ? "" : "." + property));
            string resultVariable = ParameterDicUtils.CombineVariable(itemsVariable.Replace(".", ""), "Result");

            return
                (Assign(resultVariable, false,
                        ForEach(itemsVariable, currentItemVariable, resultVariable,
                                IfValue(op, currentPropertyVariable, compareVariable,
                                        Assign(resultVariable, true)),
                                IfTrue(resultVariable, nextCommand, otherwiseCommand))));
        }
Exemple #12
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()));
        }
Exemple #13
0
        public T Get <T>(string variableKey, T defaultValue = default(T))
        {
            string variable = ParameterDicUtils.GetVariable(variableKey);

            string[] variableSplit = variable.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            var    match   = Regex.Match(variableSplit[0], RegexPatterns.ParseArrayCounterPattern);
            string varName = match.Groups["variable"].Value;
            //If the variable has an array or list index (e.g. array[0])
            int idx = match.Groups["counter"].Success ?
                      Int32.Parse(match.Groups["counter"].Value) : -1;

            if (_store.ContainsKey(varName))
            {
                object initValue = _store[varName];
                if (initValue == null)
                {
                    return(defaultValue);
                }
                if (initValue is ParameterDic && idx == -1 && variableSplit.Length > 1)
                {
                    //Omit the first variable.
                    string trailVariable = "{" + String.Join(".", variableSplit.Skip(1).ToArray()) + "}";
                    return((initValue as ParameterDic).Get <T>(trailVariable, defaultValue));
                }
                if (idx != -1 && initValue is Array)
                {
                    initValue = (initValue as Array).GetValue(idx);
                }
                var val = TypeInfoUtils.GetPropertyOrMethod(initValue, variableSplit.Skip(1).ToArray());
                if (val is T)
                {
                    return((T)val);
                }
            }

            return(defaultValue);
        }
Exemple #14
0
 public static ParameterPair FromVariable(string variable, object value)
 {
     return(new ParameterPair(ParameterDicUtils.GetVariable(variable), value));
 }