C#规则引擎NRules帮助类

2021-02-04T09:48:00

Show The Code


using NRules;
using NRules.Fluent;
using System;
using System.Collections.Generic;
using System.Reflection;

    /// <summary>
    /// 规则引擎帮助类
    /// </summary>
    public class RuleHepler
    {
        public static List<dynamic> RunRules(List<string> assemList, List<dynamic> ruleList)
        {
            ///加载规则
            var repos = new RuleRepository();

            if(null== assemList || assemList.Count <= 0)
            {
                throw new Exception($"{nameof(assemList)} must not null");
            }

            //加载动态程序集规则
            var  assemLoads = new List<Assembly>();
            assemList.ForEach(assem=> {
                var curAssem = Assembly.Load(assem);
                assemLoads.Add(curAssem);
            });
            repos.Load(x => x.From(assemLoads));

            ///编译规则
            var factory = repos.Compile();

            ///创建活动会话
            var session = factory.CreateSession();

            ///将实体放入活动会话
            foreach(var obj in ruleList)
            {
                session.Insert(obj);
            }

            ///开启规则引擎处理
            session.Fire();

            return ruleList;
        }
    }

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »