Search in sources :

Example 1 with Condition

use of org.eclipse.smarthome.automation.Condition in project smarthome by eclipse.

the class ReferenceResolverUtilTest method testModuleInputResolving.

@Test
public void testModuleInputResolving() {
    // test Composite child Module(condition) context
    Module condition = new Condition(null, null, null, compositeChildModuleInputsReferences);
    Map<String, Object> conditionContext = ReferenceResolverUtil.getCompositeChildContext(condition, context);
    Assert.assertEquals(conditionContext, expectedCompositeChildModuleContext);
    // test Composite child Module(action) context
    Module action = new Action(null, null, null, compositeChildModuleInputsReferences);
    Map<String, Object> actionContext = ReferenceResolverUtil.getCompositeChildContext(action, context);
    Assert.assertEquals(actionContext, expectedCompositeChildModuleContext);
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Action(org.eclipse.smarthome.automation.Action) Module(org.eclipse.smarthome.automation.Module) Test(org.junit.Test)

Example 2 with Condition

use of org.eclipse.smarthome.automation.Condition in project smarthome by eclipse.

the class RuleEngineTest method testRuleConditions.

/**
 * test rule condition
 */
@Test
public void testRuleConditions() {
    RuleEngine ruleEngine = createRuleEngine();
    Rule rule1 = createRule();
    List<Condition> conditions = rule1.getConditions();
    ruleEngine.addRule(rule1, true);
    RuntimeRule rule1Get = ruleEngine.getRuntimeRule("rule1");
    List<Condition> conditionsGet = rule1Get.getConditions();
    Assert.assertNotNull("Null conditions list", conditionsGet);
    Assert.assertEquals("Empty conditions list", 1, conditionsGet.size());
    Assert.assertEquals("Returned conditions list should not be a copy", conditionsGet, rule1Get.getConditions());
    conditions.add(new Condition("conditionId2", "typeUID2", null, null));
    // ruleEngine.update will update the RuntimeRule.moduleMap with the new
    ruleEngine.updateRule(rule1, true);
    // module
    RuntimeRule rule2Get = ruleEngine.getRuntimeRule("rule1");
    List<Condition> conditionsGet2 = rule2Get.getConditions();
    Assert.assertNotNull("Null conditions list", conditionsGet2);
    Assert.assertEquals("Condition was not added to the rule's list of conditions", 2, conditionsGet2.size());
    Assert.assertEquals("Returned conditions list should not be a copy", conditionsGet2, rule2Get.getConditions());
    Assert.assertNotNull("Rule condition with wrong id is returned: " + conditionsGet2, rule2Get.getModule("conditionId2"));
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Rule(org.eclipse.smarthome.automation.Rule) Test(org.junit.Test)

Example 3 with Condition

use of org.eclipse.smarthome.automation.Condition in project smarthome by eclipse.

the class RuleEngineTest method createConditions.

private List<Condition> createConditions(String type) {
    List<Condition> conditions = new ArrayList<Condition>();
    Configuration configurations = new Configuration();
    configurations.put("a", "x");
    configurations.put("b", "y");
    configurations.put("c", "z");
    Map<String, String> inputs = new HashMap<String, String>(11);
    String ouputModuleId = "triggerId";
    String outputName = "triggerOutput";
    String inputName = "conditionInput";
    inputs.put(inputName, ouputModuleId + "." + outputName);
    conditions.add(new Condition("conditionId", type, configurations, inputs));
    return conditions;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Configuration(org.eclipse.smarthome.config.core.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 4 with Condition

use of org.eclipse.smarthome.automation.Condition in project smarthome by eclipse.

the class RuleEngine method calculateConditions.

/**
 * This method checks if all rule's condition are satisfied or not.
 *
 * @param rule the checked rule
 * @return true when all conditions of the rule are satisfied, false otherwise.
 */
private boolean calculateConditions(Rule rule) {
    List<Condition> conditions = ((RuntimeRule) rule).getConditions();
    if (conditions.size() == 0) {
        return true;
    }
    RuleStatus ruleStatus = null;
    for (Iterator<Condition> it = conditions.iterator(); it.hasNext(); ) {
        ruleStatus = getRuleStatus(rule.getUID());
        if (ruleStatus != RuleStatus.RUNNING) {
            return false;
        }
        RuntimeCondition c = (RuntimeCondition) it.next();
        ConditionHandler tHandler = c.getModuleHandler();
        Map<String, Object> context = getContext(rule.getUID(), c.getConnections());
        if (!tHandler.isSatisfied(Collections.unmodifiableMap(context))) {
            logger.debug("The condition '{}' of rule '{}' is unsatisfied.", new Object[] { c.getId(), rule.getUID() });
            return false;
        }
    }
    return true;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) ConditionHandler(org.eclipse.smarthome.automation.handler.ConditionHandler) RuleStatus(org.eclipse.smarthome.automation.RuleStatus)

Example 5 with Condition

use of org.eclipse.smarthome.automation.Condition in project smarthome by eclipse.

the class RuleEngine method autoMapConnections.

/**
 * The auto mapping tries to link not connected module inputs to output of other modules. The auto mapping will link
 * input to output only when following criteria are done: 1) input must not be connected. The auto mapping will not
 * overwrite explicit connections done by the user. 2) input tags must be subset of the output tags. 3) condition
 * inputs can be connected only to triggers' outputs 4) action outputs can be connected to both conditions and
 * actions
 * outputs 5) There is only one output, based on previous criteria, where the input can connect to. If more then one
 * candidate outputs exists for connection, this is a conflict and the auto mapping leaves the input unconnected.
 * Auto
 * mapping is always applied when the rule is added or updated. It changes initial value of inputs of conditions and
 * actions participating in the rule. If an "auto map" connection has to be removed, the tags of corresponding
 * input/output have to be changed.
 *
 * @param r updated rule
 */
private void autoMapConnections(RuntimeRule r) {
    Map<Set<String>, OutputRef> triggerOutputTags = new HashMap<Set<String>, OutputRef>(11);
    for (Trigger t : r.getTriggers()) {
        TriggerType tt = (TriggerType) mtRegistry.get(t.getTypeUID());
        if (tt != null) {
            initTagsMap(t.getId(), tt.getOutputs(), triggerOutputTags);
        }
    }
    Map<Set<String>, OutputRef> actionOutputTags = new HashMap<Set<String>, OutputRef>(11);
    for (Action a : r.getActions()) {
        ActionType at = (ActionType) mtRegistry.get(a.getTypeUID());
        if (at != null) {
            initTagsMap(a.getId(), at.getOutputs(), actionOutputTags);
        }
    }
    // auto mapping of conditions
    if (!triggerOutputTags.isEmpty()) {
        for (Condition c : r.getConditions()) {
            boolean isConnectionChanged = false;
            ConditionType ct = (ConditionType) mtRegistry.get(c.getTypeUID());
            if (ct != null) {
                Set<Connection> connections = ((RuntimeCondition) c).getConnections();
                for (Input input : ct.getInputs()) {
                    if (isConnected(input, connections)) {
                        // the input is already connected. Skip it.
                        continue;
                    }
                    if (addAutoMapConnections(input, triggerOutputTags, connections)) {
                        isConnectionChanged = true;
                    }
                }
                if (isConnectionChanged) {
                    // update condition inputs
                    connections = ((RuntimeCondition) c).getConnections();
                    Map<String, String> connectionMap = getConnectionMap(connections);
                    c.setInputs(connectionMap);
                }
            }
        }
    }
    // auto mapping of actions
    if (!triggerOutputTags.isEmpty() || !actionOutputTags.isEmpty()) {
        for (Action a : r.getActions()) {
            boolean isConnectionChanged = false;
            ActionType at = (ActionType) mtRegistry.get(a.getTypeUID());
            if (at != null) {
                Set<Connection> connections = ((RuntimeAction) a).getConnections();
                for (Input input : at.getInputs()) {
                    if (isConnected(input, connections)) {
                        // the input is already connected. Skip it.
                        continue;
                    }
                    if (addAutoMapConnections(input, triggerOutputTags, connections)) {
                        isConnectionChanged = true;
                    }
                    if (addAutoMapConnections(input, actionOutputTags, connections)) {
                        isConnectionChanged = true;
                    }
                }
                if (isConnectionChanged) {
                    // update condition inputs
                    connections = ((RuntimeAction) a).getConnections();
                    Map<String, String> connectionMap = getConnectionMap(connections);
                    a.setInputs(connectionMap);
                }
            }
        }
    }
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) CompositeTriggerType(org.eclipse.smarthome.automation.type.CompositeTriggerType) TriggerType(org.eclipse.smarthome.automation.type.TriggerType) Action(org.eclipse.smarthome.automation.Action) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) HashSet(java.util.HashSet) CompositeActionType(org.eclipse.smarthome.automation.type.CompositeActionType) ActionType(org.eclipse.smarthome.automation.type.ActionType) HashMap(java.util.HashMap) Input(org.eclipse.smarthome.automation.type.Input) Trigger(org.eclipse.smarthome.automation.Trigger) CompositeConditionType(org.eclipse.smarthome.automation.type.CompositeConditionType) ConditionType(org.eclipse.smarthome.automation.type.ConditionType)

Aggregations

Condition (org.eclipse.smarthome.automation.Condition)21 Action (org.eclipse.smarthome.automation.Action)10 Configuration (org.eclipse.smarthome.config.core.Configuration)10 Trigger (org.eclipse.smarthome.automation.Trigger)9 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)4 Rule (org.eclipse.smarthome.automation.Rule)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)3 Set (java.util.Set)2 Module (org.eclipse.smarthome.automation.Module)2 RuleStatus (org.eclipse.smarthome.automation.RuleStatus)2 ConditionHandler (org.eclipse.smarthome.automation.handler.ConditionHandler)2 CompareConditionHandler (org.eclipse.smarthome.automation.module.core.handler.CompareConditionHandler)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1