All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.linkeddatafragments.datasource.AbstractRequestProcessorForTriplePatterns Maven / Gradle / Ivy

The newest version!
package org.linkeddatafragments.datasource;

import org.apache.jena.rdf.model.Model;
import org.linkeddatafragments.fragments.ILinkedDataFragment;
import org.linkeddatafragments.fragments.ILinkedDataFragmentRequest;
import org.linkeddatafragments.fragments.tpf.ITriplePatternElement;
import org.linkeddatafragments.fragments.tpf.ITriplePatternFragment;
import org.linkeddatafragments.fragments.tpf.ITriplePatternFragmentRequest;
import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentImpl;

/**
 * Base class for implementations of {@link IFragmentRequestProcessor} that
 * process {@link ITriplePatternFragmentRequest}s.
 *
 * @param 
 *          type for representing constants in triple patterns (i.e., URIs and
 *          literals)
 * @param 
 *          type for representing named variables in triple patterns
 * @param 
 *          type for representing anonymous variables in triple patterns (i.e.,
 *          variables denoted by a blank node)
 *
 * @author Olaf Hartig
 */
public abstract class
    AbstractRequestProcessorForTriplePatterns
        extends AbstractRequestProcessor
{

    /**
     *
     * @param request
     * @return
     * @throws IllegalArgumentException
     */
    @Override
    protected final Worker getWorker(
            final ILinkedDataFragmentRequest request )
                                                throws IllegalArgumentException
    {
        if ( request instanceof ITriplePatternFragmentRequest) {
            @SuppressWarnings("unchecked")
            final ITriplePatternFragmentRequest tpfRequest =
                      (ITriplePatternFragmentRequest) request;
            return getTPFSpecificWorker( tpfRequest );
        }
        else
            throw new IllegalArgumentException( request.getClass().getName() );
    }

    /**
     *
     * @param request
     * @return
     * @throws IllegalArgumentException
     */
    abstract protected Worker getTPFSpecificWorker(
            final ITriplePatternFragmentRequest request )
                    throws IllegalArgumentException;

    /**
     *
     * @param 
     * @param 
     * @param 
     */
    abstract static protected class Worker
        extends AbstractRequestProcessor.Worker
    {

        /**
         *
         * @param request
         */
        public Worker(
                 final ITriplePatternFragmentRequest request )
        {
            super( request );
        }

        /**
         *
         * @return
         * @throws IllegalArgumentException
         */
        @Override
        public ILinkedDataFragment createRequestedFragment()
                                                throws IllegalArgumentException
        {
            final long limit = ILinkedDataFragmentRequest.TRIPLESPERPAGE;
            final long offset;
            if ( request.isPageRequest() )
                offset = limit * ( request.getPageNumber() - 1L );
            else
                offset = 0L;

            @SuppressWarnings("unchecked")
            final ITriplePatternFragmentRequest tpfRequest =
                      (ITriplePatternFragmentRequest) request;

            return createFragment( tpfRequest.getSubject(),
                                   tpfRequest.getPredicate(),
                                   tpfRequest.getObject(),
                                   offset, limit );
        }

        /**
         *
         * @param subj
         * @param pred
         * @param obj
         * @param offset
         * @param limit
         * @return
         * @throws IllegalArgumentException
         */
        abstract protected ILinkedDataFragment createFragment(
                            final ITriplePatternElement subj,
                            final ITriplePatternElement pred,
                            final ITriplePatternElement obj,
                            final long offset,
                            final long limit )
                                               throws IllegalArgumentException;

        /**
         *
         * @return
         */
        protected ITriplePatternFragment createEmptyTriplePatternFragment()
        {
            return new TriplePatternFragmentImpl( request.getFragmentURL(),
                                                  request.getDatasetURL() );
        }

        /**
         *
         * @param triples
         * @param totalSize
         * @param isLastPage
         * @return
         */
        protected ITriplePatternFragment createTriplePatternFragment(
                                                     final Model triples,
                                                     final long totalSize,
                                                     final boolean isLastPage )
        {
            return new TriplePatternFragmentImpl( triples,
                                                  totalSize,
                                                  request.getFragmentURL(),
                                                  request.getDatasetURL(),
                                                  request.getPageNumber(),
                                                  isLastPage );
        }

    } // end of class Worker

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy