当前位置: 首页 > article >正文

NReco.LambdaParser使用案例

使用案例集合:

 private async void RuleEngine_Click(object sender, EventArgs e)
        {
            #region 获取变量
            string expression = this.Rule.Text.Trim();
            string pattern = @"\$(.*?)\$";
            MatchCollection matches = Regex.Matches(expression, pattern);
            foreach (Match match in matches)
            {
                string variableName = match.Groups[1].Value;
            }
            #endregion

            #region 执行器初始化
            var lambdaParser = new LambdaParser();
            lambdaParser.AllowVars = true;
            #endregion

            #region 变量初始化
            var tagsql = string.Empty;
            var dictLast = new Dictionary<string, object>();
            #endregion

            #region 常用方法与数据定义
            dictLast["PI"] = 3.14M;
            dictLast["Now"] = DateTime.Now;
            dictLast["Not"] = (Func<bool, bool>)((t) => !t);
            dictLast["Max"] = (Func<decimal, decimal, decimal>)((a, b) => Math.Max(a, b));
            dictLast["Yes"] = true;
            dictLast["No"] = false;
            dictLast["arr"] = new double[] { 1.5, 2.5 };
            dictLast["dic"] = new Dictionary<string, int>()
            {
                {"apple", 1},
                {"banana", 2},
                {"orange", 3}
            };
            dictLast["Alert"] = (Func<string, string>)((msg) => AlertAsync(msg));
            #endregion

            #region 判断字符是否正确输入
            string input = "Now.ToString(\"yyyy-MM-dd/HH\")";
            string backslash = @"(?<!\\)"""; // 匹配没有被转义的双引号
            MatchCollection matches2 = Regex.Matches(input, backslash);
            if (matches.Count > 0)
                Console.WriteLine("双引号没有被正确转义");
            else
                Console.WriteLine("双引号都有正确转义");
            #endregion

            #region test
            Console.WriteLine(lambdaParser.Eval("Now.ToString(\"yyyy-MM-dd/HH\")", dictLast));
            Console.WriteLine(lambdaParser.Eval("Max(Max(3,4),2)", dictLast));
            Console.WriteLine(lambdaParser.Eval("arr[0]+arr[1]", dictLast)); // -> 4
            Console.WriteLine(lambdaParser.Eval("dic[\"apple\"]+arr[1]", dictLast)); // -> 3.5
            Console.WriteLine(lambdaParser.Eval("(new[]{1,2})[1]", dictLast)); // 2
            #endregion
            int i = 0;
            foreach (var v in this.Variables.Text.Trim().Split(','))
            {
                if (i == 0)
                    tagsql = this.DataSet1.Text.Trim();
                else
                    tagsql = this.DataSet2.Text.Trim();
                var rtTagInfos = await _dbInstance.DBOperation.QueryAsync<dynamic>(tagsql);
                if (rtTagInfos.Count() == 1)
                {
                    var dictSingle = ((IDictionary<string, object>)rtTagInfos.FirstOrDefault())
                        .ToDictionary(kvp => kvp.Key.Split(new[] { '(', ')' }).LastOrDefault(), kvp => kvp.Value);
                    dictLast = dictLast.Union(dictSingle).ToDictionary(x => x.Key, x => x.Value);
                }
                else
                {
                    #region 没有去重
                    //var keyValuePairs = new List<KeyValuePair<string, object>>();
                    //foreach (var record in rtTagInfos)
                    //{
                    //    int i = 0;
                    //    string propertyName = string.Empty;
                    //    foreach (var property in (IDictionary<string, object>)record)
                    //    {
                    //        if (i == 0)
                    //            propertyName = Convert.ToString(property.Value);
                    //        else
                    //            keyValuePairs.Add(new KeyValuePair<string, object>(propertyName, property.Value));
                    //        i = i + 1;
                    //    }
                    //}
                    //var dictlist = keyValuePairs.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
                    #endregion
                    var keyValuePairs = new HashSet<KeyValuePair<string, object>>();
                    Parallel.ForEach(rtTagInfos, (record) =>
                    {
                        int i = 0;
                        string propertyName = string.Empty;
                        foreach (var property in (IDictionary<string, object>)record)
                        {
                            if (i == 0)
                            {
                                propertyName = Convert.ToString(property.Value);
                            }
                            else
                            {
                                var keyValuePair = new KeyValuePair<string, object>(propertyName, property.Value);
                                lock (keyValuePairs)
                                {
                                    if (!keyValuePairs.Contains(keyValuePair))
                                    {
                                        if (!keyValuePairs.Any(kvp => kvp.Key == keyValuePair.Key))
                                        {
                                            keyValuePairs.Add(keyValuePair);
                                        }
                                    }
                                }
                            }
                            i = i + 1;
                        }
                    });
                    var dictlist = keyValuePairs.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
                    dictLast = dictLast.Union(dictlist).ToDictionary(x => x.Key, x => x.Value);
                }
                i = i + 1;
            }
            var res = lambdaParser.Eval(this.Rule.Text.Trim().Replace("$", ""), dictLast);
            MessageBox.Show(res?.ToString());
        }

        public string AlertAsync(string msg)
        {
            var time = "SELECT NOW() as now";
            var rtTagInfos = AsyncHelper.RunSync(() => _dbInstance.DBOperation.QueryFirstOrDefaultAsync<DateTime>(time));
            return msg + rtTagInfos.ToString();
        }

$reportvalue$<1234?"1":Alert("告警:") 


http://www.kler.cn/a/106349.html

相关文章:

  • 【实验11】卷积神经网络(2)-基于LeNet实现手写体数字识别
  • 第二十一周机器学习笔记:动手深度学习之——数据操作、数据预处理
  • 数据研发基础 | 什么是流批一体
  • 【CV】头盔检测区域入侵项目
  • 容器内的Jenkins使用docker部署服务,服务数据文件挂载问题
  • 【React】状态管理之Zustand
  • docker、docker-compose安装教程,很详细
  • 【wvp】wvp设备上可以开启tcp被动模式
  • 人工智能和机器学习:走向智能未来的关键
  • 数字化转型系列主题:数据中台知识体系
  • vue中报 TypeError: Assignment to constant variable.
  • oracle统计信息
  • 万能鼠标设置 SteerMouse v5.6.8
  • 【高效开发工具系列】Postman
  • 交换机基础(四):MSTP负载均衡配置案例
  • 1-多媒体通信概述
  • 『力扣刷题本』:合并两个有序链表(递归解法)
  • Microsoft.Extensions 简介
  • golang 工程组件:grpc-gateway 环境安装+默认网关测试
  • LeetCode--3.无重复字符的最长子串
  • 【APP VTable】和市面上的 Table 组件一样,都是接收表格[] 以及数据源[]
  • 最新SQL注入漏洞修复建议
  • Android Glide限定onlyRetrieveFromCache取内存缓存submit超时阻塞方式,Kotlin
  • 什么是可重入,什么是可重入锁? 它用来解决什么问题?
  • 泛微OA之获取每月固定日期
  • LabVIEW更改图像特定部分的颜色