diff --git a/src/org/linkeddatafragments/datasource/AbstractRequestProcessorForTriplePatterns.java b/src/org/linkeddatafragments/datasource/AbstractRequestProcessorForTriplePatterns.java index 9ba984a..debae47 100644 --- a/src/org/linkeddatafragments/datasource/AbstractRequestProcessorForTriplePatterns.java +++ b/src/org/linkeddatafragments/datasource/AbstractRequestProcessorForTriplePatterns.java @@ -13,40 +13,46 @@ * Base class for implementations of {@link IFragmentRequestProcessor} that * process {@link TriplePatternFragmentRequest}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( + protected final Worker getWorker( final LinkedDataFragmentRequest request ) throws IllegalArgumentException { - if ( request instanceof TriplePatternFragmentRequest ) { + if ( request instanceof TriplePatternFragmentRequest ) { @SuppressWarnings("unchecked") - final TriplePatternFragmentRequest tpfRequest = - (TriplePatternFragmentRequest) request; + final TriplePatternFragmentRequest tpfRequest = + (TriplePatternFragmentRequest) request; return getTPFSpecificWorker( tpfRequest ); } else throw new IllegalArgumentException( request.getClass().getName() ); } - abstract protected Worker getTPFSpecificWorker( - final TriplePatternFragmentRequest request ) + abstract protected Worker getTPFSpecificWorker( + final TriplePatternFragmentRequest request ) throws IllegalArgumentException; - abstract static protected class Worker + abstract static protected class Worker extends AbstractRequestProcessor.Worker { public Worker( - final TriplePatternFragmentRequest request ) + final TriplePatternFragmentRequest request ) { super( request ); } @@ -63,8 +69,8 @@ public LinkedDataFragment createRequestedFragment() offset = 0L; @SuppressWarnings("unchecked") - final TriplePatternFragmentRequest tpfRequest = - (TriplePatternFragmentRequest) request; + final TriplePatternFragmentRequest tpfRequest = + (TriplePatternFragmentRequest) request; return createFragment( tpfRequest.getSubject(), tpfRequest.getPredicate(), @@ -73,9 +79,9 @@ public LinkedDataFragment createRequestedFragment() } abstract protected LinkedDataFragment createFragment( - final TriplePatternElement subj, - final TriplePatternElement pred, - final TriplePatternElement obj, + final TriplePatternElement subj, + final TriplePatternElement pred, + final TriplePatternElement obj, final long offset, final long limit ) throws IllegalArgumentException; diff --git a/src/org/linkeddatafragments/datasource/hdt/HdtBasedRequestProcessorForTPFs.java b/src/org/linkeddatafragments/datasource/hdt/HdtBasedRequestProcessorForTPFs.java index 3ed8dbe..be22e45 100644 --- a/src/org/linkeddatafragments/datasource/hdt/HdtBasedRequestProcessorForTPFs.java +++ b/src/org/linkeddatafragments/datasource/hdt/HdtBasedRequestProcessorForTPFs.java @@ -27,7 +27,7 @@ * @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 TriplePatternFragmentRequest 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 TriplePatternFragmentRequest req ) { super( req ); } @Override protected LinkedDataFragment createFragment( - final TriplePatternElement subject, - final TriplePatternElement predicate, - final TriplePatternElement object, - final long offset, - final long limit ) + final TriplePatternElement subject, + final TriplePatternElement predicate, + final TriplePatternElement 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/index/IndexRequestProcessorForTPFs.java b/src/org/linkeddatafragments/datasource/index/IndexRequestProcessorForTPFs.java index 8e4c6d4..8b74ac6 100644 --- a/src/org/linkeddatafragments/datasource/index/IndexRequestProcessorForTPFs.java +++ b/src/org/linkeddatafragments/datasource/index/IndexRequestProcessorForTPFs.java @@ -28,7 +28,7 @@ * @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 TriplePatternFragmentRequest 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 TriplePatternFragmentRequest req ) { super( req ); } @Override protected LinkedDataFragment createFragment( - final TriplePatternElement s, - final TriplePatternElement p, - final TriplePatternElement o, - final long offset, - final long limit ) + final TriplePatternElement s, + final TriplePatternElement p, + final TriplePatternElement 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..8a991d1 100644 --- a/src/org/linkeddatafragments/datasource/tdb/JenaTDBBasedRequestProcessorForTPFs.java +++ b/src/org/linkeddatafragments/datasource/tdb/JenaTDBBasedRequestProcessorForTPFs.java @@ -30,7 +30,7 @@ * @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 TriplePatternFragmentRequest 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 TriplePatternFragmentRequest req ) { super( req ); } @Override protected LinkedDataFragment createFragment( - final TriplePatternElement subject, - final TriplePatternElement predicate, - final TriplePatternElement object, - final long offset, - final long limit ) + final TriplePatternElement subject, + final TriplePatternElement predicate, + final TriplePatternElement 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/fragments/tpf/TPFRequestParser.java b/src/org/linkeddatafragments/fragments/tpf/TPFRequestParser.java index b03e7c8..def4e9a 100644 --- a/src/org/linkeddatafragments/fragments/tpf/TPFRequestParser.java +++ b/src/org/linkeddatafragments/fragments/tpf/TPFRequestParser.java @@ -16,13 +16,13 @@ * * @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; } @@ -47,7 +47,7 @@ public Worker( final HttpServletRequest request, public LinkedDataFragmentRequest createFragmentRequest() throws IllegalArgumentException { - return new TriplePatternFragmentRequestImpl( + return new TriplePatternFragmentRequestImpl( getFragmentURL(), getDatasetURL(), pageNumberWasRequested, @@ -57,22 +57,22 @@ public LinkedDataFragmentRequest createFragmentRequest() getObject() ); } - public TriplePatternElement getSubject() { + public TriplePatternElement getSubject() { return getParameterAsTriplePatternElement( TriplePatternFragmentRequest.PARAMETERNAME_SUBJ ); } - public TriplePatternElement getPredicate() { + public TriplePatternElement getPredicate() { return getParameterAsTriplePatternElement( TriplePatternFragmentRequest.PARAMETERNAME_PRED ); } - public TriplePatternElement getObject() { + public TriplePatternElement getObject() { return getParameterAsTriplePatternElement( TriplePatternFragmentRequest.PARAMETERNAME_OBJ ); } - public TriplePatternElement + public TriplePatternElement 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 index 55f0163..fe2c743 100644 --- a/src/org/linkeddatafragments/fragments/tpf/TriplePatternElement.java +++ b/src/org/linkeddatafragments/fragments/tpf/TriplePatternElement.java @@ -3,47 +3,80 @@ /** * 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 + * @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 TriplePatternElement +public interface TriplePatternElement { /** - * Returns true if this element is a variable (named or unnamed). + * 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 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()}. + * 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 a representation of this element as a specific variable (assuming - * it is a specific variable). + * 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 variable (i.e., - * if {@link #isSpecificVariable()} returns false). + * If this element is not a specific anonymous variable (i.e., + * if {@link #isAnonymousVariable()} returns false). */ - VarType asVariable() throws UnsupportedOperationException; + AnonVarType asAnonymousVariable() throws UnsupportedOperationException; /** - * Returns a representation of this element as an RDF term (assuming it is - * an RDF term and not a variable). + * 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 an RDF term but a variable + * If this element is not a constant RDF term but a variable * (i.e., if {@link #isVariable()} returns true). */ - TermType asTerm() throws UnsupportedOperationException; + ConstantTermType asConstantTerm() throws UnsupportedOperationException; } diff --git a/src/org/linkeddatafragments/fragments/tpf/TriplePatternElementFactory.java b/src/org/linkeddatafragments/fragments/tpf/TriplePatternElementFactory.java index aff548c..fdd4537 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 TriplePatternElement}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 TriplePatternElement createUnspecifiedVariable() + { + return new UnspecifiedVariable(); } - public TriplePatternElement createSpecificVariable( - final VarType variable ) { - return new SpecificVariable( variable ); + public TriplePatternElement createNamedVariable( final NVT v ) + { + return new NamedVariable( v ); } - public TriplePatternElement createRDFTerm( - final TermType term ) { - return new RDFTerm( term ); + + public TriplePatternElement createAnonymousVariable( + final AVT bnode ) + { + return new AnonymousVariable( bnode ); + } + + public TriplePatternElement createConstantRDFTerm( + final CTT term ) + { + return new ConstantRDFTerm( term ); } - static abstract public class Variable - implements TriplePatternElement + + static abstract public class Variable + implements TriplePatternElement { + @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 TriplePatternElement { - 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/TriplePatternFragmentRequest.java b/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequest.java index 85154f7..35b9a98 100644 --- a/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequest.java +++ b/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequest.java @@ -5,12 +5,15 @@ /** * 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 + * @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 TriplePatternFragmentRequest +public interface TriplePatternFragmentRequest extends LinkedDataFragmentRequest { public final static String PARAMETERNAME_SUBJ = "subject"; @@ -20,15 +23,15 @@ public interface TriplePatternFragmentRequest /** * Returns the subject position of the requested triple pattern. */ - TriplePatternElement getSubject(); + TriplePatternElement getSubject(); /** * Returns the predicate position of the requested triple pattern. */ - TriplePatternElement getPredicate(); + TriplePatternElement getPredicate(); /** * Returns the object position of the requested triple pattern. */ - TriplePatternElement getObject(); + TriplePatternElement getObject(); } diff --git a/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequestImpl.java b/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequestImpl.java index c968212..abec2ef 100644 --- a/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequestImpl.java +++ b/src/org/linkeddatafragments/fragments/tpf/TriplePatternFragmentRequestImpl.java @@ -7,21 +7,21 @@ * * @author Olaf Hartig */ -public class TriplePatternFragmentRequestImpl +public class TriplePatternFragmentRequestImpl extends LinkedDataFragmentRequestBase - implements TriplePatternFragmentRequest + implements TriplePatternFragmentRequest { - public final TriplePatternElement subject; - public final TriplePatternElement predicate; - public final TriplePatternElement object; + public final TriplePatternElement subject; + public final TriplePatternElement predicate; + public final TriplePatternElement 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 TriplePatternElement subject, + final TriplePatternElement predicate, + final TriplePatternElement object ) { super( fragmentURL, datasetURL, pageNumberWasRequested, pageNumber ); @@ -40,17 +40,17 @@ public TriplePatternFragmentRequestImpl( final String fragmentURL, } @Override - public TriplePatternElement getSubject() { + public TriplePatternElement getSubject() { return subject; } @Override - public TriplePatternElement getPredicate() { + public TriplePatternElement getPredicate() { return predicate; } @Override - public TriplePatternElement getObject() { + public TriplePatternElement getObject() { return object; } diff --git a/src/org/linkeddatafragments/util/TriplePatternElementParser.java b/src/org/linkeddatafragments/util/TriplePatternElementParser.java index ecbb2e3..8b16889 100644 --- a/src/org/linkeddatafragments/util/TriplePatternElementParser.java +++ b/src/org/linkeddatafragments/util/TriplePatternElementParser.java @@ -5,21 +5,25 @@ /** * Parses strings (as obtained from HTTP request parameters) into - * {@link TriplePatternElement}s. + * {@link TriplePatternElement}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 TriplePatternElement 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/datasource/DataSourceTest.java b/src/test/java/org/linkeddatafragments/datasource/DataSourceTest.java index 81c3589..937dcaa 100644 --- a/src/test/java/org/linkeddatafragments/datasource/DataSourceTest.java +++ b/src/test/java/org/linkeddatafragments/datasource/DataSourceTest.java @@ -26,7 +26,8 @@ * * @author Bart Hanssens */ -public abstract class DataSourceTest { +public abstract class DataSourceTest +{ private static IDataSource ds; /** @@ -47,7 +48,7 @@ public static void setDatasource(IDataSource ds) { DataSourceTest.ds = ds; } - protected abstract TriplePatternElementParser + protected abstract TriplePatternElementParser getTriplePatternElementParser(); /** @@ -90,11 +91,11 @@ public static JsonObject createConfig(String title, String desc, String type) { */ @Test public void testEmpty() { - final TriplePatternElementParser tpeParser = + final TriplePatternElementParser tpeParser = getTriplePatternElementParser(); - final TriplePatternFragmentRequest request = - new TriplePatternFragmentRequestImpl( + final TriplePatternFragmentRequest request = + new TriplePatternFragmentRequestImpl( "http://example.org/f", // fragmentURL "http://example.org/", // datasetURL, true, // pageNumberWasRequested, @@ -118,23 +119,23 @@ public void testEmpty() { */ @Test public void testEstimate() { - final TriplePatternElementParser tpeParser = + final TriplePatternElementParser tpeParser = getTriplePatternElementParser(); - final TriplePatternFragmentRequest request = - new TriplePatternFragmentRequest() { + final TriplePatternFragmentRequest request = + new TriplePatternFragmentRequest() { 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 TriplePatternElement getSubject() { return tpeParser.parseIntoTriplePatternElement("http://data.gov.be/catalog/ckanvl"); } - public TriplePatternElement getPredicate() { + public TriplePatternElement getPredicate() { return tpeParser.parseIntoTriplePatternElement(null); } - public TriplePatternElement getObject() { + public TriplePatternElement getObject() { return tpeParser.parseIntoTriplePatternElement(null); } }; 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();