Search in sources :

Example 1 with SimpleDataset

use of org.eclipse.rdf4j.query.impl.SimpleDataset 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 SimpleDataset

use of org.eclipse.rdf4j.query.impl.SimpleDataset in project rdf4j by eclipse.

the class ContextAwareConnection method initOperation.

private <O extends Operation> O initOperation(O op) {
    IRI[] readContexts = getReadContexts();
    IRI[] removeContexts = getRemoveContexts();
    IRI insertContext = getInsertContext();
    if (readContexts.length > 0 || removeContexts.length > 0 || insertContext != null) {
        SimpleDataset ds = new SimpleDataset();
        for (IRI graph : readContexts) {
            ds.addDefaultGraph(graph);
        }
        for (IRI graph : removeContexts) {
            ds.addDefaultRemoveGraph(graph);
        }
        ds.setDefaultInsertGraph(insertContext);
        op.setDataset(ds);
    }
    op.setIncludeInferred(isIncludeInferred());
    return op;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset)

Example 3 with SimpleDataset

use of org.eclipse.rdf4j.query.impl.SimpleDataset in project rdf4j by eclipse.

the class SPARQLConnection method setBindings.

/* protected/private methods */
private void setBindings(Query query, Resource subj, IRI pred, Value obj, Resource... contexts) throws RepositoryException {
    if (subj != null) {
        query.setBinding("s", subj);
    }
    if (pred != null) {
        query.setBinding("p", pred);
    }
    if (obj != null) {
        query.setBinding("o", obj);
    }
    if (contexts != null && contexts.length > 0) {
        SimpleDataset dataset = new SimpleDataset();
        for (Resource ctx : contexts) {
            if (ctx == null || ctx instanceof IRI) {
                dataset.addDefaultGraph((IRI) ctx);
            } else {
                throw new RepositoryException("Contexts must be URIs");
            }
        }
        query.setDataset(dataset);
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Resource(org.eclipse.rdf4j.model.Resource) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset)

Example 4 with SimpleDataset

use of org.eclipse.rdf4j.query.impl.SimpleDataset in project rdf4j by eclipse.

the class TransactionSAXParser method startTag.

@Override
public void startTag(String tagName, Map<String, String> atts, String text) throws SAXException {
    if (TransactionXMLConstants.URI_TAG.equals(tagName)) {
        parsedValues.add(valueFactory.createIRI(text));
    } else if (TransactionXMLConstants.BNODE_TAG.equals(tagName)) {
        parsedValues.add(valueFactory.createBNode(text));
    } else if (TransactionXMLConstants.LITERAL_TAG.equals(tagName)) {
        String lang = atts.get(TransactionXMLConstants.LANG_ATT);
        String datatype = atts.get(TransactionXMLConstants.DATATYPE_ATT);
        String encoding = atts.get(TransactionXMLConstants.ENCODING_ATT);
        if (encoding != null && "base64".equalsIgnoreCase(encoding)) {
            text = new String(javax.xml.bind.DatatypeConverter.parseBase64Binary(text));
        }
        Literal lit;
        if (lang != null) {
            lit = valueFactory.createLiteral(text, lang);
        } else if (datatype != null) {
            IRI dtURI = valueFactory.createIRI(datatype);
            lit = valueFactory.createLiteral(text, dtURI);
        } else {
            lit = valueFactory.createLiteral(text);
        }
        parsedValues.add(lit);
    } else if (TransactionXMLConstants.NULL_TAG.equals(tagName)) {
        parsedValues.add(null);
    } else if (TransactionXMLConstants.SET_NAMESPACE_TAG.equals(tagName)) {
        String prefix = atts.get(TransactionXMLConstants.PREFIX_ATT);
        String name = atts.get(TransactionXMLConstants.NAME_ATT);
        txn.add(new SetNamespaceOperation(prefix, name));
    } else if (TransactionXMLConstants.REMOVE_NAMESPACE_TAG.equals(tagName)) {
        String prefix = atts.get(TransactionXMLConstants.PREFIX_ATT);
        txn.add(new RemoveNamespaceOperation(prefix));
    } else if (TransactionXMLConstants.CLEAR_NAMESPACES_TAG.equals(tagName)) {
        txn.add(new ClearNamespacesOperation());
    } else if (TransactionXMLConstants.SPARQL_UPDATE_TAG.equals(tagName)) {
        if (currentSPARQLUpdate != null) {
            throw new SAXException("unexpected start of SPARQL Update operation");
        }
        currentSPARQLUpdate = new SPARQLUpdateOperation();
        String baseURI = atts.get(TransactionXMLConstants.BASE_URI_ATT);
        boolean includeInferred = Boolean.parseBoolean(atts.get(TransactionXMLConstants.INCLUDE_INFERRED_ATT));
        currentSPARQLUpdate.setIncludeInferred(includeInferred);
        currentSPARQLUpdate.setBaseURI(baseURI);
    } else if (TransactionXMLConstants.UPDATE_STRING_TAG.equals(tagName)) {
        currentSPARQLUpdate.setUpdateString(text);
    } else if (TransactionXMLConstants.DATASET_TAG.equals(tagName)) {
        currentDataset = new SimpleDataset();
    } else if (TransactionXMLConstants.DEFAULT_INSERT_GRAPH.equals(tagName)) {
        currentDataset.setDefaultInsertGraph(valueFactory.createIRI(text));
    } else if (TransactionXMLConstants.GRAPH_TAG.equals(tagName)) {
        parsedValues.add(valueFactory.createIRI(text));
    } else if (TransactionXMLConstants.BINDINGS.equals(tagName)) {
        if (bindings != null) {
            throw new SAXException("unexpected start of SPARQL Update operation bindings");
        }
        bindings = new ArrayList<Binding>();
    } else if (TransactionXMLConstants.BINDING_URI.equals(tagName) || TransactionXMLConstants.BINDING_BNODE.equals(tagName) || TransactionXMLConstants.BINDING_LITERAL.equals(tagName)) {
        if (bindings == null) {
            throw new SAXException("unexpected start of SPARQL Update operation binding (without <bindings>)");
        }
        String value = text;
        String name = atts.get(TransactionXMLConstants.NAME_ATT);
        if (name != null && value != null) {
            Value v;
            if (TransactionXMLConstants.BINDING_URI.equals(tagName)) {
                v = valueFactory.createIRI(value);
            } else if (TransactionXMLConstants.BINDING_BNODE.equals(tagName)) {
                v = valueFactory.createBNode(value);
            } else {
                String language = atts.get(TransactionXMLConstants.LANGUAGE_ATT);
                String dataType = atts.get(TransactionXMLConstants.DATA_TYPE_ATT);
                if (language != null) {
                    v = valueFactory.createLiteral(value, language);
                } else if (dataType != null) {
                    v = valueFactory.createLiteral(value, valueFactory.createIRI(dataType));
                } else {
                    v = valueFactory.createLiteral(value);
                }
            }
            bindings.add(new SimpleBinding(name, v));
        }
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) IRI(org.eclipse.rdf4j.model.IRI) ClearNamespacesOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.ClearNamespacesOperation) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) SPARQLUpdateOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.SPARQLUpdateOperation) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset) RemoveNamespaceOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.RemoveNamespaceOperation) SAXException(org.xml.sax.SAXException) SetNamespaceOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.SetNamespaceOperation) Literal(org.eclipse.rdf4j.model.Literal) Value(org.eclipse.rdf4j.model.Value)

