Search in sources :

Example 1 with PathErrorListener

use of com.inova8.pathql.processor.PathErrorListener in project com.inova8.intelligentgraph by peterjohnlawrence.

the class PathParser method pathPatternParser.

/**
 * Path pattern parser.
 *
 * @param pathPattern the path pattern
 * @param pathPatternVisitor the path pattern visitor
 * @return the path element
 * @throws RecognitionException the recognition exception
 * @throws PathPatternException the path pattern exception
 */
private static PathElement pathPatternParser(String pathPattern, PathPatternVisitor pathPatternVisitor) throws RecognitionException, PathPatternException {
    PathErrorListener errorListener = new PathErrorListener(pathPattern);
    CharStream input = CharStreams.fromString(pathPattern);
    PathPatternLexer lexer = new PathPatternLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(errorListener);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    PathPatternParser parser = new PathPatternParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);
    QueryStringContext queryStringTree = parser.queryString();
    PathElement pathElement = pathPatternVisitor.visit(queryStringTree);
    if (errorListener.toString() != null) {
        if (parser.getNumberOfSyntaxErrors() == 0) {
            // Lexer only error
            throw new PathPatternException(errorListener.toString(), ErrorCode.LEXER);
        } else {
            // Parser error
            throw new PathPatternException(errorListener.toString(), ErrorCode.PARSER);
        }
    }
    pathElement.setPathPattern(pathPattern);
    pathElement.indexVisitor(null, 0, null);
    pathElement.setIterations(Iterations.create(pathElement));
    // pathElement.setPathBindings(PathBindings.create(pathElement));
    return pathElement;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) QueryStringContext(com.inova8.pathql.pathPattern.PathPatternParser.QueryStringContext) PathElement(com.inova8.pathql.element.PathElement) PathPatternParser(com.inova8.pathql.pathPattern.PathPatternParser) PathPatternException(com.inova8.pathql.processor.PathPatternException) PathPatternLexer(com.inova8.pathql.pathPattern.PathPatternLexer) PathErrorListener(com.inova8.pathql.processor.PathErrorListener) CharStream(org.antlr.v4.runtime.CharStream)

Example 2 with PathErrorListener

use of com.inova8.pathql.processor.PathErrorListener in project com.inova8.intelligentgraph by peterjohnlawrence.

the class PathPatternTests method test_11.

/**
 * Test 11.
 */
@Test
@Order(11)
void test_11() {
    try {
        String expression = ":Location@:appearsOn[eq id:Calc2Graph2]#";
        CharStream input = CharStreams.fromString(expression);
        PathPatternLexer lexer = new PathPatternLexer(input);
        PathErrorListener errorListener = new PathErrorListener(expression);
        lexer.removeErrorListeners();
        lexer.addErrorListener(errorListener);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PathPatternParser parser = new PathPatternParser(tokens);
        parser.removeErrorListeners();
        parser.addErrorListener(errorListener);
        PathPatternContext pathPatternTree = parser.pathPattern();
        PathPatternVisitor pathPatternVisitor = new PathPatternVisitor(repositoryContext);
        PathElement element = pathPatternVisitor.visit(pathPatternTree);
        assertEquals("<http://default/Location>@<http://default/appearsOn>[eq <http://id/Calc2Graph2> ]#", element.toString());
    } catch (Exception e) {
        assertEquals("<http://default/Location>@<http://default/appearsOn>[eq <http://id/Calc2Graph2> ;]#", "");
    }
}
Also used : PathPatternVisitor(com.inova8.pathql.processor.PathPatternVisitor) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) PathElement(com.inova8.pathql.element.PathElement) PathPatternParser(com.inova8.pathql.pathPattern.PathPatternParser) PathPatternLexer(com.inova8.pathql.pathPattern.PathPatternLexer) PathErrorListener(com.inova8.pathql.processor.PathErrorListener) PathPatternContext(com.inova8.pathql.pathPattern.PathPatternParser.PathPatternContext) CharStream(org.antlr.v4.runtime.CharStream) RecognitionException(org.antlr.v4.runtime.RecognitionException) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Example 3 with PathErrorListener

use of com.inova8.pathql.processor.PathErrorListener in project com.inova8.intelligentgraph by peterjohnlawrence.

the class PathPatternQueryExpressionTests method test_11.

/**
 * Test 11.
 */
@Test
@Order(11)
void test_11() {
    try {
        String expression = ":Location@:appearsOn[eq id:Calc2Graph2]#";
        CharStream input = CharStreams.fromString(expression);
        PathPatternLexer lexer = new PathPatternLexer(input);
        PathErrorListener errorListener = new PathErrorListener(expression);
        lexer.removeErrorListeners();
        lexer.addErrorListener(errorListener);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PathPatternParser parser = new PathPatternParser(tokens);
        parser.removeErrorListeners();
        parser.addErrorListener(errorListener);
        PathPatternContext pathPatternTree = parser.pathPattern();
        PathPatternVisitor pathPatternVisitor = new PathPatternVisitor(repositoryContext);
        PathElement element = pathPatternVisitor.visit(pathPatternTree);
        // Query.assertEqualsWOSpaces
        assertEquals("Join\r\n" + "   Join\r\n" + "      StatementPattern\r\n" + "         Variable (name=rnull)\r\n" + "         Variable (name=subjectnull, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#subject)\r\n" + "         Variable (name=nnull)\r\n" + "      StatementPattern\r\n" + "         Variable (name=rnull)\r\n" + "         Variable (name=propertynull, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate)\r\n" + "         Variable (name=p_nnull_nnull, value=http://default/appearsOn)\r\n" + "   StatementPattern\r\n" + "      Variable (name=rnull)\r\n" + "      Variable (name=objectnull, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#object)\r\n" + "      Variable (name=nnull, value=http://id/Calc2Graph2)\r\n" + "", element.pathPatternQuery().toString());
    } catch (Exception e) {
        Query.assertEqualsWOSpaces("<http://default/Location>@<http://default/appearsOn>[eq <http://id/Calc2Graph2> ;]#", "");
    }
}
Also used : PathPatternVisitor(com.inova8.pathql.processor.PathPatternVisitor) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) PathElement(com.inova8.pathql.element.PathElement) PathPatternParser(com.inova8.pathql.pathPattern.PathPatternParser) PathPatternLexer(com.inova8.pathql.pathPattern.PathPatternLexer) PathErrorListener(com.inova8.pathql.processor.PathErrorListener) PathPatternContext(com.inova8.pathql.pathPattern.PathPatternParser.PathPatternContext) CharStream(org.antlr.v4.runtime.CharStream) RecognitionException(org.antlr.v4.runtime.RecognitionException) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Aggregations

PathElement (com.inova8.pathql.element.PathElement)3 PathPatternLexer (com.inova8.pathql.pathPattern.PathPatternLexer)3 PathPatternParser (com.inova8.pathql.pathPattern.PathPatternParser)3 PathErrorListener (com.inova8.pathql.processor.PathErrorListener)3 CharStream (org.antlr.v4.runtime.CharStream)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)3 PathPatternContext (com.inova8.pathql.pathPattern.PathPatternParser.PathPatternContext)2 PathPatternVisitor (com.inova8.pathql.processor.PathPatternVisitor)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 Order (org.junit.jupiter.api.Order)2 Test (org.junit.jupiter.api.Test)2 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)2 QueryStringContext (com.inova8.pathql.pathPattern.PathPatternParser.QueryStringContext)1 PathPatternException (com.inova8.pathql.processor.PathPatternException)1