开发者

Jena: how to dynamically create JavaBeans from SPARQL results?

开发者 https://www.devze.com 2023-03-28 09:29 出处:网络
Is it possible to have Jena or开发者_高级运维 some other library automatically generate a Javabean for results returned from a SPARQL query?I find accessing the results via Jena\'s Resultset to be tir

Is it possible to have Jena or开发者_高级运维 some other library automatically generate a Javabean for results returned from a SPARQL query? I find accessing the results via Jena's Resultset to be tiresome so I'm hoping there's a more object oriented way.

Can JenaBean help here? If I have a RDF file, how would I use Jena in conjunction with JeanBean to generate Javabeans from a Resultset?


Sure you can!

Just instanciate a Jena's Model interface and write code like the example below. This is scala.

Your code will look something like this:

    val fileName = "ffff.rdf"
    var in = new java.io.FileInputStream(fileName)
    var model = ModelFactory.createOntologyModel.read(in, null, null)
                .asInstanceOf[OntModel]

    val modeMetaId = "someid"
    val queryString =
    """
    PREFIX sbml: <http://wikimodels.cnbc.pt/ontologies/sbml.owl#>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    SELECT ?s WHERE
    { ?s rdf:type sbml:Model .
    """ + "?s sbml:metaid \"" + modelMetaId + "\"^^<http://www.w3.org/2001/XMLSchema#string> } "

    val l: java.util.LinkedList[SomeBean]
    = Sparql.exec(model, classOf[SomeBean], queryString)

This is general enough to work with any Jena backend. I'm using SDB with postgresql behind it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号