Search in sources :

Example 1 with SmartRuleParser

use of org.smartdata.rule.parser.SmartRuleParser 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)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 TranslateResult (org.smartdata.model.rule.TranslateResult)1 SmartRuleLexer (org.smartdata.rule.parser.SmartRuleLexer)1 SmartRuleParser (org.smartdata.rule.parser.SmartRuleParser)1 SmartRuleVisitTranslator (org.smartdata.rule.parser.SmartRuleVisitTranslator)1