Skip to content

Commit 3397cf4

Browse files
committed
Adopt simplified RdfQuadConsumer interface
1 parent 2b56ebf commit 3397cf4

13 files changed

+260
-67
lines changed

src/main/java/com/apicatalog/jsonld/api/ToRdfApi.java

+3-15
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import com.apicatalog.rdf.RdfDataset;
3030
import com.apicatalog.rdf.RdfDatasetSupplier;
3131
import com.apicatalog.rdf.api.RdfQuadConsumer;
32-
import com.apicatalog.rdf.api.RdfQuadEmitter;
33-
import com.apicatalog.rdf.api.RdfTripleConsumer;
3432

3533
import jakarta.json.JsonStructure;
3634

@@ -167,17 +165,17 @@ public ToRdfApi ordered(boolean enable) {
167165
*/
168166
public RdfDataset get() throws JsonLdError {
169167
final RdfDatasetSupplier consumer = new RdfDatasetSupplier();
170-
provide((RdfTripleConsumer)consumer);
168+
provide(consumer);
171169
return consumer.get();
172170
}
173171

174172
/**
175-
* Emit transformed <code>JSON-LD</code> as RDF graph scoped triples.
173+
* Emit transformed <code>JSON-LD</code> as RDF quads.
176174
*
177175
* @param consumer that accepts emitted RDF statements
178176
* @throws JsonLdError
179177
*/
180-
public void provide(RdfTripleConsumer consumer) throws JsonLdError {
178+
public void provide(RdfQuadConsumer consumer) throws JsonLdError {
181179
if (documentUri != null) {
182180
ToRdfProcessor.toRdf(consumer, documentUri, options);
183181

@@ -189,16 +187,6 @@ public void provide(RdfTripleConsumer consumer) throws JsonLdError {
189187
}
190188
}
191189

192-
/**
193-
* Emit transformed <code>JSON-LD</code> as RDF quads.
194-
*
195-
* @param consumer that accepts emitted RDF statements
196-
* @throws JsonLdError
197-
*/
198-
public void provide(RdfQuadConsumer consumer) throws JsonLdError {
199-
provide(RdfQuadEmitter.newInstance(consumer));
200-
}
201-
202190
/**
203191
* Experimental: Accept numeric <code>@id</code>. Disabled by default.
204192
*

src/main/java/com/apicatalog/jsonld/deseralization/JsonLdToRdf.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import com.apicatalog.rdf.RdfDataset;
4444
import com.apicatalog.rdf.RdfDatasetSupplier;
4545
import com.apicatalog.rdf.api.RdfConsumerException;
46-
import com.apicatalog.rdf.api.RdfTripleConsumer;
46+
import com.apicatalog.rdf.api.RdfQuadConsumer;
4747
import com.apicatalog.rdf.lang.RdfConstants;
4848
import com.apicatalog.rdf.lang.XsdConstants;
4949

@@ -109,9 +109,9 @@ public JsonLdToRdf rdfDirection(RdfDirection rdfDirection) {
109109
return this;
110110
}
111111

112-
public void provide(RdfTripleConsumer consumer) throws JsonLdError {
112+
public void provide(RdfQuadConsumer consumer) throws JsonLdError {
113113
try {
114-
from(consumer);
114+
from(RdfQuadEmitter.newInstance(consumer));
115115
} catch (RdfConsumerException e) {
116116
if (e.getCause() instanceof JsonLdError) {
117117
throw (JsonLdError)e.getCause();
@@ -120,7 +120,7 @@ public void provide(RdfTripleConsumer consumer) throws JsonLdError {
120120
}
121121
}
122122

123-
public void from(RdfTripleConsumer consumer) throws JsonLdError, RdfConsumerException {
123+
protected void from(RdfTripleConsumer consumer) throws JsonLdError, RdfConsumerException {
124124

125125
for (final String graphName : Utils.index(nodeMap.graphs(), true)) {
126126

src/main/java/com/apicatalog/rdf/RdfQuadAdapter.java src/main/java/com/apicatalog/jsonld/deseralization/RdfQuadAdapter.java

+27-23
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
package com.apicatalog.rdf;
1+
package com.apicatalog.jsonld.deseralization;
22

33
import java.util.HashMap;
44
import java.util.Map;
55

6+
import com.apicatalog.rdf.Rdf;
7+
import com.apicatalog.rdf.RdfResource;
8+
import com.apicatalog.rdf.RdfValue;
9+
import com.apicatalog.rdf.api.RdfConsumerException;
610
import com.apicatalog.rdf.api.RdfQuadConsumer;
7-
import com.apicatalog.rdf.api.RdfTripleConsumer;
811

9-
public abstract class RdfQuadAdapter implements RdfTripleConsumer, RdfQuadConsumer {
12+
public abstract class RdfQuadAdapter implements RdfQuadConsumer, RdfTripleConsumer {
1013

1114
protected final Map<String, RdfResource> resources;
1215

@@ -63,35 +66,36 @@ public RdfQuadAdapter triple(String subject, String predicate, String object) {
6366
}
6467

6568
@Override
66-
public RdfQuadConsumer quad(String subject, String predicate, String object, String graph) {
67-
quad(getResource(subject),
68-
getResource(predicate),
69-
getResource(object),
70-
getResource(graph));
71-
return this;
72-
73-
}
69+
public RdfQuadConsumer quad(
70+
String subject,
71+
String predicate,
72+
String object,
73+
String datatype,
74+
String language,
75+
String direction,
76+
String graph) throws RdfConsumerException {
77+
78+
final RdfValue objectValue;
79+
if (language != null) {
80+
objectValue = Rdf.createLangString(object, language, direction);
81+
82+
} else if (datatype != null) {
83+
objectValue = Rdf.createTypedString(object, datatype);
84+
85+
} else {
86+
objectValue = getResource(object);
87+
}
7488

75-
@Override
76-
public RdfQuadConsumer quad(String subject, String predicate, String literal, String datatype, String graph) {
7789
quad(getResource(subject),
7890
getResource(predicate),
79-
Rdf.createTypedString(literal, datatype),
91+
objectValue,
8092
getResource(graph));
81-
return this;
82-
}
8393

84-
@Override
85-
public RdfQuadConsumer quad(String subject, String predicate, String literal, String language, String direction, String graph) {
86-
quad(getResource(subject),
87-
getResource(predicate),
88-
Rdf.createLangString(literal, language, direction),
89-
getResource(graph));
9094
return this;
9195
}
9296

9397
protected final RdfResource getResource(final String name) {
94-
return name != null
98+
return name != null
9599
? resources.computeIfAbsent(name, arg0 -> name.startsWith("_:") ? Rdf.createBlankNode(name) : Rdf.createIRI(name))
96100
: null;
97101
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.apicatalog.jsonld.deseralization;
2+
3+
import com.apicatalog.rdf.api.RdfConsumerException;
4+
import com.apicatalog.rdf.api.RdfQuadConsumer;
5+
import com.apicatalog.rdf.lang.RdfConstants;
6+
import com.apicatalog.rdf.lang.XsdConstants;
7+
8+
class RdfQuadEmitter implements RdfTripleConsumer {
9+
10+
protected final RdfQuadConsumer consumer;
11+
protected String graph;
12+
13+
protected RdfQuadEmitter(RdfQuadConsumer consumer) {
14+
this.consumer = consumer;
15+
this.graph = null;
16+
}
17+
18+
public static RdfTripleConsumer newInstance(RdfQuadConsumer consumer) {
19+
return new RdfQuadEmitter(consumer);
20+
}
21+
22+
@Override
23+
public RdfTripleConsumer defaultGraph() {
24+
this.graph = null;
25+
return this;
26+
}
27+
28+
@Override
29+
public RdfTripleConsumer namedGraph(String graph) {
30+
this.graph = graph;
31+
return this;
32+
}
33+
34+
@Override
35+
public RdfTripleConsumer triple(String subject, String predicate, String object) throws RdfConsumerException {
36+
consumer.quad(subject, predicate, object, null, null, null, graph);
37+
return this;
38+
}
39+
40+
@Override
41+
public RdfTripleConsumer triple(String subject, String predicate, String literal, String datatype) throws RdfConsumerException {
42+
consumer.quad(subject, predicate, literal, datatype, null, null, graph);
43+
return this;
44+
}
45+
46+
@Override
47+
public RdfTripleConsumer triple(String subject, String predicate, String literal, String language, String direction) throws RdfConsumerException {
48+
if (direction != null) {
49+
consumer.quad(subject, predicate, literal, RdfConstants.I18N_BASE, language, direction, graph);
50+
} else {
51+
consumer.quad(subject, predicate, literal, XsdConstants.STRING, language, direction, graph);
52+
}
53+
return this;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.apicatalog.jsonld.deseralization;
2+
3+
import com.apicatalog.rdf.api.RdfConsumerException;
4+
5+
/**
6+
* An RDF triple consumer interface designed for high-speed processing and
7+
* seamless integration with third-party libraries.
8+
*
9+
* Intended to be used in cases where triples are emitted in bulk as a set
10+
* belonging to the same graph, and where a producer keeps triple sets
11+
* associated with graphs.
12+
*
13+
* This interface minimizes unnecessary object instantiation, enhancing
14+
* efficiency in RDF data processing.
15+
*/
16+
interface RdfTripleConsumer {
17+
18+
/**
19+
* Sets the default graph as the active scope. This method is invoked when
20+
* triples belong to the unnamed default graph.
21+
*
22+
* @return An instance enabling fluent programming; never {@code null}.
23+
*
24+
* @throws RdfConsumerException if an error occurs while processing the N-Quad
25+
* statement
26+
*/
27+
RdfTripleConsumer defaultGraph() throws RdfConsumerException;
28+
29+
/**
30+
* Sets a named graph as the active scope. Ensures that subsequent triples are
31+
* associated with the specified graph.
32+
*
33+
* @param graph The name of the graph (IRI or blank node identifier prefixed
34+
* with "_:"); never {@code null}.
35+
*
36+
* @return An instance enabling fluent programming; never {@code null}.
37+
*
38+
* @throws RdfConsumerException if an error occurs while processing the N-Quad
39+
* statement
40+
*/
41+
RdfTripleConsumer namedGraph(String graph) throws RdfConsumerException;
42+
43+
/**
44+
* Accepts an RDF triple where the object is an IRI or a blank node. The triple
45+
* is processed within the currently active graph scope.
46+
*
47+
* @param subject The subject of the triple (IRI or blank node identifier
48+
* prefixed with "_:"); never {@code null}.
49+
* @param predicate The predicate of the triple (IRI); never {@code null}.
50+
* @param object The object of the triple (IRI or blank node identifier
51+
* prefixed with "_:"); never {@code null}.
52+
*
53+
* @return An instance enabling fluent programming; never {@code null}.
54+
*
55+
* @throws RdfConsumerException if an error occurs while processing the N-Quad
56+
* statement
57+
*/
58+
RdfTripleConsumer triple(
59+
String subject,
60+
String predicate,
61+
String object) throws RdfConsumerException;
62+
63+
/**
64+
* Accepts an RDF triple where the object is a literal with a datatype.
65+
* Optimized for efficient handling of typed literals. The triple is processed
66+
* within the currently active graph scope.
67+
*
68+
* @param subject The subject of the triple (IRI or blank node identifier
69+
* prefixed with "_:"); never {@code null}.
70+
* @param predicate The predicate of the triple (IRI); never {@code null}.
71+
* @param literal The literal value of the object; never {@code null}.
72+
* @param datatype The datatype IRI of the literal; never {@code null}.
73+
*
74+
* @return An instance enabling fluent programming; never {@code null}.
75+
*
76+
* @throws RdfConsumerException if an error occurs while processing the N-Quad
77+
* statement
78+
*/
79+
RdfTripleConsumer triple(
80+
String subject,
81+
String predicate,
82+
String literal,
83+
String datatype) throws RdfConsumerException;
84+
85+
/**
86+
* Accepts an RDF triple where the object is a localized string value. Optimized
87+
* for efficient handling of language-tagged literals. The triple is processed
88+
* within the currently active graph scope.
89+
*
90+
* @param subject The subject of the triple (IRI or blank node identifier
91+
* prefixed with "_:"); never {@code null}.
92+
* @param predicate The predicate of the triple (IRI); never {@code null}.
93+
* @param literal The literal value of the object; never {@code null}.
94+
* @param language The language tag of the literal; never {@code null}.
95+
* @param direction The text direction of the literal (optional, may be
96+
* {@code null}).
97+
*
98+
* @return An instance enabling fluent programming; never {@code null}.
99+
*
100+
* @throws RdfConsumerException if an error occurs while processing the N-Quad
101+
* statement
102+
*/
103+
RdfTripleConsumer triple(
104+
String subject,
105+
String predicate,
106+
String literal,
107+
String language,
108+
String direction) throws RdfConsumerException;
109+
}

src/main/java/com/apicatalog/jsonld/processor/ToRdfProcessor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.apicatalog.jsonld.loader.DocumentLoaderOptions;
2828
import com.apicatalog.rdf.RdfDataset;
2929
import com.apicatalog.rdf.RdfDatasetSupplier;
30-
import com.apicatalog.rdf.api.RdfTripleConsumer;
30+
import com.apicatalog.rdf.api.RdfQuadConsumer;
3131

3232
import jakarta.json.JsonArray;
3333

@@ -58,7 +58,7 @@ public static final RdfDataset toRdf(Document input, final JsonLdOptions options
5858
return consumer.get();
5959
}
6060

61-
public static final void toRdf(final RdfTripleConsumer consumer, final URI input, final JsonLdOptions options) throws JsonLdError {
61+
public static final void toRdf(final RdfQuadConsumer consumer, final URI input, final JsonLdOptions options) throws JsonLdError {
6262
if (options.getDocumentLoader() == null) {
6363
throw new JsonLdError(JsonLdErrorCode.LOADING_DOCUMENT_FAILED, "Document loader is null. Cannot fetch [" + input + "].");
6464
}
@@ -75,7 +75,7 @@ public static final void toRdf(final RdfTripleConsumer consumer, final URI input
7575
toRdf(consumer, remoteDocument, options);
7676
}
7777

78-
public static final void toRdf(final RdfTripleConsumer consumer, final Document input, final JsonLdOptions options) throws JsonLdError {
78+
public static final void toRdf(final RdfQuadConsumer consumer, final Document input, final JsonLdOptions options) throws JsonLdError {
7979
final JsonLdOptions expansionOptions = new JsonLdOptions(options);
8080

8181
expansionOptions.setProcessingMode(options.getProcessingMode());
@@ -87,7 +87,7 @@ public static final void toRdf(final RdfTripleConsumer consumer, final Document
8787
toRdf(consumer, expandedInput, options);
8888
}
8989

90-
public static final void toRdf(final RdfTripleConsumer consumer, final JsonArray expandedInput, final JsonLdOptions options) throws JsonLdError {
90+
public static final void toRdf(final RdfQuadConsumer consumer, final JsonArray expandedInput, final JsonLdOptions options) throws JsonLdError {
9191
JsonLdToRdf
9292
.with(NodeMapBuilder.with(expandedInput, new NodeMap()).build())
9393
.produceGeneralizedRdf(options.isProduceGeneralizedRdf())

src/main/java/com/apicatalog/rdf/RdfDataset.java

-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public interface RdfDataset {
2929
* @param nquad to add
3030
* @return the same {@link RdfDataset} instance
3131
*/
32-
@Deprecated
3332
RdfDataset add(RdfNQuad nquad);
3433

3534
/**
@@ -38,10 +37,8 @@ public interface RdfDataset {
3837
* @param triple to add
3938
* @return the same {@link RdfDataset} instance
4039
*/
41-
@Deprecated
4240
RdfDataset add(RdfTriple triple);
4341

44-
@Deprecated
4542
List<RdfNQuad> toList();
4643

4744
Set<RdfResource> getGraphNames();

0 commit comments

Comments
 (0)