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

营销系统规则引擎

一、系统介绍

        规则引擎是一个用于执行营销规则的模块,其包括营销规则配置、规则校验等功能。规则引擎可以根据预先设定的条件和逻辑,自动化地执行特点的营销策略,帮助企业更好地吸引客户,增加销售和提高客户满意度。

        规则引擎功能列表:

              1、规则装配 - 根据活动类型组装不同营销规则

              2、逻辑设计 - 支持与、或、非等复杂逻辑关系。

              3、配置校验 - 支持对活动规则进行自动化校验。

二、技术实现

1.UserCenterRegistryActivityRule - 个人中心注册活动规则

class UserCenterRegistryActivityRule extends ActivityRule{public UserCenterRegistryActivityRule(){super("region_rule&channel_rule&(user_group_rule|user_tag_rule)");}public String rootRulePath(){return "usercenter_registry_activity";}
}

2.ActivityRule - 活动规则抽象

public abstract class ActivityRule implements RuleItem{/*** 规则表达式*/private String ruleExpression;/*** 规则项配置校验*/@Autowiredprivate Map<String, RuleItem> ruleItemMap;public ActivityRule(String ruleExpression){this.ruleExpression = ruleExpression;}/*** 规则项校验*/public boolean apply(CalculateRequest request, PeContext peContext) {Stack<Character> opStack = new Stack<>();Stack<Boolean> resStack = new Stack<>();Set<Character> opChSet = Set.of('(', ')', '&', '|');int i = 0;// 处理右括号while (i < ruleExpression.length()) {char ch = ruleExpression.charAt(i);switch (ch) {case '(':opStack.push(ch);i++;break;case ')': while (opStack.peek() != '(') {Character opChar = opStack.peek();if (opChar == '&') {boolean right = resStack.pop();boolean left = resStack.pop();resStack.push(left && right);} else if (opChar == '|') {boolean right = resStack.pop();boolean left = resStack.pop();resStack.push(left || right);}opStack.pop();}break;case '&': // 操作符opStack.push('&');i++;break;case '|':opStack.push('|');i++;break;default: // 操作数int j = i;while (j < ruleExpression.length() && !opChSet.contains(ruleExpression.charAt(j))) {j++;}String rule = ruleExpression.substring(i, j);resStack.push(ruleItemMap.get(rule).apply(request, peContext));i = j;}}// 处理结果while (!opStack.isEmpty()) {Character opChar = opStack.peek();if (opChar == '&') {boolean right = resStack.pop();boolean left = resStack.pop();resStack.push(left && right);} else if (opChar == '|') {boolean right = resStack.pop();boolean left = resStack.pop();resStack.push(left || right);}opStack.pop();}return resStack.pop();}default String rootRulePath();/*** 规则配置校验*/public PathError validate(String rulePath) {if (rulePath == null){rulePath = rootRulePath();}PathError pathError = PathError.of(null, rulePath);for (Map.Entry<String, RuleItem> entry : ruleItemMap.entrySet()) {String ruleItemPath = entry.getKey();RuleItem ruleItem = entry.getValue();PathError ruleItemError = ruleItem.validate(ruleItemPath);if (ruleItemError != null) {return pathError.with(ruleItemError);}}return null;}
}

3.UserGroupRuleItem - 用户组规则项

class UserGroupRuleItem extends RuleItem{private List<String> groupsLimit;@Overridepublic PathError validate(String rulePath){PathError pathError = PathError.of(null, rulePath);if (groupsLimit == null || groupsLimit.size() == 0){return pathError.withPath("groupsLimit").withError("groupsLimit不能为空");}return null;}@Overriderpublic boolean apply(CalculateRequest req, PeContext context){UserService userService = SpringUtil.getBean(UserService.class);// 判断用户是否符合用户组规则List<String> userGroups = userService.getUserGroups(req.getUserId());if (groupsLimit.stream().anyMatch(userGroups::contains)){return true;}retrun false;}
}

4.RuleItem - 规则项定义

interface RuleItem {// 规则配置校验default PathError validate(String rulePath) {PathError pathError = new PathError(null, rulePath);// 校验规则字段Field[] fields = FieldUtils.getSpecFields(getClass(), RuleItem.class);if (fields.length == 0) {return null;}try {for (Field ruleField : fields) {RuleItem rule = (Rule) FieldUtils.getField(this, ruleField);if (rule == null){continue;}String fieldName = ruleField.getName();// 规则字段校验失败PathError fieldPathError = rule.validate(fieldName);// 返回字段校验失败结果if (fieldPathError != null) {return pathError.with(fieldPathError);}}} catch (Exception e) {throw new AppException(ErrorCode.SYSTEM_ERROR, String.format("校验Rule错误: %s", e.getMessage()));}return null;}// 规则应用default boolean apply(CalculateRequest request, PeContext peContext){return true;};@Data@NoArgsConstructor@AllArgsConstructorpublic static class PathError{private String error;private List<String> paths;public PathError with(PathError other){this.error = other.error;this.paths.addAll(other.paths);return this;}public PathError withError(String error){this.error = error;return this;}public PathError withPath(String path){this.paths.add(path);return this;}}
}

http://www.lryc.cn/news/253669.html

相关文章:

  • 【Linux】命令行参数
  • 【信息安全】-个人敏感信息、个人信息、个人金融信息
  • 海外服务器和国内服务器有什么样的区别呢
  • 电脑屏幕亮度怎么调?学会4个方法,轻松调节亮度!
  • 微信小程序之猜数字和猜拳小游戏
  • CETN01 - How to Use Cloud Classroom
  • 安卓8预装可卸载应用
  • 微服务实战系列之MemCache
  • 解决服务端渲染程序SSR运行时报错: ReferenceError: document is not defined
  • 【漏洞复现】狮子鱼任意文件上传漏洞
  • LINUX 下部署github仓库
  • CentOS中安装数据库
  • GPT-Crawler一键爬虫构建GPTs知识库
  • 在微信小程序中如何改变默认打开的页面
  • Ardupilot开源飞控之VTOL之旅:配件试装
  • STM32-GPIO
  • MySQL的事务
  • go-carbon v2.2.14 发布,轻量级、语义化、对开发者友好的 Golang 时间处理库
  • 解决 IIS HTTP 403 错误问题
  • 字符设备驱动基础—并发控制
  • 5-Tornado入门、程序的原理图、tornado不能使用同步代码的演示
  • mysql原理--InnoDB记录结构
  • ES6基础语法
  • java8 常用code
  • docker 镜像管理
  • Jira 中如何修改时间为绝对时间
  • 班级查分软件制作教程:老师必备技能!
  • Linux 的性能调优的思路
  • 如何通过webdriver禁用浏览器定位功能
  • 网卡bonding绑定