Search in sources :

Example 1 with MethodFailedException

use of ognl.MethodFailedException in project stdlib by petergeneric.

the class RulesEngineImpl method run.

@Override
public void run(final Rules rules, boolean ignoreMethodErrors) throws OgnlException {
    Map<String, Object> vars = prepare(rules);
    for (RuleSet rs : rules.ruleSets) {
        try {
            log.debug("Assessing input for ruleset : " + rs.id);
            OgnlContext rsContext = createContext(vars);
            rs.runInput(rsContext);
            for (Rule rule : rs.rules) {
                if (StringUtils.isEmpty(rule.id)) {
                    throw new IllegalArgumentException("Rule with condition " + rule.condition + " has no id!");
                }
                final RuleProcessTask task = new RuleProcessTask(this, rs, rule, rsContext);
                synchronized (activeTasks) {
                    // if not currently being processed
                    if (!activeTasks.containsKey(rule.id)) {
                        try {
                            activeTasks.put(rule.id, task);
                            task.submit(executorService);
                        } catch (Exception e) {
                            log.error("Error submitting rule for execution ", e);
                            activeTasks.remove(rule.id);
                        }
                    }
                }
            }
        } catch (MethodFailedException mfe) {
            if (!ignoreMethodErrors) {
                throw mfe;
            }
            log.warn("Method failed for ruleset " + rs.id, mfe);
        }
    }
}
Also used : RuleSet(com.peterphi.rules.types.RuleSet) Rule(com.peterphi.rules.types.Rule) MethodFailedException(ognl.MethodFailedException) MethodFailedException(ognl.MethodFailedException) OgnlException(ognl.OgnlException) OgnlContext(ognl.OgnlContext)

Example 2 with MethodFailedException

use of ognl.MethodFailedException in project stdlib by petergeneric.

the class RulesEngineImpl method matching.

/**
 * returns a list of the rules that match from the supplied Rules document
 *
 * @param rules
 *
 * @return
 */
@Override
public Map<RuleSet, List<Rule>> matching(Rules rules, Map<String, Object> vars, boolean ignoreMethodErrors) throws OgnlException {
    Map<RuleSet, List<Rule>> ret = new HashMap<>();
    for (RuleSet ruleSet : rules.ruleSets) {
        try {
            OgnlContext context = createContext(vars);
            List<Rule> matching = match(ruleSet, context);
            if (!matching.isEmpty()) {
                ret.put(ruleSet, matching);
            }
        } catch (MethodFailedException mfe) {
            if (!ignoreMethodErrors) {
                throw mfe;
            }
            log.warn("Method failed for ruleset " + ruleSet.id, mfe);
        }
    }
    return ret;
}
Also used : RuleSet(com.peterphi.rules.types.RuleSet) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) List(java.util.List) Rule(com.peterphi.rules.types.Rule) MethodFailedException(ognl.MethodFailedException) OgnlContext(ognl.OgnlContext)

Aggregations

Rule (com.peterphi.rules.types.Rule)2 RuleSet (com.peterphi.rules.types.RuleSet)2 MethodFailedException (ognl.MethodFailedException)2 OgnlContext (ognl.OgnlContext)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 OgnlException (ognl.OgnlException)1