Sesame2 API in Jython
A little side by side comparison between code sippets.
The Java was lifted largely from the Sesame2 Repository API
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 org.openrdf.repository.Repository; import org.openrdf.repository.sail.SailRepository; import org.openrdf.sail.memory.MemoryStore; Repository myRepository = new SailRepository(new MemoryStore()); myRepository.initialize(); RepositoryConnection con = myRepository.getConnection(); ValueFactory f = myRepository.getValueFactory(); // create some resources and literals to make statements out of URI alice = f.createURI("http://example.org/people/alice"); URI bob = f.createURI("http://example.org/people/bob"); URI name = f.createURI("http://example.org/ontology/name"); URI person = f.createURI("http://example.org/ontology/Person"); Literal bobsName = f.createLiteral("Bob"); Literal alicesName = f.createLiteral("Alice"); URI context1 = f.createURI("http://example.org/context1"); URI context2 = f.createURI("http://example.org/context2"); URI creator = f.createURI("http://example.org/ontology/creator"); con.add(alice, name, alicesName, [context1]) con.add(bob, name, bobsName, [context1]) RepositoryResult<Statement> statements = con.getStatements(null, name, null, true); try { while (statements.hasNext()) { Statement st = statements.next(); System.out.println(st.toString()); } } finally { statements.close(); // make sure the result object is closed properly }
Jython
from org.openrdf.repository import Repository from org.openrdf.repository.sail import SailRepository from org.openrdf.sail.memory import MemoryStore myRepository = SailRepository(MemoryStore()) myRepository.initialize() con = myRepository.getConnection() f = myRepository.getValueFactory() # create some resources and literals to make statements out of alice = f.createURI("http://example.org/people/alice"); bob = f.createURI("http://example.org/people/bob"); name = f.createURI("http://example.org/ontology/name"); person = f.createURI("http://example.org/ontology/Person"); bobsName = f.createLiteral("Bob"); alicesName = f.createLiteral("Alice"); context1 = f.createURI("http://example.org/context1") context2 = f.createURI("http://example.org/context2") creator = f.createURI("http://example.org/ontology/creator") con.add(alice, name, alicesName, [context1]) con.add(bob, name, bobsName, [context1]) statements = con.getStatements(None, None, None, True, [context1]); try: while statements.hasNext(): s = statements.next() print s finally : statements.close() # make sure the result object is closed properly
Comments
If you try to run the code you need two CLASSPATH adjustments. You need to have the openrdf-sesame-2.0-onejar.jar in your classpath.
You also need adequate slf4j jars. for me that meant at least:
- slf4j-api-1.4.3.jar
- slf4j-simple-1.4.3.jar
If you are a Java pro, you probably have your own logging preferences so I won't comment further.
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.
The Java code also requires a bunch more import statements. Things like ValueFactory and even URI and Literal need import statement in the header.
Jython
Note that the Jython code works as shown with the same CLASSPATH requirements and without the additional import statements.
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 feasable to port RDFAlchemy to jython and access Sesame stores natively. I'm not sure if it's a worthwhile project as currently RDFAlchemy can access Sesame2 via the HTTP protocol (including read/write access).
The same exercise should be done for Jena where RDFAlchemy has read only access through a Joseki server. Wrapping Sesame in the RDFAlchemy would allow both RDFAlchemy users and rdflib user from python to take full advantage of Sesame2 (when we find the right project or enough time to get to it.
