Skip to content

This repository contains a Rule Engine for Android applications, implemented in Kotlin. It evaluates JSON-defined business rules against given data using nested all/any logic. and multiple operators like equal, not_equal, and contains. The project includes unit tests & supports rule evaluation by name.

License

Notifications You must be signed in to change notification settings

shahwaiz90/KotlinRuleEngine

Repository files navigation

Rule Engine for Android (Kotlin)

📌 Overview

This project is a Rule Engine implemented in Kotlin for Android applications. It evaluates business rules defined in JSON format against provided data. The engine supports complex conditions using all and any logic.

🚀 Features

  • Load rules from JSON
  • Evaluate all rules against given data
  • Evaluate a specific rule by name
  • Supports multiple operators (equal, not_equal, contains, etc.)
  • Handles nested conditions (all/any)

📂 Project Structure

|-- src/
|   |-- RuleEngine.kt         # Core rule evaluation logic
|   |-- RuleModel.kt          # Data model for rules
|   |-- Operator.kt           # Supported operators
|   |-- RuleEngineTest.kt     # Unit tests for rule evaluation
|-- README.md                 # Project documentation 

📥 Installation

  1. Clone the repository:
    git clone https://github.com/shahwaiz90/KotlinRuleEngine.git
  2. Open the project in Android Studio.
  3. Ensure you have Kotlin and Gson dependencies installed:
    implementation("com.google.code.gson:gson:2.8.9")

🔧 Usage

1. Define Rules (JSON Format)

Create a rules.json file with rules:

[
    {
        "name": "ShowWithoutVAT",
        "description": "Show without VAT in subscriptions",
        "conditions": {
            "all": [
                { "path": "$.product.category", "value": "RATEPLANS", "operator": "not_contains" },
                { "any": [
                    { "path": "$.context.serviceType", "value": "prepaid", "operator": "equal" },
                    { "path": "$.context.serviceType", "value": "quicknet_prepaid", "operator": "equal" }
                ]}
            ]
        }
    }
]

2. Load Rules into Rule Engine

val jsonRules = File("rules.json").readText()
val rules = loadRulesFromJson(jsonRules)
val ruleEngine = RuleEngine(rules)

3. Evaluate Rules Against Data

val testData = mapOf(
    "product" to mapOf("category" to "Addons"),
    "context" to mapOf("serviceType" to "prepaid")
)
val result = ruleEngine.evaluateRule("ShowWithoutVAT", testData)
println("Result: $result") // Expected: true

✅ Running Tests

Run unit tests using:

./gradlew test

Example test case:

@Test
fun testShowWithoutVAT_ShouldReturnTrue_WhenServiceTypeIsPrepaid() {
    val testData = mapOf(
        "product" to mapOf("category" to "DATA"),
        "context" to mapOf("serviceType" to "prepaid")
    )
    val result = ruleEngine.evaluateRule("ShowWithoutVAT", testData)
    assertTrue(result)
}
Screenshot 2025-02-12 at 3 29 35 PM

🤝 Contributing

  1. Fork the repo
  2. Create a feature branch (git checkout -b feature-name)
  3. Commit your changes (git commit -m 'Add new feature')
  4. Push to your branch (git push origin feature-name)
  5. Open a Pull Request 🚀

📜 License

This project is licensed under the MIT License.

⭐ Support

If you like this project, consider giving it a ⭐ on GitHub! 😊

About

This repository contains a Rule Engine for Android applications, implemented in Kotlin. It evaluates JSON-defined business rules against given data using nested all/any logic. and multiple operators like equal, not_equal, and contains. The project includes unit tests & supports rule evaluation by name.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages