|
1 | 1 | package org.linkeddatafragments.datasource;
|
2 | 2 |
|
| 3 | +import org.linkeddatafragments.util.CommonResources; |
| 4 | + |
| 5 | +import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; |
| 6 | +import com.hp.hpl.jena.rdf.model.Literal; |
3 | 7 | import com.hp.hpl.jena.rdf.model.Model;
|
4 | 8 | import com.hp.hpl.jena.rdf.model.ModelFactory;
|
| 9 | +import com.hp.hpl.jena.rdf.model.Resource; |
| 10 | +import com.hp.hpl.jena.rdf.model.StmtIterator; |
5 | 11 |
|
6 | 12 | /**
|
7 |
| - * Base implementation of a Basic Linked Data Fragment. |
| 13 | + * Implementation of {@link TriplePatternFragment}. |
| 14 | + * |
8 | 15 | * @author Ruben Verborgh
|
| 16 | + * @author <a href="http://olafhartig.de">Olaf Hartig</a> |
9 | 17 | */
|
10 |
| -public class TriplePatternFragmentBase implements TriplePatternFragment { |
| 18 | +public class TriplePatternFragmentImpl extends LinkedDataFragmentBase |
| 19 | + implements TriplePatternFragment |
| 20 | +{ |
11 | 21 | private final Model triples;
|
12 | 22 | private final long totalSize;
|
13 | 23 |
|
14 | 24 | /**
|
15 |
| - * Creates an empty Basic Linked Data Fragment. |
| 25 | + * Creates an empty Triple Pattern Fragment. |
| 26 | + */ |
| 27 | + public TriplePatternFragmentImpl( final String fragmentURL, |
| 28 | + final String datasetURL ) { |
| 29 | + this( null, 0L, fragmentURL, datasetURL, 1, true ); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Creates an empty Triple Pattern Fragment page. |
16 | 34 | */
|
17 |
| - public TriplePatternFragmentBase() { |
18 |
| - this(null, 0); |
| 35 | + public TriplePatternFragmentImpl( final String fragmentURL, |
| 36 | + final String datasetURL, |
| 37 | + final long pageNumber, |
| 38 | + final boolean isLastPage ) { |
| 39 | + this( null, 0L, fragmentURL, datasetURL, pageNumber, isLastPage ); |
19 | 40 | }
|
20 | 41 |
|
21 | 42 | /**
|
22 |
| - * Creates a new Basic Linked Data Fragment. |
| 43 | + * Creates a new Triple Pattern Fragment. |
23 | 44 | * @param triples the triples (possibly partial)
|
24 | 45 | * @param totalSize the total size
|
25 | 46 | */
|
26 |
| - public TriplePatternFragmentBase(Model triples, long totalSize) { |
| 47 | + public TriplePatternFragmentImpl( Model triples, |
| 48 | + long totalSize, |
| 49 | + final String fragmentURL, |
| 50 | + final String datasetURL, |
| 51 | + final long pageNumber, |
| 52 | + final boolean isLastPage ) { |
| 53 | + super( fragmentURL, datasetURL, pageNumber, isLastPage ); |
27 | 54 | this.triples = triples == null ? ModelFactory.createDefaultModel() : triples;
|
28 | 55 | this.totalSize = totalSize < 0 ? 0 : totalSize;
|
29 | 56 | }
|
30 | 57 |
|
31 | 58 | @Override
|
32 |
| - public Model getTriples() { |
33 |
| - return triples; |
| 59 | + public StmtIterator getTriples() { |
| 60 | + return triples.listStatements(); |
34 | 61 | }
|
35 | 62 |
|
36 | 63 | @Override
|
37 | 64 | public long getTotalSize() {
|
38 | 65 | return totalSize;
|
39 | 66 | }
|
| 67 | + |
| 68 | + @Override |
| 69 | + public void addMetadata( final Model model ) |
| 70 | + { |
| 71 | + super.addMetadata( model ); |
| 72 | + |
| 73 | + final Resource fragmentId = model.createResource( fragmentURL ); |
| 74 | + |
| 75 | + final Literal totalTyped = model.createTypedLiteral( totalSize, |
| 76 | + XSDDatatype.XSDinteger ); |
| 77 | + final Literal limitTyped = model.createTypedLiteral( getMaxPageSize(), |
| 78 | + XSDDatatype.XSDinteger ); |
| 79 | + |
| 80 | + fragmentId.addLiteral( CommonResources.VOID_TRIPLES, totalTyped ); |
| 81 | + fragmentId.addLiteral( CommonResources.HYDRA_TOTALITEMS, totalTyped ); |
| 82 | + fragmentId.addLiteral( CommonResources.HYDRA_ITEMSPERPAGE, limitTyped ); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void addControls( final Model model ) |
| 87 | + { |
| 88 | + super.addControls( model ); |
| 89 | + |
| 90 | + final Resource datasetId = model.createResource( getDatasetURI() ); |
| 91 | + |
| 92 | + final Resource triplePattern = model.createResource(); |
| 93 | + final Resource subjectMapping = model.createResource(); |
| 94 | + final Resource predicateMapping = model.createResource(); |
| 95 | + final Resource objectMapping = model.createResource(); |
| 96 | + |
| 97 | + datasetId.addProperty( CommonResources.HYDRA_SEARCH, triplePattern ); |
| 98 | + |
| 99 | + triplePattern.addProperty( CommonResources.HYDRA_TEMPLATE, getTemplate() ); |
| 100 | + triplePattern.addProperty( CommonResources.HYDRA_MAPPING, subjectMapping ); |
| 101 | + triplePattern.addProperty( CommonResources.HYDRA_MAPPING, predicateMapping ); |
| 102 | + triplePattern.addProperty( CommonResources.HYDRA_MAPPING, objectMapping ); |
| 103 | + |
| 104 | + subjectMapping.addProperty( CommonResources.HYDRA_VARIABLE, TriplePatternFragmentRequest.PARAMETERNAME_SUBJ ); |
| 105 | + subjectMapping.addProperty( CommonResources.HYDRA_PROPERTY, CommonResources.RDF_SUBJECT ); |
| 106 | + |
| 107 | + predicateMapping.addProperty( CommonResources.HYDRA_VARIABLE, TriplePatternFragmentRequest.PARAMETERNAME_PRED ); |
| 108 | + predicateMapping.addProperty( CommonResources.HYDRA_PROPERTY, CommonResources.RDF_PREDICATE ); |
| 109 | + |
| 110 | + objectMapping.addProperty( CommonResources.HYDRA_VARIABLE, TriplePatternFragmentRequest.PARAMETERNAME_OBJ ); |
| 111 | + objectMapping.addProperty( CommonResources.HYDRA_PROPERTY, CommonResources.RDF_OBJECT ); |
| 112 | + } |
| 113 | + |
| 114 | + public String getTemplate() { |
| 115 | + return datasetURL + "{?" + |
| 116 | + TriplePatternFragmentRequest.PARAMETERNAME_SUBJ + "," + |
| 117 | + TriplePatternFragmentRequest.PARAMETERNAME_PRED + "," + |
| 118 | + TriplePatternFragmentRequest.PARAMETERNAME_OBJ + "}"; |
| 119 | + } |
| 120 | + |
40 | 121 | }
|
0 commit comments