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()]);
}
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());
}
}
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);
}
Aggregations