2

我偶然发现了另一个问题......

我想实现类似的东西: 在此处输入图像描述

我想使用 RDFList 这样做,将必要的属性添加到列表中,然后调用方法 createUnionClass(或 createIntersectionClass)并将其组合在一起。然后,使用 addSuperClass() 将这个方法的结果添加到特定的 ontClass 中。

这是错的吗?我从一些非常简单的东西开始,比如:

RDFList rdfList = ontModel.createList();
rdfList.addProperty(ExampleResource1);
rdfList.addProperty(ExampleResource2); 
UnionClass uc = ontModel.createUnionClass(null, rdfList);
ExampleClass.addSuperClass(uc);

但结果不是前面所说的两者的并集的subClassOf,而只是subClassOf nil。

任何帮助,将不胜感激。

4

1 回答 1

2

在 Jena 中创建这个有点棘手,因为对合格基数限制的支持是 OWL2 的一项功能,而 Jena 对 OWL2 的支持有限:

耶拿本体 API

请注意,目前,Jena 本体 API 仅对 OWL2 的合格基数限制(即 cardinalityQ、minCardinalityQ 和 maxCardinalityQ)提供有限支持。合格的基数限制被封装在接口 CardinalityQRestriction、MinCardinalityQRestriction 和 CardinalityQRestriction 中。OntModel 还提供了创建和访问合格基数限制的方法。由于它们不是 OWL 1.0 语言定义的一部分,因此 OWL 本体不支持限定基数限制。OWL 2 更新中添加了合格的基数限制。Jena 中的 OWL2 支持将在适当的时候添加。

您可能还会看到我在 Jena 邮件列表中发布的关于类似问题的回复,即Re: Owl maxCardinality 限制

不过,您可以非常接近以下 Java 代码。 3.是我们希望能够编写的代码,但我们最终不得不使用它3a.。您可以开始研究 RDF 序列化,从而获得真正合格的限制。我已经在一个相关问题中展示了如何做到这一点:如何在 JENA 中添加合格的基数

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.ontology.OntProperty;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;

public class UnionClassExample {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        String NS = "https://stackoverflow.com/q/20561994/1281433/";
        OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
        model.setNsPrefix( "so", NS );

        OntClass a = model.createClass( NS+"A" );
        OntClass b = model.createClass( NS+"B" );
        OntClass c = model.createClass( NS+"C" );

        OntProperty p = model.createObjectProperty( NS+"p" );
        OntProperty q = model.createObjectProperty( NS+"q" );

        // 1. B or C
        OntClass b_or_c = model.createUnionClass( null, model.createList( new RDFNode[] { b, c } ));

        // 2. p only (B or C)
        OntClass p_only_b_or_c = model.createAllValuesFromRestriction( null, p, b_or_c );

        // 3. q exactly 1 C
        // OntClass q_exactly_1_C = model.createCardinalityQRestriction( null, q, 1, c );

        // 3a. q exactly 1
        OntClass q_exactly_1 = model.createCardinalityRestriction( null, q, 1 );

        // (2) and (3a)
        OntClass _2_and_3a = model.createIntersectionClass( null, model.createList( new RDFNode[] { p_only_b_or_c, q_exactly_1 } ));

        // a subClassOf ((p only (B or C)) and (q exactly 1))
        a.addSuperClass( _2_and_3a );

        model.write( System.out, "RDF/XML-ABBREV" );
    }
}

输出:

<rdf:RDF
    xmlns:so="https://stackoverflow.com/q/20561994/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="https://stackoverflow.com/q/20561994/1281433/A">
    <rdfs:subClassOf>
      <owl:Class>
        <owl:intersectionOf rdf:parseType="Collection">
          <owl:Restriction>
            <owl:allValuesFrom>
              <owl:Class>
                <owl:unionOf rdf:parseType="Collection">
                  <owl:Class rdf:about="https://stackoverflow.com/q/20561994/1281433/B"/>
                  <owl:Class rdf:about="https://stackoverflow.com/q/20561994/1281433/C"/>
                </owl:unionOf>
              </owl:Class>
            </owl:allValuesFrom>
            <owl:onProperty>
              <owl:ObjectProperty rdf:about="https://stackoverflow.com/q/20561994/1281433/p"/>
            </owl:onProperty>
          </owl:Restriction>
          <owl:Restriction>
            <owl:cardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
            >1</owl:cardinality>
            <owl:onProperty>
              <owl:ObjectProperty rdf:about="https://stackoverflow.com/q/20561994/1281433/q"/>
            </owl:onProperty>
          </owl:Restriction>
        </owl:intersectionOf>
      </owl:Class>
    </rdfs:subClassOf>
  </owl:Class>
</rdf:RDF>

加载到 Protégé:

在此处输入图像描述

于 2013-12-13T14:05:41.280 回答