File based rule
Code Example:
BusinessRule rule = BusinessRuleBuilder
.groovyRule("RULE_ID")
.withGroovyCode(new File("rule.groovy"))
.build();
The groovy script file must include two imports:
- static import of link to BusinesRuleUtils.
- import of GroovyExecutionScope
The first import adds code completion and docs for helper methods such as nullSafeEquals
. The second import gives access to various contexts that the rule operates on, such as the product, attributes, and errors. TODO: link to See documentation for further details.
Here is the content of rule.groovy
:
package groovyRules;
import static com.stibo.leap.datastandard.extractor.sdk.businessrules.BusinessRuleUtils.*;
import com.stibo.leap.datastandard.extractor.sdk.businessrules.GroovyExecutionScope;
def product = GroovyExecutionScope.CHANNEL_PRODUCT?.getProduct();
def attributeA = product?.getAttribute("A");
def attributeB = product?.getAttribute("B");
if (nullSafeEquals(attributeA?.getValue(), attributeB?.getValue())) {
GroovyExecutionScope.errors.put("A", "A and B cannot contain the same value");
GroovyExecutionScope.errors.put("B", "A and B cannot contain the same value");
};
return !GroovyExecutionScope.errors.isEmpty();
This produces the following JSON output when rule is serialized:
{
"id": "RULE_ID",
"templateId": "groovyRule",
"namedAttributes": {},
"parameters": {
"customFunction": "def product = context.get(\"CHANNEL_PRODUCT\")?.getProduct();def attributeA = product?.getAttribute(\"A\");def attributeB = product?.getAttribute(\"B\");if (nullSafeEquals(attributeA?.getValue(), attributeB?.getValue())) {errors.put(\"A\", \"A and B cannot contain the same value\");errors.put(\"B\", \"A and B cannot contain the same value\");};return !errors.isEmpty();"
},
"requiredContexts": [
"CHANNEL_PRODUCT"
],
"execution": "CONTINUOUS",
"businessRuleScopes": [],
"staticProperties": {}
}
Updated over 1 year ago