简体   繁体   中英

RDF/XML how to define RDFS classes,subclasses and link them as a type

How can we define classes and sub classes in Jena and add them as type of other resources after? I use Java,Jena and RDF/XML notation. I want to create something like:

<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>

And after: linking a resource to a subclass:

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

Edit:

As far, I found how to define a class:

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

In general, you should read the javadoc for Model and the classes and interfaces that it's related to (eg, Resource). You don't need to memorize all the details, but at least become familiar with the types of methods they provide, so that you've got an idea how you might accomplish something. I'd recommend you read about OntModel and its related classes and interfaces (eg, Individual).

Creating Subclasses

You can create subclass relationships by adding statements to a model directly, or by adding properties to a resource, or, if you're using an OntModel and OntClasses, with the addSubClass and addSuperClass methods.

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>

Adding types to a Resource (or Individual)

In this case though, you're already using some of the methods that you can use to add types to a resource, and so you've already answered your own question. When you create a resource, you can specify a type. Eg, to get

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

you could do:

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

More generally, look at code like this:

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

which produces as output:

<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>

If you're using an OntModel, you can use createIndividual to create a new individual with a specified type, and you can use its addRDFType method to add another type, and you can also create an individual from an OntClass object:

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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