use of org.eclipse.smarthome.automation.core.util.RuleBuilder in project smarthome by eclipse.
the class ScriptedAutomationManager method addRule.
public Rule addRule(Rule element) {
RuleBuilder builder = RuleBuilder.create(element.getUID());
String name = element.getName();
if (name == null || name.isEmpty()) {
name = element.getClass().getSimpleName();
if (name.contains("$")) {
name = name.substring(0, name.indexOf('$'));
}
}
builder.withName(name).withDescription(element.getDescription()).withTags(element.getTags());
// used for numbering the modules of the rule
int moduleIndex = 1;
try {
ArrayList<Condition> conditions = new ArrayList<>();
for (Condition cond : element.getConditions()) {
Condition toAdd = cond;
if (cond.getId().isEmpty()) {
toAdd = ModuleBuilder.createCondition().withId(Integer.toString(moduleIndex++)).withTypeUID(cond.getTypeUID()).withConfiguration(cond.getConfiguration()).withInputs(cond.getInputs()).build();
}
conditions.add(toAdd);
}
builder.withConditions(conditions);
} catch (Exception ex) {
// conditions are optional
}
try {
ArrayList<Trigger> triggers = new ArrayList<>();
for (Trigger trigger : element.getTriggers()) {
Trigger toAdd = trigger;
if (trigger.getId().isEmpty()) {
toAdd = ModuleBuilder.createTrigger().withId(Integer.toString(moduleIndex++)).withTypeUID(trigger.getTypeUID()).withConfiguration(trigger.getConfiguration()).build();
}
triggers.add(toAdd);
}
builder.withTriggers(triggers);
} catch (Exception ex) {
// triggers are optional
}
ArrayList<Action> actions = new ArrayList<>();
actions.addAll(element.getActions());
if (element instanceof SimpleRuleActionHandler) {
String privId = addPrivateActionHandler(new SimpleRuleActionHandlerDelegate((SimpleRuleActionHandler) element));
Action scriptedAction = ActionBuilder.create().withId(Integer.toString(moduleIndex++)).withTypeUID("jsr223.ScriptedAction").withConfiguration(new Configuration()).build();
scriptedAction.getConfiguration().put("privId", privId);
actions.add(scriptedAction);
}
builder.withActions(actions);
Rule rule = builder.build();
ruleRegistryDelegate.add(rule);
return rule;
}
Aggregations