use of org.jeasy.rules.api.RulesEngine 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.RulesEngine 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.RulesEngine 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);
}
use of org.jeasy.rules.api.RulesEngine in project easy-rules by j-easy.
the class InferenceRulesEngineTest method testCandidateOrdering.
@Test
public void testCandidateOrdering() throws Exception {
// Given
Facts facts = new Facts();
facts.put("foo", true);
facts.put("bar", true);
DummyRule dummyRule = new DummyRule();
AnotherDummyRule anotherDummyRule = new AnotherDummyRule();
Rules rules = new Rules(dummyRule, anotherDummyRule);
RulesEngine rulesEngine = new InferenceRulesEngine();
// When
rulesEngine.fire(rules, facts);
// Then
assertThat(dummyRule.isExecuted()).isTrue();
assertThat(anotherDummyRule.isExecuted()).isTrue();
assertThat(dummyRule.getTimestamp()).isLessThanOrEqualTo(anotherDummyRule.getTimestamp());
}
use of org.jeasy.rules.api.RulesEngine in project easy-rules by j-easy.
the class InferenceRulesEngineTest method testCandidateSelection.
@Test
public void testCandidateSelection() throws Exception {
// Given
Facts facts = new Facts();
facts.put("foo", true);
DummyRule dummyRule = new DummyRule();
AnotherDummyRule anotherDummyRule = new AnotherDummyRule();
Rules rules = new Rules(dummyRule, anotherDummyRule);
RulesEngine rulesEngine = new InferenceRulesEngine();
// When
rulesEngine.fire(rules, facts);
// Then
assertThat(dummyRule.isExecuted()).isTrue();
assertThat(anotherDummyRule.isExecuted()).isFalse();
}
Aggregations