规则引擎 - drools 使用讲解(简单版) - Java (2)

比如生成 music.drl 的音乐规则文件,这一步是可选的,区别在于规则文件的生成是代码生成,还是人工生成,我们的项目中是运维同学在后台管理界面通过一些图形化输入框输入一些指定参数,而生成规则文件是服务端代码生成的,因此有了这部分,比较实用,一方面可以降低生成规则文件的门槛,任何人都可以做,另一方面也避免了人工出错的可能;

public class ActivityUtil { /** * rule template string */ private static String template = "package com.aispeech.dsl\r\n\r\n" + "import {entity_package_path};\r\n\r\n" + "import {entity_package_path}.*;\r\n\r\n" + "rule \"{rule_name}\"\r\n\r\n" + "when\r\n" + "\t{instance_name}:{class_name}({rules})\r\n" + "then\r\n" + "\t{do}\r\n" + "end"; private static final String AND = " && "; private static final String OR = " || "; /** * get business rule file xxx.drl * @param carActivity user info entity * @param clazz entity class * @return */ public static File createBusinessRuleFile(Car_activity carActivity, Class clazz, String[] param_texts, String[] param_values) { String ruleStr = template; String entity_package_path = (clazz+"").substring(6); String rule_name = "rule_"+carActivity.getId(); String class_name = (clazz+"").substring((clazz+"").lastIndexOf(".")+1); String instance_name = class_name.toLowerCase(); String rules = ""; JSONArray conditionArray = JSONArray.parseArray(carActivity.getAim_condition()); for(int i=0;i<conditionArray.size();i++) { JSONObject condition = conditionArray.getJSONObject(i); rules += "\r\n\t\t("+condition.getString("param")+condition.getString("operator")+condition.getString("value")+")" + AND; } rules = rules.length()>0?rules.substring(0, rules.lastIndexOf(AND)):rules; for (String param_value : param_values) { rules += "\r\n\t\t,"+param_value.toLowerCase()+":"+param_value; } String content = JSONObject.parseObject(carActivity.getContent()).getString("content"); String tts = carActivity.getTts(); for (int i=0;i<param_texts.length;i++) { content = content.replace("#"+param_texts[i]+"#", "\"+"+param_values[i]+"+\""); tts = tts.replace("#"+param_texts[i]+"#", "\"+"+param_values[i]+"+\""); } String _do = instance_name+".setCan_push(true);"; _do += "\r\n\t" + instance_name+".setContent(\""+content+"\");"; _do += "\r\n\t" + instance_name+".setTts(\""+tts+"\");"; return returnFile(ruleStr, entity_package_path, rule_name, class_name, instance_name, _do, rules); } /** * @param ruleStr * @param entity_package_path * @param rule_name * @param class_name * @param instance_name * @param _do * @param rules * @return */ private static File returnFile(String ruleStr, String entity_package_path, String rule_name, String class_name, String instance_name, String _do, String rules) { ruleStr = ruleStr.replace("{entity_package_path}", entity_package_path) .replace("{rule_name}", rule_name) .replace("{class_name}", class_name) .replace("{instance_name}", instance_name) .replace("{do}", _do) .replace("{rules}", rules); System.out.println(ruleStr); return FileUtil.getFileFromText(rule_name, ".drl", ruleStr); } } step 4.1 -- 通过字符串创建文件,给上一步用的函数 public static File getFileFromText(String tempFileName, String fileTail, String text) { try { File file = File.createTempFile(tempFileName, fileTail); FileOutputStream fos = new FileOutputStream(file); fos.write(text.getBytes()); if(fos!=null){ fos.close(); } return file; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } step 5 -- 规则文件加载,并用以检查当前用户是否满足下发规则条件 BaseRules baseRules = new CarIllegalRules(count, money, points); if(baseRules!=null) { logger.info("before fire rules:"+baseRules); DSLUtil.fireRules(ActivityUtil.createBusinessRuleFile(car_activity, baseRules.getClass(), baseRules.getParam_text().split(","), baseRules.getParam_value().split(",")), baseRules); logger.info("after fire rules:"+baseRules); if(baseRules.isCan_push()) {         //In here, the rules are used to judge the success of the entity, and you can do something!!! } } 小结

本文通过对drools的简单使用步骤的讲解,为大家展示了drools最简单的使用方式,而它能做到的远远不止看到的这些,但是基本框架是这样,大家可以尝试挖掘规则文件的一些黑操作,可以对多变的业务进行极致的抽象,再也不用为了这些重新发版啦,LOL;

PS:想深入了解的同学还是要去看看Rete算法、drools的推理机制等等,本文主要从该引擎的入门出发哈;

最后

大家可以到我的Github上看看有没有其他需要的东西,目前主要是自己做的机器学习项目、Python各种脚本工具、数据分析挖掘项目以及Follow的大佬、Fork的项目等:https://github.com/NemoHoHaloAi

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygysz.html