Skip to content

Commit 70383b0

Browse files
author
Olaf Hartig
committed
'TriplePatternFragment' is a subinterface of 'LinkedDataFragment'
1 parent 05d8345 commit 70383b0

File tree

2 files changed

+91
-18
lines changed

2 files changed

+91
-18
lines changed

src/org/linkeddatafragments/datasource/TriplePatternFragment.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
package org.linkeddatafragments.datasource;
22

3-
import com.hp.hpl.jena.rdf.model.Model;
4-
53
/**
64
* A Triple Pattern Fragment.
75
* @author Ruben Verborgh
86
*/
9-
public interface TriplePatternFragment {
10-
/**
11-
* Gets the data of this fragment (possibly only partial).
12-
* @return the data as triples
13-
*/
14-
public Model getTriples();
15-
7+
public interface TriplePatternFragment extends LinkedDataFragment {
168
/**
179
* Gets the total number of triples in the fragment (can be an estimate).
1810
* @return the total number of triples
Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,121 @@
11
package org.linkeddatafragments.datasource;
22

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;
37
import com.hp.hpl.jena.rdf.model.Model;
48
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;
511

612
/**
7-
* Base implementation of a Basic Linked Data Fragment.
13+
* Implementation of {@link TriplePatternFragment}.
14+
*
815
* @author Ruben Verborgh
16+
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
917
*/
10-
public class TriplePatternFragmentBase implements TriplePatternFragment {
18+
public class TriplePatternFragmentImpl extends LinkedDataFragmentBase
19+
implements TriplePatternFragment
20+
{
1121
private final Model triples;
1222
private final long totalSize;
1323

1424
/**
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.
1634
*/
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 );
1940
}
2041

2142
/**
22-
* Creates a new Basic Linked Data Fragment.
43+
* Creates a new Triple Pattern Fragment.
2344
* @param triples the triples (possibly partial)
2445
* @param totalSize the total size
2546
*/
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 );
2754
this.triples = triples == null ? ModelFactory.createDefaultModel() : triples;
2855
this.totalSize = totalSize < 0 ? 0 : totalSize;
2956
}
3057

3158
@Override
32-
public Model getTriples() {
33-
return triples;
59+
public StmtIterator getTriples() {
60+
return triples.listStatements();
3461
}
3562

3663
@Override
3764
public long getTotalSize() {
3865
return totalSize;
3966
}
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+
40121
}

0 commit comments

Comments
 (0)