通常,您应该阅读Model的 javadoc以及与之相关的类和接口(例如,资源)。您不需要记住所有细节,但至少要熟悉它们提供的方法类型,这样您就知道如何完成某事。我建议您阅读有关OntModel及其相关类和接口(例如,Individual)的信息。
创建子类
您可以通过直接向模型添加语句或向资源添加属性来创建子类关系,或者,如果您使用 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>