The rules engine - drools use to explain (simple version) - Java

drools rules engine

Project links

status quo:

  1. O & M students (various students) direct configuration rules by admin interface, here is the complete input by the input boxes, drop-down box, etc., it is very simple;
  2. After the rules are configured, the rear end of the front end request, then the server based on the parameters generated drl rules file (i.e., rules);
  3. When the user side, the relevant request arrives, the server load rules file (possibly more, a general advertisement, event file corresponding to a rule), and the engine by the user to check whether the current status of the various files rule is satisfied;
  4. The file meets all the rules of the corresponding ad activity issued, and update user data;
  5. Drools complete the relevant processes;

on

drools is a standard, high efficiency, fast open source rules engine, based on ReteOO algorithm, the main scenario is very much in advertising, hair and other activities under the fields, such as development activities under the APP, usually there are a lot of conditions and endless variety of activities, the code can not be exhaustive, and if each event to resend a line on the plate, is obviously unreasonable, thus drools by varying the active portion in a separate abstract rules file, to shield this part of the change, so that the system does not need to make a change from the code level, of course, for the more extreme abstraction, usually requires some rules can be equipped conditions (greater than, less than, equal to, range, frequency, etc.) are also extracted database, so that when the rule does not meet conventional requirements, directly by changing the correspondence table of the database to improve the rule, the same does not need to change the code;

We were mainly lower demand for advertising, events compare multiple rules, but also a wide range of advertising, and therefore to research the drools, drools for not too much more of its mining properties, so we also need guidance;

Simple to use drools

Drools a few basic steps to use server-side project;

step 1 - is added to the depending maven pom.xml

<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-core</artifactId>
    <version>6.4.0.Final</version>
</dependency>
<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-compiler</artifactId>
    <version>6.4.0.Final</version>
</dependency>

step 2 - create an entity class load rule file

public class CarIllegalRules extends BaseRules{
    
    public static void main(String[] args) {
        try {
            KieServices ks = KieServices.Factory.get();  
                    KieContainer kContainer = ks.getKieClasspathContainer();  
                    KieSession ksession = kContainer.newKieSession("ksession-rules");
            
                CarIllegalRules carIllegalRules = new CarIllegalRules(10,500,10);
                ksession.insert(carIllegalRules);
                ksession.fireAllRules();
                System.out.println(carIllegalRules.isCan_push()+","+carIllegalRules.getContent());    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private int illegal_count;
    
    private int illegal_money;
    
    private int illegal_points;

    public CarIllegalRules(int illegal_count, int illegal_money, int illegal_points) {
        super();
        this.illegal_count = illegal_count;
        this.illegal_money = illegal_money;
        this.illegal_points = illegal_points;
        this.param_value = "illegal_count,illegal_money,illegal_points";
    }

    @Override
    public String toString() {
        return "CarIllegalRules [illegal_count=" + illegal_count + ", illegal_money=" + illegal_money
                + ", illegal_points=" + illegal_points + ", can_push=" + can_push + ", content=" + content + ", tts="
                + tts + "]";
    }

    public int getIllegal_count() {
        return illegal_count;
    }

    public void setIllegal_count(int illegal_count) {
        this.illegal_count = illegal_count;
    }

    public int getIllegal_money() {
        return illegal_money;
    }

    public void setIllegal_money(int illegal_money) {
        this.illegal_money = illegal_money;
    }

    public int getIllegal_points() {
        return illegal_points;
    }

    public void setIllegal_points(int illegal_points) {
        this.illegal_points = illegal_points;
    }
}

PS: main function is used to test the class;

step 3 - Create a DSLUtils class to perform the corresponding rules

public class DSLUtil {
    public static void fireRules(File file, Object rules) {
        try {
            KieServices kieServices = KieServices.Factory.get();
            KieFileSystem kfs = kieServices.newKieFileSystem();
            Resource resource = kieServices.getResources().newFileSystemResource(file);
            fire(rules, kieServices, kfs, resource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void fireRules(String urlStr, Object rules) {
        try {
            KieServices kieServices = KieServices.Factory.get();
            KieFileSystem kfs = kieServices.newKieFileSystem();
            Resource resource = kieServices.getResources().newFileSystemResource(FileUtil.getFileFromUrl(urlStr));
            fire(rules, kieServices, kfs, resource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static void fire(Object commonRules, KieServices kieServices, KieFileSystem kfs, Resource resource)
            throws Exception {
        resource.setResourceType(ResourceType.DRL);
        kfs.write(resource);
        KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
        if (kieBuilder.getResults().getMessages(Message.Level.ERROR).size() > 0) {
            throw new Exception();
        }
        KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
        KieBase kBase = kieContainer.getKieBase();
        KieSession ksession = kBase.newKieSession();
        ksession.insert(commonRules);
        ksession.fireAllRules();
    }
}

step 4 - generate rules to create a class file

For example, the rules generated music.drl music files, this step is optional, except that generate rules files is code generation, or artificially generated, our project is the operation and maintenance of the students in the admin interface by some graphical input box Some parameters are specified, the rule file is generated server-side code generation, so there is this part, more practical, one can lower the threshold to generate rules files, anyone can do, while avoiding the possibility of manual errors ;

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 - Create a file by string, to the use of a step function

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 - the rule file is loaded, and delivery rules to check whether the current user satisfies conditions

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

summary

By using simple steps to explain to the drools, drools show you the easiest way to use, but it can do much more than to see them, but the basic framework is the case, you can try some mining rules file black operations, can be the ultimate abstraction of changing business, these no longer have to re-issue version of it, LOL;

PS: the students want to learn more or go and see the Rete algorithm, drools reasoning mechanism and so on, this paper from the entry to the engine of Kazakhstan;

At last

We can go to my Github see if there are other needs things, the key is to do their own machine-learning projects, Python variety of scripting tools, data analysis and mining projects Follow chiefs, Fork projects such as: https: / /github.com/NemoHoHaloAi

Guess you like

Origin www.cnblogs.com/helongBlog/p/11611493.html