-
Notifications
You must be signed in to change notification settings - Fork 42
/
RDF4J.java
660 lines (617 loc) · 27.3 KB
/
RDF4J.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.rdf.rdf4j;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
// To avoid confusion, avoid importing
// classes that are in both
// commons.rdf and openrdf.model (e.g. IRI, Literal)
import org.apache.commons.rdf.api.BlankNode;
import org.apache.commons.rdf.api.BlankNodeOrIRI;
import org.apache.commons.rdf.api.Dataset;
import org.apache.commons.rdf.api.Graph;
import org.apache.commons.rdf.api.Quad;
import org.apache.commons.rdf.api.RDFTerm;
import org.apache.commons.rdf.api.RDF;
import org.apache.commons.rdf.api.Triple;
import org.apache.commons.rdf.api.TripleLike;
import org.apache.commons.rdf.rdf4j.impl.InternalRDF4JFactory;
import org.eclipse.rdf4j.model.BNode;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.LinkedHashModel;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.sail.Sail;
import org.eclipse.rdf4j.sail.memory.MemoryStore;
/**
* RDF4J implementation of RDF.
* <p>
* The {@link #RDF4J()} constructor uses a {@link SimpleValueFactory} to create
* corresponding RDF4J {@link Value} instances. Alternatively, this factory can
* be constructed with a different {@link ValueFactory} using
* {@link #RDF4J(ValueFactory)}.
* <p>
* {@link #asRDFTerm(Value)} can be used to convert any RDF4J {@link Value} to
* an RDFTerm. Note that adapted {@link BNode}s are considered equal if they are
* converted with the same {@link RDF4J} instance and have the same
* {@link BNode#getID()}.
* <p>
* {@link #createGraph()} creates a new Graph backed by {@link LinkedHashModel}.
* To use other models, see {@link #asGraph(Model)}.
* <p>
* To adapt a RDF4J {@link Repository} as a {@link Dataset} or {@link Graph},
* use {@link #asDataset(Repository, Option...)} or
* {@link #asGraph(Repository, Option...)}.
* <p>
* {@link #asTriple(Statement)} can be used to convert a RDF4J {@link Statement}
* to a Commons RDF {@link Triple}, and equivalent {@link #asQuad(Statement)} to
* convert a {@link Quad}.
* <p>
* To convert any {@link Triple} or {@link Quad} to to RDF4J {@link Statement},
* use {@link #asStatement(TripleLike)}. This recognises previously converted
* {@link RDF4JTriple}s and {@link RDF4JQuad}s without re-converting their
* {@link RDF4JTripleLike#asStatement()}.
* <p>
* Likewise, {@link #asValue(RDFTerm)} can be used to convert any Commons RDF
* {@link RDFTerm} to a corresponding RDF4J {@link Value}. This recognises
* previously converted {@link RDF4JTerm}s without re-converting their
* {@link RDF4JTerm#asValue()}.
* <p>
* For the purpose of {@link BlankNode} equivalence, this factory contains an
* internal {@link UUID} salt that is used by adapter methods like
* {@link #asQuad(Statement)}, {@link #asTriple(Statement)},
* {@link #asRDFTerm(Value)} as well as {@link #createBlankNode(String)}. As
* RDF4J {@link BNode} instances from multiple repositories or models may have
* the same {@link BNode#getID()}, converting them with the above methods might
* cause accidental {@link BlankNode} equivalence. Note that the {@link Graph}
* and {@link Dataset} adapter methods like
* {@link #asDataset(Repository, Option...)} and
* {@link #asGraph(Repository, Option...)} therefore uses a unique {@link RDF4J}
* internally.
*
* @see RDF
*
*/
@SuppressWarnings("PMD.UnnecessaryFullyQualifiedName") // we use fully-qualified names for clarity
public final class RDF4J implements RDF {
/**
* InternalRDF4JFactory is deliberately abstract
*/
private static InternalRDF4JFactory rdf4j = new InternalRDF4JFactory() {
};
public enum Option {
/**
* The Graph/Dataset should include any inferred statements
*/
includeInferred,
/**
* The graph/dataset should handle {@link Repository#initialize()} (if
* needed) and {@link Repository#shutDown()} on {@link Graph#close()} /
* {@link Dataset#close()}.
*/
handleInitAndShutdown
}
private final UUID salt;
private final ValueFactory valueFactory;
/**
* Construct an {@link RDF4J}.
*
*/
public RDF4J() {
this(SimpleValueFactory.getInstance(), UUID.randomUUID());
}
/**
* Construct an {@link RDF4J}.
* <p>
* This constructor is intended for use with the value factory from
* {@link Repository#getValueFactory()} when using Repository-based graphs
* and datasets.
*
* @param valueFactory
* The RDF4J {@link ValueFactory} to use
*/
public RDF4J(final ValueFactory valueFactory) {
this(valueFactory, UUID.randomUUID());
}
/**
* Construct an {@link RDF4J}.
* <p>
* This constructor may be used if reproducible
* {@link BlankNode#uniqueReference()} in {@link BlankNode} is desirable.
*
* @param salt
* An {@link UUID} salt to be used by any created
* {@link BlankNode}s for the purpose of
* {@link BlankNode#uniqueReference()}
*/
public RDF4J(final UUID salt) {
this(SimpleValueFactory.getInstance(), salt);
}
/**
* Construct an {@link RDF4J}.
* <p>
* This constructor may be used if reproducible
* {@link BlankNode#uniqueReference()} in {@link BlankNode} is desirable.
*
* @param valueFactory
* The RDF4J {@link ValueFactory} to use
* @param salt
* An {@link UUID} salt to be used by any created
* {@link BlankNode}s for the purpose of
* {@link BlankNode#uniqueReference()}
*/
public RDF4J(final ValueFactory valueFactory, final UUID salt) {
this.valueFactory = valueFactory;
this.salt = salt;
}
/**
* Adapt a RDF4J {@link Statement} as a Commons RDF {@link Quad}.
* <p>
* For the purpose of {@link BlankNode} equivalence, this method will use an
* internal salt UUID that is unique per instance of {@link RDF4J}.
* <p>
* <strong>NOTE:</strong> If combining RDF4J {@link Statement}s multiple
* repositories or models, then their {@link BNode}s may have the same
* {@link BNode#getID()}, which with this method would become equivalent
* according to {@link BlankNode#equals(Object)} and
* {@link BlankNode#uniqueReference()}, unless a separate {@link RDF4J}
* instance is used per RDF4J repository/model.
*
* @param statement
* The statement to convert
* @return A {@link RDF4JQuad} that is equivalent to the statement
*/
public RDF4JQuad asQuad(final Statement statement) {
return rdf4j.createQuadImpl(statement, salt);
}
/**
*
* Adapt a RDF4J {@link Value} as a Commons RDF {@link RDFTerm}.
* <p>
* The value will be of the same kind as the term, e.g. a
* {@link org.eclipse.rdf4j.model.BNode} is converted to a
* {@link org.apache.commons.rdf.api.BlankNode}, a
* {@link org.eclipse.rdf4j.model.IRI} is converted to a
* {@link org.apache.commons.rdf.api.IRI} and a
* {@link org.eclipse.rdf4j.model.Literal}. is converted to a
* {@link org.apache.commons.rdf.api.Literal}
* <p>
* For the purpose of {@link BlankNode} equivalence, this method will use an
* internal salt UUID that is unique per instance of {@link RDF4J}.
* <p>
* <strong>NOTE:</strong> If combining RDF4J values from multiple
* repositories or models, then their {@link BNode}s may have the same
* {@link BNode#getID()}, which with this method would become equivalent
* according to {@link BlankNode#equals(Object)} and
* {@link BlankNode#uniqueReference()}, unless a separate {@link RDF4J}
* instance is used per RDF4J repository/model.
*
* @param value
* The RDF4J {@link Value} to convert.
* @return A {@link RDFTerm} that corresponds to the RDF4J value
* @throws IllegalArgumentException
* if the value is not a BNode, Literal or IRI
*/
public RDF4JTerm asRDFTerm(final Value value) {
return asRDFTerm(value, salt);
}
/**
*
* Adapt a RDF4J
* {@link org.eclipse.rdf4j.model.BNode} as a Commons RDF
* {@link org.apache.commons.rdf.api.BlankNode}
* <p>
* For the purpose of {@link BlankNode} equivalence, this method will use an
* internal salt UUID that is unique per instance of {@link RDF4J}.
* <p>
* <strong>NOTE:</strong> If combining RDF4J values from multiple
* repositories or models, then their {@link BNode}s may have the same
* {@link BNode#getID()}, which with this method would become equivalent
* according to {@link BlankNode#equals(Object)} and
* {@link BlankNode#uniqueReference()}, unless a separate {@link RDF4J}
* instance is used per RDF4J repository/model.
*
* @param value
* The RDF4J {@link BNode} to convert.
* @return A {@link RDF4JBlankNode} that corresponds to the RDF4J BNode
*/
public RDF4JBlankNode asRDFTerm(final BNode value) {
return rdf4j.createBlankNodeImpl(value, salt);
}
/**
*
* Adapt a RDF4J
* {@link org.eclipse.rdf4j.model.Literal} as a Commons RDF
* {@link org.apache.commons.rdf.api.Literal}
* <p>
* @param value
* The RDF4J {@link Literal} to convert.
* @return A {@link RDF4JLiteral} that corresponds to the RDF4J literal
*/
public RDF4JLiteral asRDFTerm(final Literal value) {
return rdf4j.createLiteralImpl(value);
}
/**
*
* Adapt a RDF4J
* {@link org.eclipse.rdf4j.model.IRI} as a Commons RDF
* {@link org.apache.commons.rdf.api.IRI}
* <p>
* @param value
* The RDF4J {@link Value} to convert.
* @return A {@link RDF4JIRI} that corresponds to the RDF4J IRI
*/
public RDF4JIRI asRDFTerm(final org.eclipse.rdf4j.model.IRI value) {
return rdf4j.createIRIImpl(value);
}
/**
*
* Adapt a RDF4J
* {@link org.eclipse.rdf4j.model.Resource} as a Commons RDF
* {@link org.apache.commons.rdf.api.BlankNodeOrIRI}
* <p>
* @param value
* The RDF4J {@link Value} to convert.
* @return A {@link RDF4JBlankNodeOrIRI} that corresponds to the RDF4J Resource
*/
public RDF4JBlankNodeOrIRI asRDFTerm(final org.eclipse.rdf4j.model.Resource value) {
if(value instanceof IRI){
return asRDFTerm((IRI)value);
} else if (value instanceof BNode){
return asRDFTerm((BNode)value);
}
throw new IllegalArgumentException("Value is not a BNode or IRI: " + value.getClass());
}
/**
* Adapt a RDF4J {@link Value} as a Commons RDF {@link RDFTerm}.
* <p>
* The value will be of the same kind as the term, e.g. a
* {@link org.eclipse.rdf4j.model.BNode} is converted to a
* {@link org.apache.commons.rdf.api.BlankNode}, a
* {@link org.eclipse.rdf4j.model.IRI} is converted to a
* {@link org.apache.commons.rdf.api.IRI} and a
* {@link org.eclipse.rdf4j.model.Literal}. is converted to a
* {@link org.apache.commons.rdf.api.Literal}
*
* @param value
* The RDF4J {@link Value} to convert.
* @param salt
* A {@link UUID} salt to use for uniquely mapping any
* {@link BNode}s. The salt should typically be the same for
* multiple statements in the same {@link Repository} or
* {@link Model} to ensure {@link BlankNode#equals(Object)} and
* {@link BlankNode#uniqueReference()} works as intended.
* @return A {@link RDFTerm} that corresponds to the RDF4J value
* @throws IllegalArgumentException
* if the value is not a BNode, Literal or IRI
*/
public static RDF4JTerm asRDFTerm(final Value value, final UUID salt) {
if (value instanceof BNode) {
return rdf4j.createBlankNodeImpl((BNode) value, salt);
}
if (value instanceof org.eclipse.rdf4j.model.Literal) {
return rdf4j.createLiteralImpl((org.eclipse.rdf4j.model.Literal) value);
}
if (value instanceof org.eclipse.rdf4j.model.IRI) {
return rdf4j.createIRIImpl((org.eclipse.rdf4j.model.IRI) value);
}
throw new IllegalArgumentException("Value is not a BNode, Literal or IRI: " + value.getClass());
}
/**
* Adapt an RDF4J {@link Repository} as a Commons RDF {@link Dataset}.
* <p>
* Changes to the dataset are reflected in the repository, and vice versa.
* <p>
* <strong>Note:</strong> Some operations on the {@link RDF4JDataset}
* requires the use of try-with-resources to close underlying
* {@link RepositoryConnection}s, including {@link RDF4JDataset#iterate()},
* {@link RDF4JDataset#stream()} and {@link RDF4JDataset#getGraphNames()}.
*
* @param repository
* RDF4J {@link Repository} to connect to.
* @param options
* Zero or more {@link Option}
* @return A {@link Dataset} backed by the RDF4J repository.
*/
public RDF4JDataset asDataset(final Repository repository, final Option... options) {
final EnumSet<Option> opts = optionSet(options);
return rdf4j.createRepositoryDatasetImpl(repository, opts.contains(Option.handleInitAndShutdown),
opts.contains(Option.includeInferred));
}
/**
* Adapt an RDF4J {@link Model} as a Commons RDF {@link Graph}.
* <p>
* Changes to the graph are reflected in the model, and vice versa.
*
* @param model
* RDF4J {@link Model} to adapt.
* @return Adapted {@link Graph}.
*/
public RDF4JGraph asGraph(final Model model) {
return rdf4j.createModelGraphImpl(model, this);
}
/**
* Adapt an RDF4J {@link Repository} as a Commons RDF {@link Graph}.
* <p>
* The graph will only include triples in the default graph (equivalent to
* context <code>new Resource[0]{null})</code> in RDF4J).
* <p>
* Changes to the graph are reflected in the repository, and vice versa.
* <p>
* <strong>Note:</strong> Some operations on the {@link RDF4JGraph} requires
* the use of try-with-resources to close underlying
* {@link RepositoryConnection}s, including {@link RDF4JGraph#iterate()} and
* {@link RDF4JGraph#stream()}.
*
* @param repository
* RDF4J {@link Repository} to connect to.
* @param options
* Zero or more {@link Option}
* @return A {@link Graph} backed by the RDF4J repository.
*/
public RDF4JGraph asGraph(final Repository repository, final Option... options) {
final EnumSet<Option> opts = optionSet(options);
return rdf4j.createRepositoryGraphImpl(repository, opts.contains(Option.handleInitAndShutdown),
opts.contains(Option.includeInferred), new Resource[] { null }); // default
// graph
}
/**
* Adapt an RDF4J {@link Repository} as a Commons RDF {@link Graph}.
* <p>
* The graph will include triples in any contexts (e.g. the union graph).
* <p>
* Changes to the graph are reflected in the repository, and vice versa.
*
* @param repository
* RDF4J {@link Repository} to connect to.
* @param options
* Zero or more {@link Option}
* @return A union {@link Graph} backed by the RDF4J repository.
*/
public RDF4JGraph asGraphUnion(final Repository repository, final Option... options) {
final EnumSet<Option> opts = optionSet(options);
return rdf4j.createRepositoryGraphImpl(repository, opts.contains(Option.handleInitAndShutdown),
opts.contains(Option.includeInferred), new Resource[] {}); // union
// graph
}
/**
* Adapt an RDF4J {@link Repository} as a Commons RDF {@link Graph}.
* <p>
* The graph will include triples in the specified contexts.
* <p>
* Changes to the graph are reflected in the repository, and vice versa.
* Triples added/removed to the graph are reflected in all the specified
* contexts.
* <p>
* <strong>Note:</strong> Some operations on the {@link RDF4JGraph} requires
* the use of try-with-resources to close underlying
* {@link RepositoryConnection}s, including {@link RDF4JGraph#iterate()} and
* {@link RDF4JGraph#stream()}.
*
* @param repository
* RDF4J {@link Repository} to connect to.
* @param contexts
* A {@link Set} of {@link BlankNodeOrIRI} specifying the graph
* names to use as a context. The set may include the value
* <code>null</code> to indicate the default graph. The empty set
* indicates any context, e.g. the <em>union graph</em>.
* @param option
* Zero or more {@link Option}s
* @return A {@link Graph} backed by the RDF4J repository.
*/
public RDF4JGraph asGraph(final Repository repository, final Set<? extends BlankNodeOrIRI> contexts, final Option... option) {
final EnumSet<Option> opts = optionSet(option);
/** NOTE: asValue() deliberately CAN handle <code>null</code> */
final Resource[] resources = contexts.stream().map(g -> (Resource) asValue(g)).toArray(Resource[]::new);
return rdf4j.createRepositoryGraphImpl(Objects.requireNonNull(repository),
opts.contains(Option.handleInitAndShutdown), opts.contains(Option.includeInferred), resources);
}
/**
* Adapt a Commons RDF {@link Triple} or {@link Quad} as a RDF4J
* {@link Statement}.
* <p>
* If the <code>tripleLike</code> argument is an {@link RDF4JTriple} or a
* {@link RDF4JQuad}, then its {@link RDF4JTripleLike#asStatement()} is
* returned as-is. Note that this means that a {@link RDF4JTriple} would
* preserve its {@link Statement#getContext()}, and that any
* {@link BlankNode}s would be deemed equivalent in RDF4J if they have the
* same {@link BNode#getID()}.
*
* @param tripleLike
* A {@link Triple} or {@link Quad} to adapt
* @return A corresponding {@link Statement}
*/
public Statement asStatement(final TripleLike tripleLike) {
if (tripleLike instanceof RDF4JTripleLike) {
// Return original statement - this covers both RDF4JQuad and
// RDF4JTriple
return ((RDF4JTripleLike) tripleLike).asStatement();
}
final org.eclipse.rdf4j.model.Resource subject = (org.eclipse.rdf4j.model.Resource) asValue(tripleLike.getSubject());
final org.eclipse.rdf4j.model.IRI predicate = (org.eclipse.rdf4j.model.IRI) asValue(tripleLike.getPredicate());
final Value object = asValue(tripleLike.getObject());
org.eclipse.rdf4j.model.Resource context = null;
if (tripleLike instanceof Quad) {
final Quad quad = (Quad) tripleLike;
context = (org.eclipse.rdf4j.model.Resource) asValue(quad.getGraphName().orElse(null));
}
return getValueFactory().createStatement(subject, predicate, object, context);
}
/**
* Adapt a RDF4J {@link Statement} as a Commons RDF {@link Triple}.
* <p>
* For the purpose of {@link BlankNode} equivalence, this method will use an
* internal salt UUID that is unique per instance of {@link RDF4J}.
* <p>
* <strong>NOTE:</strong> If combining RDF4J statements from multiple
* repositories or models, then their {@link BNode}s may have the same
* {@link BNode#getID()}, which with this method would become equivalent
* according to {@link BlankNode#equals(Object)} and
* {@link BlankNode#uniqueReference()}, unless a separate {@link RDF4J}
* instance is used per RDF4J repository/model.
*
* @param statement
* The RDF4J {@link Statement} to adapt.
* @return A {@link RDF4JTriple} that is equivalent to the statement
*/
public RDF4JTriple asTriple(final Statement statement) {
return rdf4j.createTripleImpl(statement, salt);
}
/**
* Adapt a Commons RDF {@link RDFTerm} as a RDF4J {@link Value}.
* <p>
* The value will be of the same kind as the term, e.g. a
* {@link org.apache.commons.rdf.api.BlankNode} is converted to a
* {@link org.eclipse.rdf4j.model.BNode}, a
* {@link org.apache.commons.rdf.api.IRI} is converted to a
* {@link org.eclipse.rdf4j.model.IRI} and a
* {@link org.apache.commons.rdf.api.Literal} is converted to a
* {@link org.eclipse.rdf4j.model.Literal}.
* <p>
* If the provided {@link RDFTerm} is <code>null</code>, then the returned
* value is <code>null</code>.
* <p>
* If the provided term is an instance of {@link RDF4JTerm}, then the
* {@link RDF4JTerm#asValue()} is returned without any conversion. Note that
* this could mean that a {@link Value} from a different kind of
* {@link ValueFactory} could be returned.
*
* @param term
* RDFTerm to adapt to RDF4J Value
* @return Adapted RDF4J {@link Value}
*/
public Value asValue(final RDFTerm term) {
if (term == null) {
return null;
}
if (term instanceof RDF4JTerm) {
// One of our own - avoid converting again.
// (This is crucial to avoid double-escaping in BlankNode)
return ((RDF4JTerm) term).asValue();
}
if (term instanceof org.apache.commons.rdf.api.IRI) {
final org.apache.commons.rdf.api.IRI iri = (org.apache.commons.rdf.api.IRI) term;
return getValueFactory().createIRI(iri.getIRIString());
}
if (term instanceof org.apache.commons.rdf.api.Literal) {
final org.apache.commons.rdf.api.Literal literal = (org.apache.commons.rdf.api.Literal) term;
final String label = literal.getLexicalForm();
if (literal.getLanguageTag().isPresent()) {
final String lang = literal.getLanguageTag().get();
return getValueFactory().createLiteral(label, lang);
}
final org.eclipse.rdf4j.model.IRI dataType = (org.eclipse.rdf4j.model.IRI) asValue(literal.getDatatype());
return getValueFactory().createLiteral(label, dataType);
}
if (term instanceof BlankNode) {
// This is where it gets tricky to support round trips!
final BlankNode blankNode = (BlankNode) term;
// FIXME: The uniqueReference might not be a valid BlankNode
// identifier..
// does it have to be in RDF4J?
return getValueFactory().createBNode(blankNode.uniqueReference());
}
throw new IllegalArgumentException("RDFTerm was not an IRI, Literal or BlankNode: " + term.getClass());
}
@Override
public RDF4JBlankNode createBlankNode() {
final BNode bnode = getValueFactory().createBNode();
return asRDFTerm(bnode);
}
@Override
public RDF4JBlankNode createBlankNode(final String name) {
final BNode bnode = getValueFactory().createBNode(name);
return asRDFTerm(bnode);
}
/**
* {@inheritDoc}
* <p>
* <strong>Note:</strong> Some operations on the {@link RDF4JDataset}
* requires the use of try-with-resources to close underlying
* {@link RepositoryConnection}s, including {@link RDF4JDataset#iterate()},
* {@link RDF4JDataset#stream()} and {@link RDF4JDataset#getGraphNames()}.
*
*/
@Override
public RDF4JDataset createDataset() {
final Sail sail = new MemoryStore();
final Repository repository = new SailRepository(sail);
return rdf4j.createRepositoryDatasetImpl(repository, true, false);
}
@Override
public RDF4JGraph createGraph() {
return asGraph(new LinkedHashModel());
}
@Override
public RDF4JIRI createIRI(final String iri) throws IllegalArgumentException {
return asRDFTerm(getValueFactory().createIRI(iri));
}
@Override
public RDF4JLiteral createLiteral(final String lexicalForm) throws IllegalArgumentException {
final org.eclipse.rdf4j.model.Literal lit = getValueFactory().createLiteral(lexicalForm);
return asRDFTerm(lit);
}
@Override
public org.apache.commons.rdf.api.Literal createLiteral(final String lexicalForm, final org.apache.commons.rdf.api.IRI dataType)
throws IllegalArgumentException {
final org.eclipse.rdf4j.model.IRI iri = getValueFactory().createIRI(dataType.getIRIString());
final org.eclipse.rdf4j.model.Literal lit = getValueFactory().createLiteral(lexicalForm, iri);
return asRDFTerm(lit);
}
@Override
public org.apache.commons.rdf.api.Literal createLiteral(final String lexicalForm, final String languageTag)
throws IllegalArgumentException {
final org.eclipse.rdf4j.model.Literal lit = getValueFactory().createLiteral(lexicalForm, languageTag);
return asRDFTerm(lit);
}
@Override
public RDF4JTriple createTriple(final BlankNodeOrIRI subject, final org.apache.commons.rdf.api.IRI predicate, final RDFTerm object)
throws IllegalArgumentException {
final Statement statement = getValueFactory().createStatement(
(org.eclipse.rdf4j.model.Resource) asValue(subject), (org.eclipse.rdf4j.model.IRI) asValue(predicate),
asValue(object));
return asTriple(statement);
}
@Override
public Quad createQuad(final BlankNodeOrIRI graphName, final BlankNodeOrIRI subject, final org.apache.commons.rdf.api.IRI predicate,
final RDFTerm object) throws IllegalArgumentException {
final Statement statement = getValueFactory().createStatement(
(org.eclipse.rdf4j.model.Resource) asValue(subject), (org.eclipse.rdf4j.model.IRI) asValue(predicate),
asValue(object), (org.eclipse.rdf4j.model.Resource) asValue(graphName));
return asQuad(statement);
}
public ValueFactory getValueFactory() {
return valueFactory;
}
private EnumSet<Option> optionSet(final Option... options) {
final EnumSet<Option> opts = EnumSet.noneOf(Option.class);
opts.addAll(Arrays.asList(options));
return opts;
}
}