Class SpelRuleEvaluationService
- java.lang.Object
-
- com.broadleafcommerce.rulesengine.expression.service.SpelRuleEvaluationService
-
- All Implemented Interfaces:
RuleEvaluationService
public class SpelRuleEvaluationService extends Object implements RuleEvaluationService
Default implementation of
RuleEvaluationService
that takes the configuredExpressionParser
to parse SpELExpression
s with caching for expressions andEvaluationContexts
. Combined withCachingStdEvaluationContextFactory
, supports SpEL expression evaluation with concrete objects or maps.Expressions are cached based on the literal expression as the key while
EvaluationContexts
use the rootObject as the key.- Author:
- Nathan Moore (nathanmoore).
- See Also:
- SPeL
documentation,
MapAccessor
,MapAccessor
-
-
Field Summary
Fields Modifier and Type Field Description protected EvaluationContextFactory
evaluationContextFactory
protected static Map<String,org.springframework.expression.Expression>
EXPRESSION_CACHE
protected org.springframework.expression.ExpressionParser
spelParser
-
Constructor Summary
Constructors Constructor Description SpelRuleEvaluationService(org.springframework.expression.ExpressionParser blSpelParser, EvaluationContextFactory blEvaluationContextFactory)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
allMatch(Collection<String> rules, Object rootObject, ExpressionContext expressionContext)
Use to determine if the object matches all rules provided.boolean
anyMatch(Collection<String> rules, Object rootObject, ExpressionContext expressionContext)
Use to determine if the object matches any rule provided.boolean
evaluateRule(String rule, Object rootObject, ExpressionContext expressionContext)
Uses the configured expression parser to evaluate the rule against the root object.<T> Iterable<T>
findAll(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext)
Use to find all objects matching the rules provided.<T> Optional<T>
findAny(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext)
Use to find an object that matches the rules.<T> Iterable<T>
findAnyN(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext)
Use to find a subset of all objects matching the rules provided.<T> Optional<T>
findFirst(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext)
Use to find the first object that matches the rules.<T> Iterable<T>
findFirstN(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext)
Use to find a subset of all objects matching the rules provided.protected org.springframework.expression.Expression
getExpressionFromCache(String rule, ExpressionContext expressionContext)
protected Optional<org.springframework.expression.ParserContext>
getParserContext(ExpressionContext expressionContext)
boolean
noneMatch(Collection<String> rules, Object rootObject, ExpressionContext expressionContext)
Use to determine if the object matches no provided rule.
-
-
-
Field Detail
-
EXPRESSION_CACHE
protected static final Map<String,org.springframework.expression.Expression> EXPRESSION_CACHE
-
spelParser
protected final org.springframework.expression.ExpressionParser spelParser
-
evaluationContextFactory
protected final EvaluationContextFactory evaluationContextFactory
-
-
Constructor Detail
-
SpelRuleEvaluationService
public SpelRuleEvaluationService(org.springframework.expression.ExpressionParser blSpelParser, EvaluationContextFactory blEvaluationContextFactory)
-
-
Method Detail
-
evaluateRule
public boolean evaluateRule(String rule, Object rootObject, ExpressionContext expressionContext)
Uses the configured expression parser to evaluate the rule against the root object. If the rule is null or blank, this method returns
true
.- Specified by:
evaluateRule
in interfaceRuleEvaluationService
- Parameters:
rule
- String representation of an expressionrootObject
- Object to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- whether the object matches the rule
- See Also:
StandardExpressionContext.EMPTY_EXPRESSION_CONTEXT
-
anyMatch
public boolean anyMatch(Collection<String> rules, Object rootObject, ExpressionContext expressionContext)
Use to determine if the object matches any rule provided.
- Specified by:
anyMatch
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObject
- Object to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- whether the object matches any rules
- See Also:
StandardExpressionContext.EMPTY_EXPRESSION_CONTEXT
-
allMatch
public boolean allMatch(Collection<String> rules, Object rootObject, ExpressionContext expressionContext)
Use to determine if the object matches all rules provided.
- Specified by:
allMatch
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObject
- Object to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- whether the object matches all rules
- See Also:
StandardExpressionContext.EMPTY_EXPRESSION_CONTEXT
-
noneMatch
public boolean noneMatch(Collection<String> rules, Object rootObject, ExpressionContext expressionContext)
Use to determine if the object matches no provided rule.
- Specified by:
noneMatch
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObject
- Object to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- whether the object matches no rules
- See Also:
StandardExpressionContext.EMPTY_EXPRESSION_CONTEXT
-
findFirst
public <T> Optional<T> findFirst(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext)
Use to find the first object that matches the rules.
ExpressionContext.isMatchAnyRule()
should be set to determine if returned object must match any or all rules.- Specified by:
findFirst
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObjects
- Objects to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- the first object provided that matches the rules
- See Also:
StandardExpressionContext.EMPTY_EXPRESSION_CONTEXT
-
findAny
public <T> Optional<T> findAny(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext)
Use to find an object that matches the rules.
ExpressionContext.isMatchAnyRule()
should be set to determine if returned object must match any or all rules.- Specified by:
findAny
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObjects
- Objects to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- an object provided that matches the rules
- See Also:
StandardExpressionContext.EMPTY_EXPRESSION_CONTEXT
-
findAll
public <T> Iterable<T> findAll(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext)
Use to find all objects matching the rules provided.
ExpressionContext.isMatchAnyRule()
should be set to determine if returned objects must match any or all rules.- Specified by:
findAll
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObjects
- Objects to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- all objects provided that match rules
- See Also:
StandardExpressionContext.EMPTY_EXPRESSION_CONTEXT
-
findAnyN
public <T> Iterable<T> findAnyN(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext)
Description copied from interface:RuleEvaluationService
Use to find a subset of all objects matching the rules provided. The subset will consist of any 'n' objects found with 'n' being determined by
ExpressionContext.getLimit()
.ExpressionContext.isMatchAnyRule()
should be set to determine if returned objects must match any or all rules.- Specified by:
findAnyN
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObjects
- Objects to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- a subset of 'n' objects matching the rules
- See Also:
StandardExpressionContext.EMPTY_EXPRESSION_CONTEXT
-
findFirstN
public <T> Iterable<T> findFirstN(Collection<String> rules, Collection<T> rootObjects, ExpressionContext expressionContext)
Description copied from interface:RuleEvaluationService
Use to find a subset of all objects matching the rules provided. The subset will consist of the first 'n' objects found with 'n' being determined by
ExpressionContext.getLimit()
.ExpressionContext.isMatchAnyRule()
should be set to determine if returned objects must match any or all rules.- Specified by:
findFirstN
in interfaceRuleEvaluationService
- Parameters:
rules
- String representations of expressionsrootObjects
- Objects to evaluate the rule againstexpressionContext
- Relevant information required to evaluate an expression or group of expressions- Returns:
- a subset of the first 'n' objects matching the rules
- See Also:
StandardExpressionContext.EMPTY_EXPRESSION_CONTEXT
-
getExpressionFromCache
protected org.springframework.expression.Expression getExpressionFromCache(String rule, ExpressionContext expressionContext)
-
getParserContext
protected Optional<org.springframework.expression.ParserContext> getParserContext(ExpressionContext expressionContext)
-
-