Search in sources :

Example 1 with Dataset

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

use of org.eclipse.rdf4j.query.Dataset in project rdf4j by eclipse.

the class ContextAwareConnectionTest method testTupleQuery.

@Test
public void testTupleQuery() throws Exception {
    RepositoryConnection stub = new RepositoryConnectionStub() {

        @Override
        public TupleQuery prepareTupleQuery(QueryLanguage ql, String query, String baseURI) throws MalformedQueryException, RepositoryException {
            assertEquals(SPARQL, ql);
            assertEquals(queryString, query);
            return new TupleQueryStub() {

                @Override
                public void setDataset(Dataset dataset) {
                    Set<IRI> contexts = Collections.singleton(context);
                    assertEquals(contexts, dataset.getDefaultGraphs());
                    super.setDataset(dataset);
                }
            };
        }
    };
    Repository repo = stub.getRepository();
    ContextAwareConnection con = new ContextAwareConnection(repo, stub);
    con.setReadContexts(context);
    con.setQueryLanguage(SERQL);
    con.prepareTupleQuery(SPARQL, queryString, null);
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) IRI(org.eclipse.rdf4j.model.IRI) Repository(org.eclipse.rdf4j.repository.Repository) Dataset(org.eclipse.rdf4j.query.Dataset) QueryLanguage(org.eclipse.rdf4j.query.QueryLanguage) Test(org.junit.Test)

Example 3 with Dataset

use of org.eclipse.rdf4j.query.Dataset in project rdf4j by eclipse.

the class ContextAwareConnectionTest method testGraphQuery.

@Test
public void testGraphQuery() throws Exception {
    RepositoryConnection stub = new RepositoryConnectionStub() {

        @Override
        public GraphQuery prepareGraphQuery(QueryLanguage ql, String query, String baseURI) throws MalformedQueryException, RepositoryException {
            assertEquals(SPARQL, ql);
            assertEquals(queryString, query);
            return new GraphQueryStub() {

                @Override
                public void setDataset(Dataset dataset) {
                    Set<IRI> contexts = Collections.singleton(context);
                    assertEquals(contexts, dataset.getDefaultGraphs());
                    super.setDataset(dataset);
                }
            };
        }
    };
    Repository repo = stub.getRepository();
    ContextAwareConnection con = new ContextAwareConnection(repo, stub);
    con.setReadContexts(context);
    con.setQueryLanguage(SERQL);
    con.prepareGraphQuery(SPARQL, queryString, null);
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) IRI(org.eclipse.rdf4j.model.IRI) Repository(org.eclipse.rdf4j.repository.Repository) Dataset(org.eclipse.rdf4j.query.Dataset) QueryLanguage(org.eclipse.rdf4j.query.QueryLanguage) Test(org.junit.Test)

Example 4 with Dataset

use of org.eclipse.rdf4j.query.Dataset in project rdf4j by eclipse.

the class TransactionWriter method serialize.

