APACHE Jena Ont 模型支持 owl:maxQualifiedCardinality 和 owl:minQualifiedCardinality 限制

support of owl:maxQualifiedCardinality and owl:minQualifiedCardinality restrictions by APACHE Jena Ont model

我正在使用 APACHE Jena ONT 模型来解析 RDF/XML OWL 文件并处理它们。对于当前的 ONT 模型,ONT 模型无法识别 owl:maxQualifiedCardinalityowl:minQualifiedCardinality 的限制。我还查看了org.apache.jena.ontology包的限制接口,发现不支持这些限制,而是owl:minCardinality和owl:maxCardinality 受支持。我现在想知道 Jena ONT 模型是否也可以考虑这些限制:owl:maxQualifiedCardinality、owl:minQualifiedCardinality

如果你能告诉我你的经历,我会很高兴w.r.t。处理此类限制并使用 Jena ont 模型处理他们的数据

<owl:Class rdf:about="http://test#Numeric">
    <rdfs:subClassOf rdf:resource="http://test#Characteristic"/>
    <rdfs:subClassOf>
        <owl:Restriction>
            <owl:onProperty rdf:resource="http://test#hasUnit"/>
            <owl:maxQualifiedCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">1</owl:maxQualifiedCardinality>
            <owl:onClass rdf:resource="http://test#Scale"/>
        </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:label>Numeric</rdfs:label>
</owl:Class>

Apache Jena ontology API (org.apache.jena.ontology.OntModel) 不支持 OWL2 DL。 您可以查看基于 Jena 的替代方案(即 ONT-API)。这是另一个针对OWL-2的jena接口,支持owl:maxQualifiedCardinality

示例:

    OntModel m = OntModelFactory.createModel();
    m.setID("http://test");
    OntObjectProperty property = m.createObjectProperty("http://test#hasUnit");
    OntClass clazz = m.createOntClass("http://test#Numeric");
    clazz.addLabel("Numeric", null);
    clazz.addSuperClass(m.createOntClass("http://test#Characteristic"))
            .addSuperClass(m.createObjectMaxCardinality(property, 1,
                    m.createOntClass("http://test#Scale")));

    StringWriter sw = new StringWriter();
    m.write(sw, "rdf/xml");
    System.out.println(sw);

    // another way to create OntGraphModel:
    InputStream in = new ByteArrayInputStream(sw.toString().getBytes(StandardCharsets.UTF_8));
    OntModel reloaded = OntManagers.createONT().loadOntologyFromOntologyDocument(in).asGraphModel();
    int cardinality = reloaded.ontObjects(OntClass.ObjectMaxCardinality.class)
            .mapToInt(OntClass.CardinalityRestrictionCE::getCardinality)
            .findFirst().orElseThrow(IllegalStateException::new);
    System.out.println(cardinality);

输出:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <owl:Ontology rdf:about="http://test"/>
  <owl:Class rdf:about="http://test#Characteristic"/>
  <owl:Class rdf:about="http://test#Scale"/>
  <owl:Class rdf:about="http://test#Numeric">
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:onClass rdf:resource="http://test#Scale"/>
        <owl:maxQualifiedCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger"
        >1</owl:maxQualifiedCardinality>
        <owl:onProperty>
          <owl:ObjectProperty rdf:about="http://test#hasUnit"/>
        </owl:onProperty>
      </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf rdf:resource="http://test#Characteristic"/>
    <rdfs:label>Numeric</rdfs:label>
  </owl:Class>
</rdf:RDF>

1

而且如果觉得原来的JenaOntologyAPI更方便,可以把图传回org.apache.jena.ontology.OntModel接口:

OntModel jena = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, reloaded);
        jena.write(System.out, "rdf/xml");