Project to demonstrate the calculation of taxes on a series of products belonging to an order using a business rules framework, drools.

The products will have a price plus a tax, depending on whether the product is 
imported (5%) or not, and whether you are exempt from paying taxes or not (10%). 

The exemptions are products such as books, food and medicines.

The problem is solved using the DROOLS framework and java8, a management system for 
business rules, which allows you to create business rules in the form of a .drl file,
so that a business team can create such a file using a tool 
or it can also be generated by hand, saving it in the src/main/resources folder.

A class has been written with a unit test that demonstrates the starting hypothesis.

Run the test, please. (mvn test)
This is the rules.dlr file, the most important file.

dialect "mvel"

rule "My First Drools Rule"
    when
        $o: Object()
    then
        System.out.println(" >>> Rule Fired for Object: "+$o.toString());
end

rule "Applying taxes to imported (5%) and exempted products (0%)."
when

  // Take the order from working memory and get the product list from it
  $order: Order( $products: products != null )

  // Get only the imported and exempted products
  $product: Product( isImported == true && isTax_exempt == true) from $products
then
  $product.setSale_tax($product.getPrize() * (5.000d/100));  
  $order.setTotalPrize($product.getSale_tax() + $product.getPrize());
  $order.setTotalTaxes($product.getSale_tax());
end

rule "Applying taxes to imported (5%) and NOT exempted products (10%)."
when

  // Take the order from working memory and get the product list from it
  $order: Order( $products: products != null )

  // Get only the imported and exempted products
  $product: Product( isImported == true && isTax_exempt == false) from $products
then
  $product.setSale_tax($product.getPrize() * (15.000d/100));  
  $order.setTotalPrize($product.getSale_tax() + $product.getPrize());
  $order.setTotalTaxes($product.getSale_tax());
end

rule "Applying taxes to NOT imported (0%) and exempted products (0%)."
when

  // Take the order from working memory and get the product list from it
  $order: Order( $products: products != null )

  // Get only the imported and exempted products
  $product: Product( isImported == false && isTax_exempt == true) from $products
then
  $product.setSale_tax(0.000d);  
  $order.setTotalPrize($product.getSale_tax() + $product.getPrize());
  $order.setTotalTaxes($product.getSale_tax());
end

rule "Applying taxes to NOT imported (0%) and NOT exempted products (10%)."
when

  // Take the order from working memory and get the product list from it
  $order: Order( $products: products != null )

  // Get only the imported and exempted products
  $product: Product( isImported == false && isTax_exempt == false) from $products
then
  $product.setSale_tax($product.getPrize() * (10.000d/100));  
  $order.setTotalPrize($product.getSale_tax() + $product.getPrize());
  $order.setTotalTaxes($product.getSale_tax());
end

An important detail, if you notice, in the rules file refers to boolean fields belonging to the object Products, isExempted and isTax_exempt, well, those fields are exempted and tax_exempt, but when eclipse generates the getters, calls them isExempted and isTax_exempt, ero drools needs if or if all the fields have a get method, then look for them to start with get, even if the fields are boolean. Be careful with this detail.

This is the github link. Have fun and take care a lot, people.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s