Difference between revisions of "Apache Jena"
Jump to navigation
Jump to search
(Created page with "= Tutorial examples = == Tutorial03.java == <source lang='java'> →* Tutorial 3 Statement attribute accessor methods: public class Tutorial03 extends Object { public st...") |
|||
Line 1: | Line 1: | ||
= Tutorial examples = | = Tutorial examples = | ||
+ | == Script to compile and run tutorial examples == | ||
+ | <source lang='bash'> | ||
+ | #!/bin/bash | ||
+ | # WF 2020-06-14 | ||
+ | num=$1 | ||
+ | pwd=$(pwd) | ||
+ | base=$pwd/apache-jena-3.15.0 | ||
+ | cd $base/src-examples | ||
+ | echo compiling Tutorial $num | ||
+ | javac -cp "$base/lib/*" jena/examples/rdf/Tutorial$num.java | ||
+ | echo running Tutorial $num | ||
+ | java -cp ".:$base/lib/*" jena/examples/rdf/Tutorial$num | ||
+ | </source> | ||
== Tutorial03.java == | == Tutorial03.java == | ||
<source lang='java'> | <source lang='java'> |
Revision as of 10:51, 14 June 2020
Tutorial examples
Script to compile and run tutorial examples
#!/bin/bash
# WF 2020-06-14
num=$1
pwd=$(pwd)
base=$pwd/apache-jena-3.15.0
cd $base/src-examples
echo compiling Tutorial $num
javac -cp "$base/lib/*" jena/examples/rdf/Tutorial$num.java
echo running Tutorial $num
java -cp ".:$base/lib/*" jena/examples/rdf/Tutorial$num
Tutorial03.java
/** Tutorial 3 Statement attribute accessor methods
*/
public class Tutorial03 extends Object {
public static void main (String args[]) {
// some definitions
String personURI = "http://somewhere/JohnSmith";
String givenName = "John";
String familyName = "Smith";
String fullName = givenName + " " + familyName;
// create an empty model
Model model = ModelFactory.createDefaultModel();
// create the resource
// and add the properties cascading style
Resource johnSmith
= model.createResource(personURI)
.addProperty(VCARD.FN, fullName)
.addProperty(VCARD.N,
model.createResource()
.addProperty(VCARD.Given, givenName)
.addProperty(VCARD.Family, familyName));
// list the statements in the graph
StmtIterator iter = model.listStatements();
// print out the predicate, subject and object of each statement
while (iter.hasNext()) {
Statement stmt = iter.nextStatement(); // get next statement
Resource subject = stmt.getSubject(); // get the subject
Property predicate = stmt.getPredicate(); // get the predicate
RDFNode object = stmt.getObject(); // get the object
System.out.print(subject.toString());
System.out.print(" " + predicate.toString() + " ");
if (object instanceof Resource) {
System.out.print(object.toString());
} else {
// object is a literal
System.out.print(" \"" + object.toString() + "\"");
}
System.out.println(" .");
}
}
}