
Explore the Drools rule engine by learning its purpose, solving problems with a rule-based approach, and mastering Drools syntax through progressively added examples and live code runs.
Explore how a rule engine uses domain experts and declarative rules to evaluate data, fire actions, and iterate until no rules remain, contrasting with imperative flows in visa applications.
Design rules with independency and atomicity, where data defines when a rule fires and data changes can trigger other rules; use rules for complex, evolving systems.
We look at the example code I have created in Github: https://github.com/aasaru/drools-training
You need JDK 17 or JDK 19 to run this code.
Learn to check out and run the Drools example code with IntelliJ IDEA Community Edition by cloning from GitHub, importing as a Maven project, and configuring Java 8.
NB! After filming this part newer Drools version has been released and I have updated the source code. Please open pom.xml and find out the correct version.
Explore stateless sessions in Drools to validate passports, assess visa eligibility, and apply rules for travel insurance calculations, expenses approvals, and email filtering.
Differences between stateful and stateless use cases. For which use cases to use Drools stateless sessions.
Define two stateless session rules to validate passports, matching valid passports and invalid ones (expired or with no unused visa pages) using a Drools container in step one.
Split a single multi-purpose Drools rule into two focused rules, one matching expired passports and another matching passports with zero unused visa pages, and compare the changes in step two.
Bind a variable in step three of stateless Drools sessions to show passport ownership. Print the owner and visa status using pattern binding and getters like getName and getUnusedVisaPages.
Step four demonstrates managing a Drools session, using a bound variable and a new enum validation to set passport results, then reading states back in Java code.
Rewrite the rule to avoid repetition in steps five and six, treating passports without validation info as okay while rules mark invalid ones, and note stateless sessions can't reflect changes.
Explore stateless sessions and use cases—validation, calculation, routing, and filtering—and how to declare statelessness in XML, while keeping rules short, using dollar-prefixed variables, and noting that state changes aren’t propagated.
Explore stateful vs stateless Drools sessions, inserting and modifying passport facts, firing rules, and ensuring order-independent results while avoiding memory leaks by disposing sessions.
Use stateful sessions when you rely on inference, letting rule set information consumed by another, use modify to update objects so affected rules rerun, dispose at end to prevent leaks.
Explore how Drools rules join passports with visa applications to validate entries, using a stateful session to insert visa applications and modify objects to reflect validation results.
In step two of Drools programming, learn to enforce passport validity six months after visit end by adding a cross-product rule that flags visa applications and marks validation failed.
Add new facts with Drools, issue visas when validation passes, and learn to control rule execution order using salience or agenda groups.
You can write Drools rules in either "java" or in "mvel" dialect.
Here I explain the difference of them and why I changed section06 code to use "java" dialect.
Create a new visa fact when a visa application passes validation, insert it into the session during rule execution, and later read all visas issued by the rules.
Master Drools rule execution by controlling order with salience, and learn how conflicts arise when multiple rules could fire and how high salience fires first.
Learn to control drools execution with agenda groups instead of salience, organizing rules into invalid passport, valid passport, invalid application, and valid application groups, using a stack and debug listener.
Use three agenda groups: validate passport, validate application, and issuing visa, and apply salience within the groups to prioritize failed validations over passes.
Explore activation groups in Drools, see how only one rule fires per group, and learn how agenda groups, salience, and date-based state changes shape visa validation.
In this self-assignment you learn how to set focus to agenda group in the rules.
I have added this assignment after publishing the course. You may need to run "git pull" to get the latest code. It comes without a video and you must examine the rules and Java code yourself.
Explore logical insertion of facts in Drools, using insert logical and not to manage fact existence, bind object existence to the when clause, and add new facts to reduce rules.
demonstrates insert logical in drools by inserting visas, tracking updates and deletions with event listeners; shows how changing the when clause can delete previously inserted facts and invalidate visa applications.
In this step, rewrite Drools rules to use fact objects instead of modifying passports and visa applications, inserting valid or invalid fact objects as session stickers.
Eliminate positive cases by focusing on negative ones, rewrite the visa rule with red stickers, and automatically remove invalid visas, reducing the rule set to four.
Design Drools rules with fact objects, manage session facts, and track insertions, updates, and removals using event listeners for debugging, ensuring extensible, well-structured rule logic.
Rewrite visa application logic to support family visa applications by validating multiple passports, issuing visas per passport, and retracting them when an invalid passport is found.
Rewrite the rule to fire only once using the exists keyword. This ensures a family visa application is invalidated if at least one invalid passport exists, without duplicating facts.
Learn to use for all in Drools to extract a specific value from a group, such as selecting the oldest member as group leader when all are under 18.
Rewrite a complex drools rule to a single for all expression, ensuring the group leader and all members are under 18, then confirm all are at least ten.
Learn to work with groups of facts in Drools, using exist to confirm at least one fact and for all to validate expressions or find the lowest or highest value.
Learn how spreadsheet decision tables convert independent conditions into drools rules. Use a family visa fee example to map urgent and person count to a DRL action.
Starting this section the code examples are taken from another Git repository. In this lecture, I will give an overview of how this repository differs from the previous one.
Open the spreadsheet decision table, view the three conditional columns and one action, then convert it to a drl file to drive rule execution that prints fees 300, 100, 140.
Define multiple action columns in a spreadsheet to link visa applications with fees, add a visa fee domain object and insert action, and retrieve results from the rule session.
Demonstrates using formulas in a spreadsheet decision table for Drools, converting Java code into rules, handling quotes, and debugging generated rules.
Set a custom drl rule name and add a comment by using the name and description columns in the rule table, showing how rules are generated from rows.
Learn to fit multiple rule tables in a single spreadsheet by structuring the rule set package and imports, with each table independent and separated by a blank line.
Demonstrates how to use Drools with multiple rule tables in a spreadsheet to generate visa fees from passport data, with parameter substitution, and notes that the first sheet is used.
Join multiple fact objects in a spreadsheet decision table by linking passport and visa application with the passport number to calculate the urgent fee in a Drools rules session.
In step seven of spreadsheet decision tables, the for all construct repeats a condition across a list, enabling not-in checks and guiding DRL generation.
Apply a not clause to a where condition in a spreadsheet decision table without a pattern, checking that no visa fee object with a given application id exists.
Sequential true in rule sets adds unique salience to each rule, overriding the priority column, and demonstrates using a sequential max priority to control firing order.
Explore activation groups in spreadsheet decision tables and how they limit rule firing. See how join columns, params, and variables merge data, with salience guiding sequential execution.
Learn to use custom labels in spreadsheet decision tables to drive Drools rules, using conditions and actions starting with C and A to manage fees and discounts.
Explore how to use Drools rule templates (drt) to generate visa decision rules from tabular data, including handling age gaps, unemployment, bookings, and randomized interview thresholds.
Fetches placeholder values from a tabular data source to populate Drools rule templates, loads rules from a string into an in-memory file system, and executes them to determine visa outcomes.
Declare an intermediate type in DRL to model a risk factor with visa application id and risk, insert it, and count risk factors to decide a visa verdict.
Declare and use enumerations in Drools DRL, read back risk factor objects as enums, and handle intermediate facts with Java integration for debugging and JSON conversion.
Recap rule templates and their placeholders; explore separating data from rules using XLS or databases, noting deprecation of templates and alternative templating, including Freemarker and direct string composition.
We learn Apache Drools DSL (Drools programming language) and go over the most common DSL constructs. We move in small steps and gradually introduce new concepts, so it is easier to follow even for a beginner.
This way we cover the most common Drools topics like:
what is a rule engine
what is the difference between a method and a rule
why one would use a rule engine instead of writing regular Java code
understanding of the when/then clauses
understanding in which order the rules fire and when the order is not known
how to control the execution using activation groups, agenda groups and salience
declaring variables in DRL
difference between stateless and stateful sessions
inserting facts and modifying existing objects in the session
cross products (similar to SQL join)
exists, forall and collect
using spreadsheet decision tables to create Drools rules
using templates for creating rules and feeding data into them from spreadsheets and other tabular data sources
complex event processing (CEP) in Drools
difference between fact and event
declaring facts and events in Java and inside DRL code
different rule execution modes (active/passive, cloud/stream, sequential)
time and length-based sliding windows
entry-points
global variables
timers
how Apache Drools' internal algorithm Phreak works
This course comes with two Java projects containing all the code we worked with during the course. The first project includes all the code examples in traditional Drools syntax and a second copy of all the rules in the new Drools OOPath syntax (introduced by Drools 8). The first project has been upgraded to run using Drools 10.
You are encouraged to check out and run the code yourself to better understand and experiment with changes.
There are instructions on running the code in IntelliJ, Eclipse, and VS Code.
After completing this course, you will better understand which kinds of problems could be solved with Drools and how to write the most common business rules in Drools DSL.