// Path: src/main/java/org/linkeddatafragments/datasource/IFragmentRequestProcessor.java
// public interface IFragmentRequestProcessor extends Closeable
// {
// 
//     /**
//      *
//      * @param request
//      * @return
//      * @throws IllegalArgumentException
//      */
//     ILinkedDataFragment createRequestedFragment(
//             final ILinkedDataFragmentRequest request )
//                     throws IllegalArgumentException;
// }
// 
// Path: src/main/java/org/linkeddatafragments/fragments/ILinkedDataFragment.java
// public interface ILinkedDataFragment extends Closeable
// {
//     /**
//      * Returns an iterator over the RDF data of this fragment (possibly only
//      * partial if the data is paged, as indicated by {@link #isPageOnly()}).
//      * @return 
//      */
//     StmtIterator getTriples();
// 
//     /**
//      * Returns true if {@link #getTriples()} returns a page of data only.
//      * In this case, {@link #getPageNumber()} can be used to obtain the
//      * corresponding page number.
//      * @return 
//      */
//     boolean isPageOnly();
// 
//     /**
//      * Returns the number of the page of data returned by {@link #getTriples()}
//      * if the data is paged (that is, if {@link #isPageOnly()} returns true).
//      *
//      * If the data is not paged, this method throws an exception.
//      *
//      * @return 
//      * @throws UnsupportedOperationException
//      *         If the data of this fragment is not paged. 
//      */
//     long getPageNumber() throws UnsupportedOperationException;
// 
//     /**
//      * Returns true if {@link #getTriples()} returns a page of data only and
//      * this is the last page of the fragment.
//      *
//      * If the data is not paged (i.e., if {@link #isPageOnly()} returns false),
//      * this method throws an exception.
//      *
//      * @return 
//      * @throws UnsupportedOperationException
//      *         If the data of this fragment is not paged. 
//      */
//     boolean isLastPage() throws UnsupportedOperationException;
// 
//     /**
//      * Returns the maximum number of triples per page if {@link #getTriples()}
//      * returns a page of data only (that is, if {@link #isPageOnly()} returns
//      * true).
//      *
//      * If the data is not paged, this method throws an exception.
//      *
//      * @return 
//      * @throws UnsupportedOperationException
//      *         If the data of this fragment is not paged. 
//      */
//     long getMaxPageSize() throws UnsupportedOperationException;
// 
//     /**
//      * Returns an iterator over the metadata of this fragment.
//      * @return 
//      */
//     StmtIterator getMetadata();
// 
//     /**
//      * Returns an iterator over an RDF description of the controls associated
//      * with this fragment.
//      * @return 
//      */
//     StmtIterator getControls();
// }


import java.io.File;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.QuerySolutionMap;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.Syntax;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.tdb.TDBFactory;
import org.linkeddatafragments.datasource.AbstractRequestProcessorForTriplePatterns;
import org.linkeddatafragments.datasource.IFragmentRequestProcessor;
import org.linkeddatafragments.fragments.ILinkedDataFragment;
import org.linkeddatafragments.fragments.tpf.ITriplePatternElement;
import org.linkeddatafragments.fragments.tpf.ITriplePatternFragmentRequest;

package org.linkeddatafragments.datasource.tdb;



/**
 * Implementation of {@link IFragmentRequestProcessor} that processes
 * {@link ITriplePatternFragmentRequest}s over data stored in Jena TDB.
 *
 * @author <a href="mailto:bart.hanssens@fedict.be">Bart Hanssens</a>
 * @author <a href="http://olafhartig.de">Olaf Hartig</a>
 */
