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.
- 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)
|-- 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
- Clone the repository:
git clone https://github.com/shahwaiz90/KotlinRuleEngine.git
- Open the project in Android Studio.
- Ensure you have Kotlin and Gson dependencies installed:
implementation("com.google.code.gson:gson:2.8.9")
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" }
]}
]
}
}
]
val jsonRules = File("rules.json").readText()
val rules = loadRulesFromJson(jsonRules)
val ruleEngine = RuleEngine(rules)
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
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)
}

- Fork the repo
- Create a feature branch (
git checkout -b feature-name
) - Commit your changes (
git commit -m 'Add new feature'
) - Push to your branch (
git push origin feature-name
) - Open a Pull Request 🚀
This project is licensed under the MIT License.
If you like this project, consider giving it a ⭐ on GitHub! 😊