Skip to content

Commit

Permalink
Merge pull request #6 from LinkedDataFragments/master
Browse files Browse the repository at this point in the history
merging in latest changes from origin
  • Loading branch information
hartig committed Jan 4, 2016
2 parents f03b8ed + 9f4fe2a commit af2add6
Show file tree
Hide file tree
Showing 41 changed files with 515 additions and 372 deletions.
Original file line number Diff line number Diff line change
@@ -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}.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,97 @@

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 <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(
final LinkedDataFragmentRequest request )
protected final Worker<CTT,NVT,AVT> getWorker(
final ILinkedDataFragmentRequest request )
throws IllegalArgumentException
{
if ( request instanceof TriplePatternFragmentRequest<?,?> ) {
if ( request instanceof ITriplePatternFragmentRequest<?,?,?> ) {
@SuppressWarnings("unchecked")
final TriplePatternFragmentRequest<TermType,VarType> tpfRequest =
(TriplePatternFragmentRequest<TermType,VarType>) request;
final ITriplePatternFragmentRequest<CTT,NVT,AVT> tpfRequest =
(ITriplePatternFragmentRequest<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 ITriplePatternFragmentRequest<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 ITriplePatternFragmentRequest<CTT,NVT,AVT> 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 );
else
offset = 0L;

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

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

abstract protected LinkedDataFragment createFragment(
final TriplePatternElement<TermType,VarType> subj,
final TriplePatternElement<TermType,VarType> pred,
final TriplePatternElement<TermType,VarType> obj,
abstract protected ILinkedDataFragment createFragment(
final ITriplePatternElement<CTT,NVT,AVT> subj,
final ITriplePatternElement<CTT,NVT,AVT> pred,
final ITriplePatternElement<CTT,NVT,AVT> 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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/org/linkeddatafragments/datasource/DataSourceFactory.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/org/linkeddatafragments/datasource/IDataSourceType.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.linkeddatafragments.datasource;

import org.linkeddatafragments.exceptions.DataSourceException;
import org.linkeddatafragments.exceptions.DataSourceCreationException;

import com.google.gson.JsonObject;

Expand Down Expand Up @@ -28,5 +28,5 @@ public interface IDataSourceType
IDataSource createDataSource( final String title,
final String description,
final JsonObject settings )
throws DataSourceException;
throws DataSourceCreationException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http://olafhartig.de">Olaf Hartig</a>
*/
public interface IFragmentRequestProcessor extends Closeable
{
LinkedDataFragment createRequestedFragment(
final LinkedDataFragmentRequest request )
ILinkedDataFragment createRequestedFragment(
final ILinkedDataFragmentRequest request )
throws IllegalArgumentException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <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 ITriplePatternFragmentRequest<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 ITriplePatternFragmentRequest<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 )
protected ILinkedDataFragment createFragment(
final ITriplePatternElement<RDFNode,String,String> subject,
final ITriplePatternElement<RDFNode,String,String> predicate,
final ITriplePatternElement<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
4 changes: 2 additions & 2 deletions src/org/linkeddatafragments/datasource/hdt/HdtDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +13,7 @@
* @author Ruben Verborgh
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
*/
public class HdtDataSource extends DataSource {
public class HdtDataSource extends DataSourceBase {

protected final HdtBasedRequestProcessorForTPFs requestProcessor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,15 +20,15 @@ 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 );

try {
return new HdtDataSource(title, description, file.getAbsolutePath());
} catch (IOException ex) {
throw new DataSourceException(ex);
throw new DataSourceCreationException(ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,7 +14,7 @@
* @author Miel Vander Sande
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
*/
public class IndexDataSource extends DataSource {
public class IndexDataSource extends DataSourceBase {

protected final IndexRequestProcessorForTPFs requestProcessor;

Expand Down
Loading

0 comments on commit af2add6

Please sign in to comment.