예제 #1
0
  private void runStartupRules() {
    if (triggerManager != null) {
      Iterable<Rule> startupRules = triggerManager.getRules(STARTUP);
      List<Rule> executedRules = Lists.newArrayList();

      for (Rule rule : startupRules) {
        try {
          Script script = scriptEngine.newScriptFromXExpression(rule.getScript());
          logger.debug("Executing startup rule '{}'", rule.getName());
          RuleEvaluationContext context = new RuleEvaluationContext();
          context.setGlobalContext(RuleContextHelper.getContext(rule));
          script.execute(context);
          executedRules.add(rule);
        } catch (ScriptExecutionException e) {
          String causeMessage = getCauseMessage(e);
          if (e.getCause() instanceof ItemNotFoundException
              || causeMessage.contains("cannot be resolved to an item or type")) {
            logger.debug(
                "Not all required items in place yet for rule {}, trying again later: {}",
                new Object[] {rule.getName(), causeMessage});
            // we do not seem to have all required items in place yet
            // so we keep the rule in the list and try it again later
          } else {
            logger.error(
                "Error during the execution of startup rule '{}': {}",
                new Object[] {rule.getName(), causeMessage});
            executedRules.add(rule);
          }
        }
      }
      for (Rule rule : executedRules) {
        triggerManager.removeRule(STARTUP, rule);
      }
    }
  }
예제 #2
0
 protected synchronized void executeRules(Iterable<Rule> rules, State oldState) {
   for (Rule rule : rules) {
     RuleEvaluationContext context = new RuleEvaluationContext();
     context.newValue(QualifiedName.create(RulesJvmModelInferrer.VAR_PREVIOUS_STATE), oldState);
     executeRule(rule, context);
   }
 }
예제 #3
0
 protected synchronized void executeRules(Iterable<Rule> rules, Command command) {
   for (Rule rule : rules) {
     RuleEvaluationContext context = new RuleEvaluationContext();
     context.newValue(QualifiedName.create(RulesJvmModelInferrer.VAR_RECEIVED_COMMAND), command);
     executeRule(rule, context);
   }
 }
예제 #4
0
  private void runStartupRules() {
    if (triggerManager != null) {
      Iterable<Rule> startupRules = triggerManager.getRules(STARTUP);
      List<Rule> executedRules = Lists.newArrayList();

      for (Rule rule : startupRules) {
        try {
          Script script = scriptEngine.newScriptFromXExpression(rule.getScript());
          logger.debug("Executing startup rule '{}'", rule.getName());
          RuleEvaluationContext context = new RuleEvaluationContext();
          context.setGlobalContext(RuleContextHelper.getContext(rule, injector));
          script.execute(context);
          executedRules.add(rule);
        } catch (ScriptExecutionException e) {
          if (!e.getMessage().contains("cannot be resolved to an item or type")) {
            logger.error(
                "Error during the execution of startup rule '{}': {}",
                new Object[] {rule.getName(), e.getCause().getMessage()});
            executedRules.add(rule);
          } else {
            logger.debug(
                "Execution of startup rule '{}' has been postponed as items are still missing.",
                rule.getName());
          }
        }
      }
      for (Rule rule : executedRules) {
        triggerManager.removeRule(STARTUP, rule);
      }
    }
  }
예제 #5
0
  /** {@inheritDoc} */
  public void stateChanged(Item item, State oldState, State newState) {
    if (triggerManager != null) {
      RuleEvaluationContext context;
      Iterable<Rule> rules = triggerManager.getRules(CHANGE, item, oldState, newState);

      for (Rule rule : rules) {
        context = new RuleEvaluationContext();
        context.newValue(QualifiedName.create(RuleContextHelper.VAR_PREVIOUS_STATE), oldState);
        executeRule(rule, context);
      }
    }
  }
예제 #6
0
  public void receiveCommand(String itemName, Command command) {
    if (triggerManager != null && itemRegistry != null) {
      try {
        RuleEvaluationContext context;
        Item item = itemRegistry.getItem(itemName);
        Iterable<Rule> rules = triggerManager.getRules(COMMAND, item, command);

        for (Rule rule : rules) {
          context = new RuleEvaluationContext();
          context.newValue(QualifiedName.create(RuleContextHelper.VAR_RECEIVED_COMMAND), command);
          executeRule(rule, context);
        }
      } catch (ItemNotFoundException e) {
        // ignore commands for non-existent items
      }
    }
  }
예제 #7
0
  protected synchronized void executeRule(Rule rule, RuleEvaluationContext context) {
    Script script = scriptEngine.newScriptFromXExpression(rule.getScript());

    logger.debug("Executing rule '{}'", rule.getName());

    context.setGlobalContext(RuleContextHelper.getContext(rule));

    ScriptExecutionThread thread = new ScriptExecutionThread(rule.getName(), script, context);
    thread.start();
  }