Search in sources :

Example 1 with Condition

use of org.jeasy.rules.api.Condition in project openremote by openremote.

the class RulesetDeployment method registerRulesJavascript.

/**
 * Marshal the JavaScript rules array into {@link Rule} instances.
 */
protected void registerRulesJavascript(ScriptObjectMirror scriptRules) {
    if (scriptRules == null || !scriptRules.isArray()) {
        throw new IllegalArgumentException("No 'rules' array defined in ruleset");
    }
    Collection<Object> rulesObjects = scriptRules.values();
    for (Object rulesObject : rulesObjects) {
        ScriptObjectMirror rule = (ScriptObjectMirror) rulesObject;
        String name;
        if (!rule.containsKey("name")) {
            throw new IllegalArgumentException("Missing 'name' in rule definition");
        }
        try {
            name = (String) rule.getMember("name");
        } catch (ClassCastException ex) {
            throw new IllegalArgumentException("Defined 'name' of rule is not a string");
        }
        String description;
        try {
            description = rule.containsKey("description") ? (String) rule.getMember("description") : null;
        } catch (ClassCastException ex) {
            throw new IllegalArgumentException("Defined 'description' is not a string in rule: " + name);
        }
        int priority;
        try {
            priority = rule.containsKey("priority") ? (int) rule.getMember("priority") : DEFAULT_RULE_PRIORITY;
        } catch (ClassCastException ex) {
            throw new IllegalArgumentException("Defined 'priority' is not a number in rule: " + name);
        }
        if (!rule.containsKey("when")) {
            throw new IllegalArgumentException("Missing 'when' function in rule: " + name);
        }
        Condition when;
        try {
            ScriptObjectMirror whenMirror = (ScriptObjectMirror) rule.getMember("when");
            if (!whenMirror.isFunction()) {
                throw new IllegalArgumentException("Defined 'when' is not a function in rule: " + name);
            }
            when = whenMirror.to(Condition.class);
        } catch (ClassCastException ex) {
            throw new IllegalArgumentException("Defined 'when' is not a function in rule: " + name);
        }
        Action then;
        try {
            ScriptObjectMirror thenMirror = (ScriptObjectMirror) rule.getMember("then");
            if (!thenMirror.isFunction()) {
                throw new IllegalArgumentException("Defined 'then' is not a function in rule: " + name);
            }
            then = thenMirror.to(Action.class);
        } catch (ClassCastException ex) {
            throw new IllegalArgumentException("Defined 'then' is not a function in rule: " + name);
        }
        RulesEngine.LOG.info("Registering rule: " + name);
        rules.register(new RuleBuilder().name(name).description(description).priority(priority).when(when).then(then).build());
    }
}
Also used : Condition(org.jeasy.rules.api.Condition) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) Action(org.jeasy.rules.api.Action) RuleBuilder(org.jeasy.rules.core.RuleBuilder)

Example 2 with Condition

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

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

the class MVELConditionTest method testMVELExpressionEvaluation.

@Test
public void testMVELExpressionEvaluation() throws Exception {
    // given
    Condition isAdult = new MVELCondition("person.age > 18");
    Facts facts = new Facts();
    facts.put("person", new Person("foo", 20));
    // when
    boolean evaluationResult = isAdult.evaluate(facts);
    // then
    assertThat(evaluationResult).isTrue();
}
Also used : Condition(org.jeasy.rules.api.Condition) Facts(org.jeasy.rules.api.Facts) Test(org.junit.Test)

Aggregations

Condition (org.jeasy.rules.api.Condition)3 Facts (org.jeasy.rules.api.Facts)2 Test (org.junit.Test)2 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)1 Action (org.jeasy.rules.api.Action)1 RuleBuilder (org.jeasy.rules.core.RuleBuilder)1