protected void serialize(SPARQLUpdateOperation op, XMLWriter xmlWriter) throws IOException {
    String baseURI = op.getBaseURI();
    if (baseURI != null) {
        xmlWriter.setAttribute(TransactionXMLConstants.BASE_URI_ATT, baseURI);
    }
    xmlWriter.setAttribute(TransactionXMLConstants.INCLUDE_INFERRED_ATT, op.isIncludeInferred());
    xmlWriter.startTag(TransactionXMLConstants.SPARQL_UPDATE_TAG);
    // serialize update string
    String updateString = op.getUpdateString();
    xmlWriter.textElement(TransactionXMLConstants.UPDATE_STRING_TAG, updateString);
    // serialize dataset definition (if any)
    Dataset dataset = op.getDataset();
    if (dataset != null) {
        xmlWriter.startTag(TransactionXMLConstants.DATASET_TAG);
        xmlWriter.startTag(TransactionXMLConstants.DEFAULT_GRAPHS_TAG);
        for (IRI defaultGraph : dataset.getDefaultGraphs()) {
            xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, defaultGraph.stringValue());
        }
        xmlWriter.endTag(TransactionXMLConstants.DEFAULT_GRAPHS_TAG);
        xmlWriter.startTag(TransactionXMLConstants.NAMED_GRAPHS_TAG);
        for (IRI namedGraph : dataset.getNamedGraphs()) {
            xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, namedGraph.stringValue());
        }
        xmlWriter.endTag(TransactionXMLConstants.NAMED_GRAPHS_TAG);
        xmlWriter.startTag(TransactionXMLConstants.DEFAULT_REMOVE_GRAPHS_TAG);
        for (IRI defaultRemoveGraph : dataset.getDefaultRemoveGraphs()) {
            xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, defaultRemoveGraph.stringValue());
        }
        xmlWriter.endTag(TransactionXMLConstants.DEFAULT_REMOVE_GRAPHS_TAG);
        if (dataset.getDefaultInsertGraph() != null) {
            xmlWriter.textElement(TransactionXMLConstants.DEFAULT_INSERT_GRAPH, dataset.getDefaultInsertGraph().stringValue());
        }
        xmlWriter.endTag(TransactionXMLConstants.DATASET_TAG);
    }
    if (op.getBindings() != null && op.getBindings().length > 0) {
        xmlWriter.startTag(TransactionXMLConstants.BINDINGS);
        for (Binding binding : op.getBindings()) {
            if (binding.getName() != null && binding.getValue() != null && binding.getValue().stringValue() != null) {
                if (binding.getValue() instanceof IRI) {
                    xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
                    xmlWriter.textElement(TransactionXMLConstants.BINDING_URI, binding.getValue().stringValue());
                }
                if (binding.getValue() instanceof BNode) {
                    xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
                    xmlWriter.textElement(TransactionXMLConstants.BINDING_BNODE, binding.getValue().stringValue());
                }
                if (binding.getValue() instanceof Literal) {
                    xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
                    Literal literal = (Literal) binding.getValue();
                    if (Literals.isLanguageLiteral(literal)) {
                        xmlWriter.setAttribute(TransactionXMLConstants.LANGUAGE_ATT, literal.getLanguage().get());
                    } else {
                        xmlWriter.setAttribute(TransactionXMLConstants.DATA_TYPE_ATT, literal.getDatatype().stringValue());
                    }
                    xmlWriter.textElement(TransactionXMLConstants.BINDING_LITERAL, binding.getValue().stringValue());
                }
            }
        }
        xmlWriter.endTag(TransactionXMLConstants.BINDINGS);
    }
    xmlWriter.endTag(TransactionXMLConstants.SPARQL_UPDATE_TAG);
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Dataset(org.eclipse.rdf4j.query.Dataset) Literal(org.eclipse.rdf4j.model.Literal)

Example 5 with Dataset

use of org.eclipse.rdf4j.query.Dataset in project rdf4j by eclipse.

the class ContextAwareConnectionTest method testQuery.

@Test
public void testQuery() throws Exception {
    RepositoryConnection stub = new RepositoryConnectionStub() {

        @Override
        public Query prepareQuery(QueryLanguage ql, String query, String baseURI) throws MalformedQueryException, RepositoryException {
            assertEquals(SPARQL, ql);
            assertEquals(queryString, query);
            return new QueryStub() {

                @Override
                public void setDataset(Dataset dataset) {
                    Set<IRI> contexts = Collections.singleton(context);
                    assertEquals(contexts, dataset.getDefaultGraphs());
                    super.setDataset(dataset);
                }
            };
        }
    };
    Repository repo = stub.getRepository();
    ContextAwareConnection con = new ContextAwareConnection(repo, stub);
    con.setReadContexts(context);
    con.setQueryLanguage(SERQL);
    con.prepareQuery(SPARQL, queryString, null);
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) IRI(org.eclipse.rdf4j.model.IRI) Repository(org.eclipse.rdf4j.repository.Repository) Dataset(org.eclipse.rdf4j.query.Dataset) QueryLanguage(org.eclipse.rdf4j.query.QueryLanguage) Test(org.junit.Test)

Aggregations

Dataset (org.eclipse.rdf4j.query.Dataset)6 IRI (org.eclipse.rdf4j.model.IRI)4 QueryLanguage (org.eclipse.rdf4j.query.QueryLanguage)3 Repository (org.eclipse.rdf4j.repository.Repository)3 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)3 Test (org.junit.Test)3 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)2 ParseException (org.eclipse.rdf4j.query.parser.sparql.ast.ParseException)2 TokenMgrError (org.eclipse.rdf4j.query.parser.sparql.ast.TokenMgrError)2 HashSet (java.util.HashSet)1 BNode (org.eclipse.rdf4j.model.BNode)1 Literal (org.eclipse.rdf4j.model.Literal)1 Binding (org.eclipse.rdf4j.query.Binding)1 IncompatibleOperationException (org.eclipse.rdf4j.query.IncompatibleOperationException)1 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)1 UpdateExpr (org.eclipse.rdf4j.query.algebra.UpdateExpr)1 ParsedBooleanQuery (org.eclipse.rdf4j.query.parser.ParsedBooleanQuery)1 ParsedDescribeQuery (org.eclipse.rdf4j.query.parser.ParsedDescribeQuery)1 ParsedGraphQuery (org.eclipse.rdf4j.query.parser.ParsedGraphQuery)1 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)1