4

如何在耶拿添加合格的基数限制?我不能使用createCardinalityQRestriction,因为这OntModelSpec是 OWL 的第一个版本,而不是 OWL2。在ModelFactory的createOntologyModel中,有没有办法创建OWL2本体?我需要一个类表达式

JeVysledkom正好1 Kolik_Fazovy

我试过使用这段代码:

OntModel ontModel = ModelFactory.createOntologyModel();
OntClass ret = ontModel.createCardinalityQRestriction(null, ontProperty, cardinality,    ontClass2 );
ontClass.addSuperClass(ret);

但我得到了这个例外:

com.hp.hpl.jena.ontology.ProfileException:尝试使用当前语言配置文件不支持的语言构造 CARDINALITY_Q:OWL Full

4

3 回答 3

5

实际上,我在处理另一个问题时遇到了这个问题,添加更复杂的子类公理。在 Jena 中创建这个有点棘手,因为对合格基数限制的支持是 OWL2 的一个特性,而 Jena 对 OWL2 的支持有限:

耶拿本体 API

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

此外,OWL2 词汇类的 Javadoc说:

OWL2 词汇。注意: Jena 不提供 OWL2 推理或 OntModel 支持。提供这些常量是为了方便使用当前 OWL1 支持进行 OWL2 工作并需要一组合适名称的用户。

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

但是你还是想创建一个?那么你就是那些“正在使用当前 OWL1 支持并希望有一组合适的名称的用户”。要了解如何在 RDF 中序列化 OWL2 构造,我们需要看一下OWL 2 Web Ontology Language Mapping to RDF Graphs (Second Edition),特别是第2 节 Mapping from the Structural Specification to RDF Graphs,它告诉我们类表达式

ObjectExactCardinality( n OPE CE )

被序列化为以下三元组

_:x rdf:type owl:Restriction .
_:x owl:onProperty T(OPE) .
_:x owl:qualifiedCardinality "n"^^xsd:nonNegativeInteger .
_:x owl:onClass T(CE) .

_:x类的资源在哪里。Jena 已经处理的不合格案件转而

ObjectExactCardinality( n OPE )

进入

_:x rdf:type owl:Restriction .
_:x owl:onProperty T(OPE) .
_:x owl:cardinality "n"^^xsd:nonNegativeInteger .

如果我们有后者之一,我们可以用一个属性替换它owl:cardinalityowl:qualifiedCardinality属性,并添加适当的owl:onClass属性。下面是一些执行此操作的 Java 代码:

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.Property;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.OWL2;

public class QualifiedRestrictionExample {
    public static OntClass createCardinalityQRestriction(
            OntModel model,
            String uri,
            Property prop,
            int cardinality, 
            OntClass clas ) {
        OntClass klass = model.createCardinalityRestriction( uri, prop, cardinality );
        klass.removeAll( OWL.cardinality );
        klass.addLiteral( OWL2.qualifiedCardinality, cardinality );
        klass.addProperty( OWL2.onClass, clas );
        return klass;
    }

    public static void main(String[] args) {
        String NS = "https://stackoverflow.com/q/20562107/1281433/";
        OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
        OntClass test = model.createClass( NS+"Test" );
        OntProperty j = model.createObjectProperty( NS+"JeVysledkom" );
        OntClass k = model.createClass( NS+"Kolik_Fazovy" );
        OntClass x = createCardinalityQRestriction(model, null, j, 1, k);
        test.addSuperClass( x );
        model.write( System.out, "RDF/XML-ABBREV" );
    }
}

输出:

<rdf:RDF
    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/20562107/1281433/Kolik_Fazovy"/>
  <owl:Class rdf:about="https://stackoverflow.com/q/20562107/1281433/Test">
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:onClass rdf:resource="https://stackoverflow.com/q/20562107/1281433/Kolik_Fazovy"/>
        <owl:qualifiedCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#long"
        >1</owl:qualifiedCardinality>
        <owl:onProperty>
          <owl:ObjectProperty rdf:about="https://stackoverflow.com/q/20562107/1281433/JeVysledkom"/>
        </owl:onProperty>
      </owl:Restriction>
    </rdfs:subClassOf>
  </owl:Class>
</rdf:RDF>

在 Protégé 中:

Protege 中的子类公理

于 2013-12-13T14:11:17.540 回答
0

