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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations