Package org.openhab.model.rule.rules

Examples of org.openhab.model.rule.rules.RuleModel


   *
   * @param rule the rule to get the context for
   * @return the evaluation context
   */
  public static synchronized IEvaluationContext getContext(Rule rule) {
      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();
      }
    }
   
    // no evaluation context found, so create a new one
      IEvaluationContext evaluationContext = contextProvider.get();
      for(XExpression expr : ruleModel.getVariables()) {
        if (expr instanceof XVariableDeclaration) {
        XVariableDeclaration var = (XVariableDeclaration) expr;
        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 String[] { var.getName(), ruleModel.eResource().getURI().path(), var.getRight().toString(), e.getMessage() });
        }
      }
      }
      ruleModel.eAdapters().add(new RuleContextAdapter(evaluationContext));
    return evaluationContext;
  }
View Full Code Here


 
  private Collection<? extends IEObjectDescription> createVarFeatures(Resource resource) {
    List<IEObjectDescription> descriptions = new ArrayList<IEObjectDescription>();

    if(resource.getContents().size()>0 && resource.getContents().get(0) instanceof RuleModel) {
      RuleModel ruleModel = (RuleModel) resource.getContents().get(0);
      for(XExpression expr : ruleModel.getVariables()) {
        if (expr instanceof XVariableDeclaration) {
          XVariableDeclaration var = (XVariableDeclaration) expr;
          if(var.getName()!=null && var.getType()!=null) {
            descriptions.add(createLocalVarDescription(var));
          }
View Full Code Here

      Iterable<String> ruleModelNames = modelRepository.getAllModelNamesOfType("rules");
      ArrayList<String> clonedList = Lists.newArrayList(ruleModelNames);
      for(String ruleModelName : clonedList) {
        EObject model = modelRepository.getModel(ruleModelName);
        if(model instanceof RuleModel) {
          RuleModel ruleModel = (RuleModel) model;
          triggerManager.addRuleModel(ruleModel);
        }
      }
     
      // register us on all items which are already available in the registry
View Full Code Here

    }

    public void modelChanged(String modelName, org.openhab.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.openhab.model.core.EventType.REMOVED ||
              type == org.openhab.model.core.EventType.MODIFIED) {
            triggerManager.removeRuleModel(model);
View Full Code Here

    ScriptEngine scriptEngine = RuleModelActivator.scriptEngineTracker.getService();
   
    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 {
View Full Code Here

TOP

Related Classes of org.openhab.model.rule.rules.RuleModel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.