java - 如何在 JENA 中添加限定基数

标签 java rdf jena owl description-logic

如何在 Jena 中添加限定基数限制?我无法使用 createCardinalityQRestriction,因为 OntModelSpec 适用于 OWL 的第一个版本,而不是 OWL2。在ModelFactory的createOntologyModel中,有没有办法创建OWL2本体?我需要一个像

这样的类表达式

JeVysledkom exactly 1 Kolik_Fazovy

我试过使用这段代码:

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

但我得到了这个异常(exception):

com.hp.hpl.jena.ontology.ProfileException: Attempted to use language construct CARDINALITY_Q that is not supported in the current language profile: OWL Full

最佳答案

我实际上只是在处理另一个问题时遇到了这个问题,Adding more complicated subclass axiom .在 Jena 中创建这个有点棘手,因为支持限定基数限制是 OWL2 的一个特性,而 Jena 对 OWL2 的支持有限:

Jena Ontology API

Note that, at present, the Jena ontology API has only limited support for OWL2's qualified cardinality restrictions (i.e. cardinalityQ, minCardinalityQ and maxCardinalityQ). Qualified cardinality restrictions are encapsulated in the interfaces CardinalityQRestriction, MinCardinalityQRestriction and CardinalityQRestriction. OntModel also provides methods for creating and accessing qualified cardinality restrictions. Since they are not part of the OWL 1.0 language definition, qualified cardinality restrictions are not supported in OWL ontologies. Qualified cardinality restrictions were added to the OWL 2 update. OWL2 support in Jena will be added in due course.

另外,Javadoc for the OWL2 vocabulary class说:

OWL2 vocabulary. NOTE: Jena does not provide OWL2 inference or OntModel support. These constants are provided for the convenience of users who are doing OWL2 work with the current OWL1 support and desire a suitable set of names.

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

但是你还是想创建一个?那么你就是那些“在当前 OWL1 支持下使用 OWL2 并需要一组合适的名称的用户”中的一员。要找出 OWL2 构造应该如何在 RDF 中序列化,我们需要看一下 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 是作为类的资源。耶拿已经处理的不合格案件转

ObjectExactCardinality( n OPE )

进入

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

如果我们有后者之一,我们可以用 owl:qualifiedCardinality 属性替换它的 owl:cardinality 属性,并添加适当的 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é 中:

subclass axiom in Protege

关于java - 如何在 JENA 中添加限定基数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20562107/

相关文章:

java - 无法解析org.apache.commons:commons-lang3:3.0对等方未通过身份验证的gradle 2.2.1

java - Hibernate 自引用实体级联

rdf - JSON-LD 中@vocab 的用途是什么,与@context 有什么区别?

java - 在 Jena 自定义 PropertyFunction 中等待服务器响应

javascript - 从 javascript 进行插入查询时,Jena Fuseki 不工作。无更新参数错误

java - 如何配置/设置 OBIEE 的内存以使其运行得更快

java - 来自 StackExchange API 的 JSON URL 返回乱码?

java - Jena - 如何一起使用本体 + rdf

java - 从给定的 RDF(.ttl) 文件中查找 RDF 映射并获取 JSON 格式的输出

java - 如何在耶拿获取不同语言的对象?