Search in sources :

Example 1 with ASTOperation

use of org.eclipse.rdf4j.query.parser.sparql.ast.ASTOperation in project rdf4j by eclipse.

the class DatasetDeclProcessor method process.

/**
 * Extracts a SPARQL {@link Dataset} from an ASTQueryContainer, if one is contained. Returns null
 * otherwise.
 *
 * @param qc
 *        The query model to resolve relative URIs in.
 * @throws MalformedQueryException
 *         If DatasetClause does not contain a valid URI.
 */
public static Dataset process(ASTOperationContainer qc) throws MalformedQueryException {
    SimpleDataset dataset = null;
    ASTOperation op = qc.getOperation();
    if (op != null) {
        List<ASTDatasetClause> datasetClauses = op.getDatasetClauseList();
        if (!datasetClauses.isEmpty()) {
            dataset = new SimpleDataset();
            for (ASTDatasetClause dc : datasetClauses) {
                ASTIRI astIri = dc.jjtGetChild(ASTIRI.class);
                try {
                    IRI uri = SESAME.NIL;
                    if (astIri != null) {
                        uri = SimpleValueFactory.getInstance().createIRI(astIri.getValue());
                    }
                    boolean withClause = false;
                    if (op instanceof ASTModify) {
                        if (dc.equals(((ASTModify) op).getWithClause())) {
                            withClause = true;
                            dataset.setDefaultInsertGraph(uri);
                            dataset.addDefaultRemoveGraph(uri);
                        }
                    }
                    // clauses.
                    if (!withClause || datasetClauses.size() == 1) {
                        if (dc.isNamed()) {
                            dataset.addNamedGraph(uri);
                        } else {
                            dataset.addDefaultGraph(uri);
                        }
                    }
                } catch (IllegalArgumentException e) {
                    throw new MalformedQueryException(e.getMessage(), e);
                }
            }
        }
    }
    return dataset;
}
Also used : ASTIRI(org.eclipse.rdf4j.query.parser.sparql.ast.ASTIRI) IRI(org.eclipse.rdf4j.model.IRI) ASTModify(org.eclipse.rdf4j.query.parser.sparql.ast.ASTModify) ASTOperation(org.eclipse.rdf4j.query.parser.sparql.ast.ASTOperation) ASTDatasetClause(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDatasetClause) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset) ASTIRI(org.eclipse.rdf4j.query.parser.sparql.ast.ASTIRI)

Example 2 with ASTOperation

use of org.eclipse.rdf4j.query.parser.sparql.ast.ASTOperation in project rdf4j by eclipse.

the class WildcardProjectionProcessor method process.

public static void process(ASTOperationContainer container) throws MalformedQueryException {
    ASTOperation operation = container.getOperation();
    // scan for nested SELECT clauses in the operation's WHERE clause
    if (operation != null) {
        ASTWhereClause whereClause = operation.getWhereClause();
        // DESCRIBE queries and certain update operations can be without a WHERE clause
        if (whereClause != null) {
            SelectClauseCollector collector = new SelectClauseCollector();
            try {
                whereClause.jjtAccept(collector, null);
                Set<ASTSelect> selectClauses = collector.getSelectClauses();
                for (ASTSelect selectClause : selectClauses) {
                    if (selectClause.isWildcard()) {
                        ASTSelectQuery q = (ASTSelectQuery) selectClause.jjtGetParent();
                        addQueryVars(q.getWhereClause(), selectClause);
                        selectClause.setWildcard(false);
                    }
                }
            } catch (VisitorException e) {
                throw new MalformedQueryException(e);
            }
        }
    }
    if (operation instanceof ASTSelectQuery) {
        // check for wildcard in upper SELECT query
        ASTSelectQuery selectQuery = (ASTSelectQuery) operation;
        ASTSelect selectClause = selectQuery.getSelect();
        if (selectClause.isWildcard()) {
            addQueryVars(selectQuery.getWhereClause(), selectClause);
            selectClause.setWildcard(false);
        }
    } else if (operation instanceof ASTDescribeQuery) {
        // check for possible wildcard in DESCRIBE query
        ASTDescribeQuery describeQuery = (ASTDescribeQuery) operation;
        ASTDescribe describeClause = describeQuery.getDescribe();
        if (describeClause.isWildcard()) {
            addQueryVars(describeQuery.getWhereClause(), describeClause);
            describeClause.setWildcard(false);
        }
    }
}
Also used : ASTOperation(org.eclipse.rdf4j.query.parser.sparql.ast.ASTOperation) ASTWhereClause(org.eclipse.rdf4j.query.parser.sparql.ast.ASTWhereClause) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ASTSelectQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelectQuery) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException) ASTDescribeQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery) ASTDescribe(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribe) ASTSelect(org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelect)

Aggregations

MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)2 ASTOperation (org.eclipse.rdf4j.query.parser.sparql.ast.ASTOperation)2 IRI (org.eclipse.rdf4j.model.IRI)1 SimpleDataset (org.eclipse.rdf4j.query.impl.SimpleDataset)1 ASTDatasetClause (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDatasetClause)1 ASTDescribe (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribe)1 ASTDescribeQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery)1 ASTIRI (org.eclipse.rdf4j.query.parser.sparql.ast.ASTIRI)1 ASTModify (org.eclipse.rdf4j.query.parser.sparql.ast.ASTModify)1 ASTSelect (org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelect)1 ASTSelectQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelectQuery)1 ASTWhereClause (org.eclipse.rdf4j.query.parser.sparql.ast.ASTWhereClause)1 VisitorException (org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)1