Esempio n. 1
0
        //用户获取用户列表
        string[] IScriptParser.EvaluateUsers(string script, Statements.DataFieldExtension extension)
        {
            var result = this.Evaluate <object>(this.PrepareScriptEngine(extension), script);

            if (this._log.IsDebugEnabled)
            {
                this._log.DebugFormat("执行人规则“{0}”解析结果={1}", script, result);
            }

            if (result is Jurassic.Library.ArrayInstance)
            {
                return((result as Jurassic.Library.ArrayInstance).ElementValues.Select(o => o.ToString()).ToArray());
            }

            if (result.GetType() != typeof(string))
            {
                throw new InvalidOperationException(string.Format("脚本“{0}”解析后返回了目前不支持的类型{1}", script, result));
            }

            var jsonUser = result.ToString();

            if (string.IsNullOrEmpty(jsonUser))
            {
                return(null);
            }
            //TODO:需要合理反序列化
            return(jsonUser.StartsWith("[")
                ? this._serializer.JsonDeserialize <string[]>(jsonUser)
                : (jsonUser.StartsWith("\"") ? new string[] { this._serializer.JsonDeserialize <string>(jsonUser) } : new string[] { jsonUser }));
        }
Esempio n. 2
0
        private Jurassic.ScriptEngine PrepareScriptEngine(Statements.DataFieldExtension extension)
        {
            var engine = new Jurassic.ScriptEngine();

            //注册方法列表
            this._invoker.GetMethods().ToList().ForEach(o =>
                                                        engine.SetGlobalFunction(o.Key
                                                                                 , new Func <object, object, object, object
                                                                                             //, object, object, object, object, object
                                                                                             , string>(
                                                                                     (v1, v2, v3, v4) =>
                                                                                     //, v5, v6, v7, v8, v9) =>
                                                                                     this.Invoke(o.Key, v1, v2, v3, v4))));
            //, v5, v6, v7, v8, v9))));

            //注册内置方法 会覆盖上述方法
            engine.SetGlobalFunction("updateDataField", new Action <string, string>((k, v) => extension.Set(k, v)));
            engine.SetGlobalFunction("split"
                                     , new Func <string, string, Jurassic.Library.ArrayInstance>((s, i) =>
                                                                                                 engine.Array.Call((i ?? "").Split(new string[] { s }, StringSplitOptions.RemoveEmptyEntries))));

            //注册依赖上下文的内置方法 会覆盖上述方法
            engine.SetGlobalFunction("getSuperior", new Func <string>(() => this._userHelper.GetSuperior(extension.Originator)));

            //注册流程变量
            foreach (var pair in extension.DataFields)
            {
                engine.SetGlobalValue(pair.Key, pair.Value ?? "");
            }

            //注册内置上下文变量 会覆盖上述流程变量
            engine.SetGlobalValue(WorkflowBuilder.Variable_Originator, extension.Originator);

            return(engine);
        }
Esempio n. 3
0
        //用于执行普通脚本
        string IScriptParser.Evaluate(string script, Statements.DataFieldExtension extension)
        {
            var result = this.Evaluate <string>(this.PrepareScriptEngine(extension), script);

            if (this._log.IsDebugEnabled)
            {
                this._log.DebugFormat("脚本“{0}”解析结果={1}", script, result);
            }

            //特化处理forward函数的返回值
            if (script.StartsWith("forward("))
            {
                return(result);
            }
            try
            {
                //由于脚本都是通过NSF调用外部服务,返回的字符串均是json结果,需要反序列化
                return(!string.IsNullOrEmpty(result) && result != "undefined"
                    ? this._serializer.JsonDeserialize <string>(result)
                    : string.Empty);
            }
            catch (Exception e)
            {
                if (this._log.IsDebugEnabled)
                {
                    this._log.Debug("反序列字符串异常,直接返回原始文本:" + result, e);
                }
                return(result);
            }
        }
Esempio n. 4
0
        public void ScriptMethodTest()
        {
            var p = DependencyResolver.Resolve <IScriptParser>();
            var e = new Statements.DataFieldExtension(@"taobao-hz\houkun", 0, new Dictionary <string, string>()
            {
                { "key", "1" }
            });

            this.TraceArray(p.EvaluateUsers("getSuperior()", e));
            this.TraceArray(p.EvaluateUsers("getSuperiors(originator,1,0)", e));
            this.TraceArray(p.EvaluateUsers("getSuperiorsInRole(originator,1,'BI_Manager')", e));
            this.TraceArray(p.EvaluateUsers("getUpperSuperior(originator,2,0)", e));
            this.TraceArray(p.EvaluateUsers(string.Format("getUsers('BI_Manager',originator,'{0}')", Guid.NewGuid()), e));

            //updateDataField
            Trace.WriteLine(p.Evaluate("updateDataField('key','2')", e));
            Assert.AreEqual("2", e.DataFields["key"]);
            //split
            var ary = p.EvaluateUsers("split(';','f1;f2,;f3,')", e);

            Assert.AreEqual(3, ary.Length);
            Assert.AreEqual("f1", ary[0]);
            Assert.AreEqual("f3,", ary[2]);


            Trace.WriteLine(p.Evaluate("getSuperior()", e));
            Trace.WriteLine(p.Evaluate("forward('JustTest1','{i:1}','{i:2}')", e));
        }
Esempio n. 5
0
 //用于执行完成规则脚本
 bool IScriptParser.EvaluateRule(string script, Statements.DataFieldExtension extension)
 {
     return(this.Evaluate <bool>(this.PrepareScriptEngine(extension), script));
 }
Esempio n. 6
0
        public void ScriptMethodTest()
        {
            var p = DependencyResolver.Resolve<IScriptParser>();
            var e = new Statements.DataFieldExtension(@"taobao-hz\houkun", 0, new Dictionary<string, string>() { { "key", "1" } });
            this.TraceArray(p.EvaluateUsers("getSuperior()", e));
            this.TraceArray(p.EvaluateUsers("getSuperiors(originator,1,0)", e));
            this.TraceArray(p.EvaluateUsers("getSuperiorsInRole(originator,1,'BI_Manager')", e));
            this.TraceArray(p.EvaluateUsers("getUpperSuperior(originator,2,0)", e));
            this.TraceArray(p.EvaluateUsers(string.Format("getUsers('BI_Manager',originator,'{0}')", Guid.NewGuid()), e));

            //updateDataField
            Trace.WriteLine(p.Evaluate("updateDataField('key','2')", e));
            Assert.AreEqual("2", e.DataFields["key"]);
            //split
            var ary = p.EvaluateUsers("split(';','f1;f2,;f3,')", e);
            Assert.AreEqual(3, ary.Length);
            Assert.AreEqual("f1", ary[0]);
            Assert.AreEqual("f3,", ary[2]);


            Trace.WriteLine(p.Evaluate("getSuperior()", e));
            Trace.WriteLine(p.Evaluate("forward('JustTest1','{i:1}','{i:2}')", e));
        }