The Lightweight Rule Engine (LWRE) is a Java-based rule engine designed for dynamic rule execution in complex systems. It enables developers to define, compile, and execute business rules using a Domain-Specific Language (DSL), with support for dependency management, retries, timeouts, and parallel execution. The engine is optimized for performance with features like precomputed execution paths, thread-safe operations, and a circuit breaker pattern to prevent system overload.
ConcurrentHashMap, synchronized methods) for reliable concurrent operation.The following dependencies are required. Add them to your pom.xml:
<dependencies>
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.1.12</version>
</dependency>
</dependencies>
git clone https://github.com/HamdiGhassen/lwre.git
cd lwre
mvn clean install
Rules are defined using a DSL with directives for rule configuration, logic, and dependencies. Below is an example DSL snippet:
#GLOBAL
userId : String
threshold : Integer
#HELPER
int calculateScore(int value) {
return value * 2;
}
#RULE ValidateUser
#GROUP UserValidation
#PRIORITY 1
#USE
userId : String as user FROM Global
threshold : Integer as threshold FROM Global
#PRODUCE
score : Integer
#CONDITION
return user != null && threshold > 100;
#ACTION
score = calculateScore(threshold);
System.out.println("the score is : "+score);
#FINAL
return "Validation complete for user: " + user;
Create and execute rules using the LWREngine:
import org.pulse.lwre.core.LWREngine;
public class Main {
public static void main(String[] args) throws Exception {
// Initialize the engine with rules
LWREngine engine = new LWREngine.Builder()
.rules(dslContent) // Load DSL from file or string
.global("userId", "user123")
.global("threshold", 150)
.debug(true)
.build();
// Execute rules for a specific group
Object result = engine.executeRules("UserValidation");
System.out.println("Execution result: " + result);
// Or execute asynchronously
engine.executeRulesAsync("UserValidation")
.thenAccept(res -> System.out.println("Async result: " + res));
}
}
The DSL supports the following directives:
#GLOBAL <name> : <type>: Define global variables.#HELPER: Define reusable helper methods.#RULE <name>: Define a rule with a unique name.#GROUP <name>: Assign the rule to a group.#PRIORITY <number>: Set execution priority.#USE <variable> : <type> as <alias> FROM <Global|RULE <name>>: Declare variables used by the rule.#PRODUCE <variable> : <type>: Declare variables produced by the rule.#CONDITION: Java code block evaluating to a boolean.#ACTION: Java code block for rule actions.#FINAL: Java code block for final processing.#RETRY <count> [DELAY <ms>] [IF {condition}]: Configure retry policy.#NEXT_ON_SUCCESS <ruleName>: Specify next rule on success.#NEXT_ON_FAILURE <ruleName>: Specify next rule on failure.#TIMEOUT <value> <ms|s>: Set execution timeout.#VERSION <major.minor.patch>: Set rule version.#MAX_EXECUTIONS <number>: Limit rule executions.Rule objects and helper blocks.LWREngine using the Builder pattern.executeRules) or asynchronously (executeRulesAsync).RuleExecutionException, RuleTimeoutException).LWRE is designed for concurrent environments:
ConcurrentHashMap for shared state.CircuitBreaker ensure safe state updates.ForkJoinPool and ScheduledExecutorService.java.lang.Runtime, java.io.File) and methods (e.g., exec, exit).This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.
For questions or support, contact Hamdi Ghassen or open an issue on GitHub.