001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.repository.sail;
007:
008: import info.aduna.iteration.CloseableIteration;
009: import info.aduna.iteration.ConvertingIteration;
010: import info.aduna.iteration.FilterIteration;
011:
012: import org.openrdf.model.Resource;
013: import org.openrdf.model.Statement;
014: import org.openrdf.model.URI;
015: import org.openrdf.model.Value;
016: import org.openrdf.model.ValueFactory;
017: import org.openrdf.query.BindingSet;
018: import org.openrdf.query.GraphQuery;
019: import org.openrdf.query.GraphQueryResult;
020: import org.openrdf.query.QueryEvaluationException;
021: import org.openrdf.query.QueryResultUtil;
022: import org.openrdf.query.algebra.TupleExpr;
023: import org.openrdf.query.impl.GraphQueryResultImpl;
024: import org.openrdf.query.parser.ParsedGraphQuery;
025: import org.openrdf.rio.RDFHandler;
026: import org.openrdf.rio.RDFHandlerException;
027: import org.openrdf.sail.SailConnection;
028: import org.openrdf.sail.SailException;
029:
030: /**
031: * @author Arjohn Kampman
032: */
033: public class SailGraphQuery extends SailQuery implements GraphQuery {
034:
035: protected SailGraphQuery(ParsedGraphQuery tupleQuery,
036: SailRepositoryConnection con) {
037: super (tupleQuery, con);
038: }
039:
040: @Override
041: public ParsedGraphQuery getParsedQuery() {
042: return (ParsedGraphQuery) super .getParsedQuery();
043: }
044:
045: public GraphQueryResult evaluate() throws QueryEvaluationException {
046: TupleExpr tupleExpr = getParsedQuery().getTupleExpr();
047:
048: try {
049: SailConnection sailCon = getConnection()
050: .getSailConnection();
051: CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter = sailCon
052: .evaluate(tupleExpr, getActiveDataset(),
053: getBindings(), getIncludeInferred());
054:
055: // Filters out all partial and invalid matches
056: bindingsIter = new FilterIteration<BindingSet, QueryEvaluationException>(
057: bindingsIter) {
058:
059: @Override
060: protected boolean accept(BindingSet bindingSet) {
061: Value context = bindingSet.getValue("context");
062:
063: return bindingSet.getValue("subject") instanceof Resource
064: && bindingSet.getValue("predicate") instanceof URI
065: && bindingSet.getValue("object") instanceof Value
066: && (context == null || context instanceof Resource);
067: }
068: };
069:
070: final ValueFactory vf = getConnection().getRepository()
071: .getValueFactory();
072:
073: // Convert the BindingSet objects to actual RDF statements
074: CloseableIteration<Statement, QueryEvaluationException> stIter;
075: stIter = new ConvertingIteration<BindingSet, Statement, QueryEvaluationException>(
076: bindingsIter) {
077:
078: @Override
079: protected Statement convert(BindingSet bindingSet) {
080: Resource subject = (Resource) bindingSet
081: .getValue("subject");
082: URI predicate = (URI) bindingSet
083: .getValue("predicate");
084: Value object = bindingSet.getValue("object");
085: Resource context = (Resource) bindingSet
086: .getValue("context");
087:
088: if (context == null) {
089: return vf.createStatement(subject, predicate,
090: object);
091: } else {
092: return vf.createStatement(subject, predicate,
093: object, context);
094: }
095: }
096: };
097:
098: return new GraphQueryResultImpl(getParsedQuery()
099: .getQueryNamespaces(), stIter);
100: } catch (SailException e) {
101: throw new QueryEvaluationException(e.getMessage(), e);
102: }
103: }
104:
105: public void evaluate(RDFHandler handler)
106: throws QueryEvaluationException, RDFHandlerException {
107: GraphQueryResult queryResult = evaluate();
108: QueryResultUtil.report(queryResult, handler);
109: }
110: }
|