RDF / XML如何定义RDFS类,子类并将它们作为类型链接

时间:2013-11-26 16:05:12

标签: java rdf jena rdfs

我们如何在Jena中定义类和子类并将其作为其他资源的类型添加? 我使用Java,Jena和RDF / XML表示法。我想创建类似的东西:

<rdfs:Class rdf:about="http://www.help.me/NS/Classname"/>
<rdfs:Class rdf:about="http://www.help.me/NS/Subclassname">
    <rdfs:subClassOf rdf:resource="http://www.help.me/NS/Classname"/>
</rdfs:Class>

之后:将资源链接到子类:

<rdf:Description rdf:about="http://www.help.me/NS/NewResource">
    <rdf:type rdf:resource="http://www.help.me/NS/Subclassname"/>
    ...
</rdf:Description>

编辑:

到目前为止,我找到了如何定义一个类:

model.createResource("http://www.help.me/NS/", RDFS.Class);

1 个答案:

答案 0 :(得分:4)

通常,您应该阅读Model的javadoc以及与之相关的类和接口(例如,Resource)。您不需要记住所有细节,但至少要熟悉它们提供的方法类型,这样您就可以了解如何完成某些任务。我建议你阅读OntModel及其相关的类和接口(例如,个人)。

创建子类

您可以通过直接向模型添加语句,或者通过向资源添加属性来创建子类关系,或者,如果您使用OntModel和OntClasses,则使用addSubClass和addSuperClass方法。

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDFS;

public class AddTypesExample {
    final private static String NS = "http://stackoverflow.com/q/20222080/1281433/";

    public static void main( String[] args ) {
        subclassModel().write( System.out, "RDF/XML" );
        System.out.println();
        subclassOntModel().write( System.out, "RDF/XML" );
    }

    public static Model subclassModel() {
        final Model model = ModelFactory.createDefaultModel();
        final Resource classA = model.createResource( NS+"A" );
        final Resource classB = model.createResource( NS+"B" );
        final Resource classC = model.createResource( NS+"C" );
        classB.addProperty( RDFS.subClassOf, classA );
        model.add( classC, RDFS.subClassOf, classA );
        return model;
    }

    public static OntModel subclassOntModel() { 
        final OntModel model = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM );
        final OntClass a = model.createClass( NS+"A" );
        final OntClass b = model.createClass( NS+"B" );
        final OntClass c = model.createClass( NS+"C" );
        a.addSubClass( b );
        c.addSuperClass( a );
        return model;
    }
}
<!-- the plain Model -->
<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:Description rdf:about="http://stackoverflow.com/q/20222080/1281433/C">
    <rdfs:subClassOf rdf:resource="http://stackoverflow.com/q/20222080/1281433/A"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://stackoverflow.com/q/20222080/1281433/B">
    <rdfs:subClassOf rdf:resource="http://stackoverflow.com/q/20222080/1281433/A"/>
  </rdf:Description>
</rdf:RDF>

<!-- the OntModel -->
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" > 
  <rdf:Description rdf:about="http://stackoverflow.com/q/20222080/1281433/C">
    <rdfs:subClassOf rdf:resource="http://stackoverflow.com/q/20222080/1281433/A"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://stackoverflow.com/q/20222080/1281433/B">
    <rdfs:subClassOf rdf:resource="http://stackoverflow.com/q/20222080/1281433/A"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://stackoverflow.com/q/20222080/1281433/A">
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
  </rdf:Description>
</rdf:RDF>

向资源(或个人)添加类型

在这种情况下,您已经使用了一些可用于向资源添加类型的方法,因此您已经回答了自己的问题。创建资源时,可以指定类型。例如,获得

<rdf:Description rdf:about="http://www.help.me/NS/NewResource">
    <rdf:type rdf:resource="http://www.help.me/NS/Subclassname"/>
    ...
</rdf:Description>
你可以这样做:

model.createResource( "http://www.help.me/NS/NewResource",
                      model.createResource( "http://www.help.me/NS/Subclassname" ));

更一般地说,看看这样的代码:

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;

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

        final Resource classA = model.createResource( NS+"A" );
        final Resource classB = model.createResource( NS+"B" );
        final Resource classC = model.createResource( NS+"C" );
        final Resource classD = model.createResource( NS+"D" );

        // You can create a resource with a specified type.
        final Resource x = model.createResource( NS+"x", classA );

        // And subsequent calls to createResource will add more types.
        model.createResource( NS+"x", classB );

        // You could also add the type to the resource
        x.addProperty( RDF.type, classC );

        // Or add the statement to the model
        model.add( x, RDF.type, classD );

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

产生输出:

<rdf:RDF
    xmlns:j.0="http://stackoverflow.com/q/20222080/1281433/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" > 
  <rdf:Description rdf:about="http://stackoverflow.com/q/20222080/1281433/x">
    <rdf:type rdf:resource="http://stackoverflow.com/q/20222080/1281433/D"/>
    <rdf:type rdf:resource="http://stackoverflow.com/q/20222080/1281433/C"/>
    <rdf:type rdf:resource="http://stackoverflow.com/q/20222080/1281433/B"/>
    <rdf:type rdf:resource="http://stackoverflow.com/q/20222080/1281433/A"/>
  </rdf:Description>
</rdf:RDF>

如果您正在使用OntModel,则可以使用createIndividual创建具有指定类型的新个人,并且可以使用其addRDFType方法添加其他类型,还可以创建来自OntClass对象的个人:

public static void main( String[] args ) {
    final OntModel ontModel = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM );
    ontModel.setNsPrefix( "so", NS );
    final OntClass classA = ontModel.createClass( NS+"A" );
    final OntClass classB = ontModel.createClass( NS+"B" );
    final OntClass classC = ontModel.createClass( NS+"C" );

    final Individual x = ontModel.createIndividual( NS+"x", classA );
    x.addRDFType( classB );
    classC.createIndividual( NS+"x" );

    ontModel.write( System.out, "RDF/XML" );
}
<rdf:RDF
    xmlns:so="http://stackoverflow.com/q/20222080/1281433/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" > 
  <rdf:Description rdf:about="http://stackoverflow.com/q/20222080/1281433/C">
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://stackoverflow.com/q/20222080/1281433/B">
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://stackoverflow.com/q/20222080/1281433/x">
    <rdf:type rdf:resource="http://stackoverflow.com/q/20222080/1281433/C"/>
    <rdf:type rdf:resource="http://stackoverflow.com/q/20222080/1281433/B"/>
    <rdf:type rdf:resource="http://stackoverflow.com/q/20222080/1281433/A"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://stackoverflow.com/q/20222080/1281433/A">
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
  </rdf:Description>
</rdf:RDF>