Search in sources :

Example 6 with RulesEngine

use of org.jeasy.rules.api.RulesEngine in project easy-rules by j-easy.

the class AnnotationInheritanceTest method annotationsShouldBeInherited.

@Test
public void annotationsShouldBeInherited() throws Exception {
    // Given
    MyChildRule myChildRule = new MyChildRule();
    rules.register(myChildRule);
    // When
    RulesEngine rulesEngine = new DefaultRulesEngine();
    rulesEngine.fire(rules, facts);
    // Then
    assertThat(myChildRule.isExecuted()).isTrue();
}
Also used : RulesEngine(org.jeasy.rules.api.RulesEngine) Test(org.junit.Test)

Example 7 with RulesEngine

use of org.jeasy.rules.api.RulesEngine in project easy-rules by j-easy.

the class FactInjectionTest method whenFactTypeDoesNotMatchParameterType_thenShouldThrowRuntimeException.

@Test(expected = RuntimeException.class)
public void whenFactTypeDoesNotMatchParameterType_thenShouldThrowRuntimeException() throws Exception {
    // Given
    Facts facts = new Facts();
    facts.put("age", "foo");
    Rules rules = new Rules(new AgeRule());
    RulesEngine rulesEngine = new DefaultRulesEngine();
    // When
    rulesEngine.fire(rules, facts);
// Then
// expected exception
}
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 8 with RulesEngine

use of org.jeasy.rules.api.RulesEngine in project easy-rules by j-easy.

the class FactInjectionTest method whenADeclaredFactIsMissingInEvaluateMethod_thenTheRuleShouldNotBeExecuted.

@Test
public void whenADeclaredFactIsMissingInEvaluateMethod_thenTheRuleShouldNotBeExecuted() throws Exception {
    // Given
    Facts facts = new Facts();
    AgeRule ageRule = new AgeRule();
    Rules rules = new Rules(ageRule);
    RulesEngine rulesEngine = new DefaultRulesEngine();
    // When
    rulesEngine.fire(rules, facts);
    // Then
    assertThat(ageRule.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 9 with RulesEngine

use of org.jeasy.rules.api.RulesEngine in project easy-rules by j-easy.

the class FactInjectionTest method rulesShouldBeExecutedWhenFactsAreCorrectlyInjected.

@Test
public void rulesShouldBeExecutedWhenFactsAreCorrectlyInjected() throws Exception {
    // Given
    Facts facts = new Facts();
    facts.put("rain", true);
    facts.put("age", 18);
    WeatherRule weatherRule = new WeatherRule();
    AgeRule ageRule = new AgeRule();
    Rules rules = new Rules(weatherRule, ageRule);
    // When
    RulesEngine rulesEngine = new DefaultRulesEngine();
    rulesEngine.fire(rules, facts);
    // Then
    assertThat(ageRule.isExecuted()).isTrue();
    assertThat(weatherRule.isExecuted()).isTrue();
}
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 10 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) {
    // define facts
    Facts facts = new Facts();
    facts.put("rain", true);
    // define rules
    WeatherRule weatherRule = new WeatherRule();
    Rules rules = new Rules();
    rules.register(weatherRule);
    // fire rules on known facts
    RulesEngine rulesEngine = new DefaultRulesEngine();
    rulesEngine.fire(rules, facts);
}
Also used : DefaultRulesEngine(org.jeasy.rules.core.DefaultRulesEngine) DefaultRulesEngine(org.jeasy.rules.core.DefaultRulesEngine) RulesEngine(org.jeasy.rules.api.RulesEngine) Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts)

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