Search in sources :

Example 1 with TranslateResult

use of org.smartdata.model.rule.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");
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) SmartRuleParser(org.smartdata.rule.parser.SmartRuleParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SmartRuleLexer(org.smartdata.rule.parser.SmartRuleLexer) SmartRuleVisitTranslator(org.smartdata.rule.parser.SmartRuleVisitTranslator) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) TranslateResult(org.smartdata.model.rule.TranslateResult) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 2 with TranslateResult

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

the class TestSmartRuleStringParser method parseRule.

private void parseRule(String rule) throws Exception {
    TranslationContext tc = new TranslationContext(1, System.currentTimeMillis());
    SmartRuleStringParser parser = new SmartRuleStringParser(rule, tc, new SmartConf());
    TranslateResult tr = parser.translate();
    int index = 1;
    System.out.println("\n" + rule);
    for (String sql : tr.getSqlStatements()) {
        System.out.println("\t" + index + ". " + sql);
        index++;
    }
}
Also used : SmartRuleStringParser(org.smartdata.rule.parser.SmartRuleStringParser) TranslateResult(org.smartdata.model.rule.TranslateResult) SmartConf(org.smartdata.conf.SmartConf) TranslationContext(org.smartdata.rule.parser.TranslationContext)

Example 3 with TranslateResult

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

the class RuleManager method start.

/**
 * Start services.
 */
@Override
public void start() throws IOException {
    LOG.info("Starting ...");
    // after StateManager be ready
    int numLaunched = 0;
    // Submit runnable rules to scheduler
    for (RuleInfoRepo infoRepo : mapRules.values()) {
        RuleInfo rule = infoRepo.getRuleInfoRef();
        if (rule.getState() == RuleState.ACTIVE || rule.getState() == RuleState.DRYRUN) {
            RuleExecutor ruleExecutor = infoRepo.launchExecutor(this);
            TranslateResult tr = ruleExecutor.getTranslateResult();
            TimeBasedScheduleInfo si = tr.getTbScheduleInfo();
            if (rule.getLastCheckTime() != 0) {
                si.setFirstCheckTime(rule.getLastCheckTime());
            }
            boolean sub = submitRuleToScheduler(ruleExecutor);
            numLaunched += sub ? 1 : 0;
        }
    }
    LOG.info("Started. " + numLaunched + " rules launched for execution.");
}
Also used : RuleInfoRepo(org.smartdata.server.engine.rule.RuleInfoRepo) RuleExecutor(org.smartdata.server.engine.rule.RuleExecutor) TranslateResult(org.smartdata.model.rule.TranslateResult) DetailedRuleInfo(org.smartdata.model.DetailedRuleInfo) RuleInfo(org.smartdata.model.RuleInfo) TimeBasedScheduleInfo(org.smartdata.model.rule.TimeBasedScheduleInfo)

Example 4 with TranslateResult

use of org.smartdata.model.rule.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.debug("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);
    doCheckActions(tr.getCmdDescriptor());
    // check whitelist
    if (WhitelistHelper.isEnabled(serverContext.getConf())) {
        for (String path : tr.getGlobPathCheck()) {
            if (!WhitelistHelper.isInWhitelist(path, serverContext.getConf())) {
                throw new IOException("Path " + path + " is not in the whitelist.");
            }
        }
    }
    RuleInfo.Builder builder = RuleInfo.newBuilder();
    builder.setRuleText(rule).setState(initState);
    RuleInfo ruleInfo = builder.build();
    RulePluginManager.onAddingNewRule(ruleInfo, tr);
    try {
        metaStore.insertNewRule(ruleInfo);
    } catch (MetaStoreException e) {
        throw new IOException("RuleText = " + rule, e);
    }
    RuleInfoRepo infoRepo = new RuleInfoRepo(ruleInfo, metaStore, serverContext.getConf());
    mapRules.put(ruleInfo.getId(), infoRepo);
    submitRuleToScheduler(infoRepo.launchExecutor(this));
    RulePluginManager.onNewRuleAdded(ruleInfo, tr);
    return ruleInfo.getId();
}
Also used : RuleInfoRepo(org.smartdata.server.engine.rule.RuleInfoRepo) MetaStoreException(org.smartdata.metastore.MetaStoreException) TranslateResult(org.smartdata.model.rule.TranslateResult) IOException(java.io.IOException) DetailedRuleInfo(org.smartdata.model.DetailedRuleInfo) RuleInfo(org.smartdata.model.RuleInfo)

Example 5 with TranslateResult

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

the class SmartRuleStringParser method translate.

public TranslateResult translate() throws IOException {
    TranslateResult tr = doTranslate(rule);
    CmdletDescriptor cmdDes = tr.getCmdDescriptor();
    if (cmdDes.getActionSize() == 0) {
        throw new IOException("No cmdlet specified in Rule");
    }
    String actName = cmdDes.getActionName(0);
    if (cmdDes.getActionSize() != 1 || optCond.get(actName) == null) {
        return tr;
    }
    String repl = optCond.get(actName);
    if (cmdDes.getActionName(0).equals("ec") || cmdDes.getActionName(0).equals("unec")) {
        String policy;
        if (cmdDes.getActionName(0).equals("ec")) {
            policy = cmdDes.getActionArgs(0).get("-policy");
            if (policy == null) {
                policy = conf.getTrimmed("dfs.namenode.ec.system.default.policy", "RS-6-3-1024k");
            }
        } else {
            policy = SmartConstants.REPLICATION_CODEC_NAME;
        }
        repl = "ecPolicy != \"" + policy + "\"";
    }
    int[] condPosition = tr.getCondPosition();
    String cond = rule.substring(condPosition[0], condPosition[1] + 1);
    String optRule = rule.replace(cond, repl + " and (" + cond + ")");
    return doTranslate(optRule);
}
Also used : CmdletDescriptor(org.smartdata.model.CmdletDescriptor) TranslateResult(org.smartdata.model.rule.TranslateResult) IOException(java.io.IOException)

Aggregations

TranslateResult (org.smartdata.model.rule.TranslateResult)7 IOException (java.io.IOException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 DetailedRuleInfo (org.smartdata.model.DetailedRuleInfo)2 RuleInfo (org.smartdata.model.RuleInfo)2 SmartRuleStringParser (org.smartdata.rule.parser.SmartRuleStringParser)2 TranslationContext (org.smartdata.rule.parser.TranslationContext)2 RuleInfoRepo (org.smartdata.server.engine.rule.RuleInfoRepo)2 SmartConf (org.smartdata.conf.SmartConf)1 MetaStoreException (org.smartdata.metastore.MetaStoreException)1 CmdletDescriptor (org.smartdata.model.CmdletDescriptor)1 RuleState (org.smartdata.model.RuleState)1 RuleExecutorPlugin (org.smartdata.model.rule.RuleExecutorPlugin)1 TimeBasedScheduleInfo (org.smartdata.model.rule.TimeBasedScheduleInfo)1 SmartRuleLexer (org.smartdata.rule.parser.SmartRuleLexer)1 SmartRuleParser (org.smartdata.rule.parser.SmartRuleParser)1