diff --git a/src/org/linkeddatafragments/datasource/AbstractRequestProcessor.java b/src/org/linkeddatafragments/datasource/AbstractRequestProcessor.java index 28c87e7..096c21e 100644 --- a/src/org/linkeddatafragments/datasource/AbstractRequestProcessor.java +++ b/src/org/linkeddatafragments/datasource/AbstractRequestProcessor.java @@ -1,7 +1,7 @@ package org.linkeddatafragments.datasource; -import org.linkeddatafragments.fragments.LinkedDataFragment; -import org.linkeddatafragments.fragments.LinkedDataFragmentRequest; +import org.linkeddatafragments.fragments.ILinkedDataFragment; +import org.linkeddatafragments.fragments.ILinkedDataFragmentRequest; /** * Base class for implementations of {@link IFragmentRequestProcessor}. @@ -15,28 +15,28 @@ abstract public class AbstractRequestProcessor public void close() {} @Override - final public LinkedDataFragment createRequestedFragment( - final LinkedDataFragmentRequest request ) + final public ILinkedDataFragment createRequestedFragment( + final ILinkedDataFragmentRequest request ) throws IllegalArgumentException { return getWorker( request ).createRequestedFragment(); } abstract protected Worker getWorker( - final LinkedDataFragmentRequest request ) + final ILinkedDataFragmentRequest request ) throws IllegalArgumentException; abstract static protected class Worker { - public final LinkedDataFragmentRequest request; + public final ILinkedDataFragmentRequest request; - public Worker( final LinkedDataFragmentRequest request ) + public Worker( final ILinkedDataFragmentRequest request ) { this.request = request; } - abstract public LinkedDataFragment createRequestedFragment() + abstract public ILinkedDataFragment createRequestedFragment() throws IllegalArgumentException; } // end of class Worker diff --git a/src/org/linkeddatafragments/datasource/AbstractRequestProcessorForTriplePatterns.java b/src/org/linkeddatafragments/datasource/AbstractRequestProcessorForTriplePatterns.java index 9ba984a..e15ef76 100644 --- a/src/org/linkeddatafragments/datasource/AbstractRequestProcessorForTriplePatterns.java +++ b/src/org/linkeddatafragments/datasource/AbstractRequestProcessorForTriplePatterns.java @@ -2,60 +2,66 @@ import com.hp.hpl.jena.rdf.model.Model; -import org.linkeddatafragments.fragments.LinkedDataFragment; -import org.linkeddatafragments.fragments.LinkedDataFragmentRequest; -import org.linkeddatafragments.fragments.tpf.TriplePatternElement; -import org.linkeddatafragments.fragments.tpf.TriplePatternFragment; +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.TriplePatternFragmentImpl; -import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequest; +import org.linkeddatafragments.fragments.tpf.ITriplePatternFragmentRequest; /** * Base class for implementations of {@link IFragmentRequestProcessor} that - * process {@link TriplePatternFragmentRequest}s. + * process {@link ITriplePatternFragmentRequest}s. * - * @param type for representing RDF terms in triple patterns - * @param type for representing specific variables in triple patterns + * @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 + AbstractRequestProcessorForTriplePatterns extends AbstractRequestProcessor { @Override - protected final Worker getWorker( - final LinkedDataFragmentRequest request ) + protected final Worker getWorker( + final ILinkedDataFragmentRequest request ) throws IllegalArgumentException { - if ( request instanceof TriplePatternFragmentRequest ) { + if ( request instanceof ITriplePatternFragmentRequest ) { @SuppressWarnings("unchecked") - final TriplePatternFragmentRequest tpfRequest = - (TriplePatternFragmentRequest) request; + final ITriplePatternFragmentRequest tpfRequest = + (ITriplePatternFragmentRequest) request; return getTPFSpecificWorker( tpfRequest ); } else throw new IllegalArgumentException( request.getClass().getName() ); } - abstract protected Worker getTPFSpecificWorker( - final TriplePatternFragmentRequest request ) + abstract protected Worker getTPFSpecificWorker( + final ITriplePatternFragmentRequest request ) throws IllegalArgumentException; - abstract static protected class Worker + abstract static protected class Worker extends AbstractRequestProcessor.Worker { public Worker( - final TriplePatternFragmentRequest request ) + final ITriplePatternFragmentRequest request ) { super( request ); } @Override - public LinkedDataFragment createRequestedFragment() + public ILinkedDataFragment createRequestedFragment() throws IllegalArgumentException { - final long limit = LinkedDataFragmentRequest.TRIPLESPERPAGE; + final long limit = ILinkedDataFragmentRequest.TRIPLESPERPAGE; final long offset; if ( request.isPageRequest() ) offset = limit * ( request.getPageNumber() - 1L ); @@ -63,8 +69,8 @@ public LinkedDataFragment createRequestedFragment() offset = 0L; @SuppressWarnings("unchecked") - final TriplePatternFragmentRequest tpfRequest = - (TriplePatternFragmentRequest) request; + final ITriplePatternFragmentRequest tpfRequest = + (ITriplePatternFragmentRequest) request; return createFragment( tpfRequest.getSubject(), tpfRequest.getPredicate(), @@ -72,21 +78,21 @@ public LinkedDataFragment createRequestedFragment() offset, limit ); } - abstract protected LinkedDataFragment createFragment( - final TriplePatternElement subj, - final TriplePatternElement pred, - final TriplePatternElement obj, + abstract protected ILinkedDataFragment createFragment( + final ITriplePatternElement subj, + final ITriplePatternElement pred, + final ITriplePatternElement obj, final long offset, final long limit ) throws IllegalArgumentException; - protected TriplePatternFragment createEmptyTriplePatternFragment() + protected ITriplePatternFragment createEmptyTriplePatternFragment() { return new TriplePatternFragmentImpl( request.getFragmentURL(), request.getDatasetURL() ); } - protected TriplePatternFragment createTriplePatternFragment( + protected ITriplePatternFragment createTriplePatternFragment( final Model triples, final long totalSize, final boolean isLastPage ) diff --git a/src/org/linkeddatafragments/datasource/DataSource.java b/src/org/linkeddatafragments/datasource/DataSourceBase.java similarity index 78% rename from src/org/linkeddatafragments/datasource/DataSource.java rename to src/org/linkeddatafragments/datasource/DataSourceBase.java index 465c9a0..484cbb4 100644 --- a/src/org/linkeddatafragments/datasource/DataSource.java +++ b/src/org/linkeddatafragments/datasource/DataSourceBase.java @@ -5,11 +5,11 @@ * @author mielvandersande * @author Bart Hanssens */ -public abstract class DataSource implements IDataSource { +public abstract class DataSourceBase implements IDataSource { protected String title; protected String description; - public DataSource(String title, String description) { + public DataSourceBase(String title, String description) { this.title = title; this.description = description; } diff --git a/src/org/linkeddatafragments/datasource/DataSourceFactory.java b/src/org/linkeddatafragments/datasource/DataSourceFactory.java index b64505f..d23741d 100644 --- a/src/org/linkeddatafragments/datasource/DataSourceFactory.java +++ b/src/org/linkeddatafragments/datasource/DataSourceFactory.java @@ -1,7 +1,7 @@ package org.linkeddatafragments.datasource; import com.google.gson.JsonObject; -import org.linkeddatafragments.exceptions.DataSourceException; +import org.linkeddatafragments.exceptions.DataSourceCreationException; import org.linkeddatafragments.exceptions.UnknownDataSourceTypeException; /** @@ -16,9 +16,9 @@ public class DataSourceFactory { * * @param config * @return datasource interface - * @throws DataSourceException + * @throws DataSourceCreationException */ - public static IDataSource create(JsonObject config) throws DataSourceException { + public static IDataSource create(JsonObject config) throws DataSourceCreationException { String title = config.getAsJsonPrimitive("title").getAsString(); String description = config.getAsJsonPrimitive("description").getAsString(); String typeName = config.getAsJsonPrimitive("type").getAsString(); diff --git a/src/org/linkeddatafragments/datasource/IDataSourceType.java b/src/org/linkeddatafragments/datasource/IDataSourceType.java index f4c100c..6380f90 100644 --- a/src/org/linkeddatafragments/datasource/IDataSourceType.java +++ b/src/org/linkeddatafragments/datasource/IDataSourceType.java @@ -1,6 +1,6 @@ package org.linkeddatafragments.datasource; -import org.linkeddatafragments.exceptions.DataSourceException; +import org.linkeddatafragments.exceptions.DataSourceCreationException; import com.google.gson.JsonObject; @@ -28,5 +28,5 @@ public interface IDataSourceType IDataSource createDataSource( final String title, final String description, final JsonObject settings ) - throws DataSourceException; + throws DataSourceCreationException; } diff --git a/src/org/linkeddatafragments/datasource/IFragmentRequestProcessor.java b/src/org/linkeddatafragments/datasource/IFragmentRequestProcessor.java index 7782c68..ba6bd80 100644 --- a/src/org/linkeddatafragments/datasource/IFragmentRequestProcessor.java +++ b/src/org/linkeddatafragments/datasource/IFragmentRequestProcessor.java @@ -2,18 +2,20 @@ import java.io.Closeable; -import org.linkeddatafragments.fragments.LinkedDataFragment; -import org.linkeddatafragments.fragments.LinkedDataFragmentRequest; +import org.linkeddatafragments.fragments.ILinkedDataFragment; +import org.linkeddatafragments.fragments.ILinkedDataFragment; +import org.linkeddatafragments.fragments.ILinkedDataFragmentRequest; +import org.linkeddatafragments.fragments.ILinkedDataFragmentRequest; /** - * Processes {@link LinkedDataFragmentRequest}s and returns - * the requested {@link LinkedDataFragment}s. + * Processes {@link ILinkedDataFragmentRequest}s and returns + * the requested {@link ILinkedDataFragment}s. * * @author Olaf Hartig */ public interface IFragmentRequestProcessor extends Closeable { - LinkedDataFragment createRequestedFragment( - final LinkedDataFragmentRequest request ) + ILinkedDataFragment createRequestedFragment( + final ILinkedDataFragmentRequest request ) throws IllegalArgumentException; } diff --git a/src/org/linkeddatafragments/datasource/hdt/HdtBasedRequestProcessorForTPFs.java b/src/org/linkeddatafragments/datasource/hdt/HdtBasedRequestProcessorForTPFs.java index 3ed8dbe..cfaa011 100644 --- a/src/org/linkeddatafragments/datasource/hdt/HdtBasedRequestProcessorForTPFs.java +++ b/src/org/linkeddatafragments/datasource/hdt/HdtBasedRequestProcessorForTPFs.java @@ -4,9 +4,9 @@ import org.linkeddatafragments.datasource.AbstractRequestProcessorForTriplePatterns; import org.linkeddatafragments.datasource.IFragmentRequestProcessor; -import org.linkeddatafragments.fragments.LinkedDataFragment; -import org.linkeddatafragments.fragments.tpf.TriplePatternElement; -import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequest; +import org.linkeddatafragments.fragments.ILinkedDataFragment; +import org.linkeddatafragments.fragments.tpf.ITriplePatternElement; +import org.linkeddatafragments.fragments.tpf.ITriplePatternFragmentRequest; import org.rdfhdt.hdt.enums.TripleComponentRole; import org.rdfhdt.hdt.hdt.HDT; import org.rdfhdt.hdt.hdt.HDTManager; @@ -21,13 +21,13 @@ /** * Implementation of {@link IFragmentRequestProcessor} that processes - * {@link TriplePatternFragmentRequest}s over data stored in HDT. + * {@link ITriplePatternFragmentRequest}s over data stored in HDT. * * @author Ruben Verborgh * @author Olaf Hartig */ public class HdtBasedRequestProcessorForTPFs - extends AbstractRequestProcessorForTriplePatterns + extends AbstractRequestProcessorForTriplePatterns { protected final HDT datasource; protected final NodeDictionary dictionary; @@ -46,7 +46,7 @@ public HdtBasedRequestProcessorForTPFs( String hdtFile ) throws IOException @Override protected Worker getTPFSpecificWorker( - final TriplePatternFragmentRequest request ) + final ITriplePatternFragmentRequest request ) throws IllegalArgumentException { return new Worker( request ); @@ -54,30 +54,32 @@ protected Worker getTPFSpecificWorker( protected class Worker - extends AbstractRequestProcessorForTriplePatterns.Worker + extends AbstractRequestProcessorForTriplePatterns.Worker { - public Worker( final TriplePatternFragmentRequest req ) + public Worker( + final ITriplePatternFragmentRequest req ) { super( req ); } @Override - protected LinkedDataFragment createFragment( - final TriplePatternElement subject, - final TriplePatternElement predicate, - final TriplePatternElement object, - final long offset, - final long limit ) + protected ILinkedDataFragment createFragment( + final ITriplePatternElement subject, + final ITriplePatternElement predicate, + final ITriplePatternElement object, + final long offset, + final long limit ) { // FIXME: The following algorithm is incorrect for cases in which // the requested triple pattern contains a specific variable - // multiple times (e.g., ?x foaf:knows ?x ). + // multiple times; + // e.g., (?x foaf:knows ?x ) or (_:bn foaf:knows _:bn) // see https://github.com/LinkedDataFragments/Server.Java/issues/23 // look up the result from the HDT datasource) - int subjectId = subject.isVariable() ? 0 : dictionary.getIntID(subject.asTerm().asNode(), TripleComponentRole.SUBJECT); - int predicateId = predicate.isVariable() ? 0 : dictionary.getIntID(predicate.asTerm().asNode(), TripleComponentRole.PREDICATE); - int objectId = object.isVariable() ? 0 : dictionary.getIntID(object.asTerm().asNode(), TripleComponentRole.OBJECT); + int subjectId = subject.isVariable() ? 0 : dictionary.getIntID(subject.asConstantTerm().asNode(), TripleComponentRole.SUBJECT); + int predicateId = predicate.isVariable() ? 0 : dictionary.getIntID(predicate.asConstantTerm().asNode(), TripleComponentRole.PREDICATE); + int objectId = object.isVariable() ? 0 : dictionary.getIntID(object.asConstantTerm().asNode(), TripleComponentRole.OBJECT); if (subjectId < 0 || predicateId < 0 || objectId < 0) { return createEmptyTriplePatternFragment(); diff --git a/src/org/linkeddatafragments/datasource/hdt/HdtDataSource.java b/src/org/linkeddatafragments/datasource/hdt/HdtDataSource.java index 03d7e19..f053b75 100644 --- a/src/org/linkeddatafragments/datasource/hdt/HdtDataSource.java +++ b/src/org/linkeddatafragments/datasource/hdt/HdtDataSource.java @@ -2,7 +2,7 @@ import java.io.IOException; -import org.linkeddatafragments.datasource.DataSource; +import org.linkeddatafragments.datasource.DataSourceBase; import org.linkeddatafragments.datasource.IFragmentRequestProcessor; import org.linkeddatafragments.fragments.IFragmentRequestParser; import org.linkeddatafragments.fragments.tpf.TPFRequestParserForJenaBackends; @@ -13,7 +13,7 @@ * @author Ruben Verborgh * @author Olaf Hartig */ -public class HdtDataSource extends DataSource { +public class HdtDataSource extends DataSourceBase { protected final HdtBasedRequestProcessorForTPFs requestProcessor; diff --git a/src/org/linkeddatafragments/datasource/hdt/HdtDataSourceType.java b/src/org/linkeddatafragments/datasource/hdt/HdtDataSourceType.java index 2826c2f..0ae1afa 100644 --- a/src/org/linkeddatafragments/datasource/hdt/HdtDataSourceType.java +++ b/src/org/linkeddatafragments/datasource/hdt/HdtDataSourceType.java @@ -5,7 +5,7 @@ import org.linkeddatafragments.datasource.IDataSource; import org.linkeddatafragments.datasource.IDataSourceType; -import org.linkeddatafragments.exceptions.DataSourceException; +import org.linkeddatafragments.exceptions.DataSourceCreationException; import com.google.gson.JsonObject; @@ -20,7 +20,7 @@ public class HdtDataSourceType implements IDataSourceType public IDataSource createDataSource( final String title, final String description, final JsonObject settings ) - throws DataSourceException + throws DataSourceCreationException { final String fname = settings.getAsJsonPrimitive("file").getAsString(); final File file = new File( fname ); @@ -28,7 +28,7 @@ public IDataSource createDataSource( final String title, try { return new HdtDataSource(title, description, file.getAbsolutePath()); } catch (IOException ex) { - throw new DataSourceException(ex); + throw new DataSourceCreationException(ex); } } diff --git a/src/org/linkeddatafragments/datasource/index/IndexDataSource.java b/src/org/linkeddatafragments/datasource/index/IndexDataSource.java index 72f6267..d1500ba 100644 --- a/src/org/linkeddatafragments/datasource/index/IndexDataSource.java +++ b/src/org/linkeddatafragments/datasource/index/IndexDataSource.java @@ -2,7 +2,7 @@ import java.util.HashMap; -import org.linkeddatafragments.datasource.DataSource; +import org.linkeddatafragments.datasource.DataSourceBase; import org.linkeddatafragments.datasource.IDataSource; import org.linkeddatafragments.datasource.IFragmentRequestProcessor; import org.linkeddatafragments.fragments.IFragmentRequestParser; @@ -14,7 +14,7 @@ * @author Miel Vander Sande * @author Olaf Hartig */ -public class IndexDataSource extends DataSource { +public class IndexDataSource extends DataSourceBase { protected final IndexRequestProcessorForTPFs requestProcessor; diff --git a/src/org/linkeddatafragments/datasource/index/IndexRequestProcessorForTPFs.java b/src/org/linkeddatafragments/datasource/index/IndexRequestProcessorForTPFs.java index 8e4c6d4..65130cc 100644 --- a/src/org/linkeddatafragments/datasource/index/IndexRequestProcessorForTPFs.java +++ b/src/org/linkeddatafragments/datasource/index/IndexRequestProcessorForTPFs.java @@ -15,20 +15,20 @@ import org.linkeddatafragments.datasource.AbstractRequestProcessorForTriplePatterns; import org.linkeddatafragments.datasource.IDataSource; import org.linkeddatafragments.datasource.IFragmentRequestProcessor; -import org.linkeddatafragments.fragments.LinkedDataFragment; -import org.linkeddatafragments.fragments.tpf.TriplePatternElement; -import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequest; +import org.linkeddatafragments.fragments.ILinkedDataFragment; +import org.linkeddatafragments.fragments.tpf.ITriplePatternElement; +import org.linkeddatafragments.fragments.tpf.ITriplePatternFragmentRequest; /** * Implementation of {@link IFragmentRequestProcessor} that processes - * {@link TriplePatternFragmentRequest}s over an index that provides + * {@link ITriplePatternFragmentRequest}s over an index that provides * an overview of all available datasets. * * @author Miel Vander Sande * @author Olaf Hartig */ public class IndexRequestProcessorForTPFs - extends AbstractRequestProcessorForTriplePatterns + extends AbstractRequestProcessorForTriplePatterns { final static String RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; final static String RDFS = "http://www.w3.org/2000/01/rdf-schema#"; @@ -58,7 +58,7 @@ public IndexRequestProcessorForTPFs( @Override protected Worker getTPFSpecificWorker( - final TriplePatternFragmentRequest request ) + final ITriplePatternFragmentRequest request ) throws IllegalArgumentException { return new Worker( request ); @@ -66,32 +66,34 @@ protected Worker getTPFSpecificWorker( protected class Worker - extends AbstractRequestProcessorForTriplePatterns.Worker + extends AbstractRequestProcessorForTriplePatterns.Worker { - public Worker( final TriplePatternFragmentRequest req ) + public Worker( + final ITriplePatternFragmentRequest req ) { super( req ); } @Override - protected LinkedDataFragment createFragment( - final TriplePatternElement s, - final TriplePatternElement p, - final TriplePatternElement o, - final long offset, - final long limit ) + protected ILinkedDataFragment createFragment( + final ITriplePatternElement s, + final ITriplePatternElement p, + final ITriplePatternElement o, + final long offset, + final long limit ) { // FIXME: The following algorithm is incorrect for cases in which // the requested triple pattern contains a specific variable - // multiple times (e.g., ?x foaf:knows ?x ). + // multiple times; + // e.g., (?x foaf:knows ?x ) or (_:bn foaf:knows _:bn) // see https://github.com/LinkedDataFragments/Server.Java/issues/25 final Resource subject = s.isVariable() ? null - : s.asTerm().asResource(); + : s.asConstantTerm().asResource(); final Property predicate = p.isVariable() ? null - : ResourceFactory.createProperty(p.asTerm().asResource().getURI()); + : ResourceFactory.createProperty(p.asConstantTerm().asResource().getURI()); final RDFNode object = o.isVariable() ? null - : o.asTerm(); + : o.asConstantTerm(); StmtIterator listStatements = model.listStatements(subject, predicate, object); Model result = ModelFactory.createDefaultModel(); diff --git a/src/org/linkeddatafragments/datasource/tdb/JenaTDBBasedRequestProcessorForTPFs.java b/src/org/linkeddatafragments/datasource/tdb/JenaTDBBasedRequestProcessorForTPFs.java index 094183e..740d93b 100644 --- a/src/org/linkeddatafragments/datasource/tdb/JenaTDBBasedRequestProcessorForTPFs.java +++ b/src/org/linkeddatafragments/datasource/tdb/JenaTDBBasedRequestProcessorForTPFs.java @@ -18,19 +18,19 @@ import org.linkeddatafragments.datasource.AbstractRequestProcessorForTriplePatterns; import org.linkeddatafragments.datasource.IFragmentRequestProcessor; -import org.linkeddatafragments.fragments.LinkedDataFragment; -import org.linkeddatafragments.fragments.tpf.TriplePatternElement; -import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequest; +import org.linkeddatafragments.fragments.ILinkedDataFragment; +import org.linkeddatafragments.fragments.tpf.ITriplePatternElement; +import org.linkeddatafragments.fragments.tpf.ITriplePatternFragmentRequest; /** * Implementation of {@link IFragmentRequestProcessor} that processes - * {@link TriplePatternFragmentRequest}s over data stored in Jena TDB. + * {@link ITriplePatternFragmentRequest}s over data stored in Jena TDB. * * @author Bart Hanssens * @author Olaf Hartig */ public class JenaTDBBasedRequestProcessorForTPFs - extends AbstractRequestProcessorForTriplePatterns + extends AbstractRequestProcessorForTriplePatterns { private final Dataset tdb; private final String sparql = "CONSTRUCT WHERE { ?s ?p ?o } " + @@ -43,7 +43,7 @@ public class JenaTDBBasedRequestProcessorForTPFs @Override protected Worker getTPFSpecificWorker( - final TriplePatternFragmentRequest request ) + final ITriplePatternFragmentRequest request ) throws IllegalArgumentException { return new Worker( request ); @@ -51,36 +51,38 @@ protected Worker getTPFSpecificWorker( protected class Worker - extends AbstractRequestProcessorForTriplePatterns.Worker + extends AbstractRequestProcessorForTriplePatterns.Worker { - public Worker( final TriplePatternFragmentRequest req ) + public Worker( + final ITriplePatternFragmentRequest req ) { super( req ); } @Override - protected LinkedDataFragment createFragment( - final TriplePatternElement subject, - final TriplePatternElement predicate, - final TriplePatternElement object, - final long offset, - final long limit ) + protected ILinkedDataFragment createFragment( + final ITriplePatternElement subject, + final ITriplePatternElement predicate, + final ITriplePatternElement object, + final long offset, + final long limit ) { // FIXME: The following algorithm is incorrect for cases in which // the requested triple pattern contains a specific variable - // multiple times (e.g., ?x foaf:knows ?x ). + // multiple times; + // e.g., (?x foaf:knows ?x ) or (_:bn foaf:knows _:bn) // see https://github.com/LinkedDataFragments/Server.Java/issues/24 Model model = tdb.getDefaultModel(); QuerySolutionMap map = new QuerySolutionMap(); if ( ! subject.isVariable() ) { - map.add("s", subject.asTerm()); + map.add("s", subject.asConstantTerm()); } if ( ! predicate.isVariable() ) { - map.add("p", predicate.asTerm()); + map.add("p", predicate.asConstantTerm()); } if ( ! object.isVariable() ) { - map.add("o", object.asTerm()); + map.add("o", object.asConstantTerm()); } query.setOffset(offset); diff --git a/src/org/linkeddatafragments/datasource/tdb/JenaTDBDataSource.java b/src/org/linkeddatafragments/datasource/tdb/JenaTDBDataSource.java index 698a644..314b2dd 100644 --- a/src/org/linkeddatafragments/datasource/tdb/JenaTDBDataSource.java +++ b/src/org/linkeddatafragments/datasource/tdb/JenaTDBDataSource.java @@ -2,7 +2,7 @@ import java.io.File; -import org.linkeddatafragments.datasource.DataSource; +import org.linkeddatafragments.datasource.DataSourceBase; import org.linkeddatafragments.datasource.IFragmentRequestProcessor; import org.linkeddatafragments.fragments.IFragmentRequestParser; import org.linkeddatafragments.fragments.tpf.TPFRequestParserForJenaBackends; @@ -13,7 +13,7 @@ * @author Bart Hanssens * @author Olaf Hartig */ -public class JenaTDBDataSource extends DataSource { +public class JenaTDBDataSource extends DataSourceBase { protected final JenaTDBBasedRequestProcessorForTPFs requestProcessor; diff --git a/src/org/linkeddatafragments/datasource/tdb/JenaTDBDataSourceType.java b/src/org/linkeddatafragments/datasource/tdb/JenaTDBDataSourceType.java index 3be3dea..599b16d 100644 --- a/src/org/linkeddatafragments/datasource/tdb/JenaTDBDataSourceType.java +++ b/src/org/linkeddatafragments/datasource/tdb/JenaTDBDataSourceType.java @@ -4,7 +4,7 @@ import org.linkeddatafragments.datasource.IDataSource; import org.linkeddatafragments.datasource.IDataSourceType; -import org.linkeddatafragments.exceptions.DataSourceException; +import org.linkeddatafragments.exceptions.DataSourceCreationException; import com.google.gson.JsonObject; @@ -20,7 +20,7 @@ public class JenaTDBDataSourceType implements IDataSourceType public IDataSource createDataSource( final String title, final String description, final JsonObject settings ) - throws DataSourceException + throws DataSourceCreationException { final String dname = settings.getAsJsonPrimitive("directory").getAsString(); final File dir = new File( dname ); @@ -28,7 +28,7 @@ public IDataSource createDataSource( final String title, try { return new JenaTDBDataSource(title, description, dir); } catch (Exception ex) { - throw new DataSourceException(ex); + throw new DataSourceCreationException(ex); } } diff --git a/src/org/linkeddatafragments/exceptions/DataSourceCreationException.java b/src/org/linkeddatafragments/exceptions/DataSourceCreationException.java new file mode 100644 index 0000000..6fd9499 --- /dev/null +++ b/src/org/linkeddatafragments/exceptions/DataSourceCreationException.java @@ -0,0 +1,16 @@ +package org.linkeddatafragments.exceptions; + +/** + * + * @author mielvandersande + */ +public class DataSourceCreationException extends DataSourceException { + + public DataSourceCreationException(Throwable cause) { + super(cause); + } + + public DataSourceCreationException(String datasourceName, String message) { + super(datasourceName, "Could not create DataSource - " + message); + } +} diff --git a/src/org/linkeddatafragments/exceptions/DataSourceException.java b/src/org/linkeddatafragments/exceptions/DataSourceException.java index fb6bd6a..10fb1c0 100644 --- a/src/org/linkeddatafragments/exceptions/DataSourceException.java +++ b/src/org/linkeddatafragments/exceptions/DataSourceException.java @@ -1,17 +1,23 @@ package org.linkeddatafragments.exceptions; +import org.linkeddatafragments.datasource.IDataSource; + /** * * @author mielvandersande */ -public class DataSourceException extends Exception { - private static final long serialVersionUID = 1L; +abstract public class DataSourceException extends Exception { public DataSourceException(Throwable cause) { - super(cause.getMessage()); + super(cause); } - public DataSourceException(String message) { - super("Could not create DataSource: " + message); - } + public DataSourceException(String datasourceName, String message) { + super("Error for datasource '" + datasourceName + "': " + message); + } + + public DataSourceException(IDataSource datasource, String message) { + this(datasource.getTitle(), message); + } + } diff --git a/src/org/linkeddatafragments/exceptions/DataSourceNotFoundException.java b/src/org/linkeddatafragments/exceptions/DataSourceNotFoundException.java index aeb721c..d002aa6 100644 --- a/src/org/linkeddatafragments/exceptions/DataSourceNotFoundException.java +++ b/src/org/linkeddatafragments/exceptions/DataSourceNotFoundException.java @@ -1,19 +1,12 @@ package org.linkeddatafragments.exceptions; -import org.linkeddatafragments.datasource.IDataSource; - /** * * @author mielvandersande */ -public class DataSourceNotFoundException extends Exception { - private static final long serialVersionUID = 1L; - - public DataSourceNotFoundException(Throwable cause) { - super(cause.getMessage()); - } +public class DataSourceNotFoundException extends DataSourceException { public DataSourceNotFoundException(String dataSourceName) { - super("Data source " + dataSourceName + " not found."); + super(dataSourceName, "Datasource not found."); } } diff --git a/src/org/linkeddatafragments/exceptions/UnknownDataSourceTypeException.java b/src/org/linkeddatafragments/exceptions/UnknownDataSourceTypeException.java index e7dc9fc..5c7b358 100644 --- a/src/org/linkeddatafragments/exceptions/UnknownDataSourceTypeException.java +++ b/src/org/linkeddatafragments/exceptions/UnknownDataSourceTypeException.java @@ -4,10 +4,9 @@ * * @author mielvandersande */ -public class UnknownDataSourceTypeException extends DataSourceException { - private static final long serialVersionUID = 1L; - +public class UnknownDataSourceTypeException extends DataSourceCreationException { + public UnknownDataSourceTypeException(String type) { - super("Type " + type + " does not exist."); + super("", "Type " + type + " does not exist."); } } diff --git a/src/org/linkeddatafragments/fragments/FragmentRequestParserBase.java b/src/org/linkeddatafragments/fragments/FragmentRequestParserBase.java index 210cb1e..5910f7b 100644 --- a/src/org/linkeddatafragments/fragments/FragmentRequestParserBase.java +++ b/src/org/linkeddatafragments/fragments/FragmentRequestParserBase.java @@ -12,7 +12,7 @@ abstract public class FragmentRequestParserBase implements IFragmentRequestParser { @Override - final public LinkedDataFragmentRequest parseIntoFragmentRequest( + final public ILinkedDataFragmentRequest parseIntoFragmentRequest( final HttpServletRequest httpRequest, final ConfigReader config ) throws IllegalArgumentException @@ -40,7 +40,7 @@ public Worker( final HttpServletRequest request, this.config = config; final String givenPageNumber = request.getParameter( - LinkedDataFragmentRequest.PARAMETERNAME_PAGE ); + ILinkedDataFragmentRequest.PARAMETERNAME_PAGE ); if ( givenPageNumber != null ) { long pageNumber; try { @@ -57,7 +57,7 @@ public Worker( final HttpServletRequest request, } } - abstract public LinkedDataFragmentRequest createFragmentRequest() + abstract public ILinkedDataFragmentRequest createFragmentRequest() throws IllegalArgumentException; public String getFragmentURL() { diff --git a/src/org/linkeddatafragments/fragments/IFragmentRequestParser.java b/src/org/linkeddatafragments/fragments/IFragmentRequestParser.java index 64cf59f..99cde96 100644 --- a/src/org/linkeddatafragments/fragments/IFragmentRequestParser.java +++ b/src/org/linkeddatafragments/fragments/IFragmentRequestParser.java @@ -5,7 +5,7 @@ import org.linkeddatafragments.config.ConfigReader; /** - * Parses HTTP requests into specific {@link LinkedDataFragmentRequest}s. + * Parses HTTP requests into specific {@link ILinkedDataFragmentRequest}s. * * @author Olaf Hartig */ @@ -13,13 +13,13 @@ public interface IFragmentRequestParser { /** * Parses the given HTTP request into a specific - * {@link LinkedDataFragmentRequest}. + * {@link ILinkedDataFragmentRequest}. * * @throws IllegalArgumentException * If the given HTTP request cannot be interpreted (perhaps due to * missing request parameters). */ - LinkedDataFragmentRequest parseIntoFragmentRequest( + ILinkedDataFragmentRequest parseIntoFragmentRequest( final HttpServletRequest httpRequest, final ConfigReader config ) throws IllegalArgumentException; diff --git a/src/org/linkeddatafragments/fragments/LinkedDataFragment.java b/src/org/linkeddatafragments/fragments/ILinkedDataFragment.java similarity index 98% rename from src/org/linkeddatafragments/fragments/LinkedDataFragment.java rename to src/org/linkeddatafragments/fragments/ILinkedDataFragment.java index 9bce03c..59ad1ea 100644 --- a/src/org/linkeddatafragments/fragments/LinkedDataFragment.java +++ b/src/org/linkeddatafragments/fragments/ILinkedDataFragment.java @@ -7,7 +7,7 @@ * * @author Olaf Hartig */ -public interface LinkedDataFragment +public interface ILinkedDataFragment { /** * Returns an iterator over the RDF data of this fragment (possibly only diff --git a/src/org/linkeddatafragments/fragments/LinkedDataFragmentRequest.java b/src/org/linkeddatafragments/fragments/ILinkedDataFragmentRequest.java similarity index 95% rename from src/org/linkeddatafragments/fragments/LinkedDataFragmentRequest.java rename to src/org/linkeddatafragments/fragments/ILinkedDataFragmentRequest.java index 3cc12c0..eebfa81 100644 --- a/src/org/linkeddatafragments/fragments/LinkedDataFragmentRequest.java +++ b/src/org/linkeddatafragments/fragments/ILinkedDataFragmentRequest.java @@ -5,7 +5,7 @@ * * @author Olaf Hartig */ -public interface LinkedDataFragmentRequest +public interface ILinkedDataFragmentRequest { public final static long TRIPLESPERPAGE = 100L; public final static String PARAMETERNAME_PAGE = "page"; diff --git a/src/org/linkeddatafragments/fragments/LinkedDataFragmentBase.java b/src/org/linkeddatafragments/fragments/LinkedDataFragmentBase.java index 327e21c..9da713c 100644 --- a/src/org/linkeddatafragments/fragments/LinkedDataFragmentBase.java +++ b/src/org/linkeddatafragments/fragments/LinkedDataFragmentBase.java @@ -11,12 +11,12 @@ import com.hp.hpl.jena.rdf.model.StmtIterator; /** - * Base class of any implementation of {@link LinkedDataFragment} that uses + * Base class of any implementation of {@link ILinkedDataFragment} that uses * paging. * * @author Olaf Hartig */ -public abstract class LinkedDataFragmentBase implements LinkedDataFragment +public abstract class LinkedDataFragmentBase implements ILinkedDataFragment { public final String fragmentURL; public final String datasetURL; @@ -51,7 +51,7 @@ public boolean isLastPage() { @Override public long getMaxPageSize() { - return LinkedDataFragmentRequest.TRIPLESPERPAGE; + return ILinkedDataFragmentRequest.TRIPLESPERPAGE; } /** @@ -113,7 +113,7 @@ public void addControls( final Model model ) final Resource firstPageId = model.createResource( - pagedURL.setParameter(LinkedDataFragmentRequest.PARAMETERNAME_PAGE, + pagedURL.setParameter(ILinkedDataFragmentRequest.PARAMETERNAME_PAGE, "1").toString() ); fragmentId.addProperty( CommonResources.HYDRA_FIRSTPAGE, firstPageId ); @@ -122,7 +122,7 @@ public void addControls( final Model model ) final String prevPageNumber = Long.toString( pageNumber - 1 ); final Resource prevPageId = model.createResource( - pagedURL.setParameter(LinkedDataFragmentRequest.PARAMETERNAME_PAGE, + pagedURL.setParameter(ILinkedDataFragmentRequest.PARAMETERNAME_PAGE, prevPageNumber).toString() ); fragmentId.addProperty( CommonResources.HYDRA_PREVIOUSPAGE, prevPageId ); @@ -132,7 +132,7 @@ public void addControls( final Model model ) final String nextPageNumber = Long.toString( pageNumber + 1 ); final Resource nextPageId = model.createResource( - pagedURL.setParameter(LinkedDataFragmentRequest.PARAMETERNAME_PAGE, + pagedURL.setParameter(ILinkedDataFragmentRequest.PARAMETERNAME_PAGE, nextPageNumber).toString() ); fragmentId.addProperty( CommonResources.HYDRA_NEXTPAGE, nextPageId ); diff --git a/src/org/linkeddatafragments/fragments/LinkedDataFragmentRequestBase.java b/src/org/linkeddatafragments/fragments/LinkedDataFragmentRequestBase.java index d8e5d25..4cc6030 100644 --- a/src/org/linkeddatafragments/fragments/LinkedDataFragmentRequestBase.java +++ b/src/org/linkeddatafragments/fragments/LinkedDataFragmentRequestBase.java @@ -1,12 +1,12 @@ package org.linkeddatafragments.fragments; /** - * Base class for implementations of {@link LinkedDataFragmentRequest}. + * Base class for implementations of {@link ILinkedDataFragmentRequest}. * * @author Olaf Hartig */ public abstract class LinkedDataFragmentRequestBase - implements LinkedDataFragmentRequest + implements ILinkedDataFragmentRequest { public final String fragmentURL; public final String datasetURL; diff --git a/src/org/linkeddatafragments/fragments/tpf/ITriplePatternElement.java b/src/org/linkeddatafragments/fragments/tpf/ITriplePatternElement.java new file mode 100644 index 0000000..ee6166f --- /dev/null +++ b/src/org/linkeddatafragments/fragments/tpf/ITriplePatternElement.java @@ -0,0 +1,82 @@ +package org.linkeddatafragments.fragments.tpf; + +/** + * Represents an element of a triple pattern (i.e., subject, predicate, object). + * + * @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 interface ITriplePatternElement +{ + /** + * Returns true if this element is a variable (specific or unspecified). + */ + boolean isVariable(); + + /** + * Returns true if this element is a specific variable, and false if either + * it is not a variable (but a URI or literal) or it is some variable that + * is not specified. The latter (unspecified variables) is possible because + * when a client requests a triple pattern fragment, it may omit triple + * pattern related parameters. + */ + boolean isSpecificVariable(); + + /** + * Returns true if this element is a specific variable that has a name + * (i.e., it is denoted by a string that begins with a question mark), + * and false if either it is not a specific variable or it is a specific + * variable that is denoted by a blank node. + * + * If this element is a specific variable that has a name (that is, this + * method returns true), the named variable can be obtained by the method + * {@link #asNamedVariable()}. + */ + boolean isNamedVariable(); + + /** + * Returns a representation of this element as a named variable (assuming + * it is a specific variable that has a name). + * + * @throws UnsupportedOperationException + * If this element is not a specific variable that has a name + * (i.e., if {@link #isNamedVariable()} returns false). + */ + NamedVarType asNamedVariable() throws UnsupportedOperationException; + + /** + * Returns true if this element is a specific variable that does not have + * a name (i.e., it is denoted by a blank node), and false if either it is + * not a specific variable or it is a specific variable that has a name. + * + * If this element is a specific variable denoted by a blank node (that is, + * this method returns true), the blank node can be obtained by the method + * {@link #asAnonymousVariable()}. + */ + boolean isAnonymousVariable(); + + /** + * Returns a representation of this element as a blank node (assuming + * it is a specific, but non-named variable). + * + * @throws UnsupportedOperationException + * If this element is not a specific anonymous variable (i.e., + * if {@link #isAnonymousVariable()} returns false). + */ + AnonVarType asAnonymousVariable() throws UnsupportedOperationException; + + /** + * Returns a representation of this element as a constant RDF term (i.e., + * a URI or a literal). + * + * @throws UnsupportedOperationException + * If this element is not a constant RDF term but a variable + * (i.e., if {@link #isVariable()} returns true). + */ + ConstantTermType asConstantTerm() throws UnsupportedOperationException; +} diff --git a/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragment.java b/src/org/linkeddatafragments/fragments/tpf/ITriplePatternFragment.java similarity index 68% rename from src/org/linkeddatafragments/fragments/tpf/TriplePatternFragment.java rename to src/org/linkeddatafragments/fragments/tpf/ITriplePatternFragment.java index d4ea9e5..9f51b34 100644 --- a/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragment.java +++ b/src/org/linkeddatafragments/fragments/tpf/ITriplePatternFragment.java @@ -1,12 +1,12 @@ package org.linkeddatafragments.fragments.tpf; -import org.linkeddatafragments.fragments.LinkedDataFragment; +import org.linkeddatafragments.fragments.ILinkedDataFragment; /** * A Triple Pattern Fragment. * @author Ruben Verborgh */ -public interface TriplePatternFragment extends LinkedDataFragment { +public interface ITriplePatternFragment extends ILinkedDataFragment { /** * Gets the total number of triples in the fragment (can be an estimate). * @return the total number of triples diff --git a/src/org/linkeddatafragments/fragments/tpf/ITriplePatternFragmentRequest.java b/src/org/linkeddatafragments/fragments/tpf/ITriplePatternFragmentRequest.java new file mode 100644 index 0000000..07d026a --- /dev/null +++ b/src/org/linkeddatafragments/fragments/tpf/ITriplePatternFragmentRequest.java @@ -0,0 +1,37 @@ +package org.linkeddatafragments.fragments.tpf; + +import org.linkeddatafragments.fragments.ILinkedDataFragmentRequest; + +/** + * Represents a request of a Triple Pattern Fragment (TPF). + * + * @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 interface ITriplePatternFragmentRequest + extends ILinkedDataFragmentRequest +{ + public final static String PARAMETERNAME_SUBJ = "subject"; + public final static String PARAMETERNAME_PRED = "predicate"; + public final static String PARAMETERNAME_OBJ = "object"; + + /** + * Returns the subject position of the requested triple pattern. + */ + ITriplePatternElement getSubject(); + + /** + * Returns the predicate position of the requested triple pattern. + */ + ITriplePatternElement getPredicate(); + + /** + * Returns the object position of the requested triple pattern. + */ + ITriplePatternElement getObject(); +} diff --git a/src/org/linkeddatafragments/fragments/tpf/TPFRequestParser.java b/src/org/linkeddatafragments/fragments/tpf/TPFRequestParser.java index b03e7c8..eaa06d4 100644 --- a/src/org/linkeddatafragments/fragments/tpf/TPFRequestParser.java +++ b/src/org/linkeddatafragments/fragments/tpf/TPFRequestParser.java @@ -5,24 +5,24 @@ import org.linkeddatafragments.config.ConfigReader; import org.linkeddatafragments.fragments.FragmentRequestParserBase; import org.linkeddatafragments.fragments.IFragmentRequestParser; -import org.linkeddatafragments.fragments.LinkedDataFragmentRequest; +import org.linkeddatafragments.fragments.ILinkedDataFragmentRequest; import org.linkeddatafragments.util.TriplePatternElementParser; /** - * An {@link IFragmentRequestParser} for {@link TriplePatternFragmentRequest}s. + * An {@link IFragmentRequestParser} for {@link ITriplePatternFragmentRequest}s. * * @param type for representing RDF terms in triple patterns * @param type for representing specific variables in triple patterns * * @author Olaf Hartig */ -public class TPFRequestParser +public class TPFRequestParser extends FragmentRequestParserBase { - public final TriplePatternElementParser elmtParser; + public final TriplePatternElementParser elmtParser; public TPFRequestParser( - final TriplePatternElementParser elmtParser ) + final TriplePatternElementParser elmtParser ) { this.elmtParser = elmtParser; } @@ -44,10 +44,10 @@ public Worker( final HttpServletRequest request, } @Override - public LinkedDataFragmentRequest createFragmentRequest() + public ILinkedDataFragmentRequest createFragmentRequest() throws IllegalArgumentException { - return new TriplePatternFragmentRequestImpl( + return new TriplePatternFragmentRequestImpl( getFragmentURL(), getDatasetURL(), pageNumberWasRequested, @@ -57,22 +57,22 @@ public LinkedDataFragmentRequest createFragmentRequest() getObject() ); } - public TriplePatternElement getSubject() { + public ITriplePatternElement getSubject() { return getParameterAsTriplePatternElement( - TriplePatternFragmentRequest.PARAMETERNAME_SUBJ ); + ITriplePatternFragmentRequest.PARAMETERNAME_SUBJ ); } - public TriplePatternElement getPredicate() { + public ITriplePatternElement getPredicate() { return getParameterAsTriplePatternElement( - TriplePatternFragmentRequest.PARAMETERNAME_PRED ); + ITriplePatternFragmentRequest.PARAMETERNAME_PRED ); } - public TriplePatternElement getObject() { + public ITriplePatternElement getObject() { return getParameterAsTriplePatternElement( - TriplePatternFragmentRequest.PARAMETERNAME_OBJ ); + ITriplePatternFragmentRequest.PARAMETERNAME_OBJ ); } - public TriplePatternElement + public ITriplePatternElement getParameterAsTriplePatternElement( final String paramName ) { final String parameter = request.getParameter( paramName ); diff --git a/src/org/linkeddatafragments/fragments/tpf/TPFRequestParserForJenaBackends.java b/src/org/linkeddatafragments/fragments/tpf/TPFRequestParserForJenaBackends.java index ee5f707..8eb9c56 100644 --- a/src/org/linkeddatafragments/fragments/tpf/TPFRequestParserForJenaBackends.java +++ b/src/org/linkeddatafragments/fragments/tpf/TPFRequestParserForJenaBackends.java @@ -10,7 +10,7 @@ * @author Olaf Hartig */ public class TPFRequestParserForJenaBackends - extends TPFRequestParser + extends TPFRequestParser { private static TPFRequestParserForJenaBackends instance = null; diff --git a/src/org/linkeddatafragments/fragments/tpf/TriplePatternElement.java b/src/org/linkeddatafragments/fragments/tpf/TriplePatternElement.java deleted file mode 100644 index 55f0163..0000000 --- a/src/org/linkeddatafragments/fragments/tpf/TriplePatternElement.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.linkeddatafragments.fragments.tpf; - -/** - * Represents an element of a triple pattern (i.e., subject, predicate, object). - * - * @param type for representing RDF terms in triple patterns - * @param type for representing specific variables in triple patterns - * - * @author Olaf Hartig - */ -public interface TriplePatternElement -{ - /** - * Returns true if this element is a variable (named or unnamed). - */ - boolean isVariable(); - - /** - * Returns true if this element is a specific variable, and false if either - * it is not a variable but an RDF term or it is some variable that is not - * specified. The latter (unspecified variables) is possible because when - * a client requests a triple pattern fragment, it may omit triple pattern - * related parameters. - * - * If this element is a specific variable (that is, this method returns - * true), this specific variable can be obtained by {@link #asVariable()}. - */ - boolean isSpecificVariable(); - - /** - * Returns a representation of this element as a specific variable (assuming - * it is a specific variable). - * - * @throws UnsupportedOperationException - * If this element is not a specific variable (i.e., - * if {@link #isSpecificVariable()} returns false). - */ - VarType asVariable() throws UnsupportedOperationException; - - /** - * Returns a representation of this element as an RDF term (assuming it is - * an RDF term and not a variable). - * - * @throws UnsupportedOperationException - * If this element is not an RDF term but a variable - * (i.e., if {@link #isVariable()} returns true). - */ - TermType asTerm() throws UnsupportedOperationException; -} diff --git a/src/org/linkeddatafragments/fragments/tpf/TriplePatternElementFactory.java b/src/org/linkeddatafragments/fragments/tpf/TriplePatternElementFactory.java index aff548c..3528d4a 100644 --- a/src/org/linkeddatafragments/fragments/tpf/TriplePatternElementFactory.java +++ b/src/org/linkeddatafragments/fragments/tpf/TriplePatternElementFactory.java @@ -1,63 +1,132 @@ package org.linkeddatafragments.fragments.tpf; /** - * A factory for {@link TriplePatternElement}s. + * A factory for {@link ITriplePatternElement}s. * - * @param type for representing RDF terms in triple patterns - * @param type for representing specific variables in triple patterns + * @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 class TriplePatternElementFactory +public class TriplePatternElementFactory { - public TriplePatternElement createUnspecifiedVariable() { - return new UnspecifiedVariable(); + public ITriplePatternElement createUnspecifiedVariable() + { + return new UnspecifiedVariable(); } - public TriplePatternElement createSpecificVariable( - final VarType variable ) { - return new SpecificVariable( variable ); + public ITriplePatternElement createNamedVariable( final NVT v ) + { + return new NamedVariable( v ); } - public TriplePatternElement createRDFTerm( - final TermType term ) { - return new RDFTerm( term ); + + public ITriplePatternElement createAnonymousVariable( + final AVT bnode ) + { + return new AnonymousVariable( bnode ); + } + + public ITriplePatternElement createConstantRDFTerm( + final CTT term ) + { + return new ConstantRDFTerm( term ); } - static abstract public class Variable - implements TriplePatternElement + + static abstract public class Variable + implements ITriplePatternElement { + @Override public boolean isVariable() { return true; } - public TermType asTerm() { throw new UnsupportedOperationException(); } + @Override + public CTT asConstantTerm() { throw new UnsupportedOperationException(); } } - static public class UnspecifiedVariable - extends Variable + static public class UnspecifiedVariable + extends Variable { + @Override public boolean isSpecificVariable() { return false; } - public VarType asVariable() { throw new UnsupportedOperationException(); } + @Override + public boolean isNamedVariable() { return false; } + @Override + public NVT asNamedVariable() { throw new UnsupportedOperationException(); } + @Override + public boolean isAnonymousVariable() { return false; } + @Override + public AVT asAnonymousVariable() { throw new UnsupportedOperationException(); } + @Override public String toString() { return "UnspecifiedVariable"; } } - static public class SpecificVariable - extends Variable + static abstract public class SpecificVariable + extends Variable { - protected final VarType v; - public SpecificVariable( final VarType variable ) { v = variable; } + @Override public boolean isSpecificVariable() { return true; } - public VarType asVariable() { return v; } - public String toString() { return "SpecificVariable(" + v.toString() + ")"; } } - static public class RDFTerm - implements TriplePatternElement + static public class NamedVariable + extends SpecificVariable + { + protected final NVT v; + public NamedVariable( final NVT variable ) { v = variable; } + @Override + public boolean isNamedVariable() { return true; } + @Override + public NVT asNamedVariable() { return v; } + @Override + public boolean isAnonymousVariable() { return false; } + @Override + public AVT asAnonymousVariable() { throw new UnsupportedOperationException(); } + @Override + public String toString() { return "NamedVariable(" + v.toString() + ")"; } + } + + static public class AnonymousVariable + extends SpecificVariable + { + protected final AVT bn; + public AnonymousVariable( final AVT bnode ) { bn = bnode; } + @Override + public boolean isNamedVariable() { return false; } + @Override + public NVT asNamedVariable() { throw new UnsupportedOperationException(); } + @Override + public boolean isAnonymousVariable() { return true; } + @Override + public AVT asAnonymousVariable() { return bn; } + @Override + public String toString() { return "AnonymousVariable(" + bn.toString() + ")"; } + } + + static public class ConstantRDFTerm + implements ITriplePatternElement { - protected final TermType t; - public RDFTerm( final TermType term ) { t = term; } + protected final CTT t; + public ConstantRDFTerm( final CTT term ) { t = term; } + @Override public boolean isVariable() { return false; } + @Override public boolean isSpecificVariable() { return false; } - public VarType asVariable() { throw new UnsupportedOperationException(); } - public TermType asTerm() { return t; } - public String toString() { return "RDFTerm(" + t.toString() + ")(type: " + t.getClass().getSimpleName() + ")"; } + @Override + public boolean isNamedVariable() { return false; } + @Override + public NVT asNamedVariable() { throw new UnsupportedOperationException(); } + @Override + public boolean isAnonymousVariable() { return false; } + @Override + public AVT asAnonymousVariable() { throw new UnsupportedOperationException(); } + @Override + public CTT asConstantTerm() { return t; } + @Override + public String toString() { return "ConstantRDFTerm(" + t.toString() + ")(type: " + t.getClass().getSimpleName() + ")"; } } } diff --git a/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentBase.java b/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentBase.java index 2afbf1f..09f24a4 100644 --- a/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentBase.java +++ b/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentBase.java @@ -14,13 +14,13 @@ import com.hp.hpl.jena.util.iterator.NiceIterator; /** - * Base class for implementations of {@link TriplePatternFragment}. + * Base class for implementations of {@link ITriplePatternFragment}. * * @author Ruben Verborgh * @author Olaf Hartig */ abstract public class TriplePatternFragmentBase extends LinkedDataFragmentBase - implements TriplePatternFragment + implements ITriplePatternFragment { private final long totalSize; @@ -106,21 +106,21 @@ public void addControls( final Model model ) triplePattern.addProperty( CommonResources.HYDRA_MAPPING, predicateMapping ); triplePattern.addProperty( CommonResources.HYDRA_MAPPING, objectMapping ); - subjectMapping.addProperty( CommonResources.HYDRA_VARIABLE, TriplePatternFragmentRequest.PARAMETERNAME_SUBJ ); + subjectMapping.addProperty( CommonResources.HYDRA_VARIABLE, ITriplePatternFragmentRequest.PARAMETERNAME_SUBJ ); subjectMapping.addProperty( CommonResources.HYDRA_PROPERTY, CommonResources.RDF_SUBJECT ); - predicateMapping.addProperty( CommonResources.HYDRA_VARIABLE, TriplePatternFragmentRequest.PARAMETERNAME_PRED ); + predicateMapping.addProperty( CommonResources.HYDRA_VARIABLE, ITriplePatternFragmentRequest.PARAMETERNAME_PRED ); predicateMapping.addProperty( CommonResources.HYDRA_PROPERTY, CommonResources.RDF_PREDICATE ); - objectMapping.addProperty( CommonResources.HYDRA_VARIABLE, TriplePatternFragmentRequest.PARAMETERNAME_OBJ ); + objectMapping.addProperty( CommonResources.HYDRA_VARIABLE, ITriplePatternFragmentRequest.PARAMETERNAME_OBJ ); objectMapping.addProperty( CommonResources.HYDRA_PROPERTY, CommonResources.RDF_OBJECT ); } public String getTemplate() { return datasetURL + "{?" + - TriplePatternFragmentRequest.PARAMETERNAME_SUBJ + "," + - TriplePatternFragmentRequest.PARAMETERNAME_PRED + "," + - TriplePatternFragmentRequest.PARAMETERNAME_OBJ + "}"; + ITriplePatternFragmentRequest.PARAMETERNAME_SUBJ + "," + + ITriplePatternFragmentRequest.PARAMETERNAME_PRED + "," + + ITriplePatternFragmentRequest.PARAMETERNAME_OBJ + "}"; } diff --git a/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequest.java b/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequest.java deleted file mode 100644 index 85154f7..0000000 --- a/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequest.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.linkeddatafragments.fragments.tpf; - -import org.linkeddatafragments.fragments.LinkedDataFragmentRequest; - -/** - * Represents a request of a Triple Pattern Fragment (TPF). - * - * @param type for representing RDF terms in triple patterns - * @param type for representing specific variables in triple patterns - * - * @author Olaf Hartig - */ -public interface TriplePatternFragmentRequest - extends LinkedDataFragmentRequest -{ - public final static String PARAMETERNAME_SUBJ = "subject"; - public final static String PARAMETERNAME_PRED = "predicate"; - public final static String PARAMETERNAME_OBJ = "object"; - - /** - * Returns the subject position of the requested triple pattern. - */ - TriplePatternElement getSubject(); - - /** - * Returns the predicate position of the requested triple pattern. - */ - TriplePatternElement getPredicate(); - - /** - * Returns the object position of the requested triple pattern. - */ - TriplePatternElement getObject(); -} diff --git a/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequestImpl.java b/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequestImpl.java index c968212..aeabd40 100644 --- a/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequestImpl.java +++ b/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequestImpl.java @@ -3,25 +3,25 @@ import org.linkeddatafragments.fragments.LinkedDataFragmentRequestBase; /** - * An implementation of {@link TriplePatternFragmentRequest}. + * An implementation of {@link ITriplePatternFragmentRequest}. * * @author Olaf Hartig */ -public class TriplePatternFragmentRequestImpl +public class TriplePatternFragmentRequestImpl extends LinkedDataFragmentRequestBase - implements TriplePatternFragmentRequest + implements ITriplePatternFragmentRequest { - public final TriplePatternElement subject; - public final TriplePatternElement predicate; - public final TriplePatternElement object; + public final ITriplePatternElement subject; + public final ITriplePatternElement predicate; + public final ITriplePatternElement object; public TriplePatternFragmentRequestImpl( final String fragmentURL, final String datasetURL, final boolean pageNumberWasRequested, final long pageNumber, - final TriplePatternElement subject, - final TriplePatternElement predicate, - final TriplePatternElement object ) + final ITriplePatternElement subject, + final ITriplePatternElement predicate, + final ITriplePatternElement object ) { super( fragmentURL, datasetURL, pageNumberWasRequested, pageNumber ); @@ -40,17 +40,17 @@ public TriplePatternFragmentRequestImpl( final String fragmentURL, } @Override - public TriplePatternElement getSubject() { + public ITriplePatternElement getSubject() { return subject; } @Override - public TriplePatternElement getPredicate() { + public ITriplePatternElement getPredicate() { return predicate; } @Override - public TriplePatternElement getObject() { + public ITriplePatternElement getObject() { return object; } diff --git a/src/org/linkeddatafragments/servlet/LinkedDataFragmentServlet.java b/src/org/linkeddatafragments/servlet/LinkedDataFragmentServlet.java index 6b4f89b..1ef06e2 100644 --- a/src/org/linkeddatafragments/servlet/LinkedDataFragmentServlet.java +++ b/src/org/linkeddatafragments/servlet/LinkedDataFragmentServlet.java @@ -26,8 +26,8 @@ import org.linkeddatafragments.datasource.index.IndexDataSource; import org.linkeddatafragments.exceptions.DataSourceNotFoundException; import org.linkeddatafragments.fragments.FragmentRequestParserBase; -import org.linkeddatafragments.fragments.LinkedDataFragment; -import org.linkeddatafragments.fragments.LinkedDataFragmentRequest; +import org.linkeddatafragments.fragments.ILinkedDataFragment; +import org.linkeddatafragments.fragments.ILinkedDataFragmentRequest; import org.linkeddatafragments.util.MIMEParse; /** @@ -141,11 +141,11 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro try { final IDataSource dataSource = getDataSource( request ); - final LinkedDataFragmentRequest ldfRequest = + final ILinkedDataFragmentRequest ldfRequest = dataSource.getRequestParser() .parseIntoFragmentRequest( request, config ); - final LinkedDataFragment fragment = + final ILinkedDataFragment fragment = dataSource.getRequestProcessor() .createRequestedFragment( ldfRequest ); diff --git a/src/org/linkeddatafragments/util/TriplePatternElementParser.java b/src/org/linkeddatafragments/util/TriplePatternElementParser.java index ecbb2e3..40e6ccc 100644 --- a/src/org/linkeddatafragments/util/TriplePatternElementParser.java +++ b/src/org/linkeddatafragments/util/TriplePatternElementParser.java @@ -1,25 +1,29 @@ package org.linkeddatafragments.util; -import org.linkeddatafragments.fragments.tpf.TriplePatternElement; +import org.linkeddatafragments.fragments.tpf.ITriplePatternElement; import org.linkeddatafragments.fragments.tpf.TriplePatternElementFactory; /** * Parses strings (as obtained from HTTP request parameters) into - * {@link TriplePatternElement}s. + * {@link ITriplePatternElement}s. * - * @param type for representing RDF terms - * @param type for representing specific variables + * @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 * @author Ruben Verborgh */ -abstract public class TriplePatternElementParser - extends RDFTermParser +abstract public + class TriplePatternElementParser + extends RDFTermParser { - public final TriplePatternElementFactory factory = - new TriplePatternElementFactory(); + public final TriplePatternElementFactory + factory = new TriplePatternElementFactory(); - public TriplePatternElement + public ITriplePatternElement parseIntoTriplePatternElement( final String param ) { // nothing or empty indicates an unspecified variable @@ -30,25 +34,28 @@ abstract public class TriplePatternElementParser char firstChar = param.charAt(0); switch ( firstChar ) { - // specific variable + // specific variable that has a name case '?': { final String varName = param.substring(1); - final VarType var = createSpecificVariable( varName ); - return factory.createSpecificVariable( var ); + final NamedVarType var = createNamedVariable( varName ); + return factory.createNamedVariable( var ); } - // blank node indicates an unspecified variable + // specific variable that is denoted by a blank node case '_': { - return factory.createUnspecifiedVariable(); + final AnonVarType var = createAnonymousVariable( param ); + return factory.createAnonymousVariable( var ); } // assume it is an RDF term default: - return factory.createRDFTerm( parseIntoRDFNode(param) ); + return factory.createConstantRDFTerm( parseIntoRDFNode(param) ); } } - abstract public VarType createSpecificVariable( final String varName ); + abstract public NamedVarType createNamedVariable( final String varName ); + + abstract public AnonVarType createAnonymousVariable( final String label ); } diff --git a/src/org/linkeddatafragments/util/TriplePatternElementParserForJena.java b/src/org/linkeddatafragments/util/TriplePatternElementParserForJena.java index 41dd83c..029c512 100644 --- a/src/org/linkeddatafragments/util/TriplePatternElementParserForJena.java +++ b/src/org/linkeddatafragments/util/TriplePatternElementParserForJena.java @@ -11,7 +11,7 @@ * @author Olaf Hartig */ public class TriplePatternElementParserForJena - extends TriplePatternElementParser + extends TriplePatternElementParser { private static TriplePatternElementParserForJena instance = null; @@ -26,11 +26,17 @@ public static TriplePatternElementParserForJena getInstance() protected TriplePatternElementParserForJena() {} @Override - public String createSpecificVariable( final String varName ) + public String createNamedVariable( final String varName ) { return varName; } + @Override + public String createAnonymousVariable( final String label ) + { + return label; + } + @Override public RDFNode createBlankNode( final String label ) { diff --git a/src/test/java/org/linkeddatafragments/TestSuite.java b/src/test/java/org/linkeddatafragments/TestSuite.java index 0ce6d2e..3faaaea 100644 --- a/src/test/java/org/linkeddatafragments/TestSuite.java +++ b/src/test/java/org/linkeddatafragments/TestSuite.java @@ -1,8 +1,3 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package test.java.org.linkeddatafragments; import org.junit.runner.RunWith; diff --git a/src/test/java/org/linkeddatafragments/datasource/DataSourceTest.java b/src/test/java/org/linkeddatafragments/datasource/DataSourceTest.java index 81c3589..d2ad1c4 100644 --- a/src/test/java/org/linkeddatafragments/datasource/DataSourceTest.java +++ b/src/test/java/org/linkeddatafragments/datasource/DataSourceTest.java @@ -14,10 +14,10 @@ import org.linkeddatafragments.datasource.IDataSource; import org.linkeddatafragments.datasource.IFragmentRequestProcessor; -import org.linkeddatafragments.fragments.LinkedDataFragment; -import org.linkeddatafragments.fragments.tpf.TriplePatternElement; -import org.linkeddatafragments.fragments.tpf.TriplePatternFragment; -import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequest; +import org.linkeddatafragments.fragments.ILinkedDataFragment; +import org.linkeddatafragments.fragments.tpf.ITriplePatternElement; +import org.linkeddatafragments.fragments.tpf.ITriplePatternFragment; +import org.linkeddatafragments.fragments.tpf.ITriplePatternFragmentRequest; import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequestImpl; import org.linkeddatafragments.util.TriplePatternElementParser; @@ -26,13 +26,14 @@ * * @author Bart Hanssens */ -public abstract class DataSourceTest { +public abstract class DataSourceTest +{ private static IDataSource ds; - + /** * Get data source - * - * @return data source interface + * + * @return data source interface */ public static IDataSource getDatasource() { return ds; @@ -40,21 +41,21 @@ public static IDataSource getDatasource() { /** * Set the data source - * - * @param ds data source + * + * @param ds data source */ public static void setDatasource(IDataSource ds) { DataSourceTest.ds = ds; } - protected abstract TriplePatternElementParser + protected abstract TriplePatternElementParser getTriplePatternElementParser(); - + /** * Copy the demo triple in the jar to a temp file. - * + * * @return temp file - * @throws IOException + * @throws IOException */ public static File getResourceAsFile() throws IOException { File temp = File.createTempFile("ldf-test-hdt", ".ttl"); @@ -65,10 +66,10 @@ public static File getResourceAsFile() throws IOException { return temp; } - + /** * Generate a basic Json configuration - * + * * @param title * @param desc * @param type @@ -79,22 +80,22 @@ public static JsonObject createConfig(String title, String desc, String type) { config.addProperty("title", title); config.addProperty("description", desc); config.addProperty("type", type); - + return config; } - - + + /** * Test total size of empty TPF - * + * */ @Test - public void testEmpty() { - final TriplePatternElementParser tpeParser = + public void testEmpty() { + final TriplePatternElementParser tpeParser = getTriplePatternElementParser(); - final TriplePatternFragmentRequest request = - new TriplePatternFragmentRequestImpl( + final ITriplePatternFragmentRequest request = + new TriplePatternFragmentRequestImpl( "http://example.org/f", // fragmentURL "http://example.org/", // datasetURL, true, // pageNumberWasRequested, @@ -104,47 +105,47 @@ public void testEmpty() { tpeParser.parseIntoTriplePatternElement(null) ); //object final IFragmentRequestProcessor proc = getDatasource().getRequestProcessor(); - final LinkedDataFragment ldf = proc.createRequestedFragment( request ); - final TriplePatternFragment tpf = (TriplePatternFragment) ldf; + final ILinkedDataFragment ldf = proc.createRequestedFragment( request ); + final ITriplePatternFragment tpf = (ITriplePatternFragment) ldf; long totalSize = tpf.getTotalSize(); - - Assert.assertTrue("Estimate is too big : " + totalSize, totalSize == 0); - + + Assert.assertTrue("Estimate is too big : " + totalSize, totalSize == 0); + } - + /** * Test if estimate seems reasonable. */ @Test public void testEstimate() { - final TriplePatternElementParser tpeParser = + final TriplePatternElementParser tpeParser = getTriplePatternElementParser(); - final TriplePatternFragmentRequest request = - new TriplePatternFragmentRequest() { + final ITriplePatternFragmentRequest request = + new ITriplePatternFragmentRequest() { public boolean isPageRequest() { return true; } public long getPageNumber() { return 1L; } public String getFragmentURL() { return "http://example.org/f"; } public String getDatasetURL() { return "http://example.org/"; } - public TriplePatternElement getSubject() { + public ITriplePatternElement getSubject() { return tpeParser.parseIntoTriplePatternElement("http://data.gov.be/catalog/ckanvl"); } - public TriplePatternElement getPredicate() { + public ITriplePatternElement getPredicate() { return tpeParser.parseIntoTriplePatternElement(null); } - public TriplePatternElement getObject() { + public ITriplePatternElement getObject() { return tpeParser.parseIntoTriplePatternElement(null); } }; final IFragmentRequestProcessor proc = getDatasource().getRequestProcessor(); - final LinkedDataFragment ldf = proc.createRequestedFragment( request ); - final TriplePatternFragment tpf = (TriplePatternFragment) ldf; + final ILinkedDataFragment ldf = proc.createRequestedFragment( request ); + final ITriplePatternFragment tpf = (ITriplePatternFragment) ldf; long totalSize = tpf.getTotalSize(); - - Assert.assertTrue("Estimate is too small : " + totalSize, totalSize > 100); + + Assert.assertTrue("Estimate is too small : " + totalSize, totalSize > 100); } } diff --git a/src/test/java/org/linkeddatafragments/datasource/HdtDataSourceTest.java b/src/test/java/org/linkeddatafragments/datasource/HdtDataSourceTest.java index 09e5e2c..712a2a9 100644 --- a/src/test/java/org/linkeddatafragments/datasource/HdtDataSourceTest.java +++ b/src/test/java/org/linkeddatafragments/datasource/HdtDataSourceTest.java @@ -22,12 +22,12 @@ * * @author Bart Hanssens */ -public class HdtDataSourceTest extends DataSourceTest { +public class HdtDataSourceTest extends DataSourceTest { private static File hdtfile; @Override - protected TriplePatternElementParser + protected TriplePatternElementParser getTriplePatternElementParser() { return TriplePatternElementParserForJena.getInstance(); diff --git a/src/test/java/org/linkeddatafragments/datasource/JenaTDBDataSourceTest.java b/src/test/java/org/linkeddatafragments/datasource/JenaTDBDataSourceTest.java index 9ee6935..7ea7d42 100644 --- a/src/test/java/org/linkeddatafragments/datasource/JenaTDBDataSourceTest.java +++ b/src/test/java/org/linkeddatafragments/datasource/JenaTDBDataSourceTest.java @@ -29,12 +29,13 @@ * * @author Bart Hanssens */ -public class JenaTDBDataSourceTest extends DataSourceTest { +public class JenaTDBDataSourceTest extends DataSourceTest +{ private static File jena; private static Dataset dataset; @Override - protected TriplePatternElementParser + protected TriplePatternElementParser getTriplePatternElementParser() { return TriplePatternElementParserForJena.getInstance();