Search in sources :

Example 1 with ParsedGraphQuery

use of org.eclipse.rdf4j.query.parser.ParsedGraphQuery in project rdf4j by eclipse.

the class SPARQLParser method parseQuery.

@Override
public ParsedQuery parseQuery(String queryStr, String baseURI) throws MalformedQueryException {
    try {
        ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr);
        StringEscapesProcessor.process(qc);
        BaseDeclProcessor.process(qc, baseURI);
        Map<String, String> prefixes = PrefixDeclProcessor.process(qc);
        WildcardProjectionProcessor.process(qc);
        BlankNodeVarProcessor.process(qc);
        if (qc.containsQuery()) {
            // handle query operation
            TupleExpr tupleExpr = buildQueryModel(qc);
            ParsedQuery query;
            ASTQuery queryNode = qc.getQuery();
            if (queryNode instanceof ASTSelectQuery) {
                query = new ParsedTupleQuery(queryStr, tupleExpr);
            } else if (queryNode instanceof ASTConstructQuery) {
                query = new ParsedGraphQuery(queryStr, tupleExpr, prefixes);
            } else if (queryNode instanceof ASTAskQuery) {
                query = new ParsedBooleanQuery(queryStr, tupleExpr);
            } else if (queryNode instanceof ASTDescribeQuery) {
                query = new ParsedDescribeQuery(queryStr, tupleExpr, prefixes);
            } else {
                throw new RuntimeException("Unexpected query type: " + queryNode.getClass());
            }
            // Handle dataset declaration
            Dataset dataset = DatasetDeclProcessor.process(qc);
            if (dataset != null) {
                query.setDataset(dataset);
            }
            return query;
        } else {
            throw new IncompatibleOperationException("supplied string is not a query operation");
        }
    } catch (ParseException e) {
        throw new MalformedQueryException(e.getMessage(), e);
    } catch (TokenMgrError e) {
        throw new MalformedQueryException(e.getMessage(), e);
    }
}
Also used : ASTAskQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTAskQuery) ParsedDescribeQuery(org.eclipse.rdf4j.query.parser.ParsedDescribeQuery) IncompatibleOperationException(org.eclipse.rdf4j.query.IncompatibleOperationException) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Dataset(org.eclipse.rdf4j.query.Dataset) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) ASTQueryContainer(org.eclipse.rdf4j.query.parser.sparql.ast.ASTQueryContainer) TokenMgrError(org.eclipse.rdf4j.query.parser.sparql.ast.TokenMgrError) ASTDescribeQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTQuery) ParsedBooleanQuery(org.eclipse.rdf4j.query.parser.ParsedBooleanQuery) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ASTConstructQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTConstructQuery) ASTSelectQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelectQuery) ParseException(org.eclipse.rdf4j.query.parser.sparql.ast.ParseException) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Example 2 with ParsedGraphQuery

use of org.eclipse.rdf4j.query.parser.ParsedGraphQuery in project rdf4j by eclipse.

the class SeRQLParser method parseQuery.

public ParsedQuery parseQuery(String queryStr, String baseURI) throws MalformedQueryException {
    try {
        ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr);
        // Replace deprecated NULL nodes with semantically equivalent
        // alternatives
        NullProcessor.process(qc);
        StringEscapesProcessor.process(qc);
        Map<String, String> namespaces = NamespaceDeclProcessor.process(qc);
        ProjectionProcessor.process(qc);
        qc.jjtAccept(new ProjectionAliasProcessor(), null);
        qc.jjtAccept(new AnonymousVarGenerator(), null);
        // TODO: check use of unbound variables?
        TupleExpr tupleExpr = QueryModelBuilder.buildQueryModel(qc, SimpleValueFactory.getInstance());
        ASTQuery queryNode = qc.getQuery();
        ParsedQuery query;
        if (queryNode instanceof ASTTupleQuery) {
            query = new ParsedTupleQuery(tupleExpr);
        } else if (queryNode instanceof ASTGraphQuery) {
            query = new ParsedGraphQuery(tupleExpr, namespaces);
        } else {
            throw new RuntimeException("Unexpected query type: " + queryNode.getClass());
        }
        return query;
    } catch (ParseException e) {
        throw new MalformedQueryException(e.getMessage(), e);
    } catch (TokenMgrError e) {
        throw new MalformedQueryException(e.getMessage(), e);
    } catch (VisitorException e) {
        throw new MalformedQueryException(e.getMessage(), e);
    }
}
Also used : ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) ASTQueryContainer(org.eclipse.rdf4j.query.parser.serql.ast.ASTQueryContainer) TokenMgrError(org.eclipse.rdf4j.query.parser.serql.ast.TokenMgrError) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTTupleQuery(org.eclipse.rdf4j.query.parser.serql.ast.ASTTupleQuery) ASTQuery(org.eclipse.rdf4j.query.parser.serql.ast.ASTQuery) ASTGraphQuery(org.eclipse.rdf4j.query.parser.serql.ast.ASTGraphQuery) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ParseException(org.eclipse.rdf4j.query.parser.serql.ast.ParseException) VisitorException(org.eclipse.rdf4j.query.parser.serql.ast.VisitorException) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Example 3 with ParsedGraphQuery

