AEPRulesEngine is currently in beta. Use of this code is by invitation only and not otherwise supported by Adobe. Please contact your Adobe Customer Success Manager to learn more.
A simple, generic, extensible Rules Engine in Swift.
- Xcode 11.0 (or newer)
- Swift 5.1 (or newer)
# Podfile
use_frameworks!
target 'YOUR_TARGET_NAME' do
pod 'AEPRulesEngine', :git => 'https://github.com/adobe/aepsdk-rulesengine-ios.git', :branch => 'main'
end
Replace YOUR_TARGET_NAME
and then, in the Podfile
directory, type:
$ pod install
To add the AEPRulesEngine package to your application, from the Xcode menu select:
File > Swift Packages > Add Package Dependency...
Enter the URL for the AEPRulesEngine package repository: https://github.com/adobe/aepsdk-rulesengine-ios.git
.
When prompted, make sure you change the branch to main
.
There are three options for selecting your dependencies as identified by the suffix of the library name:
- "Dynamic" - the library will be linked dynamically
- "Static" - the library will be linked statically
- (none) - (default) SPM will determine whether the library will be linked dynamically or statically
Alternatively, if your project has a Package.swift
file, you can add AEPRulesEngine directly to your dependencies:
dependencies: [
.package(url: "https://github.com/adobe/aepsdk-rulesengine-ios.git", .branch("main"))
]
To create a RulesEngine
instance, define an Evaluator
and pass it to the RulesEngine
's initializer:
let evaluator = ConditionEvaluator(options: .caseInsensitive)
let rulesEngine = RulesEngine(evaluator: evaluator)
Anything that conforms to the Rule
protocol can be used as rule:
public class MobileRule: Rule {
init(condition: Evaluable) { self.condition = condition }
var condition: Evaluable
}
let condition = ComparisonExpression(lhs: "abc", operationName: "equals", rhs: "abc")
let rule = MobileRule(condition: condition)
rulesEngine.addRules(rules: [rule])
A rule without the flexibility to dynamically fetch a value will always evaluate to true or false. To fetch the value for a rule at runtime, use a Mustache Token:
let mustache = Operand<String>(mustache: "{{company}}")
let condition = ComparisonExpression(lhs: mustache, operationName: "equals", rhs: "adobe")
let rule = MobileRule(condition: condition)
rulesEngine.addRules(rules: [rule])
Use the evaluate
method to process Traversable
data through the RulesEngine
:
let matchedRules = rulesEngine.evaluate(data: ["company":"adobe"])
Contributions are welcomed! Read the Contributing Guide for more information.
This project is licensed under the Apache V2 License. See LICENSE for more information.