Search in sources :

Example 6 with PathPatternVisitor

use of com.inova8.pathql.processor.PathPatternVisitor 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 7 with PathPatternVisitor

use of com.inova8.pathql.processor.PathPatternVisitor 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 8 with PathPatternVisitor

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

the class QueryStringParserTests method prepareElement.

/**
 * Prepare element.
 *
 * @param input the input
 * @return the path element
 * @throws RecognitionException the recognition exception
 */
private PathElement prepareElement(CharStream input) throws RecognitionException {
    PathPatternLexer lexer = new PathPatternLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    PathPatternParser parser = new PathPatternParser(tokens);
    // PathPatternContext pathPatternTree = parser.pathPattern();
    QueryStringContext queryStringTree = parser.queryString();
    PathPatternVisitor pathPatternVisitor = new PathPatternVisitor(repositoryContext);
    PathElement element = pathPatternVisitor.visit(queryStringTree);
    return element;
}
Also used : PathPatternVisitor(com.inova8.pathql.processor.PathPatternVisitor) 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) PathPatternLexer(com.inova8.pathql.pathPattern.PathPatternLexer)

Example 9 with PathPatternVisitor

use of com.inova8.pathql.processor.PathPatternVisitor 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)9 PathPatternLexer (com.inova8.pathql.pathPattern.PathPatternLexer)8 PathPatternParser (com.inova8.pathql.pathPattern.PathPatternParser)8 PathPatternVisitor (com.inova8.pathql.processor.PathPatternVisitor)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)8 CharStream (org.antlr.v4.runtime.CharStream)5 PathPatternContext (com.inova8.pathql.pathPattern.PathPatternParser.PathPatternContext)4 PathErrorListener (com.inova8.pathql.processor.PathErrorListener)3 RecognitionException (org.antlr.v4.runtime.RecognitionException)3 QueryStringContext (com.inova8.pathql.pathPattern.PathPatternParser.QueryStringContext)2 PathPatternException (com.inova8.pathql.processor.PathPatternException)2 Order (org.junit.jupiter.api.Order)2 Test (org.junit.jupiter.api.Test)2 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)2 IriRefValueElement (com.inova8.pathql.element.IriRefValueElement)1 PredicateElement (com.inova8.pathql.element.PredicateElement)1 IriRefContext (com.inova8.pathql.pathPattern.PathPatternParser.IriRefContext)1 PathEltOrInverseContext (com.inova8.pathql.pathPattern.PathPatternParser.PathEltOrInverseContext)1