use of org.eclipse.rdf4j.query.parser.ParsedGraphQuery in project rdf4j by eclipse.

the class SPARQLParserTest method testParsedGraphQueryRootNode.

@Test
public void testParsedGraphQueryRootNode() throws Exception {
    StringBuilder qb = new StringBuilder();
    qb.append("CONSTRUCT WHERE {?a <foo:bar> \"test\"}");
    ParsedGraphQuery q = (ParsedGraphQuery) parser.parseQuery(qb.toString(), null);
    TupleExpr te = q.getTupleExpr();
    assertNotNull(te);
    assertTrue(te instanceof Projection);
    assertNull(te.getParentNode());
}
Also used : ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) Projection(org.eclipse.rdf4j.query.algebra.Projection) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Test(org.junit.Test)

Example 4 with ParsedGraphQuery

use of org.eclipse.rdf4j.query.parser.ParsedGraphQuery in project rdf4j by eclipse.

the class AbstractQueryBuilder method query.

/**
 * @inheritDoc
 */
public T query() {
    UnaryTupleOperator aRoot = null;
    UnaryTupleOperator aCurr = null;
    if (mLimit != -1 || mOffset != -1) {
        Slice aSlice = new Slice();
        if (mLimit != -1) {
            aSlice.setLimit(mLimit);
        }
        if (mOffset != -1) {
            aSlice.setOffset(mOffset);
        }
        aRoot = aCurr = aSlice;
    }
    if (mOrderByElems != null && !mOrderByElems.isEmpty()) {
        Order aOrder = new Order();
        aOrder.addElements(mOrderByElems);
        if (aRoot == null) {
            aRoot = aCurr = aOrder;
        } else {
            aCurr.setArg(aOrder);
            aCurr = aOrder;
        }
    }
    if (mDistinct) {
        Distinct aDistinct = new Distinct();
        if (aRoot == null) {
            aRoot = aCurr = aDistinct;
        } else {
            aCurr.setArg(aDistinct);
            aCurr = aDistinct;
        }
    }
    if (mReduced) {
        Reduced aReduced = new Reduced();
        if (aRoot == null) {
            aRoot = aCurr = aReduced;
        } else {
            aCurr.setArg(aReduced);
            aCurr = aReduced;
        }
    }
    TupleExpr aJoin = join();
    if (mQuery instanceof ParsedTupleQuery && mProjectionVars.isEmpty()) {
        VarNameCollector aCollector = new VarNameCollector();
        aJoin.visit(aCollector);
        mProjectionVars.addAll(aCollector.getVarNames());
    } else if (mQuery instanceof ParsedGraphQuery && mProjectionPatterns.isEmpty()) {
        StatementPatternCollector aCollector = new StatementPatternCollector();
        aJoin.visit(aCollector);
        mProjectionPatterns.addAll(aCollector.getStatementPatterns());
    }
    UnaryTupleOperator aProjection = projection();
    if (aRoot == null) {
        aRoot = aCurr = aProjection;
    } else {
        aCurr.setArg(aProjection);
    }
    if (aProjection.getArg() == null) {
        aCurr = aProjection;
    } else {
        // I think this is always a safe cast
        aCurr = (UnaryTupleOperator) aProjection.getArg();
    }
    if (aJoin != null) {
        aCurr.setArg(aJoin);
    }
    mQuery.setTupleExpr(aRoot);
    if (!mFrom.isEmpty() || !mFromNamed.isEmpty()) {
        SimpleDataset aDataset = new SimpleDataset();
        for (IRI aFrom : mFrom) {
            aDataset.addDefaultGraph(aFrom);
        }
        for (IRI aFrom : mFromNamed) {
            aDataset.addNamedGraph(aFrom);
        }
        mQuery.setDataset(aDataset);
    }
    return mQuery;
}
Also used : Order(org.eclipse.rdf4j.query.algebra.Order) StatementPatternCollector(org.eclipse.rdf4j.query.algebra.helpers.StatementPatternCollector) IRI(org.eclipse.rdf4j.model.IRI) Distinct(org.eclipse.rdf4j.query.algebra.Distinct) UnaryTupleOperator(org.eclipse.rdf4j.query.algebra.UnaryTupleOperator) Slice(org.eclipse.rdf4j.query.algebra.Slice) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) VarNameCollector(org.eclipse.rdf4j.query.algebra.helpers.VarNameCollector) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset) Reduced(org.eclipse.rdf4j.query.algebra.Reduced) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Example 5 with ParsedGraphQuery

