【问题标题】:How to create disjoint classes in jena?如何在耶拿创建不相交的类?
【发布时间】:2014-02-24 14:15:56
【问题描述】:

如何在 Jena 中创建不相交的类?例如,经典的 Pizza.owl 示例中,CheeseToppingFishToppingVegetableToppingMeatTopping 等不相交其他。在 Protégé 中,这显示为:

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

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

但是每下一行都会覆盖之前和最后的那一行,和我只写一样

CheeseTopping.setDisjointWith(NutTopping);

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

【问题讨论】:

    标签: rdf jena owl ontology


    【解决方案1】:

    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 .
    

    【讨论】:

    • 哦……真丢脸:/
    猜你喜欢
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2011-12-02
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多