C#规则引擎NRules帮助类

枫殇NET开发
2021-02-04 / 0 评论 / 718 阅读 / 正在检测是否收录...

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;
        }
    }

2

评论 (0)

取消