Jena API in Jython
A little side by side comparison between code sippets.
The Java was lifted largely from the Jena tutorial (actually example 3 from there)
The Jython code is a line for line translation (and it works!). I'll let you look at the code without further commentary until the end so that they can be viewed in one brower window without having to scroll.
Java
import com.hp.hpl.jena.rdf.model.*; import com.hp.hpl.jena.vocabulary.*; // some more lines for the wrapper class and main function go here 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) johnSmith.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(" .");
Jython
from com.hp.hpl.jena.rdf.model import * from com.hp.hpl.jena.vocabulary import * personURI = "http:#somewhere/JohnSmith" givenName = "John" familyName = "Smith" fullName = givenName + " " + familyName # create an empty model model = ModelFactory.createDefaultModel() # create the resource # and add the properties cascading style johnSmith = model.createResource(personURI) johnSmith.addProperty(VCARD.FN, fullName)\ .addProperty(VCARD.N, \ model.createResource().addProperty(VCARD.Given, givenName)\ .addProperty(VCARD.Family, familyName)) # list the statements in the graph iter_ = model.listStatements() # print out the predicate, subject and object of each statement while iter_.hasNext(): stmt = iter_.nextStatement() # get next statement sub = stmt.getSubject() # get the subject pred = stmt.getPredicate() # get the predicate obj = stmt.getObject() # get the object print sub.toString(), print " " + pred.toString() + " ", if isinstance(obj, Resource): print obj.toString(), else: # object is a literal print " \"" + obj.toString() + "\"", print " ."
Comments
If you try to run the code you need proper CLASSPATH settings. I won't list them here as I simply used a CLASSPATH from an existing Joseki server I run.
Java
The Java snippet above will not run standalone as it would need to be wrapped in a class with a main and such. But if you are a Java guy (or gal), you already know that. I've wrapped it and run it.
Jython
You can have an interactive session where you can play with the data in whatever tool you prefer (I used jythonconsole)
Conclusion
OK now so much a conclusion as an initial reaction. It appears feasible to port RDFAlchemy to jython and access Jena stores natively.