use of org.eclipse.rdf4j.query.parser.ParsedGraphQuery in project rdf4j by eclipse.

the class QueryBuilderFactory method describe.

/**
 * Create a QueryBuilder for creating a describe query
 *
 * @param theVars
 *        the variables to be described
 * @param theValues
 *        the specific bound URI values to be described
 * @return a describe query builder
 */
public static QueryBuilder<ParsedGraphQuery> describe(String[] theVars, Resource... theValues) {
    QueryBuilder<ParsedGraphQuery> aBuilder = new AbstractQueryBuilder<ParsedGraphQuery>(new ParsedDescribeQuery());
    aBuilder.reduced();
    aBuilder.addProjectionVar("descr_subj", "descr_pred", "descr_obj");
    GroupBuilder<?, ?> aGroup = aBuilder.group();
    if (theVars != null) {
        for (String aVar : theVars) {
            Var aVarObj = new Var(aVar);
            aVarObj.setAnonymous(true);
            aGroup.filter().or(new SameTerm(aVarObj, new Var("descr_subj")), new SameTerm(aVarObj, new Var("descr_obj")));
        }
    }
    if (theValues != null) {
        for (Resource aVar : theValues) {
            Var aSubjVar = new Var("descr_subj");
            aSubjVar.setAnonymous(true);
            Var aObjVar = new Var("descr_obj");
            aObjVar.setAnonymous(true);
            aGroup.filter().or(new SameTerm(new ValueConstant(aVar), aSubjVar), new SameTerm(new ValueConstant(aVar), aObjVar));
        }
    }
    aGroup.atom("descr_subj", "descr_pred", "descr_obj");
    return aBuilder;
}
Also used : ParsedDescribeQuery(org.eclipse.rdf4j.query.parser.ParsedDescribeQuery) Var(org.eclipse.rdf4j.query.algebra.Var) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) SameTerm(org.eclipse.rdf4j.query.algebra.SameTerm) ValueConstant(org.eclipse.rdf4j.query.algebra.ValueConstant) Resource(org.eclipse.rdf4j.model.Resource)

Aggregations

ParsedGraphQuery (org.eclipse.rdf4j.query.parser.ParsedGraphQuery)6 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)4 ParsedTupleQuery (org.eclipse.rdf4j.query.parser.ParsedTupleQuery)4 IRI (org.eclipse.rdf4j.model.IRI)2 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)2 ParsedBooleanQuery (org.eclipse.rdf4j.query.parser.ParsedBooleanQuery)2 ParsedDescribeQuery (org.eclipse.rdf4j.query.parser.ParsedDescribeQuery)2 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)2 Resource (org.eclipse.rdf4j.model.Resource)1 Dataset (org.eclipse.rdf4j.query.Dataset)1 IncompatibleOperationException (org.eclipse.rdf4j.query.IncompatibleOperationException)1 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)1 Order (org.eclipse.rdf4j.query.algebra.Order)1 OrderElem (org.eclipse.rdf4j.query.algebra.OrderElem)1 Projection (org.eclipse.rdf4j.query.algebra.Projection)1 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)1 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)1 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)1 SameTerm (org.eclipse.rdf4j.query.algebra.SameTerm)1 Slice (org.eclipse.rdf4j.query.algebra.Slice)1