- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.1k
 
expression language support
Easy Rules provides support for defining rules with MVEL and SpEL.
MVEL and SpEL support is provided through the easy-rules-mvel and easy-rules-spel modules respectively. These modules provide APIs to define conditions, actions and rules.
Conditions, actions and rules are represented respectively by the MVELCondition/SpELCondition, MVELAction/SpELAction and MVELRule/SpELRule classes. The following is an example of rule definition using MVEL (similar API for SpEL as well):
Rule ageRule = new MVELRule()
        .name("age rule")
        .description("Check if person's age is > 18 and marks the person as adult")
        .priority(1)
        .when("person.age > 18")
        .then("person.setAdult(true);");You can also define a rule in a descriptor file and use the MVELRuleFactory/SpELRuleFactory to create a rule from the descriptor. Here is an example of a rule defined in YAML format in alcohol-rule.yml:
name: "alcohol rule"
description: "children are not allowed to buy alcohol"
priority: 2
condition: "person.isAdult() == false"
actions:
  - "System.out.println(\"Shop: Sorry, you are not allowed to buy alcohol\");"MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader());
MVELRule alcoholRule = ruleFactory.createRule(new FileReader("alcohol-rule.yml"));You can also create multiple rules at the same time from a single file. For instance, here is a rules.yml file:
---
name: adult rule
description: when age is greater then 18, then mark as adult
priority: 1
condition: "person.age > 18"
actions:
  - "person.setAdult(true);"
---
name: weather rule
description: when it rains, then take an umbrella
priority: 2
condition: "rain == true"
actions:
  - "System.out.println(\"It rains, take an umbrella!\");"To load these rules into a Rules object, you can use the following snippet:
MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader());
Rules rules = ruleFactory.createRules(new FileReader("rules.yml"));You can find a complete example of how to use Easy Rules with MVEL in the shop tutorial.
As of version 3.3, Easy Rules provides support to load rules from a JSON descriptor. You can use the JsonRuleDefinitionReader with the MVELRuleFactory as follows:
MVELRuleFactory ruleFactory = new MVELRuleFactory(new JsonRuleDefinitionReader());Please note that rules descriptors should be defined as an array of JSON object (each object defines a rule) even if a single rule is defined. Here is the previous alcohol-rule expressed in JSON:
[
  {
    "name": "alcohol rule",
    "description": "children are not allowed to buy alcohol",
    "priority": 2,
    "condition": "person.isAdult() == false",
    "actions": [
      "System.out.println(\"Shop: Sorry, you are not allowed to buy alcohol\");"
    ]
  }
]Easy Rules is created by Mahmoud Ben Hassine with the help of some awesome contributors
- 
Introduction
 - 
User guide
 - 
Tutorials
 - 
Get involved