# 对象规则校验 **Repository Path**: moving_bricks/object_rule_validation ## Basic Information - **Project Name**: 对象规则校验 - **Description**: 用java语句来实现和数据库相同的交易 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2020-06-24 - **Last Updated**: 2022-07-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 对象规则校验 #### 介绍 用java语句来实现和数据库相同的交易 #### 软件架构 软件架构说明 #### 安装教程 1. xxxx 2. xxxx 3. xxxx #### 使用说明 public class Test2 { public static void main(String[] args) { //默认引擎实例:只要有符合的规则就返回结果 LessRuleEngine lre = LessEngineFactory.getWholeEngine(); //全量引擎实例:返回全部符合的规则 // LessRuleEngine lre = LessEngineFactory.getWholeEngine(); //获取规则 List ruleEntities = setRule(); // _规则注册到规则引擎_ EngineResponse opResult = lre.addAll(ruleEntities); //判断规则解析添加是否成功 if (opResult.getOpCode() == EngineResponse.ENGINE_OP_RES_FAIL) { System.out.println("规则解析有错误,此规则没有添加成功:" + opResult.getMsg()); return; } // _新建一个事实_ Map fact = new HashMap(); fact.put("alarmId", "121"); fact.put("clearFlag", "22"); fact.put("www", "222221"); Map map = new HashMap<>(); map.put("id","1111"); map.put("name","张三"); map.put("age","27"); fact.put("obj",map); // _引擎判断事实是否符合规则_ EngineResponse result = lre.fireFail(fact); if (result.getMatchCode() == EngineResponse.ENGINE_MATCH_RES_SUCC) { //结果判断 Map> fireResult = result.getFireResult(); if (fireResult != null && !fireResult.isEmpty()) { fireResult.forEach((k, v) -> System.out.println("k:" + k + ",v:" + v)); System.out.println("检验没有通过"); } System.out.println("---------------------分割线---------------------------"); List queryResult = result.getQueryResult(); queryResult.forEach(ruleUnit -> { System.out.println(ruleUnit.toString()); }); } } /** * 设置规则 * * @return */ private static List setRule() { List ruleEntities = new ArrayList<>(); String express0 = "clearFlag notin ['2','3','4']"; String express1 = " ( alarmId == '123' or alarmId == '124' )"; String express2 = "www == '' or www == null"; String express3 = "clearFlag < 2"; String express4 = "www notlike 22%"; String express5 = "clearFlag notrange 3,54"; String express6 = "obj.id == 1112"; // _新建一个规则_ RuleEntity re = new RuleEntity(); // _设置规则分组标识_ re.setGroupId("wjh"); // _设置规则唯一标识_ re.setId("00"); //表达式字符串 // _设置规则表达式_ re.setExpress(express0); re.setRuleInfo("清理标识检验不通过,不在[2,3,4]中间"); RuleEntity re1 = (RuleEntity) re.clone(); re1.setId("11"); re1.setExpress(express1); re1.setRuleInfo("alarmId检验不通过,不等于123或者124"); RuleEntity re2 = (RuleEntity) re.clone(); re2.setId("22"); re2.setExpress(express2); re2.setRuleInfo("www检验不通过,等于null"); RuleEntity re3 = (RuleEntity) re.clone(); re3.setId("33"); re3.setExpress(express3); re3.setRuleInfo("clearFlag检验不通过,不大等2"); RuleEntity re4 = (RuleEntity) re.clone(); re4.setId("44"); re4.setExpress(express4); re4.setRuleInfo("www检验不通过,not模糊查询221"); RuleEntity re5 = (RuleEntity) re.clone(); re5.setId("55"); re5.setExpress(express5); re5.setRuleInfo("clearFlag检验不通过,clearFlag在3,5之间"); RuleEntity re6 = (RuleEntity) re.clone(); re5.setId("66"); re5.setExpress(express6); re5.setRuleInfo("obj.id检验不通过,id不等于1111"); //规则设置到集合 ruleEntities.add(re6); ruleEntities.add(re); ruleEntities.add(re1); ruleEntities.add(re2); ruleEntities.add(re3); ruleEntities.add(re4); ruleEntities.add(re5); return ruleEntities; } } #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 码云特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目 5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) 7. 本源码是基于https://gitee.com/izocen/less-rules?_from=gitee_search进行改造的