OntClass.setDisjointWith(Resource) 正在做它所说的事情(强调):
断言这个类与给定的类是不相交的。 任何现有的
disjointWith 的语句将被删除。
您不想用 setDisjointWith“设置”一个不相交的类,而是想用OntClass.addDisjointWith(Resource)(实际上在同一个 JavaDoc 页面上列出)“添加”一个不相交的类:
添加一个与该类不相交的类。
这里的示例代码和输出显示了它是如何使用的:
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.ModelFactory;
public class DisjointClassesExample {
public static void main(String[] args) {
// We'll create a model and define a namespace here. In your
// actual application, you probably already have these, or they're
// already defined for you by the ontology that you're loading.
String NS = "http://stackoverflow.com/q/21990312/1281433/";
OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
model.setNsPrefix( "", NS );
// Let's create some classes. Then we simply use the OntClass#addDisjointWith
// method to declare that the classes are disjoint.
OntClass a = model.createClass( NS+"A" );
OntClass b = model.createClass( NS+"B" );
OntClass c = model.createClass( NS+"C" );
a.addDisjointWith( b );
a.addDisjointWith( c );
// We see the expected results in the ontology.
model.write( System.out, "RDF/XML-ABBREV" );
model.write( System.out, "TURTLE" );
}
}
<rdf:RDF
xmlns="http://stackoverflow.com/q/21990312/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#">
<owl:Class rdf:about="http://stackoverflow.com/q/21990312/1281433/B"/>
<owl:Class rdf:about="http://stackoverflow.com/q/21990312/1281433/C"/>
<owl:Class rdf:about="http://stackoverflow.com/q/21990312/1281433/A">
<owl:disjointWith rdf:resource="http://stackoverflow.com/q/21990312/1281433/C"/>
<owl:disjointWith rdf:resource="http://stackoverflow.com/q/21990312/1281433/B"/>
</owl:Class>
</rdf:RDF>
@prefix : <http://stackoverflow.com/q/21990312/1281433/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
:A a owl:Class ;
owl:disjointWith :C , :B .
:C a owl:Class .
:B a owl:Class .