0

Is there some Interface available in Apache Jena like ModelBuilder in RDF4J?

I can see ModelMaker in Jena but that is not something similar to builder I suppose. Following is the function using rdf4j that need to be implemented in Jena:

public static org.eclipse.rdf4j.model.Model convertGraph2RDFModel(Graph graph, String label) {
        ModelBuilder builder = new ModelBuilder();
        GraphTraversalSource t = graph.traversal();
        GraphTraversal<Vertex, Vertex> hasLabel = t.V().hasLabel(label);
        Vertex s;
        if(hasLabel.hasNext()){
            s = hasLabel.next();
            extractModelFromVertex(builder, s);
        }
        return builder.build();
    }

private static void extractModelFromVertex(ModelBuilder builder, Vertex s) {
        builder.subject(s.label());
        Iterator<VertexProperty<String>> propertyIter = s.properties();
        while (propertyIter.hasNext()){
            VertexProperty<String> property = propertyIter.next();
            builder.add(property.label(), property.value());
        }
        Iterator<Edge> edgeIter = s.edges(Direction.OUT);
        Edge edge;
        Stack<Vertex> vStack = new Stack<Vertex>();
        while(edgeIter.hasNext()){
            edge = edgeIter.next();
            s = edge.inVertex();
            builder.add(edge.label(), s.label());
            vStack.push(s);
        }
        Iterator<Vertex> vIterator = vStack.iterator();
        while(vIterator.hasNext()){
            s = vIterator.next();
            extractModelFromVertex(builder,s);
        }
    }
Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
sumit
  • 133
  • 2
  • 10
  • 1
    I don't know RDF4J, but this code appears to be converting one RDF4J domain object into another. Obviously, Jena doesn't have these same domain objects so would have no need to convert between them. Why do you think this code should be implemented in Jena? – jaco0646 Feb 22 '18 at 17:18
  • 1
    @jaco0646 `Graph`, `Edge` and `Vertex` in the above code example are not RDF4J objects. I would speculate that they are part of some graph modelling package / graph database and the above code is about converting a generic graph model to an RDF model. As for why he wants to replace RDF4J with Jena, I'm kinda curious myself. – Jeen Broekstra Feb 23 '18 at 00:39

1 Answers1

1

I don't know if Jena has similar functionality, but you could of course just continue using the RDF4J ModelBuilder, serialize its output Model to, say, a Turtle or TriG string (or file), then use Jena to read it in again.

 org.eclipse.rdf4j.model.Model m = ...; // RDF4J Model built by the ModelBuilder
 java.io.Writer writer = new StringWriter();
 org.eclipse.rdf4j.rio.Rio.write(m, writer, RDFFormat.TRIG); 

 String = writer.toString();

 // Use Jena's parser to read the string back in.

Or alternatively just iterate over the RDF4J model and convert each statement directly (without serializing and deserializing in between):

 org.eclipse.rdf4j.model.Model rdf4jModel = ...; // RDF4J Model built by the ModelBuilder
 org.apache.jena.rdf.model.Model jenaModel = ...; // (empty) Jena model to receive converted rdf4j model 
 rdf4jModel.forEach(stmt -> jenaModel.add(convert(stmt)));

 ...

 public org.apache.jena.rdf.model.Statement convert(
                org.eclipse.rdf4j.model.Statement stmt) {
      ... // create a Jena statement from the RDF4J one. 
 }

I'll admit it's probably easier to settle on using a single framework in most applications, but there's no fundamental reason you can't use bits of RDF4J and Jena in combination.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73