Search in sources :

Example 11 with Facts

use of org.jeasy.rules.api.Facts 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 12 with Facts

use of org.jeasy.rules.api.Facts 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 13 with Facts

use of org.jeasy.rules.api.Facts 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 14 with Facts

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

the class NullFactAnnotationParameterTest method testNullFact.

@Test
public void testNullFact() {
    Rules rules = new Rules();
    rules.register(new AnnotatedParametersRule());
    Facts facts = new Facts();
    facts.put("fact1", new Object());
    facts.put("fact2", null);
    Map<org.jeasy.rules.api.Rule, Boolean> results = rulesEngine.check(rules, facts);
    for (boolean b : results.values()) {
        Assert.assertTrue(b);
    }
}
Also used : Rule(org.jeasy.rules.annotation.Rule) Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts) Test(org.junit.Test)

Example 15 with Facts

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

the class NullFactAnnotationParameterTest method testMissingFact.

@Test
public void testMissingFact() {
    Rules rules = new Rules();
    rules.register(new AnnotatedParametersRule());
    Facts facts = new Facts();
    facts.put("fact1", new Object());
    Map<org.jeasy.rules.api.Rule, Boolean> results = rulesEngine.check(rules, facts);
    for (boolean b : results.values()) {
        Assert.assertFalse(b);
    }
}
Also used : Rule(org.jeasy.rules.annotation.Rule) Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts) Test(org.junit.Test)

Aggregations

Facts (org.jeasy.rules.api.Facts)23 Rules (org.jeasy.rules.api.Rules)16 RulesEngine (org.jeasy.rules.api.RulesEngine)13 Test (org.junit.Test)13 DefaultRulesEngine (org.jeasy.rules.core.DefaultRulesEngine)5 Method (java.lang.reflect.Method)2 Rule (org.jeasy.rules.annotation.Rule)2 Action (org.jeasy.rules.api.Action)2 Condition (org.jeasy.rules.api.Condition)2 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 Before (org.junit.Before)1