Search in sources :

Example 1 with RuleModel

use of org.eclipse.smarthome.model.rule.rules.RuleModel in project smarthome by eclipse.

the class RuleEngineImpl method modelChanged.

@Override
public void modelChanged(String modelName, org.eclipse.smarthome.model.core.EventType type) {
    if (triggerManager != null) {
        if (isEnabled() && modelName.endsWith("rules")) {
            RuleModel model = (RuleModel) modelRepository.getModel(modelName);
            // remove the rules from the trigger sets
            if (type == org.eclipse.smarthome.model.core.EventType.REMOVED || type == org.eclipse.smarthome.model.core.EventType.MODIFIED) {
                triggerManager.removeRuleModel(model);
            }
            // add new and modified rules to the trigger sets
            if (model != null && (type == org.eclipse.smarthome.model.core.EventType.ADDED || type == org.eclipse.smarthome.model.core.EventType.MODIFIED)) {
                triggerManager.addRuleModel(model);
                // now execute all rules that are meant to trigger at startup
                scheduleStartupRules();
            }
        }
    }
}
Also used : RuleModel(org.eclipse.smarthome.model.rule.rules.RuleModel)

Example 2 with RuleModel

use of org.eclipse.smarthome.model.rule.rules.RuleModel in project smarthome by eclipse.

the class RuleEngineImpl method activate.

@Activate
public void activate() {
    injector = RulesStandaloneSetup.getInjector();
    triggerManager = injector.getInstance(RuleTriggerManager.class);
    if (!isEnabled()) {
        logger.info("Rule engine is disabled.");
        return;
    }
    logger.debug("Started rule engine");
    // read all rule files
    for (String ruleModelName : modelRepository.getAllModelNamesOfType("rules")) {
        EObject model = modelRepository.getModel(ruleModelName);
        if (model instanceof RuleModel) {
            RuleModel ruleModel = (RuleModel) model;
            triggerManager.addRuleModel(ruleModel);
        }
    }
    // register us as listeners
    itemRegistry.addRegistryChangeListener(this);
    modelRepository.addModelRepositoryChangeListener(this);
    // register us on all items which are already available in the registry
    for (Item item : itemRegistry.getItems()) {
        internalItemAdded(item);
    }
    scheduleStartupRules();
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) EObject(org.eclipse.emf.ecore.EObject) RuleModel(org.eclipse.smarthome.model.rule.rules.RuleModel) Activate(org.osgi.service.component.annotations.Activate)

Example 3 with RuleModel

use of org.eclipse.smarthome.model.rule.rules.RuleModel in project smarthome by eclipse.

the class RuleContextHelper method getContext.

/**
 * Retrieves the evaluation context (= set of variables) for a rule. The context is shared with all rules in the
 * same model (= rule file).
 *
 * @param rule the rule to get the context for
 * @return the evaluation context
 */
public static synchronized IEvaluationContext getContext(Rule rule, Injector injector) {
    Logger logger = LoggerFactory.getLogger(RuleContextHelper.class);
    RuleModel ruleModel = (RuleModel) rule.eContainer();
    // check if a context already exists on the resource
    for (Adapter adapter : ruleModel.eAdapters()) {
        if (adapter instanceof RuleContextAdapter) {
            return ((RuleContextAdapter) adapter).getContext();
        }
    }
    Provider<IEvaluationContext> contextProvider = injector.getProvider(IEvaluationContext.class);
    // no evaluation context found, so create a new one
    ScriptEngine scriptEngine = injector.getInstance(ScriptEngine.class);
    if (scriptEngine != null) {
        IEvaluationContext evaluationContext = contextProvider.get();
        for (VariableDeclaration var : ruleModel.getVariables()) {
            try {
                Object initialValue = var.getRight() == null ? null : scriptEngine.newScriptFromXExpression(var.getRight()).execute();
                evaluationContext.newValue(QualifiedName.create(var.getName()), initialValue);
            } catch (ScriptExecutionException e) {
                logger.warn("Variable '{}' on rule file '{}' cannot be initialized with value '{}': {}", new Object[] { var.getName(), ruleModel.eResource().getURI().path(), var.getRight().toString(), e.getMessage() });
            }
        }
        ruleModel.eAdapters().add(new RuleContextAdapter(evaluationContext));
        return evaluationContext;
    } else {
        logger.debug("Rule variables of rule {} cannot be evaluated as no scriptengine is available!", ruleModel.eResource().getURI().path());
        return contextProvider.get();
    }
}
Also used : ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException) IEvaluationContext(org.eclipse.xtext.xbase.interpreter.IEvaluationContext) EContentAdapter(org.eclipse.emf.ecore.util.EContentAdapter) Adapter(org.eclipse.emf.common.notify.Adapter) VariableDeclaration(org.eclipse.smarthome.model.rule.rules.VariableDeclaration) Logger(org.slf4j.Logger) RuleModel(org.eclipse.smarthome.model.rule.rules.RuleModel) ScriptEngine(org.eclipse.smarthome.model.script.engine.ScriptEngine)

Example 4 with RuleModel

use of org.eclipse.smarthome.model.rule.rules.RuleModel in project smarthome by eclipse.

the class ExecuteRuleJob method execute.

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    String modelName = (String) context.getJobDetail().getJobDataMap().get(JOB_DATA_RULEMODEL);
    String ruleName = (String) context.getJobDetail().getJobDataMap().get(JOB_DATA_RULENAME);
    if (modelRepository != null && scriptEngine != null) {
        EObject model = modelRepository.getModel(modelName);
        if (model instanceof RuleModel) {
            RuleModel ruleModel = (RuleModel) model;
            Rule rule = getRule(ruleModel, ruleName);
            if (rule != null) {
                Script script = scriptEngine.newScriptFromXExpression(rule.getScript());
                logger.debug("Executing scheduled rule '{}'", rule.getName());
                try {
                    script.execute(RuleContextHelper.getContext(rule, injector));
                } catch (ScriptExecutionException e) {
                    logger.error("Error during the execution of rule '{}': {}", rule.getName(), e.getMessage());
                }
            } else {
                logger.debug("Scheduled rule '{}' does not exist", ruleName);
            }
        } else {
            logger.debug("Rule file '{}' does not exist", modelName);
        }
    }
}
Also used : Script(org.eclipse.smarthome.model.script.engine.Script) ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException) EObject(org.eclipse.emf.ecore.EObject) Rule(org.eclipse.smarthome.model.rule.rules.Rule) RuleModel(org.eclipse.smarthome.model.rule.rules.RuleModel)

Aggregations

RuleModel (org.eclipse.smarthome.model.rule.rules.RuleModel)4 EObject (org.eclipse.emf.ecore.EObject)2 ScriptExecutionException (org.eclipse.smarthome.model.script.engine.ScriptExecutionException)2 Adapter (org.eclipse.emf.common.notify.Adapter)1 EContentAdapter (org.eclipse.emf.ecore.util.EContentAdapter)1 GenericItem (org.eclipse.smarthome.core.items.GenericItem)1 Item (org.eclipse.smarthome.core.items.Item)1 Rule (org.eclipse.smarthome.model.rule.rules.Rule)1 VariableDeclaration (org.eclipse.smarthome.model.rule.rules.VariableDeclaration)1 Script (org.eclipse.smarthome.model.script.engine.Script)1 ScriptEngine (org.eclipse.smarthome.model.script.engine.ScriptEngine)1 IEvaluationContext (org.eclipse.xtext.xbase.interpreter.IEvaluationContext)1 Activate (org.osgi.service.component.annotations.Activate)1 Logger (org.slf4j.Logger)1