Question

How can I create disjoint classes in Jena? For instance, the classic pizza.owl example has CheeseTopping as disjoint with FishTopping, VegetableTopping, MeatTopping, and several others. In Protégé, this is shown as:

enter image description here

I've tried using OntClass.setDisjointWith(Resource):

CheeseTopping.setDisjointWith(FishTopping);
…
CheeseTopping.setDisjointWith(NutTopping);

but each next line overwrite the line before and in the end, its same as I write only

CheeseTopping.setDisjointWith(NutTopping);

How can I assert that a class is disjoint with each of a number of classes?

Was it helpful?

Solution

OntClass.setDisjointWith(Resource) is doing exactly what it says it does (emphasis added):

Assert that this class is disjoint with the given class. Any existing statements for disjointWith will be removed.

Instead of “setting” a disjoint class with setDisjointWith, you want to be “adding” a disjoint class with OntClass.addDisjointWith(Resource) (which is actually listed on the same JavaDoc page):

Add a class that this class is disjoint with.

Here's sample code and output that shows how it is used:

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 .
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top