Search in sources :

Example 16 with Action

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");
}
Also used : Action(org.openhab.core.automation.Action) ArrayList(java.util.ArrayList) Rule(org.openhab.core.automation.Rule) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 17 with Action

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;
}
Also used : Action(org.openhab.core.automation.Action) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Output(org.openhab.core.automation.type.Output) ActionHandler(org.openhab.core.automation.handler.ActionHandler) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 18 with Action

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;
}
Also used : Action(org.openhab.core.automation.Action) TriggerHandler(org.openhab.core.automation.handler.TriggerHandler) CompositeConditionType(org.openhab.core.automation.type.CompositeConditionType) LinkedHashMap(java.util.LinkedHashMap) ModuleHandler(org.openhab.core.automation.handler.ModuleHandler) CompositeTriggerType(org.openhab.core.automation.type.CompositeTriggerType) ModuleType(org.openhab.core.automation.type.ModuleType) Trigger(org.openhab.core.automation.Trigger) ArrayList(java.util.ArrayList) List(java.util.List) CompositeActionType(org.openhab.core.automation.type.CompositeActionType) ActionHandler(org.openhab.core.automation.handler.ActionHandler) Nullable(org.eclipse.jdt.annotation.Nullable) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 19 with Action

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;
}
Also used : Condition(org.openhab.core.automation.Condition) Action(org.openhab.core.automation.Action) Trigger(org.openhab.core.automation.Trigger) Bundle(org.osgi.framework.Bundle) RuleTemplate(org.openhab.core.automation.template.RuleTemplate) ConfigDescriptionParameter(org.openhab.core.config.core.ConfigDescriptionParameter) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 20 with Action

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;
}
Also used : Input(org.openhab.core.automation.type.Input) Action(org.openhab.core.automation.Action) ActionType(org.openhab.core.automation.type.ActionType) CompositeActionType(org.openhab.core.automation.type.CompositeActionType) Output(org.openhab.core.automation.type.Output) CompositeActionType(org.openhab.core.automation.type.CompositeActionType) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

Action (org.openhab.core.automation.Action)40 Trigger (org.openhab.core.automation.Trigger)27 Configuration (org.openhab.core.config.core.Configuration)22 Rule (org.openhab.core.automation.Rule)20 HashMap (java.util.HashMap)19 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)16 Test (org.junit.jupiter.api.Test)15 Condition (org.openhab.core.automation.Condition)15 ArrayList (java.util.ArrayList)14 Random (java.util.Random)13 Nullable (org.eclipse.jdt.annotation.Nullable)12 Event (org.openhab.core.events.Event)12 EventSubscriber (org.openhab.core.events.EventSubscriber)12 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)12 RuleRegistry (org.openhab.core.automation.RuleRegistry)9 RuleStatusInfoEvent (org.openhab.core.automation.events.RuleStatusInfoEvent)9 EventPublisher (org.openhab.core.events.EventPublisher)9 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)8 Item (org.openhab.core.items.Item)8 Logger (org.slf4j.Logger)8