This repository was archived by the owner on Mar 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathTestAddTerms.java
More file actions
555 lines (370 loc) · 17.1 KB
/
TestAddTerms.java
File metadata and controls
555 lines (370 loc) · 17.1 KB
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
/**
Copyright (C) SYSTAP, LLC 2006-2015. All rights reserved.
Contact:
SYSTAP, LLC
2501 Calvert ST NW #106
Washington, DC 20008
licenses@systap.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Created on Nov 6, 2007
*/
package com.bigdata.rdf.lexicon;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import org.openrdf.model.vocabulary.RDF;
import com.bigdata.rdf.axioms.NoAxioms;
import com.bigdata.rdf.internal.IV;
import com.bigdata.rdf.model.BigdataBNode;
import com.bigdata.rdf.model.BigdataLiteral;
import com.bigdata.rdf.model.BigdataURI;
import com.bigdata.rdf.model.BigdataValue;
import com.bigdata.rdf.model.BigdataValueFactory;
import com.bigdata.rdf.store.AbstractTripleStore;
import com.bigdata.rdf.store.AbstractTripleStoreTestCase;
import com.bigdata.rdf.store.AbstractTripleStore.Options;
import com.bigdata.rdf.vocab.NoVocabulary;
/**
* Test suite for adding terms to the lexicon.
*
* @author <a href="mailto:thompsonbry@users.sourceforge.net">Bryan Thompson</a>
* @version $Id$
*/
public class TestAddTerms extends AbstractTripleStoreTestCase {
/**
*
*/
public TestAddTerms() {
super();
}
/**
* @param name
*/
public TestAddTerms(String name) {
super(name);
}
public void test_addTerms() {
final Properties properties = getProperties();
// test w/o predefined vocab.
properties.setProperty(Options.VOCABULARY_CLASS, NoVocabulary.class
.getName());
// test w/o axioms - they imply a predefined vocab.
properties.setProperty(Options.AXIOMS_CLASS, NoAxioms.class.getName());
// test w/o the full text index.
properties.setProperty(Options.TEXT_INDEX, "false");
// test w/o inlining
properties.setProperty(Options.INLINE_XSD_DATATYPE_LITERALS, "false");
AbstractTripleStore store = getStore(properties);
try {
final Collection<BigdataValue> terms = new HashSet<BigdataValue>();
// lookup/add some values.
final BigdataValueFactory f = store.getValueFactory();
terms.add(f.asValue(RDF.TYPE));
terms.add(f.asValue(RDF.PROPERTY));
terms.add(f.createURI(getVeryLargeURI()));
terms.add(f.createLiteral("test"));
terms.add(f.createLiteral("test", "en"));
terms.add(f.createLiteral(getVeryLargeLiteral()));
terms.add(f.createLiteral("10", f
.createURI("http://www.w3.org/2001/XMLSchema#int")));
terms.add(f.createLiteral("12", f
.createURI("http://www.w3.org/2001/XMLSchema#float")));
terms.add(f.createLiteral("12.", f
.createURI("http://www.w3.org/2001/XMLSchema#float")));
terms.add(f.createLiteral("12.0", f
.createURI("http://www.w3.org/2001/XMLSchema#float")));
terms.add(f.createLiteral("12.00", f
.createURI("http://www.w3.org/2001/XMLSchema#float")));
if (store.getLexiconRelation().isStoreBlankNodes()) {
/*
* Note: Blank nodes will not round trip through the lexicon unless
* the "told bnodes" is enabled.
*/
terms.add(f.createBNode());
terms.add(f.createBNode("a"));
terms.add(f.createBNode(getVeryLargeLiteral()));
}
final Map<IV<?,?>, BigdataValue> ids = doAddTermsTest(store, terms);
if (store.isStable()) {
store.commit();
store = reopenStore(store);
// verify same reverse mappings.
final Map<IV<?,?>, BigdataValue> ids2 = store.getLexiconRelation()
.getTerms(ids.keySet());
assertEquals(ids.size(), ids2.size());
for (Map.Entry<IV<?,?>, BigdataValue> e : ids2.entrySet()) {
final IV<?,?> id = e.getKey();
assertEquals("Id mapped to a different term? : termId="
+ id, ids.get(id), ids2.get(id));
}
}
} finally {
store.__tearDownUnitTest();
}
}
/**
* The "told bnodes" mode uses the blank node ID as specified rather than
* assigning one based on a UUID. For this case, we need to store the blank
* nodes in the reverse index (T2ID) so we can translate a blank node back
* to a specific identifier.
*/
public void test_toldBNodes() {
final Properties properties = getProperties();
// test w/o predefined vocab.
properties.setProperty(Options.VOCABULARY_CLASS, NoVocabulary.class
.getName());
// test w/o axioms - they imply a predefined vocab.
properties.setProperty(Options.AXIOMS_CLASS, NoAxioms.class.getName());
// test w/o the full text index.
properties.setProperty(Options.TEXT_INDEX, "false");
// this is the "told bnodes" mode.
properties.setProperty(Options.STORE_BLANK_NODES, "true");
AbstractTripleStore store = getStore(properties);
try {
if (!store.isStable()) {
/*
* We need a restart safe store to test this since otherwise a
* term cache could give us a false positive.
*/
return;
}
final Collection<BigdataValue> terms = new HashSet<BigdataValue>();
// lookup/add some values.
final BigdataValueFactory f = store.getValueFactory();
terms.add(f.createBNode());
terms.add(f.createBNode("a"));
final Map<IV<?,?>, BigdataValue> ids = doAddTermsTest(store, terms);
if (store.isStable()) {
store.commit();
store = reopenStore(store);
// verify same reverse mappings.
final Map<IV<?,?>, BigdataValue> ids2 = store.getLexiconRelation()
.getTerms(ids.keySet());
assertEquals(ids.size(),ids2.size());
for (Map.Entry<IV<?, ?>, BigdataValue> e : ids.entrySet()) {
final IV<?, ?> id = e.getKey();
assertEquals("Id mapped to a different term? : termId="
+ id, ids.get(id), ids2.get(id));
}
}
} finally {
store.__tearDownUnitTest();
}
}
/**
* Unit test for addTerms() when the {@link BigdataValue}[] contains
* multiple instances of a given reference.
*/
public void test_duplicates_same_reference() {
final Properties properties = getProperties();
// test w/o predefined vocab.
properties.setProperty(Options.VOCABULARY_CLASS, NoVocabulary.class
.getName());
// test w/o axioms - they imply a predefined vocab.
properties.setProperty(Options.AXIOMS_CLASS, NoAxioms.class.getName());
// test w/o the full text index.
properties.setProperty(Options.TEXT_INDEX, "false");
// test w/o xsd inlining
properties.setProperty(Options.INLINE_XSD_DATATYPE_LITERALS, "false");
AbstractTripleStore store = getStore(properties);
try {
// Note: List allows duplicates!
final Collection<BigdataValue> terms = new LinkedList<BigdataValue>();
// lookup/add some values.
final BigdataValueFactory f = store.getValueFactory();
// Add two instances of the same reference.
final BigdataURI type = f.asValue(RDF.TYPE);
terms.add(type);
terms.add(type);
assertEquals(2,terms.size());
// Add two instances of the same reference.
final BigdataURI largeURI = f.createURI(getVeryLargeURI());
terms.add(largeURI);
terms.add(largeURI);
assertEquals(4,terms.size());
// Add two instances of the same reference.
final BigdataLiteral lit1 = f.createLiteral("test");
final BigdataLiteral lit2 = f.createLiteral("test","en");
final BigdataLiteral lit3 = f.createLiteral(getVeryLargeLiteral());
terms.add(lit1);
terms.add(lit1);
terms.add(lit2);
terms.add(lit2);
terms.add(lit3);
terms.add(lit3);
assertEquals(10,terms.size());
if (store.getLexiconRelation().isStoreBlankNodes()) {
/*
* Note: Blank nodes will not round trip through the lexicon unless
* the "told bnodes" is enabled.
*/
final BigdataBNode bnode1 = f.createBNode();
final BigdataBNode bnode2 = f.createBNode("a");
final BigdataBNode bnode3 = f.createBNode(getVeryLargeLiteral());
terms.add(bnode1);
terms.add(bnode1);
terms.add(bnode2);
terms.add(bnode2);
terms.add(bnode3);
terms.add(bnode3);
assertEquals(16,terms.size());
}
final Map<IV<?,?>, BigdataValue> ids = doAddTermsTest(store, terms);
if (store.isStable()) {
store.commit();
store = reopenStore(store);
// verify same reverse mappings.
final Map<IV<?,?>, BigdataValue> ids2 = store.getLexiconRelation()
.getTerms(ids.keySet());
assertEquals(ids.size(), ids2.size());
for (Map.Entry<IV<?,?>, BigdataValue> e : ids2.entrySet()) {
final IV<?,?> id = e.getKey();
assertEquals("Id mapped to a different term? : termId="
+ id, ids.get(id), ids2.get(id));
}
}
} finally {
store.__tearDownUnitTest();
}
}
/**
* Unit test for addTerms() when the {@link BigdataValue}[] contains
* distinct instances of {@link BigdataValue}s which are equals().
*/
public void test_duplicates_distinct_references() {
final Properties properties = getProperties();
// test w/o predefined vocab.
properties.setProperty(Options.VOCABULARY_CLASS, NoVocabulary.class
.getName());
// test w/o axioms - they imply a predefined vocab.
properties.setProperty(Options.AXIOMS_CLASS, NoAxioms.class.getName());
// test w/o the full text index.
properties.setProperty(Options.TEXT_INDEX, "false");
// test w/o xsd inlining
properties.setProperty(Options.INLINE_XSD_DATATYPE_LITERALS, "false");
AbstractTripleStore store = getStore(properties);
try {
// Note: List allows duplicates!
final Collection<BigdataValue> terms = new LinkedList<BigdataValue>();
// lookup/add some values.
final BigdataValueFactory f = store.getValueFactory();
// Add two distinct instances of the same Value.
terms.add(f.asValue(RDF.TYPE));
terms.add(f.asValue(RDF.TYPE));
assertEquals(2,terms.size());
// Add two distinct instances of the same Value.
terms.add(f.createURI(getVeryLargeURI()));
terms.add(f.createURI(getVeryLargeURI()));
assertEquals(4,terms.size());
// Add two distinct instances of the same Value.
terms.add(f.createLiteral("test"));
terms.add(f.createLiteral("test"));
terms.add(f.createLiteral("test","en"));
terms.add(f.createLiteral("test","en"));
terms.add(f.createLiteral(getVeryLargeLiteral()));
terms.add(f.createLiteral(getVeryLargeLiteral()));
assertEquals(10,terms.size());
if (store.getLexiconRelation().isStoreBlankNodes()) {
/*
* Note: Blank nodes will not round trip through the lexicon unless
* the "told bnodes" is enabled.
*/
// Add two distinct instances of the same Value.
terms.add(f.createBNode());
terms.add(f.createBNode());
terms.add(f.createBNode("a"));
terms.add(f.createBNode("a"));
terms.add(f.createBNode(getVeryLargeLiteral()));
terms.add(f.createBNode(getVeryLargeLiteral()));
assertEquals(16,terms.size());
}
final Map<IV<?,?>, BigdataValue> ids = doAddTermsTest(store, terms);
if (store.isStable()) {
store.commit();
store = reopenStore(store);
// verify same reverse mappings.
final Map<IV<?,?>, BigdataValue> ids2 = store.getLexiconRelation()
.getTerms(ids.keySet());
assertEquals(ids.size(), ids2.size());
for (Map.Entry<IV<?,?>, BigdataValue> e : ids2.entrySet()) {
final IV<?,?> id = e.getKey();
assertEquals("Id mapped to a different term? : termId="
+ id, ids.get(id), ids2.get(id));
}
}
} finally {
store.__tearDownUnitTest();
}
}
/**
* @param store
* @param terms
* @return
*/
private Map<IV<?,?>, BigdataValue> doAddTermsTest(
final AbstractTripleStore store,
final Collection<BigdataValue> terms) {
final int size = terms.size();
final BigdataValue[] a = terms.toArray(new BigdataValue[size]);
// Resolve/add IVs.
store.getLexiconRelation().addTerms(a, size, false/* readOnly */);
// Collect the assigned IVs.
final Collection<IV<?,?>> ids = new ArrayList<IV<?,?>>();
for(BigdataValue t : a)
ids.add(t.getIV());
// Resolve assigned IVs against the lexicon.
final Map<IV<?,?>, BigdataValue> tmp = store.getLexiconRelation()
.getTerms(ids);
// Note: This is not true if there are duplicates.
// assertEquals(size, tmp.size());
/*
* Verify that the lexicon reports the same RDF Values for those term
* identifiers (they will be "equals()" to the values that we added to
* the lexicon).
*/
for(BigdataValue expected : a) {
assertNotNull("Did not assign IV? : " + expected, expected.getIV());
final BigdataValue actual = tmp.get(expected.getIV());
if (actual == null) {
fail("Lexicon does not have value: iv=" + expected.getIV()
+ ", expected=" + expected);
}
assertEquals("Id mapped to a different term? iv="
+ expected.getIV(), expected, actual);
}
return tmp;
}
static String getVeryLargeURI() {
final int len = 1024000;
final StringBuilder sb = new StringBuilder(len + 20);
sb.append("http://www.bigdata.com/");
for (int i = 0; i < len; i++) {
sb.append(Character.toChars('A' + (i % 26)));
}
final String s = sb.toString();
return s;
}
static String getVeryLargeLiteral() {
final int len = 1024000;
final StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
sb.append(Character.toChars('A' + (i % 26)));
}
final String s = sb.toString();
return s;
}
}