use of org.openhab.core.automation.Action in project openhab-core by openhab.
the class RuleEngineTest method testRuleActions.
/**
* test rule actions
*/
@Test
public void testRuleActions() {
RuleImpl rule1 = createRule();
List<Action> actions = new ArrayList<>(rule1.getActions());
ruleRegistry.add(rule1);
Rule rule1Get = ruleEngine.getRule("rule1");
List<Action> actionsGet = rule1Get.getActions();
assertNotNull(actionsGet, "Null actions list");
assertEquals(1, actionsGet.size(), "Empty actions list");
assertEquals(actionsGet, rule1Get.getActions(), "Returned actions list should not be a copy");
actions.add(ModuleBuilder.createAction().withId("actionId2").withTypeUID("typeUID2").build());
rule1.setActions(actions);
ruleEngine.addRule(rule1);
rule1Get = ruleEngine.getRule("rule1");
List<Action> actionsGet2 = rule1Get.getActions();
assertNotNull(actionsGet2, "Null actions list");
assertEquals(2, actionsGet2.size(), "Action was not added to the rule's list of actions");
assertNotNull(rule1Get.getModule("actionId2"), "RuleImpl action with wrong id is returned");
actions.add(ModuleBuilder.createAction().withId("actionId3").withTypeUID("typeUID3").build());
// ruleEngine.update will update the RuleImpl2.moduleMap with the new module
ruleEngine.addRule(rule1);
rule1Get = ruleEngine.getRule("rule1");
List<Action> actionsGet3 = rule1Get.getActions();
assertNotNull(actionsGet3, "Null actions list");
assertEquals(3, actionsGet3.size(), "Action was not added to the rule's list of actions");
assertNotNull(ruleEngine.getRule("rule1").getModule("actionId3"), "RuleImpl modules map was not updated");
}
use of org.openhab.core.automation.Action in project openhab-core by openhab.
the class CompositeActionHandler method execute.
/**
* The method calls handlers of child action, collect their outputs and sets the output of the parent action.
*
* @see org.openhab.core.automation.handler.ActionHandler#execute(java.util.Map)
*/
@Override
@Nullable
public Map<String, Object> execute(Map<String, Object> context) {
final Map<String, Object> result = new HashMap<>();
final List<Action> children = getChildren();
final Map<String, Object> compositeContext = getCompositeContext(context);
for (Action child : children) {
ActionHandler childHandler = moduleHandlerMap.get(child);
Map<String, Object> childContext = Collections.unmodifiableMap(getChildContext(child, compositeContext));
Map<String, Object> childResults = childHandler == null ? null : childHandler.execute(childContext);
if (childResults != null) {
for (Entry<String, Object> childResult : childResults.entrySet()) {
String childOuputName = child.getId() + "." + childResult.getKey();
Output output = compositeOutputs.get(childOuputName);
if (output != null) {
String childOuputRef = output.getReference();
if (childOuputRef != null && childOuputRef.length() > childOuputName.length()) {
childOuputRef = childOuputRef.substring(childOuputName.length());
result.put(output.getName(), ReferenceResolver.resolveComplexDataReference(childResult.getValue(), childOuputRef));
} else {
result.put(output.getName(), childResult.getValue());
}
}
}
}
}
return !result.isEmpty() ? result : null;
}
use of org.openhab.core.automation.Action in project openhab-core by openhab.
the class CompositeModuleHandlerFactory method internalCreate.
@Override
@Nullable
public ModuleHandler internalCreate(Module module, String ruleUID) {
ModuleHandler handler = null;
String moduleType = module.getTypeUID();
ModuleType mt = mtRegistry.get(moduleType);
if (mt instanceof CompositeTriggerType) {
List<Trigger> childModules = ((CompositeTriggerType) mt).getChildren();
LinkedHashMap<Trigger, @Nullable TriggerHandler> mapModuleToHandler = getChildHandlers(module.getId(), module.getConfiguration(), childModules, ruleUID);
if (mapModuleToHandler != null) {
handler = new CompositeTriggerHandler((Trigger) module, (CompositeTriggerType) mt, mapModuleToHandler, ruleUID);
}
} else if (mt instanceof CompositeConditionType) {
List<Condition> childModules = ((CompositeConditionType) mt).getChildren();
LinkedHashMap<Condition, @Nullable ConditionHandler> mapModuleToHandler = getChildHandlers(module.getId(), module.getConfiguration(), childModules, ruleUID);
if (mapModuleToHandler != null) {
handler = new CompositeConditionHandler((Condition) module, (CompositeConditionType) mt, mapModuleToHandler, ruleUID);
}
} else if (mt instanceof CompositeActionType) {
List<Action> childModules = ((CompositeActionType) mt).getChildren();
LinkedHashMap<Action, @Nullable ActionHandler> mapModuleToHandler = getChildHandlers(module.getId(), module.getConfiguration(), childModules, ruleUID);
if (mapModuleToHandler != null) {
handler = new CompositeActionHandler((Action) module, (CompositeActionType) mt, mapModuleToHandler, ruleUID);
}
}
if (handler != null) {
logger.debug("Set module handler: {} -> {} of rule {}.", module.getId(), handler.getClass().getSimpleName() + "(" + moduleType + ")", ruleUID);
} else {
logger.debug("Not found module handler {} for moduleType {} of rule {}.", module.getId(), moduleType, ruleUID);
}
return handler;
}
use of org.openhab.core.automation.Action in project openhab-core by openhab.
the class TemplateResourceBundleProvider method getPerLocale.
/**
* This method is used to localize the {@link Template}s.
*
* @param element is the {@link Template} that must be localized.
* @param locale represents a specific geographical, political, or cultural region.
* @return the localized {@link Template}.
*/
@Nullable
private RuleTemplate getPerLocale(@Nullable RuleTemplate defTemplate, @Nullable Locale locale) {
if (locale == null || defTemplate == null) {
return defTemplate;
}
String uid = defTemplate.getUID();
Bundle bundle = getBundle(uid);
if (bundle != null && defTemplate instanceof RuleTemplate) {
String llabel = ruleTemplateI18nUtil.getLocalizedRuleTemplateLabel(bundle, uid, defTemplate.getLabel(), locale);
String ldescription = ruleTemplateI18nUtil.getLocalizedRuleTemplateDescription(bundle, uid, defTemplate.getDescription(), locale);
List<ConfigDescriptionParameter> lconfigDescriptions = getLocalizedConfigurationDescription(defTemplate.getConfigurationDescriptions(), bundle, uid, RuleTemplateI18nUtil.RULE_TEMPLATE, locale);
List<Action> lactions = moduleI18nUtil.getLocalizedModules(defTemplate.getActions(), bundle, uid, RuleTemplateI18nUtil.RULE_TEMPLATE, locale);
List<Condition> lconditions = moduleI18nUtil.getLocalizedModules(defTemplate.getConditions(), bundle, uid, RuleTemplateI18nUtil.RULE_TEMPLATE, locale);
List<Trigger> ltriggers = moduleI18nUtil.getLocalizedModules(defTemplate.getTriggers(), bundle, uid, RuleTemplateI18nUtil.RULE_TEMPLATE, locale);
return new RuleTemplate(uid, llabel, ldescription, defTemplate.getTags(), ltriggers, lconditions, lactions, lconfigDescriptions, defTemplate.getVisibility());
}
return null;
}
use of org.openhab.core.automation.Action in project openhab-core by openhab.
the class ModuleTypeI18nServiceImpl method createLocalizedActionType.
/**
* Utility method for localization of ActionTypes.
*
* @param at is an ActionType for localization.
* @param bundle the bundle providing localization resources.
* @param moduleTypeUID is an ActionType uid.
* @param locale represents a specific geographical, political, or cultural region.
* @param lconfigDescriptions are ActionType localized config descriptions.
* @param llabel is an ActionType localized label.
* @param ldescription is an ActionType localized description.
* @return localized ActionType.
*/
@Nullable
private ActionType createLocalizedActionType(ActionType at, Bundle bundle, String moduleTypeUID, @Nullable Locale locale, @Nullable List<ConfigDescriptionParameter> lconfigDescriptions, @Nullable String llabel, @Nullable String ldescription) {
List<Input> inputs = moduleTypeI18nUtil.getLocalizedInputs(at.getInputs(), bundle, moduleTypeUID, locale);
List<Output> outputs = moduleTypeI18nUtil.getLocalizedOutputs(at.getOutputs(), bundle, moduleTypeUID, locale);
ActionType lat = null;
if (at instanceof CompositeActionType) {
List<Action> modules = moduleI18nUtil.getLocalizedModules(((CompositeActionType) at).getChildren(), bundle, moduleTypeUID, ModuleTypeI18nUtil.MODULE_TYPE, locale);
lat = new CompositeActionType(moduleTypeUID, lconfigDescriptions, llabel, ldescription, at.getTags(), at.getVisibility(), inputs, outputs, modules);
} else {
lat = new ActionType(moduleTypeUID, lconfigDescriptions, llabel, ldescription, at.getTags(), at.getVisibility(), inputs, outputs);
}
return lat;
}
Aggregations