Search in sources :

Example 6 with Rules

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

use of org.jeasy.rules.api.Rules 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)

Example 8 with Rules

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

the class BasicRuleTest method testSortSequence.

@Test
public void testSortSequence() {
    FirstRule rule1 = new FirstRule();
    SecondRule rule2 = new SecondRule();
    ThirdRule rule3 = new ThirdRule();
    rules = new Rules(rule1, rule2, rule3);
    rulesEngine.check(rules, facts);
    assertThat(rules).containsSequence(rule1, rule3, rule2);
}
Also used : Rules(org.jeasy.rules.api.Rules) Test(org.junit.Test)

Example 9 with Rules

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

use of org.jeasy.rules.api.Rules 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)

Aggregations

Rules (org.jeasy.rules.api.Rules)18 Facts (org.jeasy.rules.api.Facts)16 RulesEngine (org.jeasy.rules.api.RulesEngine)13 Test (org.junit.Test)10 DefaultRulesEngine (org.jeasy.rules.core.DefaultRulesEngine)6 Rule (org.jeasy.rules.annotation.Rule)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