Difference between revisions of "Apache Jena"

From BITPlan Wiki
Jump to navigation Jump to search
Line 73: Line 73:
 
7555a24e-8f13-4508-91f0-9dfb26cd2239 http://www.w3.org/2001/vcard-rdf/3.0#Given  "John" .
 
7555a24e-8f13-4508-91f0-9dfb26cd2239 http://www.w3.org/2001/vcard-rdf/3.0#Given  "John" .
 
</source>
 
</source>
 +
== Tutorial04.java ==
 +
=== Java Code ===
 +
<source lang='java'>
 +
/*
 +
* Licensed to the Apache Software Foundation (ASF) under one
 +
* or more contributor license agreements.  See the NOTICE file
 +
* distributed with this work for additional information
 +
* regarding copyright ownership.  The ASF licenses this file
 +
* to you under the Apache License, Version 2.0 (the
 +
* "License"); you may not use this file except in compliance
 +
* with the License.  You may obtain a copy of the License at
 +
*
 +
*    http://www.apache.org/licenses/LICENSE-2.0
 +
*
 +
* Unless required by applicable law or agreed to in writing, software
 +
* distributed under the License is distributed on an "AS IS" BASIS,
 +
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 +
* See the License for the specific language governing permissions and
 +
* limitations under the License.
 +
*/
 +
 +
package jena.examples.rdf ;
 +
 +
import org.apache.jena.rdf.model.*;
 +
import org.apache.jena.vocabulary.*;
 +
 +
/** Tutorial 4 - create a model and write it in XML form to standard out
 +
*/
 +
public class Tutorial04 extends Object {
 +
   
 +
    // some definitions
 +
    static String tutorialURI  = "http://hostname/rdf/tutorial/";
 +
    static String briansName  = "Brian McBride";
 +
    static String briansEmail1 = "brian_mcbride@hp.com";
 +
    static String briansEmail2 = "brian_mcbride@hpl.hp.com";
 +
    static String title        = "An Introduction to RDF and the Jena API";
 +
    static String date        = "23/01/2001";
 +
   
 +
    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));
 +
       
 +
        // now write the model in XML form to a file
 +
        model.write(System.out);
 +
    }
 +
}
 +
 +
<source>

Revision as of 10:54, 14 June 2020

Tutorial examples

see https://jena.apache.org/tutorials/rdf_api.html

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

Java Code

/** 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(" .");
        }
    }
}

Trying it

./ct 03
compiling Tutorial 03
running Tutorial 03
http://somewhere/JohnSmith http://www.w3.org/2001/vcard-rdf/3.0#N 7555a24e-8f13-4508-91f0-9dfb26cd2239 .
http://somewhere/JohnSmith http://www.w3.org/2001/vcard-rdf/3.0#FN  "John Smith" .
7555a24e-8f13-4508-91f0-9dfb26cd2239 http://www.w3.org/2001/vcard-rdf/3.0#Family  "Smith" .
7555a24e-8f13-4508-91f0-9dfb26cd2239 http://www.w3.org/2001/vcard-rdf/3.0#Given  "John" .

Tutorial04.java

Java Code

<source lang='java'> /*

* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package jena.examples.rdf ;

import org.apache.jena.rdf.model.*; import org.apache.jena.vocabulary.*;

/** Tutorial 4 - create a model and write it in XML form to standard out

*/

public class Tutorial04 extends Object {

   // some definitions
   static String tutorialURI  = "http://hostname/rdf/tutorial/";
   static String briansName   = "Brian McBride";
   static String briansEmail1 = "brian_mcbride@hp.com";
   static String briansEmail2 = "brian_mcbride@hpl.hp.com";
   static String title        = "An Introduction to RDF and the Jena API";
   static String date         = "23/01/2001";
   
   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));
       
       // now write the model in XML form to a file
       model.write(System.out);
   }

}

<source>