Search in sources :

Example 6 with Facts

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

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

the class RuleProxy method evaluateMethod.

private Object evaluateMethod(final Object[] args) throws IllegalAccessException, InvocationTargetException {
    Facts facts = (Facts) args[0];
    Method conditionMethod = getConditionMethod();
    try {
        List<Object> actualParameters = getActualParameters(conditionMethod, facts);
        // validated upfront
        return conditionMethod.invoke(target, actualParameters.toArray());
    } catch (NoSuchFactException e) {
        LOGGER.info("Rule '{}' has been evaluated to false due to a declared but missing fact '{}' in {}", getTargetClass().getName(), e.getMissingFact(), facts);
        return false;
    } catch (IllegalArgumentException e) {
        String error = "Types of injected facts in method '%s' in rule '%s' do not match parameters types";
        throw new RuntimeException(format(error, conditionMethod.getName(), getTargetClass().getName()), e);
    }
}
Also used : Method(java.lang.reflect.Method) Facts(org.jeasy.rules.api.Facts)

Example 8 with Facts

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

the class MVELActionTest method testMVELFunctionExecution.

@Test
public void testMVELFunctionExecution() throws Exception {
    // given
    Action printAction = new MVELAction("def hello() { System.out.println(\"Hello from MVEL!\"); }; hello();");
    Facts facts = new Facts();
    // when
    printAction.execute(facts);
    // then
    assertThat(systemOutRule.getLog()).contains("Hello from MVEL!");
}
Also used : Action(org.jeasy.rules.api.Action) Facts(org.jeasy.rules.api.Facts) Test(org.junit.Test)

Example 9 with Facts

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

the class MVELConditionTest method whenDeclaredFactIsNotPresent_thenShouldReturnFalse.

@Test
public void whenDeclaredFactIsNotPresent_thenShouldReturnFalse() throws Exception {
    // given
    Condition isHot = new MVELCondition("temperature > 30");
    Facts facts = new Facts();
    // when
    boolean evaluationResult = isHot.evaluate(facts);
    // then
    assertThat(evaluationResult).isFalse();
}
Also used : Condition(org.jeasy.rules.api.Condition) Facts(org.jeasy.rules.api.Facts) Test(org.junit.Test)

Example 10 with Facts

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

the class RuleProxy method executeMethod.

private Object executeMethod(final Object[] args) throws IllegalAccessException, InvocationTargetException {
    Facts facts = (Facts) args[0];
    for (ActionMethodOrderBean actionMethodBean : getActionMethodBeans()) {
        Method actionMethod = actionMethodBean.getMethod();
        List<Object> actualParameters = getActualParameters(actionMethod, facts);
        actionMethod.invoke(target, actualParameters.toArray());
    }
    return null;
}
Also used : Method(java.lang.reflect.Method) Facts(org.jeasy.rules.api.Facts)

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