Search in sources :

Example 1 with RulesEngine

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);
}
Also used : DefaultRulesEngine(org.jeasy.rules.core.DefaultRulesEngine) DefaultRulesEngine(org.jeasy.rules.core.DefaultRulesEngine) RulesEngine(org.jeasy.rules.api.RulesEngine) MVELRule(org.jeasy.rules.mvel.MVELRule) File(java.io.File) Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts)

Example 2 with RulesEngine

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();
}
Also used : RulesEngine(org.jeasy.rules.api.RulesEngine) Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts) Test(org.junit.Test)

Example 3 with RulesEngine

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);
}
Also used : RulesEngine(org.jeasy.rules.api.RulesEngine) Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts) Test(org.junit.Test)

Example 4 with RulesEngine

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());
}
Also used : RulesEngine(org.jeasy.rules.api.RulesEngine) Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts) Test(org.junit.Test)

Example 5 with RulesEngine

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();
}
Also used : RulesEngine(org.jeasy.rules.api.RulesEngine) Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts) Test(org.junit.Test)

Aggregations

RulesEngine (org.jeasy.rules.api.RulesEngine)14 Facts (org.jeasy.rules.api.Facts)13 Rules (org.jeasy.rules.api.Rules)13 Test (org.junit.Test)8 DefaultRulesEngine (org.jeasy.rules.core.DefaultRulesEngine)5 File (java.io.File)1 Rule (org.jeasy.rules.api.Rule)1 InferenceRulesEngine (org.jeasy.rules.core.InferenceRulesEngine)1 RuleBuilder (org.jeasy.rules.core.RuleBuilder)1 RulesEngineParameters (org.jeasy.rules.core.RulesEngineParameters)1 MVELRule (org.jeasy.rules.mvel.MVELRule)1