Jena alternative of ModelBuilder in RDF4J

风流意气都作罢 提交于 2019-12-11 16:45:48

问题


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);
        }
    }

回答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.



来源:https://stackoverflow.com/questions/48880884/jena-alternative-of-modelbuilder-in-rdf4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!