Пример #1
0
        async Task BuildTransition <T>(WorkflowData workflow, T source, T target, Transition data, Func <T, IInstanceNode> nodegetter)
        {
            IScript condition = string.IsNullOrEmpty(data.Condition) ? null : await compiler.CompileCodeAsync(data.Condition, data.Language ?? workflow.Language ?? ScriptLanguage.NCScript);

            List <InstanceTransition> transitions;

            switch (data.Type)
            {
            case TransitionType.Standard:
                transitions = nodegetter(source).Transitions;
                break;

            case TransitionType.Error:
                transitions = nodegetter(source).ErrorTransitions;
                break;

            case TransitionType.Loop:
                transitions = nodegetter(source).LoopTransitions;
                break;

            default:
                throw new ArgumentException($"Invalid type '{data.Type}'");
            }

            transitions.Add(new InstanceTransition {
                Target    = nodegetter(target),
                Condition = condition,
                Log       = string.IsNullOrEmpty(data.Log) ? null : await compiler.CompileCodeAsync(data.Log, data.Language ?? workflow.Language ?? ScriptLanguage.NCScript)
            });
        }
Пример #2
0
        /// <inheritdoc />
        public override async Task <object> Execute(WorkflowInstanceState state, CancellationToken token)
        {
            logparameter ??= await compiler.CompileCodeAsync(Parameters.Text, state.Language ?? ScriptLanguage.NCScript);

            state.Logger.Log(Parameters.Type, await logparameter.ExecuteAsync <string>(state.Variables, token));
            return(null);
        }
Пример #3
0
        async Task <IEnumerator> CreateEnumerator(WorkflowInstanceState state, CancellationToken token)
        {
            IScript enumerationscript = await compiler.CompileCodeAsync(Parameters.Collection, state.Language ?? ScriptLanguage.NCScript);

            object collection = await enumerationscript.ExecuteAsync(state.Variables, token);

            if (!(collection is IEnumerable enumerable))
            {
                throw new WorkflowException("Can not enumerate null");
            }

            return(enumerable.GetEnumerator());
        }
        /// <inheritdoc />
        public async Task <WorkableTask> Execute(NamedCode code, IDictionary <string, object> variables = null, TimeSpan?wait = null)
        {
            IDictionary <string, object> runtimevariables = await variables.TranslateVariables(scriptcompiler, ScriptLanguage.NCScript);

            WorkableTask scripttask = scriptinstances.CreateTask(WorkableType.Script, 0, 0, code.Name, runtimevariables);

            try {
                return(await Execute(await scriptcompiler.CompileCodeAsync(code.Code, code.Language), scripttask, runtimevariables, wait));
            }
            catch (Exception e) {
                scripttask.Log.Add(e.ToString());
                scripttask.Status = TaskStatus.Failure;
                await scriptinstances.FinishTask(scripttask.Id);
            }

            return(scripttask);
        }
        /// <inheritdoc />
        public override async Task <object> Execute(WorkflowInstanceState state, CancellationToken token)
        {
            expression ??= await compiler.CompileCodeAsync(GenerateCode(state.Language ?? ScriptLanguage.NCScript), state.Language ?? ScriptLanguage.NCScript);

            object result = await expression.ExecuteAsync(state.Variables, token);

            if (result is Task task)
            {
                await        task;
                PropertyInfo resultproperty = task.GetType().GetProperty("Result");
                if (resultproperty != null)
                {
                    return(resultproperty.GetValue(task));
                }
                return(null);
            }

            return(result);
        }
        /// <summary>
        /// translates workable variables to parameter values
        /// </summary>
        /// <param name="variables">variables to translate</param>
        /// <param name="compiler">compiler to use to parse variable scripts</param>
        /// <returns>workable parameter variables</returns>
        public static async Task <IDictionary <string, object> > TranslateVariables(this IDictionary <string, object> variables, IScriptCompiler compiler, ScriptLanguage?language)
        {
            if (variables == null)
            {
                return(null);
            }

            Dictionary <string, object> translated = new Dictionary <string, object>();

            foreach (KeyValuePair <string, object> kvp in variables)
            {
                if (kvp.Value is string code)
                {
                    translated[kvp.Key] = await(await compiler.CompileCodeAsync(code, language ?? ScriptLanguage.NCScript)).ExecuteAsync();
                }
                else
                {
                    translated[kvp.Key] = kvp.Value;
                }
            }

            return(translated);
        }
Пример #7
0
        /// <inheritdoc />
        public override async Task <object> Execute(WorkflowInstanceState state, CancellationToken token)
        {
            if (Parameters?.Parameters?.Length > 0)
            {
                foreach (ParameterDeclaration parameter in Parameters.Parameters)
                {
                    // not sure whether type specs are possible in every script language
                    Type type = (await compiler.CompileCodeAsync(parameter.Type, ScriptLanguage.NCScript)).Execute <Type>();

                    if (!state.Variables.TryGetValue(parameter.Name, out object parametervalue))
                    {
                        if (string.IsNullOrEmpty(parameter.Default))
                        {
                            throw new WorkflowException($"Missing required parameter '{parameter.Name}'");
                        }

                        parametervalue = await(await compiler.CompileCodeAsync(parameter.Default, state.Language ?? ScriptLanguage.NCScript)).ExecuteAsync(state.Variables, token);
                    }

                    if (parametervalue == null)
                    {
                        if (type.IsValueType)
                        {
                            throw new WorkflowException($"Unable to pass null to the value parameter '{parameter.Name}'");
                        }
                        state.Variables[parameter.Name] = null;
                    }
                    else
                    {
                        if (!type.IsInstanceOfType(parametervalue))
                        {
                            try {
                                if (type.IsArray)
                                {
                                    Type elementtype = type.GetElementType();
                                    if (parametervalue is IEnumerable enumeration)
                                    {
                                        object[] items = enumeration.Cast <object>().ToArray();
                                        Array    array = Array.CreateInstance(elementtype, items.Length);
                                        int      index = 0;
                                        foreach (object item in items)
                                        {
                                            if (item is IDictionary dic)
                                            {
                                                array.SetValue(dic.ToType(elementtype), index++);
                                            }
                                            else
                                            {
                                                array.SetValue(Converter.Convert(item, elementtype), index++);
                                            }
                                        }

                                        parametervalue = array;
                                    }
                                    else
                                    {
                                        Array array = Array.CreateInstance(elementtype, 1);
                                        array.SetValue(Converter.Convert(parametervalue, elementtype), 0);
                                        parametervalue = array;
                                    }
                                }
                                else
                                {
                                    if (parametervalue is IDictionary dic)
                                    {
                                        parametervalue = dic.ToType(type);
                                    }
                                    else if (parametervalue is IDictionary <string, object> expando)
                                    {
                                        parametervalue = expando.ToType(type);
                                    }
                                    else
                                    {
                                        parametervalue = Converter.Convert(parametervalue, type);
                                    }
                                }
                            }
                            catch (Exception e) {
                                throw new WorkflowException($"Unable to convert parameter '{parametervalue}' to '{type.Name}'", e);
                            }
                        }
                        state.Variables[parameter.Name] = parametervalue;
                    }
                }
            }
            return(Task.FromResult((object)null));
        }