Search in sources :

Example 1 with TranslateResult

use of org.smartdata.server.rule.parser.TranslateResult in project SSM by Intel-bigdata.

the class RuleManager method submitRule.

/**
   * Submit a rule to RuleManger.
   * @param rule
   * @param initState
   * @return
   * @throws IOException
   */
public long submitRule(String rule, RuleState initState) throws IOException {
    LOG.error("Received Rule -> [" + rule + "]");
    if (initState != RuleState.ACTIVE && initState != RuleState.DISABLED && initState != RuleState.DRYRUN) {
        throw new IOException("Invalid initState = " + initState + ", it MUST be one of [" + RuleState.ACTIVE + ", " + RuleState.DRYRUN + ", " + RuleState.DISABLED + "]");
    }
    TranslateResult tr = doCheckRule(rule, null);
    RuleInfo.Builder builder = RuleInfo.newBuilder();
    builder.setRuleText(rule).setState(initState);
    RuleInfo ruleInfo = builder.build();
    try {
        dbAdapter.insertNewRule(ruleInfo);
    } catch (SQLException e) {
        throw new IOException("RuleText = " + rule, e);
    }
    RuleContainer container = new RuleContainer(ruleInfo, dbAdapter);
    mapRules.put(ruleInfo.getId(), container);
    submitRuleToScheduler(container.launchExecutor(this));
    return ruleInfo.getId();
}
Also used : SQLException(java.sql.SQLException) TranslateResult(org.smartdata.server.rule.parser.TranslateResult) IOException(java.io.IOException) RuleInfo(org.smartdata.common.rule.RuleInfo)

Example 2 with TranslateResult

use of org.smartdata.server.rule.parser.TranslateResult in project SSM by Intel-bigdata.

the class TestSmartRuleParser method parseAndExecuteRule.

private void parseAndExecuteRule(String rule) throws Exception {
    System.out.println("--> " + rule);
    InputStream input = new ByteArrayInputStream(rule.getBytes());
    ANTLRInputStream antlrInput = new ANTLRInputStream(input);
    SmartRuleLexer lexer = new SmartRuleLexer(antlrInput);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SmartRuleParser parser = new SmartRuleParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new SSMRuleErrorListener());
    ParseTree tree = parser.ssmrule();
    System.out.println("Parser tree: " + tree.toStringTree(parser));
    System.out.println("Total number of errors: " + parseErrors.size());
    SmartRuleVisitTranslator visitor = new SmartRuleVisitTranslator();
    visitor.visit(tree);
    System.out.println("\nQuery:");
    TranslateResult result = visitor.generateSql();
    int index = 1;
    for (String sql : result.getSqlStatements()) {
        System.out.println("" + index + ". " + sql);
        index++;
    }
    if (parseErrors.size() > 0) {
        throw new IOException("Error while parse rule");
    }
    ExecutionContext ctx = new ExecutionContext();
    ctx.setProperty(ExecutionContext.RULE_ID, 2016);
    DBAdapter dbAdapter = new DBAdapter(TestDBUtil.getTestDBInstance());
    RuleQueryExecutor qe = new RuleQueryExecutor(null, ctx, result, dbAdapter);
    List<String> paths = qe.executeFileRuleQuery();
    index = 1;
    System.out.println("\nFiles:");
    for (String path : paths) {
        System.out.println("" + index + ". " + path);
        index++;
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) SmartRuleParser(org.smartdata.server.rule.parser.SmartRuleParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SmartRuleLexer(org.smartdata.server.rule.parser.SmartRuleLexer) SmartRuleVisitTranslator(org.smartdata.server.rule.parser.SmartRuleVisitTranslator) IOException(java.io.IOException) DBAdapter(org.smartdata.server.metastore.DBAdapter) ExecutionContext(org.smartdata.server.metastore.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) TranslateResult(org.smartdata.server.rule.parser.TranslateResult) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 3 with TranslateResult

use of org.smartdata.server.rule.parser.TranslateResult in project SSM by Intel-bigdata.

the class RuleContainer method doLaunchExecutor.

private RuleQueryExecutor doLaunchExecutor(RuleManager ruleManager) throws IOException {
    RuleState state = ruleInfo.getState();
    if (state == RuleState.ACTIVE || state == RuleState.DRYRUN) {
        if (executor != null && !executor.isExited()) {
            return null;
        }
        ExecutionContext ctx = new ExecutionContext();
        ctx.setProperty(ExecutionContext.RULE_ID, ruleInfo.getId());
        TranslationContext transCtx = new TranslationContext(ruleInfo.getId(), ruleInfo.getSubmitTime());
        TranslateResult tr = executor != null ? executor.getTranslateResult() : ruleManager.doCheckRule(ruleInfo.getRuleText(), transCtx);
        executor = new RuleQueryExecutor(ruleManager, ctx, tr, ruleManager.getDbAdapter());
        return executor;
    }
    return null;
}
Also used : RuleState(org.smartdata.common.rule.RuleState) ExecutionContext(org.smartdata.server.metastore.ExecutionContext) TranslateResult(org.smartdata.server.rule.parser.TranslateResult) TranslationContext(org.smartdata.server.rule.parser.TranslationContext)

Aggregations

TranslateResult (org.smartdata.server.rule.parser.TranslateResult)3 IOException (java.io.IOException)2 ExecutionContext (org.smartdata.server.metastore.ExecutionContext)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 SQLException (java.sql.SQLException)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 RuleInfo (org.smartdata.common.rule.RuleInfo)1 RuleState (org.smartdata.common.rule.RuleState)1 DBAdapter (org.smartdata.server.metastore.DBAdapter)1 SmartRuleLexer (org.smartdata.server.rule.parser.SmartRuleLexer)1 SmartRuleParser (org.smartdata.server.rule.parser.SmartRuleParser)1 SmartRuleVisitTranslator (org.smartdata.server.rule.parser.SmartRuleVisitTranslator)1 TranslationContext (org.smartdata.server.rule.parser.TranslationContext)1