Example 5 with SimpleDataset

use of org.eclipse.rdf4j.query.impl.SimpleDataset 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)

Aggregations

IRI (org.eclipse.rdf4j.model.IRI)5 SimpleDataset (org.eclipse.rdf4j.query.impl.SimpleDataset)5 ClearNamespacesOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.ClearNamespacesOperation)1 RemoveNamespaceOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.RemoveNamespaceOperation)1 SPARQLUpdateOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.SPARQLUpdateOperation)1 SetNamespaceOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.SetNamespaceOperation)1 Literal (org.eclipse.rdf4j.model.Literal)1 Resource (org.eclipse.rdf4j.model.Resource)1 Value (org.eclipse.rdf4j.model.Value)1 Binding (org.eclipse.rdf4j.query.Binding)1 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)1 Distinct (org.eclipse.rdf4j.query.algebra.Distinct)1 Order (org.eclipse.rdf4j.query.algebra.Order)1 Reduced (org.eclipse.rdf4j.query.algebra.Reduced)1 Slice (org.eclipse.rdf4j.query.algebra.Slice)1 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)1 UnaryTupleOperator (org.eclipse.rdf4j.query.algebra.UnaryTupleOperator)1 StatementPatternCollector (org.eclipse.rdf4j.query.algebra.helpers.StatementPatternCollector)1 VarNameCollector (org.eclipse.rdf4j.query.algebra.helpers.VarNameCollector)1 SimpleBinding (org.eclipse.rdf4j.query.impl.SimpleBinding)1