如何在jena中创建不相交的类?

时间:2014-02-24 14:15:56

标签: rdf jena owl ontology

如何在耶拿创建不相交的课程?例如,经典的pizza.owl示例将 CheeseTopping FishTopping VegetableTopping MeatTopping 以及几个不相交其他。在Protégé中,这显示为:

enter image description here

我尝试过使用OntClass.setDisjointWith(Resource)

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

但是每一行都会覆盖前一行和最后一行,与我只写

相同
CheeseTopping.setDisjointWith(NutTopping);

如何断言某个类与多个类中的每一个都不相交?

1 个答案:

答案 0 :(得分:2)

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 .