01: package org.ontoware.semweb4j.lessons.lesson2;
02:
03: import org.ontoware.aifbcommons.collection.ClosableIterable;
04: import org.ontoware.rdf2go.RDF2Go;
05: import org.ontoware.rdf2go.exception.ModelRuntimeException;
06: import org.ontoware.rdf2go.model.Model;
07: import org.ontoware.rdf2go.model.Statement;
08: import org.ontoware.rdf2go.model.node.Node;
09: import org.ontoware.rdf2go.model.node.PlainLiteral;
10: import org.ontoware.rdf2go.model.node.Resource;
11: import org.ontoware.rdf2go.model.node.URI;
12: import org.ontoware.rdf2go.vocabulary.XSD;
13:
14: public class Step6 {
15:
16: private static Model model;
17: private static URI hasTag;
18:
19: private static void init() throws ModelRuntimeException {
20: model = RDF2Go.getModelFactory().createModel();
21: model.open();
22: }
23:
24: public static void tag(Resource resource, Node tag)
25: throws ModelRuntimeException {
26: model.addStatement(resource, hasTag, tag);
27: }
28:
29: public static void main(String[] args) throws ModelRuntimeException {
30:
31: init();
32:
33: // creating URIs
34: // persons
35: URI max = model.createURI("http://xam.de/foaf.rdf.xml#i");
36: URI konrad = model
37: .createURI("http://example.com/persons#konrad");
38: URI guido = model.createURI("http://example.com/persons#guido");
39: URI james = model.createURI("http://example.com/persons#james");
40: // relations
41: URI hasName = model
42: .createURI("http://xmlns.com/foaf/0.1/#term_name");
43: URI hasAge = model
44: .createURI("http://example.com/relations#age");
45: hasTag = model.createURI("http://example.com/relations#hasTag");
46: // tags
47: PlainLiteral tagJava = model.createPlainLiteral("Java");
48: PlainLiteral tagPython = model.createPlainLiteral("Python");
49:
50: // adding statements
51: // naming
52: model.addStatement(max, hasName, "Max Völkel");
53: model.addStatement(konrad, hasName, "Konrad Völkel");
54: model.addStatement(guido, hasName, "Guido van Rossum");
55: model.addStatement(james, hasName, "James Gosling");
56:
57: // a typed property, age
58: model.addStatement(konrad, hasAge, model.createDatatypeLiteral(
59: "19", XSD._integer));
60: model.addStatement(max, hasAge, model.createDatatypeLiteral(
61: "29", XSD._integer));
62:
63: // tagging
64: tag(max, tagJava);
65: tag(james, tagJava);
66: tag(konrad, tagJava);
67: tag(konrad, tagPython);
68: tag(guido, tagPython);
69:
70: // simple SPARQL DESCRIBE
71: System.out.println("Query 1:");
72: // describe max
73: String queryString = "DESCRIBE <" + max + ">";
74: ClosableIterable<? extends Statement> results = model
75: .sparqlDescribe(queryString);
76: for (Statement result : results) {
77: System.out.println(result);
78: }
79:
80: // another SPARQL DESCRIBE
81: System.out.println("Query 2:");
82: // describe every resource tagged with java
83: queryString = "DESCRIBE ?resource WHERE { ?resource <" + hasTag
84: + "> " + tagJava.toSPARQL() + " }";
85: results = model.sparqlDescribe(queryString);
86: for (Statement result : results) {
87: System.out.println(result);
88: }
89:
90: }
91:
92: }
|