creating a simple rule engine in java

后端 未结 15 1991
梦如初夏
梦如初夏 2020-12-02 04:47

I am exploring different ways to create a simple business rule engine in Java. I need to present the client with a simple webapp that lets him configure a bunch of rules. A

15条回答
  •  时光说笑
    2020-12-02 05:09

    A simple rule engine can be build upon closures, i.e in Groovy:

    def sendToOutPatient = { ... };
    
    def sendToInPatient = { ... };
    
    def patientRule = { PATIENT_TYPE ->
        {'A': sendToOutPatient,
         'B': sendToInPatient}.get(PATIENT_TYPE)
    }
    
    static main(){
        (patientRule('A'))()
    }
    

    You could define your rules as closures, reuse/reassign them or even build a DSL over them.

    And Groovy can be easily embedded into Java, example:

    GroovyShell shell = new GroovyShell(binding);
    binding.setVariable("foo", "World");
    System.out.println(shell.evaluate("println 'Hello ${foo}!';));
    

提交回复
热议问题