public class Engine extends Object
IActions or simply provide an
ordered list of the best matching Rules.Rules.Rule API.
Consider the following four rules:
Rule r1 = new Rule("YouthTarif",
"input.age < 26", new StringAction("YT2011"), 3,
"ch.maxant.someapp.tarifs", null);
Rule r2 = new Rule("SeniorTarif",
"input.age > 59", new StringAction("ST2011"), 3,
"ch.maxant.someapp.tarifs", null);
Rule r3 = new Rule("DefaultTarif",
"!#YouthTarif && !#SeniorTarif", new StringAction("DT2011"), 3,
"ch.maxant.someapp.tarifs", null);
Rule r4 = new Rule("LoyaltyTarif",
"#DefaultTarif && input.account.ageInMonths > 24",
new StringAction("LT2011"), 4,
"ch.maxant.someapp.tarifs", null);
TarifRequest request = new TarifRequest();
request.setPerson(new Person());
request.setAccount(new Account());
request.getPerson().setAge(35);
request.getAccount().setAgeInMonths(25);
//will return both DT2011 and LT2011, since both are relevant.
//Since LT2011 has a higher priority, it is first in the list.
List<Rule> matches = engine.getMatchingRules(request);
// returns "LT2011", since the loyalty tarif has a higher priority
String tarif = engine.runBestAction(request, String.class);
Rule for more details. See EngineTest for more examples.
See http://mvel.codehaus.org for full details of the expression language.| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_INPUT_NAME
the name which scripts should use for the input, unless overriden in the constructor/builder.
|
protected String |
inputName |
protected String[] |
javascriptFilesToLoad |
protected List<Rule> |
parsedRules |
protected Integer |
poolSize |
protected boolean |
throwExceptionIfCompilationFails |
protected Set<String> |
uniqueOutcomes |
| Modifier | Constructor and Description |
|---|---|
|
Engine(Collection<Rule> rules,
boolean throwExceptionIfCompilationFails) |
|
Engine(Collection<Rule> rules,
String inputName,
boolean throwExceptionIfCompilationFails)
|
protected |
Engine(Collection<Rule> rules,
String inputName,
boolean throwExceptionIfCompilationFails,
Integer poolSize,
String[] javascriptFilesToLoad) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
compile() |
<Input,Output> |
executeAllActions(Input input,
Collection<? extends IAction<Input,Output>> actions)
See
executeAllActions(String, Object, Collection), except that all namespaces will be considered. |
<Input,Output> |
executeAllActions(String nameSpacePattern,
Input input,
Collection<? extends IAction<Input,Output>> actions)
Evaluates all rules against the input and then executes all action associated with the positive rules outcomes, in order of highest priority first.
Any outcome is only ever executed once! NOTE THAT THIS METHOD DISREGARDS ANY RETURN VALUES OF ACTIONS!! |
<Input,Output> |
executeBestAction(Input input,
Collection<? extends IAction<Input,Output>> actions)
See
executeBestAction(String, Object, Collection), except that all namespaces will be considered. |
<Input,Output> |
executeBestAction(String nameSpacePattern,
Input input,
Collection<? extends IAction<Input,Output>> actions)
Evaluates all rules against the input and returns the result of the action associated with the rule having the highest priority.
|
<Input> String |
getBestOutcome(Input input)
See
getBestOutcome(String, Object), except that all namespaces will be considered. |
<Input> String |
getBestOutcome(String nameSpacePattern,
Input input)
Evaluates all rules against the input and returns the result of the outcome associated with the rule having the highest priority.
|
<Input> List<Rule> |
getMatchingRules(Input input)
See
getMatchingRules(String, Object), except that all namespaces will be considered. |
<Input> List<Rule> |
getMatchingRules(String nameSpacePattern,
Input input) |
protected void |
init(Collection<Rule> rules)
handles the initialisation
|
public static final String DEFAULT_INPUT_NAME
protected final boolean throwExceptionIfCompilationFails
protected final String inputName
protected final String[] javascriptFilesToLoad
protected final Integer poolSize
public Engine(Collection<Rule> rules, boolean throwExceptionIfCompilationFails) throws DuplicateNameException, CompileException, ParseException
rules - The rules which define the system.throwExceptionIfCompilationFails - if true, and a rule cannot be compiled, then a CompileException will be thrown.DuplicateNameException - thrown if any rules have the same name within a namespaceCompileException - thrown if throwExceptionIfCompilationFails is true, and a rule fails to compile, because its expression is invalidParseException - Thrown if a subrule which is referenced in a rule cannot be resolved.public Engine(Collection<Rule> rules, String inputName, boolean throwExceptionIfCompilationFails) throws DuplicateNameException, CompileException, ParseException
inputName - the name of the input in scripts, normally "input", but you can specify your own name here.DuplicateNameExceptionCompileExceptionParseExceptionprotected Engine(Collection<Rule> rules, String inputName, boolean throwExceptionIfCompilationFails, Integer poolSize, String[] javascriptFilesToLoad) throws DuplicateNameException, CompileException, ParseException
protected void init(Collection<Rule> rules) throws DuplicateNameException, CompileException, ParseException
protected void compile()
throws CompileException
CompileExceptionpublic <Input> String getBestOutcome(Input input) throws NoMatchingRuleFoundException
getBestOutcome(String, Object), except that all namespaces will be considered.Input - An input object to match against rules.NoMatchingRuleFoundExceptionpublic <Input> String getBestOutcome(String nameSpacePattern, Input input) throws NoMatchingRuleFoundException
Input - An input object to match against rules.nameSpacePattern - optional. if not null, then only rules with matching namespaces are evaluated.input - the Object containing all inputs to the expression language rule.NoMatchingRuleFoundException - If no matching rule was found. Rules must evaluate to true in order to be candidates.public <Input,Output> Output executeBestAction(Input input,
Collection<? extends IAction<Input,Output>> actions)
throws NoMatchingRuleFoundException,
NoActionFoundException,
DuplicateNameException
executeBestAction(String, Object, Collection), except that all namespaces will be considered.public <Input,Output> Output executeBestAction(String nameSpacePattern, Input input, Collection<? extends IAction<Input,Output>> actions) throws NoMatchingRuleFoundException, NoActionFoundException, DuplicateNameException
nameSpacePattern - optional. if not null, then only rules with matching namespaces are evaluated.input - the Object containing all inputs to the expression language rule.actions - a collection of actions containing one action per possible outcome. The action whose name is equal to the winning outcome will be executed.IAction with the same name as the winning rules outcome.NoMatchingRuleFoundException - If no matching rule was found. Rules must evaluate to true in order to be candidates.NoActionFoundException - If no action with a name matching the winning rules outcome was found.DuplicateNameException - if any actions have the same name.public <Input,Output> void executeAllActions(Input input,
Collection<? extends IAction<Input,Output>> actions)
throws NoMatchingRuleFoundException,
NoActionFoundException,
DuplicateNameException
executeAllActions(String, Object, Collection), except that all namespaces will be considered.
NOTE THAT THIS METHOD DISREGARDS ANY RETURN VALUES OF ACTIONS!!public <Input,Output> void executeAllActions(String nameSpacePattern, Input input, Collection<? extends IAction<Input,Output>> actions) throws NoMatchingRuleFoundException, NoActionFoundException, DuplicateNameException
Input - The type of input to the actions.Output - The type of output from the actions.nameSpacePattern - optional. if not null, then only rules with matching namespaces are evaluated.input - the Object containing all inputs to the expression language rule.actions - a collection of actions containing one action per possible outcome. The actions whose names is equal to the positive outcomes will be executed.NoMatchingRuleFoundException - If no matching rule was found. Rules must evaluate to true in order to be candidates.NoActionFoundException - If no action with a name matching the winning rules outcome was found.DuplicateNameException - if any actions have the same name.public <Input> List<Rule> getMatchingRules(Input input)
getMatchingRules(String, Object), except that all namespaces will be considered.public <Input> List<Rule> getMatchingRules(String nameSpacePattern, Input input)
Input - the type of inputnameSpacePattern - optional. if not null, then only rules with matching namespaces are evaluated.input - the Object containing all inputs to the expression language rule.Rule.getPriority(), with the highest priority rules first in the list.Copyright © 2017 Ant Kutschera. All rights reserved.