public class JenaTDBBasedRequestProcessorForTPFs
    extends AbstractRequestProcessorForTriplePatterns<RDFNode,String,String>
{
    private final Dataset tdb;
    private final String defaultGraph;
    private final String sparql = "CONSTRUCT WHERE { ?s ?p ?o } " +
                                    "ORDER BY ?s ?p ?o";

    private final String count = "SELECT (COUNT(?s) AS ?count) WHERE { ?s ?p ?o }";

    private final Query query = QueryFactory.create(sparql, Syntax.syntaxSPARQL_11);
    private final Query countQuery = QueryFactory.create(count, Syntax.syntaxSPARQL_11);

    /**
     *
     * @param request
     * @return
     * @throws IllegalArgumentException
     */
    @Override
    protected Worker getTPFSpecificWorker(
            final ITriplePatternFragmentRequest<RDFNode,String,String> request )
                                                throws IllegalArgumentException
    {
        return new Worker( request );
    }

    /**
     *
     */
    protected class Worker
       extends AbstractRequestProcessorForTriplePatterns.Worker<RDFNode,String,String>
    {

        /**
         *
         * @param req
         */
        public Worker(
                final ITriplePatternFragmentRequest<RDFNode,String,String> req )
        {
            super( req );
        }

        /**
         *
         * @param subject
         * @param predicate
         * @param object
         * @param offset
         * @param limit
         * @return
         */
        @Override

// Path: src/main/java/org/linkeddatafragments/datasource/IFragmentRequestProcessor.java
// public interface IFragmentRequestProcessor extends Closeable
// {
// 
//     /**
//      *
//      * @param request
//      * @return
//      * @throws IllegalArgumentException
//      */
//     ILinkedDataFragment createRequestedFragment(
//             final ILinkedDataFragmentRequest request )
//                     throws IllegalArgumentException;
// }
// 
// Path: src/main/java/org/linkeddatafragments/fragments/ILinkedDataFragment.java
// public interface ILinkedDataFragment extends Closeable
// {
//     /**
//      * Returns an iterator over the RDF data of this fragment (possibly only
//      * partial if the data is paged, as indicated by {@link #isPageOnly()}).
//      * @return 
//      */
//     StmtIterator getTriples();
// 
//     /**
//      * Returns true if {@link #getTriples()} returns a page of data only.
//      * In this case, {@link #getPageNumber()} can be used to obtain the
//      * corresponding page number.
//      * @return 
//      */
//     boolean isPageOnly();
// 
//     /**
//      * Returns the number of the page of data returned by {@link #getTriples()}
//      * if the data is paged (that is, if {@link #isPageOnly()} returns true).
//      *
//      * If the data is not paged, this method throws an exception.
//      *
//      * @return 
//      * @throws UnsupportedOperationException
//      *         If the data of this fragment is not paged. 
//      */
//     long getPageNumber() throws UnsupportedOperationException;
// 
//     /**
//      * Returns true if {@link #getTriples()} returns a page of data only and
//      * this is the last page of the fragment.
//      *
//      * If the data is not paged (i.e., if {@link #isPageOnly()} returns false),
//      * this method throws an exception.
//      *
//      * @return 
//      * @throws UnsupportedOperationException
//      *         If the data of this fragment is not paged. 
//      */
//     boolean isLastPage() throws UnsupportedOperationException;
// 
//     /**
//      * Returns the maximum number of triples per page if {@link #getTriples()}
//      * returns a page of data only (that is, if {@link #isPageOnly()} returns
//      * true).
//      *
//      * If the data is not paged, this method throws an exception.
//      *
//      * @return 
//      * @throws UnsupportedOperationException
//      *         If the data of this fragment is not paged. 
//      */
//     long getMaxPageSize() throws UnsupportedOperationException;
// 
//     /**
//      * Returns an iterator over the metadata of this fragment.
//      * @return 
//      */
//     StmtIterator getMetadata();
// 
//     /**
//      * Returns an iterator over an RDF description of the controls associated
//      * with this fragment.
//      * @return 
//      */
//     StmtIterator getControls();
// }


// Path: src/main/java/org/linkeddatafragments/datasource/tdb/JenaTDBBasedRequestProcessorForTPFs.java
import java.io.File;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.QuerySolutionMap;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.Syntax;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.tdb.TDBFactory;
import org.linkeddatafragments.datasource.AbstractRequestProcessorForTriplePatterns;
import org.linkeddatafragments.datasource.IFragmentRequestProcessor;
import org.linkeddatafragments.fragments.ILinkedDataFragment;
import org.linkeddatafragments.fragments.tpf.ITriplePatternElement;
import org.linkeddatafragments.fragments.tpf.ITriplePatternFragmentRequest;

package org.linkeddatafragments.datasource.tdb;



/**
 * Implementation of {@link IFragmentRequestProcessor} that processes
 * {@link ITriplePatternFragmentRequest}s over data stored in Jena TDB.
 *
 * @author <a href="mailto:bart.hanssens@fedict.be">Bart Hanssens</a>
 * @author <a href="http://olafhartig.de">Olaf Hartig</a>
 */
public class JenaTDBBasedRequestProcessorForTPFs
    extends AbstractRequestProcessorForTriplePatterns<RDFNode,String,String>
{
    private final Dataset tdb;
    private final String defaultGraph;
    private final String sparql = "CONSTRUCT WHERE { ?s ?p ?o } " +
                                    "ORDER BY ?s ?p ?o";

    private final String count = "SELECT (COUNT(?s) AS ?count) WHERE { ?s ?p ?o }";

    private final Query query = QueryFactory.create(sparql, Syntax.syntaxSPARQL_11);
    private final Query countQuery = QueryFactory.create(count, Syntax.syntaxSPARQL_11);

    /**
     *
     * @param request
     * @return
     * @throws IllegalArgumentException
     */
    @Override
    protected Worker getTPFSpecificWorker(
            final ITriplePatternFragmentRequest<RDFNode,String,String> request )
                                                throws IllegalArgumentException
    {
        return new Worker( request );
    }

    /**
     *
     */
    protected class Worker
       extends AbstractRequestProcessorForTriplePatterns.Worker<RDFNode,String,String>
    {

        /**
         *
         * @param req
         */
        public Worker(
                final ITriplePatternFragmentRequest<RDFNode,String,String> req )
        {
            super( req );
        }

        /**
         *
         * @param subject
         * @param predicate
         * @param object
         * @param offset
         * @param limit
         * @return
         */
        @Override

        protected ILinkedDataFragment createFragment(
