I have encountered questions like how to use rules in a typical JEE application in Seam forum. And there are some other questions on general application design too. I have been using Seam and Drools together and figured out a way which ties the business logic to entities in an ultimately flexible manner and this design also significantly enhance the readability of the controller code.
Background: I am writing an application called EatSmart for my thesis. Based on the nutrient data from USDA SR19, EatSmart can automatically analyze the nutrient facts in a recipe and tag them as “Low Fat”, “High Fat”, “Low Salt”, “Fat Free”, …etc. The centre task of EatSmart is weekly cooking planning. It should find out the optimal weekly cooking plan according to the criteria given by user. If there are 1000 recipes in the database, the size of solution space in worst case (no criteria at all, the user clicks “I’m feeling lucky” button) is Permutation(1000, 7). For this planning task there is some Monte Carlo optimization going on, but here I won’t go into the details of the optimization algorithms. Anyway, under such situation, there are a lot of “business methods” in my entity beans. (You will need to know a bit about Seam and Drools in order to fully understand the annotations and API calls in my code. For Seam and Drools information please refer to the sites I linked above.)
Here are two example entity classes implemented with the widely used style, which has the business methods directly implemented in the entity class:
The “business methods” in my entity beans are largely used to calculate the global score in the planning optimization algorithms. My problem was, I do not exactly know how to write some methods in a strictly scientific manner (for example, the doTotalTime() method in the Recipe.java). I’d rather have a cooking/food expert to write these methods for me so I can focus on my planning optimization algorithms and the application building. While tuning the global scoring function, I am changing some of the math formulas in these “business methods” all the time. To reduce the pain of rebuilding, I externalized all the “business methods” to rule files. One ruleBase per each entity class, Seam makes the integration effort indeed trivial. You do not need manually write any rule engine wrapper class at all.
(to be continued)
Info added on August 18th: There is still integration problem to solve for using stateful rule sessions together with Seam in my application, I’m refactoring to a slightly different approach for now, but it still makes very heavy use of rules, especially in the view code….I’ll post the documented code in another blog, probably next week or over next week.