public override IScriptCommand Execute(IParameterDic pm) { object source = pm.Get <object>(SourceVariableKey); if (source == null) { logger.Error("Source not found."); } if (ValueConverterKey != null) { object valueConverter = pm.Get <object>(ValueConverterKey); if (valueConverter is Func <object, object> ) //GetProperty, ExecuteMethod, GetArrayItem { Func <object, object> valueConverterFunc = valueConverter as Func <object, object>; object value = valueConverterFunc(source); pm.Set(DestinationVariableKey, value, SkipIfExists); } else if (valueConverter is Action <object, object> ) //SetProperty { Action <object, object> valueConverterAct = valueConverter as Action <object, object>; object value = pm.Get <object>(DestinationVariableKey); valueConverterAct(source, value); } } else { pm.Set(DestinationVariableKey, source, SkipIfExists); } return(NextCommand); }
public override IScriptCommand Execute(IParameterDic pm) { string variable = pm.ReplaceVariableInsideBracketed(VariableKey); print(variable); return(NextCommand); }
public static T RunScript <T>(string resultVariable = "{Result}", IParameterDic initialParameters = null, params IScriptCommand[] commands) { initialParameters = initialParameters ?? new ParameterDic(); RunScript(initialParameters, false, commands); return(initialParameters.Get(resultVariable, default(T))); }
public override IScriptCommand Execute(IParameterDic pm) { object value = Value; if (ValueFunc != null) { value = ValueFunc(); } if (value is string) { string valueString = (string)value; if (valueString.StartsWith("{") && valueString.EndsWith("}")) { value = pm.Get(valueString); } } if (pm.Set <Object>(VariableKey, value, SkipIfExists)) { logger.Debug(String.Format("{0} = {1}", VariableKey, value)); } // else logger.Debug(String.Format("Skipped {0}, already exists.", VariableKey)); return(NextCommand); }
public override bool CanExecute(IParameterDic pm) { IParameterDic pm2 = pm.Clone(); ScriptRunner.RunScriptAsync(pm2, ConditionCommand); return(pm2.IsHandled() && pm2.Error() == null); }
public override IScriptCommand Execute(IParameterDic pm) { switch (Mode) { case RunMode.Parallel: case RunMode.Queue: ScriptRunner.RunScript(pm, ScriptCommands); break; case RunMode.Sequence: foreach (var cmd in ScriptCommands) { ScriptRunner.RunScript(pm, cmd); 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); } }
public override async Task <IScriptCommand> ExecuteAsync(IParameterDic pm) { if (_condition(pm)) { return(_ifTrueCommand); } return(_otherwiseCommand); }
public override IScriptCommand Execute(IParameterDic pm) { if (_condition(pm)) { return(_ifTrueCommand); } return(_otherwiseCommand); }
public override IScriptCommand Execute(IParameterDic pm) { if (!ParameterDicDictionary.ContainsKey(VariableKey)) { ParameterDicDictionary.Add(VariableKey, new ParameterDic()); } Value = ParameterDicDictionary[VariableKey]; return(base.Execute(pm)); }
private bool runCommand(IScriptCommand command, IParameterDic pm = null) { pm = pm ?? new ParameterDic(); while (command != null && !(command is ResultCommand)) { command = command.Execute(pm); } return((command == null) && (pm.Error() == null)); }
public async Task RunAsync(Queue <IScriptCommand> cmds, IParameterDic initialParameters) { IParameterDic pd = initialParameters; pd.ScriptRunner(this); while (cmds.Any()) { try { var current = cmds.Dequeue(); if (current.CanExecute(pd)) { //pd.CommandHistory.Add(current.CommandKey); //logger.Info("Running " + current.CommandKey); try { var retCmd = await current.ExecuteAsync(pd) .ConfigureAwait(current.RequireCaptureContext()); if (retCmd != null) { if (pd.Error() != null) { logger.Error("Error when running script", pd.Error()); return; } cmds.Enqueue(retCmd); } } catch (Exception ex) { throw ex; } } else { throw new Exception(String.Format("Cannot execute {0}", current)); } } catch (Exception ex) { pd.Error(ex); logger.Error("Error when running script", ex); var progress = pd.Progress <Defines.TransferProgress>(); if (progress != null) { progress.Report(Defines.TransferProgress.Error(ex)); } throw ex; } } }
/// <summary> /// Subtract specified values to the value assigned to the parameter dic. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dic"></param> /// <param name="variableKey"></param> /// <param name="values"></param> /// <returns></returns> public static T Subtract <T>(this IParameterDic dic, string variableKey, params T[] values) { T retValue = dic.Get <T>(variableKey, default(T)); foreach (T value in values) { retValue = ExpressionUtils.Subtract <T>(value, retValue); } dic.Set <T>(variableKey, retValue); return(retValue); }
public override async Task <IScriptCommand> ExecuteAsync(IParameterDic pm) { if (CanExecute(pm)) { return(NextCommand); } else { return(ResultCommand.Error(pm.Error() ?? new ArgumentException("pm"))); } }
public static void RunScript(IParameterDic initialParameters, bool cloneParameters, params IScriptCommand[] commands) { if (cloneParameters) { initialParameters = initialParameters.Clone(); } IScriptRunner runner = initialParameters.Get <IScriptRunner>("{ScriptRunner}", Instance); runner.Run(new Queue <IScriptCommand>(commands), initialParameters); }
public static Task RunScriptAsync(IParameterDic initialParameters, bool cloneParameters, params IScriptCommand[] commands) { if (cloneParameters) { initialParameters = initialParameters.Clone(); } IScriptRunner runner = initialParameters.Get <IScriptRunner>("{ScriptRunner}", Instance); return(runner.RunAsync(new Queue <IScriptCommand>(commands), initialParameters)); }
public override bool CanExecute(IParameterDic pm) { if (_canExecuteFunc == null) { return(true); } else { return(_canExecuteFunc(pm)); } }
public override bool CanExecute(IParameterDic pm) { if (_condition != null && _condition(pm)) { return(_ifTrueCommand == null || _ifTrueCommand.CanExecute(pm)); } else { return(_otherwiseCommand == null || _otherwiseCommand.CanExecute(pm)); } }
public static string RandomVariable(this IParameterDic dic, string prefix = "") { var rnd = new Random(); string nextVariable = prefix + rnd.Next().ToString(); while (dic.List().Contains(nextVariable)) { nextVariable = prefix + rnd.Next().ToString(); } return("{" + nextVariable + "}"); }
private bool compare(IParameterDic pm) { try { object value1 = pm.Get <Object>(Variable1Key); object value2 = pm.Get <Object>(Variable2Key); if (value1 != null && value2 != null) { var left = Expression.Constant(value1); var right = Expression.Constant(value2); Expression expression; switch (Operator) { case ComparsionOperator.Equals: expression = Expression.Equal(left, right); break; case ComparsionOperator.GreaterThan: expression = Expression.GreaterThan(left, right); break; case ComparsionOperator.GreaterThanOrEqual: expression = Expression.GreaterThanOrEqual(left, right); break; case ComparsionOperator.LessThan: expression = Expression.LessThan(left, right); break; case ComparsionOperator.LessThanOrEqual: expression = Expression.LessThanOrEqual(left, right); break; case ComparsionOperator.StartWith: case ComparsionOperator.StartWithIgnoreCase: return(value1.ToString().StartsWith(value2.ToString(), Operator == ComparsionOperator.StartWith ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase)); case ComparsionOperator.EndsWith: case ComparsionOperator.EndsWithIgnoreCase: return(value1.ToString().EndsWith(value2.ToString(), Operator == ComparsionOperator.EndsWith ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase)); default: throw new NotSupportedException(Operator.ToString()); } return(Expression.Lambda <Func <bool> >(expression).Compile().Invoke()); } else { return(Operator == ComparsionOperator.Equals && value1 == null && value2 == null); } } catch (Exception ex) { throw ex; } }
public override IScriptCommand Execute(IParameterDic pm) { ScriptRunner.RunScript(pm, ScriptCommands); if (pm.Error() != null) { return(ResultCommand.Error(pm.Error())); } else { return(_nextCommand); } }
public override async Task <IScriptCommand> ExecuteAsync(IParameterDic pm) { await ScriptRunner.RunScriptAsync(pm, ScriptCommands); if (pm.Error() != null) { return(ResultCommand.Error(pm.Error())); } else { return(_nextCommand); } }
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)); }
public override IScriptCommand Execute(IParameterDic pm) { object obj = pm.Get(VariableKey); if (obj != null) { print(obj.ToString()); var typeInfo = obj is Array ? typeof(Array).GetTypeInfo() : obj.GetType().GetTypeInfo(); foreach (var pi in typeInfo.EnumeratePropertyInfoRecursive()) { print(string.Format(Format, pi.Name, pi.GetValue(obj))); } } return(base.Execute(pm)); }
public override IScriptCommand Execute(IParameterDic pm) { Func <object, object> checkParameters = p => { if (p is string) { string pString = p as string; if (pString.StartsWith("{") && pString.EndsWith("}")) { return(pm.Get(pString)); } } return(p); }; object firstValue = pm.Get(Value1Key); object secondValue = pm.Get(Value2Key); List <object> secondArrayList = secondValue is Array ? (secondValue as Array).Cast <object>().ToList() : new List <object>() { secondValue }; object value = firstValue; string methodName = OperatorType.ToString(); var mInfo = typeof(FileExplorer.Utils.ExpressionUtils) .GetRuntimeMethods().First(m => m.Name == methodName) .MakeGenericMethod(value.GetType()); foreach (var addItem in secondArrayList.Select(p => checkParameters(p)).ToArray()) { switch (mInfo.GetParameters().Length) { case 1: value = mInfo.Invoke(null, new object[] { value }); break; case 2: value = mInfo.Invoke(null, new object[] { value, addItem }); break; default: throw new NotSupportedException(); } } Value = value; return(base.Execute(pm)); }
public override async Task <IScriptCommand> ExecuteAsync(IParameterDic pm) { IEnumerable e = pm.Get <IEnumerable>(ItemsKey); if (e == null) { return(ResultCommand.Error(new ArgumentException(ItemsKey))); } IProgress <TransferProgress> progress = NullProgress <TransferProgress> .Instance; if (IsProgressEnabled) { List <object> list; e = list = e.Cast <object>().ToList(); progress = pm.Progress <TransferProgress>(); progress.Report(TransferProgress.IncrementTotalEntries(list.Count)); } uint counter = 0; pm.Set <bool>(BreakKey, false); foreach (var item in e) { if (pm.Get <bool>(BreakKey)) { break; } counter++; pm.Set(CurrentItemKey, item); await ScriptRunner.RunScriptAsync(pm, NextCommand); progress.Report(TransferProgress.IncrementProcessedEntries()); if (pm.Error() != null) { pm.Set <Object>(CurrentItemKey, null); return(ResultCommand.Error(pm.Error())); } } logger.Info(String.Format("Looped {0} items", counter)); pm.Set <Object>(CurrentItemKey, null); return(ThenCommand); }
public override IScriptCommand Execute(IParameterDic pm) { if (_exception == null) { if (MarkHandled) { pm.IsHandled(true); } //logger.Debug("OK"); } else { logger.Error(_exception.Message, _exception); pm.Error(_exception); } return(NextCommand); }
public void Run(Queue <IScriptCommand> cmds, IParameterDic initialParameters) { IParameterDic pd = initialParameters; pd.ScriptRunner(this); while (cmds.Any()) { try { var current = cmds.Dequeue(); //logger.Info("Running " + current.CommandKey); if (current.CanExecute(pd)) { //(pd.Progress<IScriptCommand>() as IProgress<IScriptCommand>).Report() //pd.CommandHistory.Add(current.CommandKey); var retCmd = current.Execute(pd); if (retCmd != null) { if (pd.Error() != null) { throw pd.Error(); } cmds.Enqueue(retCmd); } } else if (!(current is NullScriptCommand)) { throw new Exception(String.Format("Cannot execute {0}", current)); } } catch (Exception ex) { pd.Error(ex); logger.Error("Error when running script", ex); var progress = pd.Progress <Defines.TransferProgress>(); if (progress != null) { progress.Report(Defines.TransferProgress.Error(ex)); } throw ex; } } }
public override IScriptCommand Execute(IParameterDic pm) { List <IScriptCommand> outputCommands = new List <IScriptCommand>(); foreach (var s in _source) { var command = _commandFunc(s); var outputCommand = command.Execute(pm); if (pm.Error() != null) { return(outputCommand); } if (outputCommand != ResultCommand.NoError && outputCommand != ResultCommand.OK) { outputCommands.Add(outputCommand); } } return(new RunInSequenceScriptCommand(outputCommands.ToArray(), _nextCommand)); }
public override IScriptCommand Execute(IParameterDic pm) { try { object value1 = pm.Get <Object>(Variable1Key); object value2 = pm.Get <Object>(Variable2Key); bool result = compare(pm); logger.Debug(String.Format("Executing {0} Command ({1})", result, result ? NextCommand : OtherwiseCommand)); return(result ? NextCommand : OtherwiseCommand); } catch (Exception ex) { return(OtherwiseCommand); } }
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); } }