Demos

MRules is shipped with various demo projects, packaged under the form of standalone maven projects, containing JUnit tests. They are versionned in the same way than the core library. They can all be downloaded via the Downloads page.

You will find here a quick description of all available demonstrations.

E-Commerce

We are creating here a commercial web sites, selling various products to customers. MRules is used to finalize the commands, performing the following steps:

  • Computing customer level, depending on command history:
    • GOLD if more than 1500 CUR in history.
    • SILVER if more than 1000 CUR or at least one command over 500 CUR in history.
    • STANDARD in other cases.
  • Computing VAT: 5.5% for articles of type FOOD, 20% for others.
  • Computing commands amounts.
  • Computing discounts:
    • Checking discounts validity (if not valid, raise an error)
    • Applying discounts
    • Computing if special discount is applyable: if at least 2 of [deliveryMode: ECONOMIC | deliveryCity: PARIS | level: GOLD]: 15 CUR discount is applied.
  • Computing final commands amounts.
  • Computing discounts that the customer earns with this command :
    • Final command amount > 500: 10% future discount
    • 5th command: 5% future discount

The JUnit test will do the following :

  • Build different types of baskets to test all cases
  • Perform all the operations listed above with Java code
  • Launch MRules to perform the same operations and check results
  • Compare execution times
  • Test that MRules configuration factory reads and writes the configuration correctly

Transportation fees

This is here a very simplified case of a company using MRules to compute fees which may be paid to employees to compensate transportation cost and time.

Depending on:

  • The town where the employee works (PARIS, LONDON, …)
  • The transportation mode (BICYCLE, CAR, BUS, …)
  • The amount of time spent travelling each days

The following data will be computed:

  • Will there be a payment ?
  • If so, what is the payment mode, between Compensation and Flat Rate ?
  • If Flat Rate, what is the fixed amount which will be paid ?

Three possible ways to write configurations are provided, in order to show differences in terms of complexity and execution time.

The JUnit test will do the following:

  • Build different cases
  • Launch MRules on all cases and check the results, for all three configurations
  • Benchmark execution on all three configurations
  • Test that MRules configuration factory reads and writes all three configurations correctly

Classroom

MRules is used here to compute final results of all students of a classroom. Provided all marks obtained for all tests over the year, and the coefficient attributed to each test, the following output will be computed:

  • Student data:
    • The average mark
    • Is the student admitted ? If so, with a mention ? If not, is it possible to retry ?
  • Classroom data:
    • The classroom average mark
    • The number of admitted / rejected

Two possible configurations are provided, with slight differences on the way rules are written.

The JUnit test will do the following:

  • Build the test classroom input data
  • Perform all the operations listed above with Java code
  • Launch MRules to perform the same operations and check results
  • Compare execution times
  • Test that MRules configuration factory reads and writes the configuration correctly

VAT computation

MRules is used here to compute VAT rate ans maximum  to be applied on commercial products, depending on some input parameters:

  • Region (FRANCE, SPAIN, …)
  • The customer type (Company, Association, Person)
  • The product type (Food, Book, …)
  • The commande date

These rules are applied, in this order:

  • If France and product type is Book, since 2016-01-01: VAT 5.5% and 3% max discount.
  • If France and product type is Book: VAT 5.5% and 5% max discount.
  • If Association: VAT 5.5%.
  • If product type is Book: VAT 10%.
  • If product type is Food: VAT 5.5% and 0% max discount.
  • If Company and product type is Electronics: VAT 10% and 15% max discount.
  • If Person and product type is in House or Music: VAT 10% and 25% max discount.
  • In any other case: VAT 20%.

This demo is interesting because rules are written using the simple grammar, and also because it illustrates the behaviour of stopping execution after first validated rule.

The JUnit test will do the following:

  • Build some test data
  • Launch MRules to perform the operations and check results
  • Benchmark execution
  • Test that MRules configuration factory reads and writes the configuration correctly

Sudoku

Even if MRules has not been created in the purpose of solving this type of problems, but is more dedicated to business rules, the power of the library and its simplicity allow to easily use it to do so.

We use here the very basic backtracking algorithm, which is more or less a brute force way to solve all types of grid, even empty ones 🙂

This demo will show you:

  • Advanced usage of functions
  • How to iterate on number ranges
  • How  to return a value from the rule set to the calling Java program

We also have included a Java solver, and one provided by Drools examples, in order to compare how rules are written, and how fast they are executed, knowing that this solver is based on an algorithm more complicated than the back tracking.

The JUnit test will do the following:

  • Build different test grids
  • Solve them with the Java solver
  • Solve them with the Drools solver
  • Solve them with the MRules solver
  • Check that grids are solved if they can be
  • Compare execution times
  • Test that MRules configuration factory reads and writes the configuration correctly

Drools Fight

This demo was realised for our benchmark between Drools and MRules. Everything is described with details here.

Advanced

This last demo project just illustrates some advance features, but does not simulate a functional case.

You will see here how to:

  •  Deactivate part of the optimization process for a specific rule set
  • Create a sublist by selecting items in a main list if conditions are verified
  • Round several numbers
  • Return the 3 first characters of a String, or “N/A”
  • Run a system command and test result. If incorrect, raise an error.
  • Test if input bean has a property (i.e. if the field exists in the Java class)
  • Add 21 year, 1 month and remove 1 day to current date, set a variable with the result and print it. Then, compute the difference in year between current date and the variable. If not 21: raise an error.