use of org.jeasy.rules.api.Rules in project easy-rules by j-easy.
the class Launcher method main.
public static void main(String[] args) throws FileNotFoundException {
// create a person instance (fact)
Person tom = new Person("Tom", 14);
Facts facts = new Facts();
facts.put("person", tom);
// create rules
MVELRule 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);");
MVELRule alcoholRule = MVELRuleFactory.createRuleFrom(new File("src/main/java/org/jeasy/rules/tutorials/shop/alcohol-rule.yml"));
// create a rule set
Rules rules = new Rules();
rules.register(ageRule);
rules.register(alcoholRule);
// create a default rules engine and fire rules on known facts
RulesEngine rulesEngine = new DefaultRulesEngine();
System.out.println("Tom: Hi! can I have some Vodka please?");
rulesEngine.fire(rules, facts);
}
use of org.jeasy.rules.api.Rules in project easy-rules by j-easy.
the class SuspiciousRequestFilter method init.
@Override
public void init(FilterConfig filterConfig) throws ServletException {
rulesEngine = new DefaultRulesEngine();
rules = new Rules();
rules.register(new SuspiciousRequestRule());
}
use of org.jeasy.rules.api.Rules in project easy-rules by j-easy.
the class AbstractTest method setup.
@Before
public void setup() throws Exception {
facts = new Facts();
facts.put("fact1", fact1);
facts.put("fact2", fact2);
rules = new Rules();
rulesEngine = new DefaultRulesEngine();
}
use of org.jeasy.rules.api.Rules in project easy-rules by j-easy.
the class FactInjectionTest method whenADeclaredFactIsMissingInExecuteMethod_thenTheRuleShouldNotBeExecuted.
@Test
public void whenADeclaredFactIsMissingInExecuteMethod_thenTheRuleShouldNotBeExecuted() throws Exception {
// Given
Facts facts = new Facts();
AnotherDummyRule rule = new AnotherDummyRule();
Rules rules = new Rules(rule);
RulesEngine rulesEngine = new DefaultRulesEngine();
// When
rulesEngine.fire(rules, facts);
// Then
assertThat(rule.isExecuted()).isFalse();
}
use of org.jeasy.rules.api.Rules in project easy-rules by j-easy.
the class FactInjectionTest method declaredFactsShouldBeCorrectlyInjectedByNameOrType.
@Test
public void declaredFactsShouldBeCorrectlyInjectedByNameOrType() throws Exception {
// Given
Object fact1 = new Object();
Object fact2 = new Object();
Facts facts = new Facts();
facts.put("fact1", fact1);
facts.put("fact2", fact2);
DummyRule rule = new DummyRule();
Rules rules = new Rules(rule);
// When
RulesEngine rulesEngine = new DefaultRulesEngine();
rulesEngine.fire(rules, facts);
// Then
assertThat(rule.getFact1()).isSameAs(fact1);
assertThat(rule.getFact2()).isSameAs(fact2);
assertThat(rule.getFacts()).isSameAs(facts);
}
Aggregations