Search in sources :

Example 1 with RuleBuilder

use of org.jeasy.rules.core.RuleBuilder in project openremote by openremote.

the class RulesBuilder method build.

public Rule[] build() {
    List<Rule> rules = new ArrayList<>();
    for (Builder builder : builders) {
        Rule rule = new RuleBuilder().name(builder.name).description(builder.description).priority(builder.priority).when(facts -> {
            Object result;
            try {
                result = builder.condition.evaluate((RulesFacts) facts);
            } catch (Exception ex) {
                throw new RuntimeException("Error evaluating condition of rule '" + builder.name + "': " + ex.getMessage(), ex);
            }
            if (result instanceof Boolean) {
                return (boolean) result;
            } else {
                throw new IllegalArgumentException("Error evaluating condition of rule '" + builder.name + "': result is not boolean but " + result);
            }
        }).then(facts -> builder.action.execute((RulesFacts) facts)).build();
        rules.add(rule);
    }
    return rules.toArray(new Rule[rules.size()]);
}
Also used : List(java.util.List) Rule(org.jeasy.rules.api.Rule) RuleBuilder(org.jeasy.rules.core.RuleBuilder) ArrayList(java.util.ArrayList) RuleBuilder(org.jeasy.rules.core.RuleBuilder) ArrayList(java.util.ArrayList) Rule(org.jeasy.rules.api.Rule) RuleBuilder(org.jeasy.rules.core.RuleBuilder)

Example 2 with RuleBuilder

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

use of org.jeasy.rules.core.RuleBuilder in project easy-rules by j-easy.

the class Launcher method main.

public static void main(String[] args) {
    // define facts
    Facts facts = new Facts();
    facts.put("temperature", 30);
    // define rules
    Rule airConditioningRule = new RuleBuilder().name("air conditioning rule").when(itIsHot()).then(decreaseTemperature()).build();
    Rules rules = new Rules();
    rules.register(airConditioningRule);
    // fire rules on known facts
    RulesEngine rulesEngine = new InferenceRulesEngine();
    rulesEngine.fire(rules, facts);
}
Also used : InferenceRulesEngine(org.jeasy.rules.core.InferenceRulesEngine) InferenceRulesEngine(org.jeasy.rules.core.InferenceRulesEngine) RulesEngine(org.jeasy.rules.api.RulesEngine) Rule(org.jeasy.rules.api.Rule) RuleBuilder(org.jeasy.rules.core.RuleBuilder) Rules(org.jeasy.rules.api.Rules) Facts(org.jeasy.rules.api.Facts)

Aggregations

RuleBuilder (org.jeasy.rules.core.RuleBuilder)3 Rule (org.jeasy.rules.api.Rule)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)1 Action (org.jeasy.rules.api.Action)1 Condition (org.jeasy.rules.api.Condition)1 Facts (org.jeasy.rules.api.Facts)1 Rules (org.jeasy.rules.api.Rules)1 RulesEngine (org.jeasy.rules.api.RulesEngine)1 InferenceRulesEngine (org.jeasy.rules.core.InferenceRulesEngine)1