Skip to content

Commit

Permalink
extended the TriplePatternInterface to support anonymous variables; see
Browse files Browse the repository at this point in the history
  • Loading branch information
Olaf Hartig committed Dec 31, 2015
1 parent f03b8ed commit 29b2e70
Show file tree
Hide file tree
Showing 15 changed files with 296 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,46 @@
* Base class for implementations of {@link IFragmentRequestProcessor} that
* process {@link TriplePatternFragmentRequest}s.
*
* @param <TermType> type for representing RDF terms in triple patterns
* @param <VarType> type for representing specific variables in triple patterns
* @param <CTT>
* type for representing constants in triple patterns (i.e., URIs and
* literals)
* @param <NVT>
* type for representing named variables in triple patterns
* @param <AVT>
* type for representing anonymous variables in triple patterns (i.e.,
* variables denoted by a blank node)
*
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
*/
public abstract class
AbstractRequestProcessorForTriplePatterns<TermType,VarType>
AbstractRequestProcessorForTriplePatterns<CTT,NVT,AVT>
extends AbstractRequestProcessor
{
@Override
protected final Worker<TermType,VarType> getWorker(
protected final Worker<CTT,NVT,AVT> getWorker(
final LinkedDataFragmentRequest request )
throws IllegalArgumentException
{
if ( request instanceof TriplePatternFragmentRequest<?,?> ) {
if ( request instanceof TriplePatternFragmentRequest<?,?,?> ) {
@SuppressWarnings("unchecked")
final TriplePatternFragmentRequest<TermType,VarType> tpfRequest =
(TriplePatternFragmentRequest<TermType,VarType>) request;
final TriplePatternFragmentRequest<CTT,NVT,AVT> tpfRequest =
(TriplePatternFragmentRequest<CTT,NVT,AVT>) request;
return getTPFSpecificWorker( tpfRequest );
}
else
throw new IllegalArgumentException( request.getClass().getName() );
}

abstract protected Worker<TermType,VarType> getTPFSpecificWorker(
final TriplePatternFragmentRequest<TermType,VarType> request )
abstract protected Worker<CTT,NVT,AVT> getTPFSpecificWorker(
final TriplePatternFragmentRequest<CTT,NVT,AVT> request )
throws IllegalArgumentException;


abstract static protected class Worker<TermType,VarType>
abstract static protected class Worker<CTT,NVT,AVT>
extends AbstractRequestProcessor.Worker
{
public Worker(
final TriplePatternFragmentRequest<TermType,VarType> request )
final TriplePatternFragmentRequest<CTT,NVT,AVT> request )
{
super( request );
}
Expand All @@ -63,8 +69,8 @@ public LinkedDataFragment createRequestedFragment()
offset = 0L;

@SuppressWarnings("unchecked")
final TriplePatternFragmentRequest<TermType,VarType> tpfRequest =
(TriplePatternFragmentRequest<TermType,VarType>) request;
final TriplePatternFragmentRequest<CTT,NVT,AVT> tpfRequest =
(TriplePatternFragmentRequest<CTT,NVT,AVT>) request;

return createFragment( tpfRequest.getSubject(),
tpfRequest.getPredicate(),
Expand All @@ -73,9 +79,9 @@ public LinkedDataFragment createRequestedFragment()
}

abstract protected LinkedDataFragment createFragment(
final TriplePatternElement<TermType,VarType> subj,
final TriplePatternElement<TermType,VarType> pred,
final TriplePatternElement<TermType,VarType> obj,
final TriplePatternElement<CTT,NVT,AVT> subj,
final TriplePatternElement<CTT,NVT,AVT> pred,
final TriplePatternElement<CTT,NVT,AVT> obj,
final long offset,
final long limit )
throws IllegalArgumentException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
*/
public class HdtBasedRequestProcessorForTPFs
extends AbstractRequestProcessorForTriplePatterns<RDFNode,String>
extends AbstractRequestProcessorForTriplePatterns<RDFNode,String,String>
{
protected final HDT datasource;
protected final NodeDictionary dictionary;
Expand All @@ -46,38 +46,40 @@ public HdtBasedRequestProcessorForTPFs( String hdtFile ) throws IOException

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


protected class Worker
extends AbstractRequestProcessorForTriplePatterns.Worker<RDFNode,String>
extends AbstractRequestProcessorForTriplePatterns.Worker<RDFNode,String,String>
{
public Worker( final TriplePatternFragmentRequest<RDFNode,String> req )
public Worker(
final TriplePatternFragmentRequest<RDFNode,String,String> req )
{
super( req );
}

@Override
protected LinkedDataFragment createFragment(
final TriplePatternElement<RDFNode,String> subject,
final TriplePatternElement<RDFNode,String> predicate,
final TriplePatternElement<RDFNode,String> object,
final long offset,
final long limit )
final TriplePatternElement<RDFNode,String,String> subject,
final TriplePatternElement<RDFNode,String,String> predicate,
final TriplePatternElement<RDFNode,String,String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
*/
public class IndexRequestProcessorForTPFs
extends AbstractRequestProcessorForTriplePatterns<RDFNode,String>
extends AbstractRequestProcessorForTriplePatterns<RDFNode,String,String>
{
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#";
Expand Down Expand Up @@ -58,40 +58,42 @@ public IndexRequestProcessorForTPFs(

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


protected class Worker
extends AbstractRequestProcessorForTriplePatterns.Worker<RDFNode,String>
extends AbstractRequestProcessorForTriplePatterns.Worker<RDFNode,String,String>
{
public Worker( final TriplePatternFragmentRequest<RDFNode,String> req )
public Worker(
final TriplePatternFragmentRequest<RDFNode,String,String> req )
{
super( req );
}

@Override
protected LinkedDataFragment createFragment(
final TriplePatternElement<RDFNode,String> s,
final TriplePatternElement<RDFNode,String> p,
final TriplePatternElement<RDFNode,String> o,
final long offset,
final long limit )
final TriplePatternElement<RDFNode,String,String> s,
final TriplePatternElement<RDFNode,String,String> p,
final TriplePatternElement<RDFNode,String,String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
*/
public class JenaTDBBasedRequestProcessorForTPFs
extends AbstractRequestProcessorForTriplePatterns<RDFNode,String>
extends AbstractRequestProcessorForTriplePatterns<RDFNode,String,String>
{
private final Dataset tdb;
private final String sparql = "CONSTRUCT WHERE { ?s ?p ?o } " +
Expand All @@ -43,44 +43,46 @@ public class JenaTDBBasedRequestProcessorForTPFs

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


protected class Worker
extends AbstractRequestProcessorForTriplePatterns.Worker<RDFNode,String>
extends AbstractRequestProcessorForTriplePatterns.Worker<RDFNode,String,String>
{
public Worker( final TriplePatternFragmentRequest<RDFNode,String> req )
public Worker(
final TriplePatternFragmentRequest<RDFNode,String,String> req )
{
super( req );
}

@Override
protected LinkedDataFragment createFragment(
final TriplePatternElement<RDFNode,String> subject,
final TriplePatternElement<RDFNode,String> predicate,
final TriplePatternElement<RDFNode,String> object,
final long offset,
final long limit )
final TriplePatternElement<RDFNode,String,String> subject,
final TriplePatternElement<RDFNode,String,String> predicate,
final TriplePatternElement<RDFNode,String,String> 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);
Expand Down
16 changes: 8 additions & 8 deletions src/org/linkeddatafragments/fragments/tpf/TPFRequestParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
*/
public class TPFRequestParser<TermType,VarType>
public class TPFRequestParser<ConstantTermType,NamedVarType,AnonVarType>
extends FragmentRequestParserBase
{
public final TriplePatternElementParser<TermType,VarType> elmtParser;
public final TriplePatternElementParser<ConstantTermType,NamedVarType,AnonVarType> elmtParser;

public TPFRequestParser(
final TriplePatternElementParser<TermType,VarType> elmtParser )
final TriplePatternElementParser<ConstantTermType,NamedVarType,AnonVarType> elmtParser )
{
this.elmtParser = elmtParser;
}
Expand All @@ -47,7 +47,7 @@ public Worker( final HttpServletRequest request,
public LinkedDataFragmentRequest createFragmentRequest()
throws IllegalArgumentException
{
return new TriplePatternFragmentRequestImpl<TermType,VarType>(
return new TriplePatternFragmentRequestImpl<ConstantTermType,NamedVarType,AnonVarType>(
getFragmentURL(),
getDatasetURL(),
pageNumberWasRequested,
Expand All @@ -57,22 +57,22 @@ public LinkedDataFragmentRequest createFragmentRequest()
getObject() );
}

public TriplePatternElement<TermType,VarType> getSubject() {
public TriplePatternElement<ConstantTermType,NamedVarType,AnonVarType> getSubject() {
return getParameterAsTriplePatternElement(
TriplePatternFragmentRequest.PARAMETERNAME_SUBJ );
}

public TriplePatternElement<TermType,VarType> getPredicate() {
public TriplePatternElement<ConstantTermType,NamedVarType,AnonVarType> getPredicate() {
return getParameterAsTriplePatternElement(
TriplePatternFragmentRequest.PARAMETERNAME_PRED );
}

public TriplePatternElement<TermType,VarType> getObject() {
public TriplePatternElement<ConstantTermType,NamedVarType,AnonVarType> getObject() {
return getParameterAsTriplePatternElement(
TriplePatternFragmentRequest.PARAMETERNAME_OBJ );
}

public TriplePatternElement<TermType,VarType>
public TriplePatternElement<ConstantTermType,NamedVarType,AnonVarType>
getParameterAsTriplePatternElement( final String paramName )
{
final String parameter = request.getParameter( paramName );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
*/
public class TPFRequestParserForJenaBackends
extends TPFRequestParser<RDFNode,String>
extends TPFRequestParser<RDFNode,String,String>
{
private static TPFRequestParserForJenaBackends instance = null;

Expand Down
Loading

0 comments on commit 29b2e70

Please sign in to comment.