-
Notifications
You must be signed in to change notification settings - Fork 10
/
JenaTutorialExamples.java
734 lines (700 loc) · 24.9 KB
/
JenaTutorialExamples.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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
package tutorial;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import com.franz.agraph.jena.AGGraph;
import com.franz.agraph.jena.AGGraphMaker;
import com.franz.agraph.jena.AGInfModel;
import com.franz.agraph.jena.AGModel;
import com.franz.agraph.jena.AGQuery;
import com.franz.agraph.jena.AGQueryExecutionFactory;
import com.franz.agraph.jena.AGQueryFactory;
import com.franz.agraph.jena.AGReasoner;
import com.franz.agraph.repository.AGCatalog;
import com.franz.agraph.repository.AGRepository;
import com.franz.agraph.repository.AGRepositoryConnection;
import com.franz.agraph.repository.AGServer;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
public class JenaTutorialExamples {
static private final String SERVER_URL = "http://localhost:10035";
static private final String CATALOG_ID = "java-catalog";
static private final String REPOSITORY_ID = "jenatutorial";
static private final String USERNAME = "test";
static private final String PASSWORD = "xyzzy";
static private final String TEMPORARY_DIRECTORY = "";
static final String FOAF_NS = "http://xmlns.com/foaf/0.1/";
/**
* Creating a Repository
*/
public static AGGraphMaker example1(boolean close)
throws Exception {
// Tests getting the repository up.
println("\nStarting example1().");
AGServer server = new AGServer(SERVER_URL, USERNAME, PASSWORD);
println("Available catalogs: " + server.listCatalogs());
AGCatalog catalog = server.getCatalog(CATALOG_ID);
println("Available repositories in catalog "
+ (catalog.getCatalogName()) + ": "
+ catalog.listRepositories());
catalog.deleteRepository(REPOSITORY_ID);
AGRepository myRepository = catalog.createRepository(REPOSITORY_ID);
println("Got a repository.");
myRepository.initialize();
println("Initialized repository.");
println("Repository is writable? " + myRepository.isWritable());
AGRepositoryConnection conn = myRepository.getConnection();
closeBeforeExit(conn);
println("Got a connection.");
println("Repository " + (myRepository.getRepositoryID())
+ " is up! It contains " + (conn.size()) + " statements.");
AGGraphMaker maker = new AGGraphMaker(conn);
println("Got a graph maker for the connection.");
if (close) {
// tidy up
maker.close();
conn.close();
myRepository.shutDown();
return null;
}
return maker;
}
/**
* Asserting and Retracting Triples
*/
public static AGModel example2(boolean close) throws Exception {
AGGraphMaker maker = example1(false);
AGGraph graph = maker.getGraph();
AGModel model = new AGModel(graph);
println("\nStarting example2().");
// Create some resources and literals to make statements from.
Resource alice = model
.createResource("http://example.org/people/alice");
Resource bob = model.createResource("http://example.org/people/bob");
Property name = model
.createProperty("http://example.org/ontology/name");
Resource person = model
.createResource("http://example.org/ontology/Person");
Literal bobsName = model.createLiteral("Bob");
Literal alicesName = model.createLiteral("Alice");
println("Triple count before inserts: " + model.size());
// Alice's name is "Alice"
model.add(alice, name, alicesName);
// Alice is a person
model.add(alice, RDF.type, person);
// Bob's name is "Bob"
model.add(bob, name, bobsName);
// Bob is a person, too.
model.add(bob, RDF.type, person);
println("Added four triples.");
println("Triple count after inserts: " + (model.size()));
StmtIterator result = model.listStatements();
while (result.hasNext()) {
Statement st = result.next();
println(st);
}
model.remove(bob, name, bobsName);
println("Removed one triple.");
println("Triple count after deletion: " + (model.size()));
// put it back so we can continue with other examples
model.add(bob, name, bobsName);
if (close) {
model.close();
graph.close();
maker.close();
return null;
}
return model;
}
/**
* A SPARQL Query
*/
public static void example3() throws Exception {
AGModel model = example2(false);
println("\nStarting example3().");
try {
String queryString = "SELECT ?s ?p ?o WHERE {?s ?p ?o .}";
AGQuery sparql = AGQueryFactory.create(queryString);
QueryExecution qe = AGQueryExecutionFactory.create(sparql, model);
try {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution result = results.next();
RDFNode s = result.get("s");
RDFNode p = result.get("p");
RDFNode o = result.get("o");
// System.out.format("%s %s %s\n", s, p, o);
System.out.println(" { " + s + " " + p + " " + o + " . }");
}
} finally {
qe.close();
}
} finally {
model.close();
}
}
/**
* Statement Matching
*/
public static void example4() throws Exception {
AGModel model = example2(false);
println("\nStarting example4().");
Resource alice = model
.createResource("http://example.org/people/alice");
StmtIterator statements = model.listStatements(alice, null,
(RDFNode) null);
try {
while (statements.hasNext()) {
println(statements.next());
}
} finally {
statements.close();
model.close();
}
}
/**
* Literal Values
*/
public static void example5() throws Exception {
AGModel model = example2(false);
println("\nStarting example5().");
model.removeAll();
Resource alice = model.createResource("http://example.org/people/alice");
String exns = "http://example.org/people/";
Resource ted = model.createResource(exns + "ted");
Property age = model.createProperty(exns,"age");
Property weight = model.createProperty(exns, "weight");
Property favoriteColor = model.createProperty(exns, "favoriteColor");
Property birthdate = model.createProperty(exns, "birthdate");
Literal red = model.createLiteral("Red");
Literal rouge = model.createLiteral("Rouge", "fr");
Literal fortyTwoInt = model.createTypedLiteral("42", XSDDatatype.XSDint);
Literal fortyTwoLong = model.createTypedLiteral("42", XSDDatatype.XSDlong);
Literal fortyTwoUntyped = model.createLiteral("42");
Literal date = model.createTypedLiteral("1984-12-06", XSDDatatype.XSDdate);
Literal time = model.createTypedLiteral("1984-12-06T09:00:00", XSDDatatype.XSDdateTime);
Literal weightUntyped = model.createLiteral("120.5");
Literal weightFloat = model.createTypedLiteral("120.5", XSDDatatype.XSDfloat);
Statement stmt1 = model.createStatement(alice, age, fortyTwoInt);
Statement stmt2 = model.createStatement(ted, age, fortyTwoLong);
Statement stmt3 = model.createStatement(ted, age, fortyTwoUntyped);
model.add(stmt1);
model.add(stmt2);
model.add(stmt3);
model.add(alice, weight, weightFloat);
model.add(ted, weight, weightUntyped);
model.add(alice, favoriteColor, red);
model.add(ted, favoriteColor, rouge);
model.add(alice, birthdate, date);
model.add(ted, birthdate, time);
for (Literal obj : new Literal[] {null, fortyTwoInt, fortyTwoLong, fortyTwoUntyped, weightFloat, weightUntyped,
red, rouge}) {
println( "\nRetrieve triples matching " + obj + ".");
StmtIterator statements = model.listStatements(null, null, obj);
try {
while (statements.hasNext()) {
println(statements.next());
}
} finally {
statements.close();
}
}
// SPARQL
for (String obj : new String[]{"42", "\"42\"", "120.5", "\"120.5\"", "\"120.5\"^^xsd:float",
"\"Rouge\"@fr", "\"Rouge\"", "\"1984-12-06\"^^xsd:date"}) {
println( "\nQuery triples matching " + obj + ".");
String queryString = "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT ?s ?p ?o WHERE {?s ?p ?o . filter (?o = " + obj + ")}";
AGQuery query = AGQueryFactory.create(queryString);
QueryExecution qe = AGQueryExecutionFactory.create(query, model);
try {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution result = results.next();
RDFNode s = result.get("s");
RDFNode p = result.get("p");
RDFNode o = result.get("o");
println(" " + s + " " + p + " " + o);
}
} finally {
qe.close();
}
}
{
// Search for date using date object in triple pattern.
println("\nRetrieve triples matching DATE object.");
StmtIterator statements = model.listStatements(null, null, date);
try {
while (statements.hasNext()) {
println(statements.next());
}
} finally {
statements.close();
}
}
{
println("\nMatch triples having a specific DATE value.");
StmtIterator statements = model.listStatements(null, null,
model.createTypedLiteral("1984-12-06",XSDDatatype.XSDdate));
try {
while (statements.hasNext()) {
println(statements.next());
}
} finally {
statements.close();
}
}
{
// Search for time using datetime object in triple pattern.
println("\nRetrieve triples matching DATETIME object.");
StmtIterator statements = model.listStatements(null, null, time);
try {
while (statements.hasNext()) {
println(statements.next());
}
} finally {
statements.close();
}
}
{
println("\nMatch triples having a specific DATETIME value.");
StmtIterator statements = model.listStatements(null, null,
model.createTypedLiteral("1984-12-06T09:00:00",XSDDatatype.XSDdateTime));
try {
while (statements.hasNext()) {
println(statements.next());
}
} finally {
statements.close();
}
}
model.close();
}
/**
* Importing Triples
*/
public static AGGraphMaker example6() throws Exception {
AGGraphMaker maker = example1(false);
AGModel model = new AGModel(maker.getGraph());
AGModel model_vcards = new AGModel(maker.createGraph("http://example.org#vcards"));
String path1 = "src/tutorial/java-vcards.rdf";
String path2 = "src/tutorial/java-kennedy.ntriples";
String baseURI = "http://example.org/example/local";
model_vcards.read(new FileInputStream(path1), baseURI);
model.read(new FileInputStream(path2), baseURI, "N-TRIPLE");
println("After loading, model_vcards contains " + model_vcards.size()
+ " triples in graph '" + model_vcards.getGraph()
+ "'\n and model contains " + model.size()
+ " triples in graph '" + model.getGraph() + "'.");
return maker;
}
/**
* Importing Triples, query
*/
public static void example7() throws Exception {
AGGraphMaker maker = example6();
AGModel model = new AGModel(maker.getGraph());
AGModel model_vcards = new AGModel(maker.openGraph("http://example.org#vcards"));
println("\nMatch all and print subjects and graph (model)");
StmtIterator statements = model.listStatements();
for (int i = 0; i < 25 && statements.hasNext(); i++) {
Statement stmt = statements.next();
println(stmt.getSubject() + " " + stmt.getModel().getGraph());
}
println("\nMatch all and print subjects and graph (model_vcards)");
statements = model_vcards.listStatements();
for (int i = 0; i < 25 && statements.hasNext(); i++) {
Statement stmt = statements.next();
println(stmt.getSubject() + " " + stmt.getModel().getGraph());
}
statements.close();
println("\nSPARQL query over the default graph (model).");
String queryString = "SELECT DISTINCT ?s ?p ?o WHERE {?s ?p ?o . } LIMIT 25";
AGQuery query = AGQueryFactory.create(queryString);
QueryExecution qe = AGQueryExecutionFactory.create(query, model);
try {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution result = results.next();
RDFNode s = result.get("s");
RDFNode p = result.get("p");
RDFNode o = result.get("o");
println(" " + s + " " + p + " " + o);
}
} finally {
qe.close();
}
println("\nSPARQL query over the default graph (model_vcards).");
qe = AGQueryExecutionFactory.create(query, model_vcards);
try {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution result = results.next();
RDFNode s = result.get("s");
RDFNode p = result.get("p");
RDFNode o = result.get("o");
println(" " + s + " " + p + " " + o);
}
} finally {
qe.close();
}
println("\nSPARQL query for triples in any named graph (model).");
queryString = "SELECT DISTINCT ?s ?p ?o ?g WHERE {graph ?g {?s ?p ?o .} } LIMIT 25";
query = AGQueryFactory.create(queryString);
qe = AGQueryExecutionFactory.create(query, model);
try {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution result = results.next();
RDFNode s = result.get("s");
RDFNode p = result.get("p");
RDFNode o = result.get("o");
RDFNode g = result.get("g");
println(" " + s + " " + p + " " + o + " " + g);
}
} finally {
qe.close();
}
println("\nSPARQL query for triples in any named graph (model_vcards).");
qe = AGQueryExecutionFactory.create(query, model_vcards);
try {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution result = results.next();
RDFNode s = result.get("s");
RDFNode p = result.get("p");
RDFNode o = result.get("o");
RDFNode g = result.get("g");
println(" " + s + " " + p + " " + o + " " + g);
}
} finally {
qe.close();
}
}
/**
* Writing RDF or NTriples to a file
*/
public static void example8() throws Exception {
AGGraphMaker maker = example6();
AGModel model = new AGModel(maker.getGraph());
AGModel model_vcards = new AGModel(maker.openGraph("http://example.org#vcards"));
String outputFile = TEMPORARY_DIRECTORY + "temp.nt";
// outputFile = null;
if (outputFile == null) {
println("\nWriting n-triples to Standard Out instead of to a file");
} else {
println("\nWriting n-triples to: " + outputFile);
}
OutputStream output = (outputFile != null) ? new FileOutputStream(
outputFile) : System.out;
model.write(output, "N-TRIPLE");
String outputFile2 = TEMPORARY_DIRECTORY + "temp.rdf";
// outputFile2 = null;
if (outputFile2 == null) {
println("\nWriting RDF to Standard Out instead of to a file");
} else {
println("\nWriting RDF to: " + outputFile2);
}
output = (outputFile2 != null) ? new FileOutputStream(outputFile2)
: System.out;
model_vcards.write(output);
}
/**
* Writing the result of a statements match.
*/
public static void example9() throws Exception {
AGGraphMaker maker = example6();
AGModel model_vcards = new AGModel(maker.openGraph("http://example.org#vcards"));
StmtIterator statements = model_vcards.listStatements(null,RDF.type, (RDFNode)null);
Model m = ModelFactory.createDefaultModel();
m.add(statements);
m.write(System.out);
}
/**
* Namespaces
*/
public static void example11() throws Exception {
AGGraphMaker maker = example1(false);
AGModel model = new AGModel(maker.getGraph());
String exns = "http://example.org/people/";
Resource alice = model.createResource(exns + "alice");
Resource person = model.createResource(exns + "Person");
model.add(alice, RDF.type, person);
model.setNsPrefix("ex", exns);
String queryString = "SELECT ?s ?p ?o WHERE { ?s ?p ?o . FILTER ((?p = rdf:type) && (?o = ex:Person) ) }";
AGQuery query = AGQueryFactory.create(queryString);
QueryExecution qe = AGQueryExecutionFactory.create(query, model);
try {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
println(results.next());
}
} finally {
qe.close();
}
}
/**
* Ask, Construct, and Describe queries
*/
public static void example13() throws Exception {
AGModel model = example2(false);
model.setNsPrefix("ex", "http://example.org/people/");
model.setNsPrefix("ont", "http://example.org/ontology/");
println("\nSELECT result:");
String queryString = "select ?s ?p ?o where { ?s ?p ?o} ";
AGQuery query = AGQueryFactory.create(queryString);
QueryExecution qe = AGQueryExecutionFactory.create(query, model);
try {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
println(results.next());
}
} finally {
qe.close();
}
queryString = "ask { ?s ont:name \"Alice\" } ";
query = AGQueryFactory.create(queryString);
qe = AGQueryExecutionFactory.create(query, model);
try {
println("\nBoolean result: " + qe.execAsk());
} finally {
qe.close();
}
/*TODO:
queryString = "construct {?s ?p ?o} where { ?s ?p ?o . filter (?o = \"Alice\") } ";
query = AGQueryFactory.create(queryString);
qe = AGQueryExecutionFactory.create(query, model);
try {
Model m = qe.execConstruct();
println("\nConstruct result:");
m.write(System.out);
} finally {
qe.close();
}
queryString = "describe ?s where { ?s ?p ?o . filter (?o = \"Alice\") } ";
query = AGQueryFactory.create(queryString);
qe = AGQueryExecutionFactory.create(query, model);
try {
Model m = qe.execDescribe();
println("\nDescribe result:");
m.write(System.out);
} finally {
qe.close();
}*/
}
/**
* RDFS++ Reasoning
*/
public static void example19() throws Exception {
AGGraphMaker maker = example1(false);
AGModel model = new AGModel(maker.getGraph());
// Examples of RDFS++ inference. Was originally example 2A.
Resource robert = model.createResource("http://example.org/people/robert");
Resource roberta = model.createResource("http://example.org/people/roberta");
Resource bob = model.createResource("http://example.org/people/bob");
Resource bobby = model.createResource("http://example.org/people/bobby");
// create name and child predicates, and Person class.
Property name = model.createProperty("http://example.org/ontology/name");
Property fatherOf = model.createProperty("http://example.org/ontology/fatherOf");
Resource person = model.createResource("http://example.org/ontology/Person");
// create literal values for names
Literal bobsName = model.createLiteral("Bob");
Literal bobbysName = model.createLiteral("Bobby");
Literal robertsName = model.createLiteral("Robert");
Literal robertasName = model.createLiteral("Roberta");
// Bob is the same person as Robert
model.add(bob, OWL.sameAs, robert);
// Robert, Bob, and children are people
model.add(robert, RDF.type, person);
model.add(roberta, RDF.type, person);
model.add(bob, RDF.type, person);
model.add(bobby, RDF.type, person);
// They all have names.
model.add(robert, name, robertsName);
model.add(roberta, name, robertasName);
model.add(bob, name, bobsName);
model.add(bobby, name, bobbysName);
// robert has a child
model.add(robert, fatherOf, roberta);
// bob has a child
model.add(bob, fatherOf, bobby);
// List the children of Robert, with inference OFF.
println("\nChildren of Robert, inference OFF");
printRows(model.listStatements(robert, fatherOf, (RDFNode) null));
// List the children of Robert with inference ON. The owl:sameAs
// link combines the children of Bob with those of Robert.
println("\nChildren of Robert, inference ON");
AGReasoner reasoner = new AGReasoner();
InfModel infmodel = new AGInfModel(reasoner, model);
printRows(infmodel.listStatements(robert, fatherOf, (RDFNode)null));
// Remove the owl:sameAs link so we can try the next example.
model.remove(bob, OWL.sameAs, robert);
// Define new predicate, hasFather, as the inverse of fatherOf.
Property hasFather = model.createProperty("http://example.org/ontology/hasFather");
model.add(hasFather, OWL.inverseOf, fatherOf);
// Search for people who have fathers, even though there are no
// hasFather triples.
// With inference OFF.
println("\nPeople with fathers, inference OFF");
printRows(model.listStatements(null, hasFather, (RDFNode)null));
// With inference ON. The owl:inverseOf link allows AllegroGraph to
// deduce the inverse links.
println("\nPeople with fathers, inference ON");
printRows(infmodel.listStatements(null, hasFather, (RDFNode)null));
// Remove owl:inverseOf property.
model.remove(hasFather, OWL.inverseOf, fatherOf);
Property parentOf = model.createProperty("http://example.org/ontology/parentOf");
model.add(fatherOf, RDFS.subPropertyOf, parentOf);
// Now search for inferred parentOf links.
// Search for parentOf links, even though there are no parentOf triples.
// With inference OFF.
println("\nPeople with parents, inference OFF");
printRows(model.listStatements(null, parentOf, (RDFNode)null));
// With inference ON. The rdfs:subpropertyOf link allows AllegroGraph to
// deduce that fatherOf links imply parentOf links.
println("\nPeople with parents, inference ON");
printRows(infmodel.listStatements(null, parentOf, (RDFNode)null));
model.remove(fatherOf, RDFS.subPropertyOf, parentOf);
// The next example shows rdfs:range and rdfs:domain in action.
// We'll create two new rdf:type classes. Note that classes are
// capitalized.
Resource parent = model.createResource("http://example.org/ontology/Parent");
Resource child = model.createResource("http://exmaple.org/ontology/Child");
// The following triples say that a fatherOf link points from a parent
// to a child.
model.add(fatherOf, RDFS.domain, parent);
model.add(fatherOf, RDFS.range, child);
// Now we can search for rdf:type parent.
println("\nWho are the parents? Inference ON.");
printRows(infmodel.listStatements(null, RDF.type, parent));
// And we can search for rdf:type child.
println("\nWho are the children? Inference ON.");
printRows(infmodel.listStatements(null, RDF.type, child));
}
/**
* Usage: all Usage: [1-9,11,13]+
*/
public static void main(String[] args) throws Exception {
List<Integer> choices = new ArrayList<Integer>();
if (args.length == 0) {
// for choosing by editing this code
choices.add(1);
} else if (args[0].equals("all")) {
for (int i = 1; i <= 9; i++) {
choices.add(i);
}
choices.add(11);
choices.add(13);
} else {
for (int i = 0; i < args.length; i++) {
choices.add(Integer.parseInt(args[i]));
}
}
try {
for (Integer choice : choices) {
println("\n** Running example " + choice);
switch (choice) {
case 1:
example1(true);
break;
case 2:
example2(true);
break;
case 3:
example3();
break;
case 4:
example4();
break;
case 5:
example5();
break;
case 6:
example6();
break;
case 7:
example7();
break;
case 8:
example8();
break;
case 9:
example9();
break;
case 11:
example11();
break;
case 13:
example13();
break;
case 19:
//example19();
break;
default:
throw new IllegalArgumentException("There is no example "
+ choice);
}
closeAll();
}
} finally {
closeAll();
}
}
public static void println(Object x) {
System.out.println(x);
}
static void printRows(StmtIterator rows) throws Exception {
while (rows.hasNext()) {
println(rows.next());
}
rows.close();
}
static void printRows(String headerMsg, int limit, StmtIterator rows) throws Exception {
println(headerMsg);
int count = 0;
while (count < limit && rows.hasNext()) {
println(rows.next());
count++;
}
println("Number of results: " + count);
rows.close();
}
static void close(AGRepositoryConnection conn) {
try {
conn.close();
} catch (Exception e) {
System.err.println("Error closing repository connection: " + e);
e.printStackTrace();
}
}
private static List<AGRepositoryConnection> toClose = new ArrayList<AGRepositoryConnection>();
/**
* This is just a quick mechanism to make sure all connections get closed.
*/
private static void closeBeforeExit(AGRepositoryConnection conn) {
toClose.add(conn);
}
private static void closeAll() {
while (toClose.isEmpty() == false) {
AGRepositoryConnection conn = toClose.get(0);
close(conn);
while (toClose.remove(conn)) {
}
}
}
}