1 |
| package photospace.meta.rdf; |
2 |
| |
3 |
| import java.io.*; |
4 |
| import org.apache.commons.logging.*; |
5 |
| import com.hp.hpl.jena.rdf.model.*; |
6 |
| import com.hp.hpl.jena.vocabulary.*; |
7 |
| import junit.framework.*; |
8 |
| |
9 |
| public class JenaTest |
10 |
| extends TestCase |
11 |
| { |
12 |
| private static final Log log = LogFactory.getLog(JenaTest.class); |
13 |
| |
14 |
1
| public void testTutorials() throws Exception
|
15 |
| { |
16 |
| |
17 |
1
| String personURI = "http://somewhere/JohnSmith";
|
18 |
1
| String givenName = "John";
|
19 |
1
| String familyName = "Smith";
|
20 |
1
| String fullName = givenName + " " + familyName;
|
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
1
| Model model = ModelFactory.createDefaultModel();
|
26 |
| |
27 |
| |
28 |
| |
29 |
1
| Resource johnSmith
|
30 |
| = model.createResource(personURI) |
31 |
| .addProperty(VCARD.FN, fullName) |
32 |
| .addProperty(VCARD.N, |
33 |
| model.createResource() |
34 |
| .addProperty(VCARD.Given, givenName) |
35 |
| .addProperty(VCARD.Family, familyName)); |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
1
| StmtIterator iter = model.listStatements();
|
42 |
| |
43 |
| |
44 |
1
| while (iter.hasNext()) {
|
45 |
4
| Statement stmt = iter.nextStatement();
|
46 |
4
| Resource subject = stmt.getSubject();
|
47 |
4
| Property predicate = stmt.getPredicate();
|
48 |
4
| RDFNode object = stmt.getObject();
|
49 |
| |
50 |
4
| String stmtStr = subject.toString() + " " + predicate.toString() + " ";
|
51 |
4
| if (object instanceof Resource) {
|
52 |
1
| stmtStr += object.toString();
|
53 |
| } else { |
54 |
| |
55 |
3
| stmtStr += " \"" + object.toString() + "\"";
|
56 |
| } |
57 |
4
| log.info(stmtStr + " .");
|
58 |
| } |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
1
| model.write(System.out);
|
64 |
1
| model.write(System.out, "RDF/XML-ABBREV");
|
65 |
1
| model.write(System.out, "N-TRIPLE");
|
66 |
| } |
67 |
| |
68 |
1
| public void testTutorial05() throws Exception
|
69 |
| { |
70 |
| |
71 |
1
| Model model = ModelFactory.createDefaultModel();
|
72 |
| |
73 |
1
| File rdf = new File(System.getProperty("project.root"), "build/test/jena-test.rdf");
|
74 |
1
| InputStream in = new FileInputStream(rdf);
|
75 |
| |
76 |
| |
77 |
1
| model.read(new InputStreamReader(in), "");
|
78 |
| |
79 |
| |
80 |
1
| model.write(System.out);
|
81 |
1
| model.write(System.out, "RDF/XML-ABBREV");
|
82 |
1
| model.write(System.out, "N-TRIPLE");
|
83 |
| } |
84 |
| |
85 |
1
| public void testTutorial06070810() throws Exception
|
86 |
| { |
87 |
1
| String personURI = "http://somewhere/JohnSmith/";
|
88 |
| |
89 |
| |
90 |
1
| Model model = ModelFactory.createDefaultModel();
|
91 |
| |
92 |
1
| File rdf = new File(System.getProperty("project.root"), "build/test/jena-test.rdf");
|
93 |
1
| InputStream in = new FileInputStream(rdf);
|
94 |
| |
95 |
| |
96 |
1
| model.read(new InputStreamReader(in), "");
|
97 |
1
| model.write(System.out);
|
98 |
| |
99 |
| |
100 |
1
| Resource vcard = model.getResource(personURI);
|
101 |
| |
102 |
| |
103 |
1
| Resource name = (Resource) vcard.getRequiredProperty(VCARD.N).getObject();
|
104 |
| |
105 |
| |
106 |
1
| String fullName = vcard.getRequiredProperty(VCARD.FN).getString();
|
107 |
| |
108 |
| |
109 |
1
| vcard.addProperty(VCARD.NICKNAME, "Smithy")
|
110 |
| .addProperty(VCARD.NICKNAME, "Adman"); |
111 |
| |
112 |
| |
113 |
1
| log.info("The nicknames of \"" + fullName + "\" are:");
|
114 |
| |
115 |
| |
116 |
1
| StmtIterator iter = vcard.listProperties(VCARD.NICKNAME);
|
117 |
1
| while (iter.hasNext())
|
118 |
| { |
119 |
2
| log.info(" " + iter.nextStatement().getObject().toString());
|
120 |
| } |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
1
| StmtIterator stmts = model.listStatements(
|
127 |
| new SimpleSelector(null, VCARD.FN, (RDFNode) null) |
128 |
| { |
129 |
4
| public boolean selects(Statement s) {
|
130 |
4
| return s.getString().endsWith("Smith");
|
131 |
| } |
132 |
| }); |
133 |
1
| if (stmts.hasNext())
|
134 |
| { |
135 |
1
| log.info("The database contains vcards for:");
|
136 |
2
| while (stmts.hasNext()) log.info(" " + stmts.nextStatement().getString());
|
137 |
| } |
138 |
| else |
139 |
| { |
140 |
0
| log.info("No Smith's were found in the database");
|
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
1
| Bag smiths = model.createBag();
|
147 |
| |
148 |
| |
149 |
| |
150 |
1
| StmtIterator stmts2 = model.listStatements (
|
151 |
| new SimpleSelector(null, VCARD.FN, (RDFNode) null) |
152 |
| { |
153 |
4
| public boolean selects(Statement s) {
|
154 |
4
| return s.getString().endsWith("Smith");
|
155 |
| } |
156 |
| }); |
157 |
| |
158 |
| |
159 |
1
| while (stmts2.hasNext())
|
160 |
| { |
161 |
2
| smiths.add(stmts2.nextStatement().getSubject());
|
162 |
| } |
163 |
| |
164 |
1
| model.write(System.out);
|
165 |
| } |
166 |
| |
167 |
| } |