Search in sources :

Example 1 with WrappedRule

use of org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule in project smarthome by eclipse.

the class RuleEngineImpl method setEnabled.

@Override
public synchronized void setEnabled(String uid, boolean enable) {
    final WrappedRule rule = managedRules.get(uid);
    if (rule == null) {
        throw new IllegalArgumentException(String.format("No rule with id=%s was found!", uid));
    }
    if (enable) {
        if (disabledRulesStorage != null) {
            disabledRulesStorage.remove(uid);
        }
        if (getStatus(rule.getUID()) == RuleStatus.UNINITIALIZED) {
            register(rule);
            // change status to IDLE
            setStatus(rule.getUID(), new RuleStatusInfo(RuleStatus.IDLE));
        }
    } else {
        if (disabledRulesStorage != null) {
            disabledRulesStorage.put(uid, true);
        }
        unregister(rule, RuleStatusDetail.DISABLED, null);
    }
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) WrappedRule(org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule)

Example 2 with WrappedRule

use of org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule in project smarthome by eclipse.

the class RuleEngineImpl method addRule.

/**
 * This method add a new rule into rule engine. Scope identity of the Rule is the identity of the caller.
 *
 * @param rule a rule which has to be added.
 */
protected void addRule(Rule newRule) {
    synchronized (this) {
        if (isDisposed) {
            throw new IllegalStateException("RuleEngineImpl is disposed!");
        }
    }
    final String rUID = newRule.getUID();
    final WrappedRule rule = new WrappedRule(newRule);
    managedRules.put(rUID, rule);
    RuleStatusInfo initStatusInfo = disabledRulesStorage == null || disabledRulesStorage.get(rUID) == null ? new RuleStatusInfo(RuleStatus.INITIALIZING) : new RuleStatusInfo(RuleStatus.UNINITIALIZED, RuleStatusDetail.DISABLED);
    rule.setStatusInfo(initStatusInfo);
    WrappedRule oldRule = getManagedRule(rUID);
    if (oldRule != null) {
        unregister(oldRule);
    }
    if (isEnabled(rUID)) {
        setRule(rule);
    }
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) WrappedRule(org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule)

Example 3 with WrappedRule

use of org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule in project smarthome by eclipse.

the class RuleEngineImpl method runNow.

@Override
public void runNow(String ruleUID, boolean considerConditions, @Nullable Map<String, Object> context) {
    final WrappedRule rule = getManagedRule(ruleUID);
    if (rule == null) {
        logger.warn("Failed to execute rule '{}': Invalid Rule UID", ruleUID);
        return;
    }
    synchronized (this) {
        final RuleStatus ruleStatus = getRuleStatus(ruleUID);
        if (ruleStatus != RuleStatus.IDLE) {
            logger.error("Failed to execute rule ‘{}' with status '{}'", ruleUID, ruleStatus.name());
            return;
        }
        // change state to RUNNING
        setStatus(ruleUID, new RuleStatusInfo(RuleStatus.RUNNING));
    }
    try {
        clearContext(ruleUID);
        if (context != null && !context.isEmpty()) {
            getContext(ruleUID, null).putAll(context);
        }
        if (considerConditions) {
            if (calculateConditions(rule)) {
                executeActions(rule, false);
            }
        } else {
            executeActions(rule, false);
        }
        logger.debug("The rule '{}' is executed.", ruleUID);
    } catch (Throwable t) {
        logger.error("Failed to execute rule '{}': ", ruleUID, t);
    }
    // change state to IDLE only if the rule has not been DISABLED.
    synchronized (this) {
        if (getRuleStatus(ruleUID) == RuleStatus.RUNNING) {
            setStatus(ruleUID, new RuleStatusInfo(RuleStatus.IDLE));
        }
    }
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) RuleStatus(org.eclipse.smarthome.automation.RuleStatus) WrappedRule(org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule)

Example 4 with WrappedRule

use of org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule in project smarthome by eclipse.

the class RuleEngineImpl method setStatus.

/**
 * This method updates the status of the {@link Rule}
 *
 * @param ruleUID unique id of the rule
 * @param newStatusInfo the new status of the rule
 */
private void setStatus(String ruleUID, RuleStatusInfo newStatusInfo) {
    final WrappedRule rule = managedRules.get(ruleUID);
    if (rule == null) {
        return;
    }
    rule.setStatusInfo(newStatusInfo);
    postRuleStatusInfoEvent(ruleUID, newStatusInfo);
}
Also used : WrappedRule(org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule)

Example 5 with WrappedRule

use of org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule in project smarthome by eclipse.

the class RuleEngineImpl method runRule.

/**
 * This method runs a {@link Rule}. It is called by the {@link TriggerHandlerCallback}'s thread when a new
 * {@link TriggerData} is available. This method switches
 *
 * @param ruleUID the {@link Rule} which has to evaluate new {@link TriggerData}.
 * @param td {@link TriggerData} object containing new values for {@link Trigger}'s {@link Output}s
 */
protected void runRule(String ruleUID, TriggerHandlerCallbackImpl.TriggerData td) {
    if (thCallbacks.get(ruleUID) == null) {
        // the rule was unregistered
        return;
    }
    synchronized (this) {
        final RuleStatus ruleStatus = getRuleStatus(ruleUID);
        if (ruleStatus != RuleStatus.IDLE) {
            logger.error("Failed to execute rule ‘{}' with status '{}'", ruleUID, ruleStatus.name());
            return;
        }
        // change state to RUNNING
        setStatus(ruleUID, new RuleStatusInfo(RuleStatus.RUNNING));
    }
    try {
        clearContext(ruleUID);
        setTriggerOutputs(ruleUID, td);
        final WrappedRule rule = managedRules.get(ruleUID);
        boolean isSatisfied = calculateConditions(rule);
        if (isSatisfied) {
            executeActions(rule, true);
            logger.debug("The rule '{}' is executed.", ruleUID);
        } else {
            logger.debug("The rule '{}' is NOT executed, since it has unsatisfied conditions.", ruleUID);
        }
    } catch (Throwable t) {
        logger.error("Failed to execute rule '{}': {}", ruleUID, t.getMessage());
        logger.debug("", t);
    }
    // change state to IDLE only if the rule has not been DISABLED.
    synchronized (this) {
        if (getRuleStatus(ruleUID) == RuleStatus.RUNNING) {
            setStatus(ruleUID, new RuleStatusInfo(RuleStatus.IDLE));
        }
    }
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) RuleStatus(org.eclipse.smarthome.automation.RuleStatus) WrappedRule(org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule)

Aggregations

WrappedRule (org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule)7 RuleStatusInfo (org.eclipse.smarthome.automation.RuleStatusInfo)4 RuleStatus (org.eclipse.smarthome.automation.RuleStatus)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1