Rule Compilation
During rule compilation, rules are transformed from source code (high-level representation of conditions and actions) into Drools syntax, which the Rule Engine can then execute.
Compilation Process
The compilation process is a sequence of various phases. Each phase takes input from the previous stage and feeds its output to the next phase of the compilation. Compilation of a rule is executed through the following phases:
-
Source code translation (using the RuleSourceCodeTranslator interface) transforms the conditions and actions (high-level representation) into the intermediate representation. Most of the validation occurs during this phase. The intermediate representation is generated in a way that makes it easier to be translated into the Rule Engine-specific code.
-
Intermediate representation processing (using the RuleIrProcessor interface) applies some transformations on the intermediate representation. For example, the intermediate representation can be optimized by removing unnecessary conditions.
-
Target code generation (using the RuleTargetCodeGenerator interface) transforms the intermediate representation into the Rule Engine-specific syntax (Drools). In this phase, the code generator takes the optimized representation of the intermediate code and maps it to the Rule Engine language.
Compilation Sequence
The following sequence diagram shows the flow of logic within the compiler:
RuleCompilerService
This interface is the main entry point. It is the service that can be used to perform the compilation of rules.
RuleCompilerContext
This interface represents the context valid for the compilation of a rule. It is passed to, and potentially modified by, each phase of the compilation process.
RuleProcessor
Implementations of this interface are responsible for processing the rule intermediate representation. Many processors can exist.
RuleTargetCodeGenerator
This interface is responsible for converting what is in the intermediate representation into a Rule Engine-specific format.
Intermediate Representation
The Intermediate Representation (IR) is a data structure constructed from input conditions and actions that is used to generate the Rule Engine-specific code. For details, see Intermediate Representation in Rule Compiler.
Translation of Conditions and Actions
Conditions and Actions are the two main components of a rule. The main purpose of the compiler is to convert input conditions and actions into the intermediate representation. For details, see Conditions and Actions in Rule Compiler.
Validation
The purpose of validation is to provide a high degree of assurance that a specific rule behaves as expected. It should be able to detect cases where, for example, a parameter was introduced incorrectly. For details, see Validation Mechanism in Rule Compiler.
Extensibility
The rule compiler provides some extension points. For details, see Rule Compiler Extensibility.