Loading and updating rules from a database in Drools 6

前端 未结 2 961
夕颜
夕颜 2020-12-29 17:08

How would one go about loading rules from a database table at startup and updating them from the same table in Drools 6.2.0? I\'ve found an example using Drools 5 that I cou

2条回答
  •  旧时难觅i
    2020-12-29 18:00

    drools-templates has ResultSetGenerator.java which has method compile(resultSet, template) to do the job.

    I had data coming over HTTP to be converted to rule. I found a way to do this using ObjectDataCompiler. May be some people might find this useful.

    ObjectDataCompiler compiler = new ObjectDataCompiler();
    String generatedDRL = compiler.compile(ruleAttributes, new FileInputStream(REGULATION_TEMPLATE_FILE));
    

    where ruleAttributes is

    List> ruleAttributes = new ArrayList<>();
    Map rule1 = new HashMap<>();
    rule1.put("ruleid", "2");
    rule1.put("ifcondition", "abc: Abc(xyz.getId() == 2);");
    rule1.put("thencondition", "myGlobal.setPqr(200.1D);");
    ruleAttributes.add(rule1);
    

    KieBase can then be created like this:

    KieServices kieServices = KieServices.Factory.get();
    
    KieHelper kieHelper = new KieHelper();
    
    //multiple such resoures/rules can be added
    byte[] b1 = generatedDRL.getBytes();
    Resource resource1 = kieServices.getResources().newByteArrayResource(b1);
    kieHelper.addResource(resource1, ResourceType.DRL);
    
    KieBase kieBase = kieHelper.build();
    

    Rules can be applied like this:

    KieSession kieSession = kieBase.newKieSession();
    kieSession.setGlobal("myGlobal", myGlobal);
    kieSession.insert(abc);
    int numberOfRulesFired = kieSession.fireAllRules();
    kieSession.dispose();
    

    Template file looks like this:

    template header
    ruleid
    ifcondition
    thencondition
    
    import fk.sp.seldon.msku.MSKU
    
    global com.something.blah.MyGlobal myGlobal
    
    template "tmp1"
    
    rule "@{ruleid}"
      dialect "mvel"
      when
        @{ifcondition}
      then
        @{thencondition};
    end
    end template
    

提交回复
热议问题