@Override public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { super.prepare(stormConf, context, collector); if (this.rulesProcessor == null) { throw new RuntimeException("rulesProcessor cannot be null"); } ruleProcessorRuntime = new RuleProcessorRuntime(rulesProcessor, scriptType); Map<String, Object> config = Collections.emptyMap(); ruleProcessorRuntime.initialize(config); }
@Override public void initialize(Map<String, Object> config) { initializeRuleRuntimes(config); buildStreamToRulesRuntime(); this.processAll = this.rulesProcessor.getProcessAll(); }
private void initializeRuleRuntimes(Map<String, Object> config) { List<Rule> rules = rulesProcessor.getRules(); if (rules != null) { for (Rule rule: rules) { RuleRuntime ruleRuntime; Script script = null; if (ScriptType.GROOVY.equals(scriptType)) { script = createGroovyScript(rule); } else if (ScriptType.SQL.equals(scriptType)) { script = createSqlScript(rule); } else { throw new RuntimeException("Ruleruntime scriptType unsupported: " + scriptType ); } ruleRuntime = new RuleRuntime(rule, script, createActionRuntimes(rule)); rulesRuntime.add(ruleRuntime); ruleRuntime.initialize(config); } LOG.info("ruleRuntimes [{}]", rulesRuntime); } }
void initializeWithRuleRuntimesForTesting(Map<String, Object> config, List<RuleRuntime> ruleRuntimes) { rulesRuntime = ruleRuntimes; buildStreamToRulesRuntime(); this.processAll = this.rulesProcessor.getProcessAll(); }
private Script createSqlScript(Rule rule) { SqlEngine sqlEngine = new SqlEngine(); LOG.info("Built sqlEngine {}", sqlEngine); StormSqlExpression stormSqlExpression = createSqlExpression(rule); SqlScript sqlScript = new SqlScript(stormSqlExpression, sqlEngine); LOG.info("Built SqlScript {}", sqlScript); SqlScript.CorrelatedValuesToStreamlineEventConverter valuesConverter = new SqlScript.CorrelatedValuesToStreamlineEventConverter(sqlScript.getOutputFields()); sqlScript.setValuesConverter(valuesConverter); LOG.info("valuesConverter {}", valuesConverter); return sqlScript; }
@Override public List<Result> process(StreamlineEvent event) throws ProcessingException { List<Result> results = new ArrayList<>(); try { List<RuleRuntime> ruleRuntimes = getRulesRuntime(event); LOG.debug("Process event {}, rule runtimes {}", event, ruleRuntimes); for (RuleRuntime rr : ruleRuntimes) { boolean succeeded = false; for (StreamlineEvent result : rr.evaluate(event)) { if (result != null) { results.addAll(rr.process(result)); succeeded = true; } } if(!processAll && succeeded) break; } } catch (Exception e) { String message = String.format("Error evaluating rule processor with id: %s, error: %s", rulesProcessor.getId(), e.getMessage()); LOG.error(message, e); throw new ProcessingException(message, e); } return results; }
private Script createGroovyScript(Rule rule) { LOG.info("Creating groovy execution script for rule {} ", rule); GroovyExpression groovyExpression = new GroovyExpression(rule.getCondition()); GroovyScriptEngine groovyScriptEngine = new GroovyScriptEngine(); GroovyScript<Boolean> groovyScript = createHelperGroovyScript(groovyExpression, groovyScriptEngine); GroovyScript<Collection<StreamlineEvent>> wrapper = new GroovyScript<Collection<StreamlineEvent>>(groovyExpression.asString(), groovyScriptEngine) { @Override public Collection<StreamlineEvent> evaluate(StreamlineEvent event) throws ScriptException { if (groovyScript.evaluate(event)) { return Collections.singletonList(event); } else { return Collections.emptyList(); } } }; return wrapper; }
@Override public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { super.prepare(stormConf, context, collector); if (this.rulesProcessor == null) { throw new RuntimeException("rulesProcessor cannot be null"); } ruleProcessorRuntime = new RuleProcessorRuntime(rulesProcessor, scriptType); Map<String, Object> config = Collections.emptyMap(); if (stormConf != null) { config = new HashMap<>(); config.put(Constants.CATALOG_ROOT_URL, stormConf.get(Constants.CATALOG_ROOT_URL)); config.put(Constants.LOCAL_FILES_PATH, stormConf.get(Constants.LOCAL_FILES_PATH)); } ruleProcessorRuntime.initialize(config); }