Skip to main content

DocuMine Documentation

Rule engine basics

DocuMine harnesses the power of the Drools business rule management system. Its rule engine serves as the cornerstone of DocuMine’s rule management, facilitating seamless and easy rule creation and implementation.

Understanding the basics of the Drools rule engine is essential for leveraging its capabilities effectively.

Rule engine components

The rule engine is responsible for storing, processing, and evaluating data relevant for executing the defined business rules. It matches the facts stored in its working memory to the conditions of the rules stored in its production memory to decide which actions to take.

The following diagram illustrates the basic components of the Drools rule engine:

rule-decision-engine__1_.png

Rules engine components

Facts

Data that enters or changes in the rule engine and that is matched against the rule conditions. (The Java objects you put into the rules engine.)

Working memory

Location where the facts are stored in the rule engine.

Rules

The DocuMine rules you define. All rules must contain at least one condition that triggers the rule and the action(s) that will be executed if the condition is met.

The conditions are defined in the when section of a rule, the action(s) in the “then” section.

Production memory

Location where the rules are stored in the rule engine.

Pattern matcher

The Drools engine matches the facts in the working system against the conditions of each rule (“when” block).If it finds patterns that fulfill the conditions, it queues the action block (“then” block) for execution, i. e. we can have multiple matches and will then have multiple executions queued. This means, the “then” block is executed for every entity that is found and no loop is necessary.

Agenda

Registry where matching rules are listed and prioritized in preparation for execution.

The rule engine matches the facts in the working memory against the conditions of the rules stored in the production memory (a process known as pattern matching). When the conditions are met, the rules are registered and prioritized on the agenda for execution.

The Java objects and the facts in the rules engine are separate. This implies that when rule-related information in the rule engine is updated by either you or an automated process, truth maintenance is required to insert or update the facts in the working memory. For each fact, there is a fact handle, which associates the fact in the working memory with the Java object.

Rule execution flow

After the initial firing of all rules, the rule engine repeatedly cycles through two phases:

  • “When” evaluation (Agenda evaluation):

    The rule engine evaluates the conditions in the “when” blocks of the rules and identifies all rules eligible for execution. Upon finding an executable rule, the engine registers it on the agenda.

  • Working memory actions:

    The rule engine executes the “then” block of all rules listed on the agenda.

After executing the rules, the engine switches back to the “when” evaluation phase to re-evaluate the rules, as new rule data may have entered the working memory, resulting in additional rules becoming fully matched and eligible for execution. If no matching rules are identified, the cycle ends.

rule_execution_flow.png

Rule execution flow

If several rules match, the agenda follows the order of the rules in the rule editor. In this case, the removal of one rule may lead to the removal of another rule from the agenda. (This applies at least to the current Drools version, where the order is unspecified but not randomized, although Drools does not guarantee it in general.) To avoid conflicts like the one mentioned above, you can prioritize rules by specifying execution flow parameters (rule salience and/or agenda group) where necessary. Please have a look at the Drools documentation if you are interested in further approaches.

Keywords

When you create rules that modify Java objects, you will have to update the corresponding facts in the rules engine’s working memory to ensure synchronization between Java and the working memory. In DocuMine, you can accomplish this by invoking the EntityCreationService class.Invoking the EntityCreationService class: "then" part

rule "DOC.7.0: Performing Laboratory (Name)"
    when
       $section: Section(containsString("PERFORMING LABORATORY:"))
    then
        entityCreationService.lineAfterString("PERFORMING LABORATORY:", "laboratory_name", EntityType.ENTITY, $section).findFirst().ifPresent(entity -> {
            entity.apply("DOC.7.0", "Performing Laboratory found", "n-a");
        });
    end

Drools actually supports various keywords. However, we only recommend the following and only to advanced users, as these keywords can result in endless loops. For instance, using “insert” is not necessary in DocuMine as it is handled by the entity creation service.

  • insert

    Inserts a new Java object as a fact into the working memory, allowing you to define the fields and values required of the fact.

  • update

    Updates the fact via the fact handle to ensure alignment with the Java code. After a fact has changed, call “update” before changing another fact that might be affected by the changed value.

Matched entities priority

If it turns out during rule firing that multiple rules apply to the same entity, the matched entities are applied in the following order:

  • Rule Group (e.g., 'DOC'): The rule groups FINAL, X, DICT, MAN have priority in that order. All other groups are treated equally.

  • Rule Unit: The rule unit (a sequence of numbers, e.g., '47') dictates the order in ascending numerical value when the rule groups are identical.

  • Order of rule in rule file: The order of appearance in the rule file in the rule editor if the rule identifiers and rule units are the same.

For further information, please see Rule naming conventions.

The execution flow parameters (rule salience and agenda group) have no impact on the priority of the matched entities. These parameters affect the rule execution order, not the matched entities priority.

Example:

Rule order in the rule file:

  • Rule “DOC.47.11”

  • Rule “DOC.47.12”

  • Rule “DOC.1.2”

  • Rule "MAN.5.0"

  • Rule "MAN.6.0"

  • Rule “LRD.1.2”

  • Rule "X.10.0"

The following matched entities application order applies:

  1. Rule "X.10.0"

  2. Rule "MAN.5.0"

  3. Rule "MAN.6.0"

  4. Rule “DOC.1.2”

  5. Rule “LRD.1.2”

  6. Rule “DOC.47.11”

  7. Rule “DOC.47.12”