现在还有ONT-API,它是 OWL2 的 Jena 扩展:

    String ns = "https://stackoverflow.com/q/20562107/1281433/";
    OntModel model = OntModelFactory.createModel()
            .setNsPrefixes(OntModelFactory.STANDARD)
            .setNsPrefix("test", ns);

    OntClass c = model.createOntClass(ns + "Kolik_Fazovy");
    OntObjectProperty p = model.createObjectProperty(ns + "JeVysledkom");
    model.createOntClass(ns + "Test").addSuperClass(model.createObjectCardinality(p, 1, c));

    // list all class expressions (2 owl classes, 1 restriction):
    Assert.assertEquals(3, model.ontObjects(OntClass.class).count());
    // print model
    model.write(System.out, "ttl");

输出:

@prefix test:  <https://stackoverflow.com/q/20562107/1281433/> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .

test:Kolik_Fazovy  a  owl:Class .

test:JeVysledkom  a  owl:ObjectProperty .

test:Test  a             owl:Class ;
        rdfs:subClassOf  [ a                         owl:Restriction ;
                           owl:onClass               test:Kolik_Fazovy ;
                           owl:onProperty            test:JeVysledkom ;
                           owl:qualifiedCardinality  "1"^^xsd:nonNegativeInteger
                         ] .
于 2018-09-05T10:13:52.517 回答
0
    private void createOneToMany(OntModel ontoModel, OntClass ontoClass, OntProperty prop, Resource resource) {
        OntClass allValuesFromRestriction = ontoModel.createAllValuesFromRestriction(null, prop, resource);
        ontoClass.addSuperClass(allValuesFromRestriction);

        OntClass minCardinalityRestriction = ontoModel.createMinCardinalityRestriction(null, prop, 1);
        ontoClass.addSuperClass(minCardinalityRestriction);
    }

    private void createZeroToMany(OntModel ontoModel, OntClass ontoClass, OntProperty prop, Resource resource) {
        OntClass allValuesFromRestriction = ontoModel.createAllValuesFromRestriction(null, prop, resource);
        ontoClass.addSuperClass(allValuesFromRestriction);

        OntClass minCardinalityRestriction = ontoModel.createMinCardinalityRestriction(null, prop, 0);
        ontoClass.addSuperClass(minCardinalityRestriction);
    }

    private void createZeroToOne(OntModel ontoModel, OntClass ontoClass1, OntProperty prop, OntClass ontoClass2) {

        OntClass minCardinalityRestriction = ontoModel.createMinCardinalityRestriction(null, prop, 0);
        ontoClass1.addSuperClass(minCardinalityRestriction);

        OntClass maxCardinalityRestriction = ontoModel.createMaxCardinalityRestriction(null, prop, 1);
        ontoClass1.addSuperClass(maxCardinalityRestriction);
    }

    private void createOneToOne(OntModel ontoModel, OntClass ontoClass1, OntProperty prop, OntClass ontoClass2) {
        OntClass minCardinalityRestriction = ontoModel.createMinCardinalityRestriction(null, prop, 1);
        ontoClass1.addSuperClass(minCardinalityRestriction);

        OntClass maxCardinalityRestriction = ontoModel.createMaxCardinalityRestriction(null, prop, 1);
        ontoClass1.addSuperClass(maxCardinalityRestriction);
    }

然后你可以这样做:

OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
OntClass task = ontModel.createClass(OWL.NS + "Task");
OntClass actor = ontModel.createClass(OWL.NS + "Actor");
OntProperty propTask = ontModel.createObjectProperty( OWL.NS + "Task-performedBy-Actor");
OntProperty propActor = ontModel.createObjectProperty( OWL.NS + "Actor-performs-Task");

致电:

   createOneToMany(ontModel, task, prop, actor);
   createZeroToMany(ontModel, task, prop, actor);
   createOneToOne(ontModel, task, propTask, actor);
   createZeroToOne(ontModel, actor, propActor, task);

结果样本:

<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#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
  <owl:Class rdf:about="http://www.w3.org/2002/07/owl#Task">
    <rdfs:subClassOf>
      <owl:Restriction rdf:nodeID="A0">
        <owl:minCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
        >1</owl:minCardinality>
        <owl:onProperty>
          <owl:ObjectProperty rdf:about="http://www.w3.org/2002/07/owl#Actor-performs-Task"/>
        </owl:onProperty>
      </owl:Restriction>
    </rdfs:subClassOf>
  </owl:Class>
  <owl:Class rdf:about="http://www.w3.org/2002/07/owl#Actor">
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:maxCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
        >1</owl:maxCardinality>
        <owl:onProperty rdf:resource="http://www.w3.org/2002/07/owl#Actor-performs-Task"/>
      </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf rdf:nodeID="A0"/>
  </owl:Class>
  <owl:ObjectProperty rdf:about="http://www.w3.org/2002/07/owl#Task-performedBy-Actor"/>
</rdf:RDF>
于 2017-04-24T23:54:11.257 回答