简体   繁体   English

JENA RDF / XML格式的rdfs示例

[英]JENA RDF/XML format rdfs example

Can someone give me a working example ( in Java code ) of how to create a RDFS related statement like the following using Jena? 有人能给我一个工作示例( 在Java代码中 )如何使用Jena创建如下所示的RDFS相关语句?

<rdf:Property rdf:about="http://www.help.me/confused/PropertyName">
    <rdfs:domain rdf:resource="http://www.help.me/confused/ClassName"/>
    <rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal"/>
</rdf:Property>

I created a RDF/XML schema by hand, and it validates right but somehow the entities won't work together in SPARQL (even with inference engine on). 我手动创建了一个RDF / XML模式,它验证正确但不知何故实体在SPARQL中不能一起工作(即使启用了推理引擎)。 So, I decided to create the whole thing from start using the Jena API to ensure that it's correct. 所以,我决定从一开始就使用Jena API创建整个事情,以确保它是正确的。

I've changed the namespace a bit, just so that this code ends up pointing back to this post, but at any rate, I get this output: 我已经更改了命名空间,只是为了让这段代码最终指向这篇文章,但无论如何,我得到了这个输出:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <rdf:Property rdf:about="http://stackoverflow.com/q/20194409/1281433/PropertyName">
    <rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal"/>
    <rdfs:domain rdf:resource="http://stackoverflow.com/q/20194409/1281433/ClassName"/>
  </rdf:Property>
</rdf:RDF>

from this code: 从这段代码:

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.vocabulary.RDFS;

public class JenaPropertyExample {
    public static void main(String[] args) {
        final String NS = "http://stackoverflow.com/q/20194409/1281433/";
        final Model model = ModelFactory.createDefaultModel();

        final Property p = model.createResource( NS+"PropertyName", RDF.Property ).as( Property.class );
        p.addProperty( RDFS.domain, model.createResource( NS+"ClassName" ));
        p.addProperty( RDFS.range, RDFS.Literal );

        model.write( System.out, "RDF/XML-ABBREV" );
    }
}

In general, just because the output is legal RDF doesn't mean that you're using the properties and classes in the way that's expected. 一般来说,仅仅因为输出是合法的RDF并不意味着你以预期的方式使用属性和类。 Jena's plain Model interface can't help you too much with that, since you could still use the properties incorrectly, but at least, if you're using Jena's predefined vocabulary classes, you'll get the IRIs right. Jena的普通Model界面对此无法帮助你,因为你仍然可以错误地使用属性,但至少,如果你使用Jena的预定义词汇表类,你将获得正确的IRI。 If you can use an OntModel, you can get a slightly nicer layer of abstraction, though. 如果你可以使用OntModel,你可以获得一个稍微好一点的抽象层。 Eg, the following method produces the same RDF/XML output, but lets you use methods like createOntProperty and get the p rdf:type rdf:Property triple for free, and methods like addRange and addDomain : 例如,以下方法生成相同的RDF / XML输出,但允许您使用createOntProperty等方法并免费获取p rdf:type rdf:Property triple,以及addRangeaddDomain等方法:

public static void main2( String[] args ) {
    final String NS = "http://stackoverflow.com/q/20194409/1281433/";
    final OntModel model = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM );
    OntProperty p = model.createOntProperty( NS+"PropertyName" );
    p.addDomain( model.getOntClass( NS+"ClassName" ));
    p.addRange( RDFS.Literal );
    model.write( System.out, "RDF/XML-ABBREV" );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM