creating a simple rule engine in java

后端 未结 15 1949
梦如初夏
梦如初夏 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:17

    Have a good talk with your users, asking them why this needs to be configurable, and what changes in the configuration they expect to be coming up. Find out what upcoming changes are certain, likely, remotely possible, outrageously unlikely. And how quickly they'd need to be implemented. For each change, would writing a small update release be acceptable or not?

    With this amount of flexibility needed in mind, evaluate the option of rolling your own solution against that of incorporating a full engine. "Test" your simple solution against the upcoming change scenarios by briefly writing down how each change would be implemented. It is quite okay if some unlikely scenarios have big cost. If likely scenarios are costly too, however, you had better pick a more generic solution.

    As for the options to consider, I like both drools and the suggestion to write your own. A third option: When implementing a financial registration package with yearly legal updates, we've had quite good success implementing the rules in code but leaving their settings configurable in sql tables. So in your case that might mean a table something like this:

    patient_type | admission_type | inpatient_or_outpatient
    -------------------------------------------------------
    'A'          | 'O'            | 'Outpatient'
    'B'          | NULL           | 'Inpatient'
    

    (Our tables tend to have date-from and date-to validity columns which allow the user to stage changes)

    If you end up writing a DSL, take a look at http://martinfowler.com/books/dsl.html which offers thorough descriptions of the several approaches. As a caveat: in his Q and A section Martin Fowler writes:

    So is this the hook - business people write the rules themselves?

    In general I don't think so. It's a lot of work to make an environment that allows business people to write their own rules. You have to make a comfortable editing tool, debugging tools, testing tools, and so on. You get most of the benefit of business facing DSLs by doing enough to allow business people to be able to read the rules. They can then review them for accuracy, talk about them with the developers and draft changes for developers to implement properly. Getting DSLs to be business readable is far less effort than business writable, but yields most of the benefits. There are times where it's worth making the effort to make the DSLs business-writable, but it's a more advanced goal.

    0 讨论(0)
  • 2020-12-02 05:17

    As parsing code with Java only is an implementation suicide, you may want to write a simple compiler using Jflex and CUP, which are the Java version of GNU FLEX and YACC. In this way you can generate simple tokens with Jflex (a token is a keyword like IF, ELSE etc) while CUP will consume those token in order to execute some code.

    0 讨论(0)
  • 2020-12-02 05:21

    There's a rule engine for Clojure called Clara, which can be used from java as well as Clojure[Java]Script. I think it would be quite easy to create something usable from that.

    0 讨论(0)
  • 2020-12-02 05:23

    This is what I would do. I create a set of regex variables, depending on the matching, I code the business logic. If the rule-set goes complex than this, I would go for apache commons CommandLineParser implementation on the server.

    But you can use GUI / HTML and a set of dropdowns and sub dropdowns. That way you can make database queries clearly.

    0 讨论(0)
  • 2020-12-02 05:25

    Basically... Don't do it

    To understand why see:

    1. http://thedailywtf.com/Articles/The_Customer-Friendly_System.aspx
    2. http://thedailywtf.com/Articles/The-Mythical-Business-Layer.aspx
    3. http://thedailywtf.com/Articles/Soft_Coding.aspx

    I know it looks like a great idea from afar, but the business rules engine will invariably end up being harder to maintain, deploy and debug then the programming language it was written in - don't make up your own programming languages if you can help it.

    I've personally been down that road in an ex firm and I've seen where it goes after a couple years (giant undebuggable scripts sitting in a database written in a language that came straight from a parallel dimension where God hates us that in the end never meet 100% of customer expectation because they're not as powerful as a proper programming language and at the same time they're far too convoluted and evil for devs to handle (never mind the client)).

    I know there's a certain kind of client that's enamoured with the idea that they won't pay programmer hours for "business rule adaptations" and little understand that they'll be worse off in the end and to attract this kind of client you'll have to make something in this direction - but whatever you do don't invent something of your own.

    There's a plethora of decent scripting languages that come with good tools (that don't require compilation, so can be uploaded dynamically etc) out there that can be slickly interfaced and called from Java code and take advantage of your implemented Java apis that you make available, see http://www.slideshare.net/jazzman1980/j-ruby-injavapublic#btnNext for example, Jython possibly too,

    and when the client gives up writing these scripts you will be left with the happy duty of maintaining his failed legacy - make sure that that legacy is as painless as it can be.

    0 讨论(0)
  • 2020-12-02 05:26

    Another addition to this list is http://openl-tablets.org/ . It allows you to define rules in Excel, upload it to a Web based editor. It is very flexible and has built in ability to run tests against the rules with some data

    0 讨论(0)
提交回复
热议问题