lwre

Lightweight Rule Engine (LWRE)

Java CI with Maven and ReleaseCodeQLQuality Gate StatusBugsCode SmellsReliability RatingSecurity RatingMaintainability RatingVulnerabilitiesKnown Vulnerabilities

Overview

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.

Features

Installation

Prerequisites

Dependencies

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>

Building from Source

  1. Clone the repository:
    git clone https://github.com/HamdiGhassen/lwre.git
    cd lwre
    
  2. Build the project using Maven:
    mvn clean install
    

Usage

Defining Rules

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;

Integrating with Java

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));
    }
}

DSL Syntax

The DSL supports the following directives:

Key Classes

Example Workflow

  1. Define rules in a DSL file or string, specifying conditions, actions, and dependencies.
  2. Load rules into LWREngine using the Builder pattern.
  3. Set global variables and configure tracing or metrics if needed.
  4. Execute rules synchronously (executeRules) or asynchronously (executeRulesAsync).
  5. Retrieve results or handle exceptions (e.g., RuleExecutionException, RuleTimeoutException).

Thread Safety

LWRE is designed for concurrent environments:

Security

Performance Optimizations

License

This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.

Contact

For questions or support, contact Hamdi Ghassen or open an issue on GitHub.