Search code examples
rdfsesame

converting org.openrdf.model.impl.GraphImpl to info.aduna.iteration.Iteration


I have a graph object of class org.openrdf.model.impl.GraphImpl and I can get out of it org.openrdf.model.util.PatternIterator or could call .iterator() on it. I'm trying to utilize this add method from org.openrdf.repository.RepositoryConnection:

add(info.aduna.iteration.Iteration, org.openrdf.model.Resource)

So far I had no success getting the graph's iterator or PatternIterator converted to info.aduna.iteration.Iteration, although it seems like it should be a straight forward thing. Any help is much appreciated.


Solution

  • First of all, since Sesame release 2.7.0, org.openrdf.model.Graph (and the default GraphImpl implementation) are deprecated, in favor of the org.openrdf.model.Model interface (with the accompanying LinkedHashModel and TreeModel implementations). If you are using Sesame 2.7.0 or higher, you might want to consider switching to using Model, as it is more feature-rich and generally easier to use (see the relevant section in the userdocs for details).

    However, since both Model and Graph extend java.util.Collection, and therefore are both java.lang.Iterable instances, you can easily add them to a store by just using RepositoryConnection.add(Iterable<? extends Statement> statements, Resource... contexts) instead.

    In other words, instead of trying to convert to an Iteration somehow, you can simply do this:

    Model model = new LinkedHashModel();
    Graph graph = new GraphImpl();
    
    ...
    RepositoryConnection conn = repo.getConnection();
    conn.add(model); // a Model is an Iterable, so this works
    conn.add(graph); // a Graph is an Iterable, so this works