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");
}
}
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++;
}
}
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.");
}
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();
}
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);
}
Aggregations