Categories
RDF

Jena Week: Containers and namespaces

Recovered from the Wayback Machine.

The RDF/XML syntax differs a great deal from vanilla XML, not the least of which is there is no assumptions associated with the order of elements, and XML lacks many of the precision refinements built directly into RDF/XML.

For instance, in XML you can have several children of an element, but then you’d have to use DTDs or XML Schema to treat the children other than a group whereby order matters. In RDF/XML you can use containers and collections to provide additional information about the members.

Example five in the book created a specific type of Container, a Seq. The code for the example for Jena1 is as follows:

import com.hp.hpl.mesa.rdf.jena.mem.ModelMem;
import com.hp.hpl.mesa.rdf.jena.model.*;
import com.hp.hpl.mesa.rdf.jena.vocabulary.*;
import com.burningbird.postcon.vocabulary.POSTCON;
import java.io.FileOutputStream;
import java.io.PrintWriter;

 

public class pracRDFFifth extends Object {

public static void main (String args[]) {

// resource names String sResource = “http://burningbird.net/articles/monsters1.htm”; String sHistory1 = “http://www.yasd.com/dynaearth/monsters1.htm”; String sHistory2 = “http://www.dynamicearth.com/articles/monsters1.htm”; String sHistory3 = “http://www.burningbird.net/articles/monsters1.htm”;

try { // create an empty graph Model model = new ModelMem();

// create Seq Seq hist = model.createSeq() .add (1, model.createResource(sHistory1) .addProperty(POSTCON.movementtype, model.createLiteral(“Add”)) .addProperty(POSTCON.reason, model.createLiteral(“New Article”)) .addProperty(DC.date, model.createLiteral(“1998-01-01T00:00:00-05:00″))) .add (2, model.createResource(sHistory2) .addProperty(POSTCON.movementtype, model.createLiteral(“Move”)) .addProperty(POSTCON.reason, model.createLiteral(“Moved to separate dynamicearth.com domain”)) .addProperty(DC.date, model.createLiteral(“1999-10-31:T00:00:00-05:00″))) .add (3, model.createResource(sHistory3) .addProperty(POSTCON.movementtype, model.createLiteral(“Move”)) .addProperty(POSTCON.reason, model.createLiteral(“Collapsed into Burningbird”)) .addProperty(DC.date, model.createLiteral(“2002-11-01:T00:00:00-5:00″)));

// create the resource // and add the properties cascading style Resource article = model.createResource(sResource) .addProperty(POSTCON.history, hist);

// Print RDF/XML of model to system output RDFWriter writer = model.getWriter(); writer.setNsPrefix(“pstcn”, “http://burningbird.net/postcon/elements/1.0/”); writer.write(model, new PrintWriter(System.out), “http://burningbird.net/articles” );

} catch (Exception e) { System.out.println(“Failed: ” + e); } } }

 

Note also that this code example introduces a method of changing the namespace abbreviation used, so that we don’t get the generic J0 we’ve been seeing in previous examples:

            // Print RDF/XML of model to system output
            RDFWriter writer = model.getWriter();
            writer.setNsPrefix("pstcn", “http://burningbird.net/postcon/elements/1.0/");
            writer.write(model, new PrintWriter(System.out), “http://burningbird.net/articles” );

 

As with previous examples the primary changes are to the class library structure and to use the factory object to create the memory model. When compiling the application, though, errors appeared.

The setNSPrefix method on the Jena1 RDFWriter class has been removed. To set the namespace, we’ll need to use the code that Ian provided in my comments in the last example:

		// set namespace qualifier
		model.getGraph()
		.getPrefixMapping()
		.setNsPrefix( “pstcn",
		“http://burningbird.net/postcon/elements/1.0/” );

 

At this point no errors are occurring, and I get the generated RDF/XML shown in this file.

This RDF/XML is close, and valid, but not what I was expecting – the Jena code stripped the absolute URI for the primary resource down to a relative URI when attaching the Seq to the resource using a bnode:

  <rdf:Description rdf:about="http://burningbird.net/articles/monsters1.htm">
    <pstcn:history rdf:nodeID="A0″/>
  </rdf:Description>

 

Relative URIs are valid, they resolve to their parent document, or to whatever is specified with xml:base. Why Jena2 alters the URI to a relative one is a big mystery.

You can also set xml:base, but I haven’t been able to locate the class method to use to do this with Jena2. I’ll continue hunting this and hopefully post the information in the next essay

The complete code for this example is:

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;
import com.burningbird.postcon.vocabulary.POSTCON;
import java.io.PrintWriter;

 

public class pracRDFFifth extends Object {

public static void main (String args[]) {

// resource names String sResource = “http://burningbird.net/articles/monsters1.htm”; String sHistory1 = “http://www.yasd.com/dynaearth/monsters1.htm”; String sHistory2 = “http://www.dynamicearth.com/articles/monsters1.htm”; String sHistory3 = “http://www.burningbird.net/articles/monsters1.htm”;

try { // create an empty graph Model model = ModelFactory.createDefaultModel();

// create Seq Seq hist = model.createSeq() .add (1, model.createResource(sHistory1) .addProperty(POSTCON.movementtype, model.createLiteral(“Add”)) .addProperty(POSTCON.reason, model.createLiteral(“New Article”)) .addProperty(DC.date, model.createLiteral(“1998-01-01T00:00:00-05:00″))) .add (2, model.createResource(sHistory2) .addProperty(POSTCON.movementtype, model.createLiteral(“Move”)) .addProperty(POSTCON.reason, model.createLiteral(“Moved to separate dynamicearth.com domain”)) .addProperty(DC.date, model.createLiteral(“1999-10-31:T00:00:00-05:00″))) .add (3, model.createResource(sHistory3) .addProperty(POSTCON.movementtype, model.createLiteral(“Move”)) .addProperty(POSTCON.reason, model.createLiteral(“Collapsed into Burningbird”)) .addProperty(DC.date, model.createLiteral(“2002-11-01:T00:00:00-5:00″)));

// create the resource // and add the properties cascading style Resource article = model.createResource(sResource) .addProperty(POSTCON.history, hist);

// set namespace qualifier model.getGraph() .getPrefixMapping() .setNsPrefix( “pstcn”, “http://burningbird.net/postcon/elements/1.0/” );

// Print RDF/XML of model to system output RDFWriter writer = model.getWriter(); writer.write(model, new PrintWriter(System.out), “http://burningbird.net/articles” );

} catch (Exception e) { System.out.println(“Failed: ” + e); } } }

 

Well, this example’s conversion wasn’t without confusion. We’ve spent enough time on creating models – next example, we’ll look at reading a model in from an RDF/XML file, and accessing the individual statements.

Print Friendly, PDF & Email