Search code examples
javaspringhibernatedrools

Design guidance needed on implementing rules engine in spring MVC , Hibernate , Oracle project


I am trying to implement a rules engine through which my business objects have to pass through before getting persisted to database.

Rules will be created by users through a User Interface.

Can you please suggest me the best way to implement this. Any Docs or existing tools will be useful.

I am aware of Drools but believe rules in drools are coded into .drl files which do not allow user to create rules dynamically anytime.


Solution

  • Rules in drools are coded into .drl files which do not allow user to create rules dynamically anytime.

    Drools are particularly use for dynamic rule creation and integration without actually touching the application code. The correct approach is to use Kie-workbench for rule creation and management and load this rules in your application dynamically.

    Rules will be created by users through a User Interface.

    For requirement, in fact, kie-workbench works best as it even provide you with guided GUI to create and update rules.

    Can you please suggest me the best way to implement this. Any Docs or existing tools will be useful.

    For starter, I would suggest you to go through this on how to deploy the workbench, this on how to create and manage rule in workbench and this on how to load and execute rule from workbench in Java

    Using custom built interface for Rule management

    If you want to create rules using your own built User Interface, you could deploy the created rules in filesystem or java classpath or even expose them through url. Drools provide ways to integrate all of the above scenario through it API. You can check the example below to access, compile and execute the rules from filesystem. Similar approach can also be taken loading for loading rules from classpath or external url.

    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieFileSystem kfs = ks.newKieFileSystem();
    File file = new File("path/to/drl");
    kfs.write(ks.getResources().newFileSystemResource(file)
                  .setResourceType(ResourceType.DRL));
    KieBuilder kb = ks.newKieBuilder(kfs);
    kb.buildAll(); 
    if (kb.getResults().hasMessages(Level.ERROR)) {
        throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
    }
    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    KieSession kSession = kContainer.newKieSession();
    kSession.insert(fact);
    kSession.fireAllRules();