Search in sources :

Example 1 with Action

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

Aggregations

ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)1 Action (org.jeasy.rules.api.Action)1 Condition (org.jeasy.rules.api.Condition)1 RuleBuilder (org.jeasy.rules.core.RuleBuilder)1