Ideas to build a rule engine

Hello,

This is a very nice tool for building RAD applications. Thank you for creating such a wonderful tool.
I am looking to build a rule engine/formula parser for an application.
Example1 : Dimension of an item = L * B * H of an Order line item to calculate for example how many pieces of the same could fit within a packing box of certain dimension.
Example 2 : I will have a table with age and gender as the dimensions and there will be factor. I will pass the data from an person entity to the same table to get the factor . Then the end user can multiply it by a base amount to arrive at a rate.
The formulas should support many mathematical functions and I should be able to pass the attribute values from any entity dynamically to evaluate the result that can be either true/false or Integer/Long.
I have seen some elements of this within the tool on the constraints building using the wizard/ groovy script support on the Access Group functionality.
Any ideas/tips would be appreciated.

Hi Arvind,

generally executing something like a groovy script works well in CUBA. I actually did that in the app component cuba-component-runtime-diagnose. CUBA has a bean to run groovy scripts: the Scripting interface.

I used it in the app component here. You can also inject certain variables to the script so that they are available within the script to use (see here).

Besides this general information, I don’t really see this to be applicable with your examples. Here’s why:

In this example, what do you want to be dynamic here. With dynamic I mean that the calculation should be changeable at runtime like it is done with the groovy script in the access groups.
To me it seems that it is a very concrete calculation that will never change. Therefore it can perfectly be represented as an algorithim in the application as source code. Can you elaborate on what needs to be dynamic here?

Besides this, the problem “to calculate how many small boxes fit into a lager box” you describe is actually a NP-hard problem. This means, that you most likely will need a little bit more then just a formular to calculate it. You can read more about the Bin packing problem and at a related question on SO.

This as well something that can be modeled perfectly in the application code. Because you need to have some UI that will give the user the possibility to select a trigger this thing by entering age & gender and should return the rate.

For the second example what might be working is that you use the reports feature of CUBA. Since it has more or less the building blocks that you need for that. You can dynamically select the input entities / attributes (with the generic UI for the user to select concrete values). Then you have a scripting part which runs some groovy which would allow this kinds of calculation. And then the output could be something like a PDF with the result, or as CUBA recently added the feature to render a CUBA table to display the results.

This just came to my mind as it might be something that will conceptually fit. But only for the second example. For the first example there is no correlation what so ever.

Or are you talking about this examples itself as the things that should be dynamically added into your applications? In this case, this would not only require the calculation but also the UI, the workflow to by dynamic, right?

Perhaps you can clarify a little bit what you mean and how you think the solution should look like. Perhaps you do some UI mockups to understand the UI workflow etc.

Bye
Mario

1 Like

Thank you Mario for your insight and prompt response. Indeed it has been helpful to explore more of the tool and I have able to look into the scripting feature although still struggling with it. Answers to your questions.

For the first example, as you rightly observed, there will be more parameters like box dimension, volume etc that user will be input and i was hoping to provide the user to configure the formula . Since the items can be all different shapes and sizes, i don’t want to put in the source code and externalize the same.

For the second example, i cannot use the report , because the result needs to be stored in the business object Ex: Premium . I am trying to avoid interfacing to external rule engines Ex: drools, openL etc.

You have given enough points to ponder through and so thanks for the same. I will provide mockups as I progress on the ideas you have given me .

Thanks for sharing a lot of useful info.