A rule engine in less than 70 lines of code

time to read 2 min | 220 words

As I continue to explore what we can do with DSL, I am getting more and more excited. Let us take a look at the following syntax:

when User.IsPreferred and Order.TotalCost > 1000:
    addDiscountPrecentage  5
    applyFreeShipping
when not User.IsPreferred and Order.TotalCost > 1000:
    suggestUpgradeToPreferred 
    applyFreeShipping
when User.IsNotPreferred and Order.TotalCost > 500:
    applyFreeShipping

The backend for that is a simple 68 lines class. Again, we had to extend the language to support the when keyword, but that is all we really had to do.

image

I tried to build the same using Windows Workflow, but I gave up after a few minutes. It was too much clicking, and not enough results.

It should look something like the image to the left, I assume.

From the perspective of maintainability and actually being able to look at what is going on, I know what I would like to have.

At any rate, you can get the backend here, and get the unit tests here. Both are part of the test suites of the Rhino DSL project.

To be frank, I can't believe how easy this stuff is.

 

.