简体   繁体   English

如何使用 Apache Jena 设置 OWL 限制的数据类型?

[英]How to set data type for OWL restriction using Apache Jena?

I am using Apache Jena.我正在使用 Apache Jena。 I have created the data property, its range as xsd:string and restriction that is added as a superclass to specific (already created) class:我已经创建了数据属性,它的范围为 xsd:string 和作为超类添加到特定(已创建)类的限制:

DatatypeProperty dataProperty = model.createDatatypeProperty(baseURI + possibleProperty);
dataProperty.setRange(XSD.xstring);
MaxCardinalityRestriction restriction = model.createMaxCardinalityRestriction(null, dataProperty, 1);
itemClass.addSuperClass(restriction);

When I open the generated ontology in Protege the mentioned restriction looks like:当我在 Protege 中打开生成的本体时,提到的限制如下:

DataProperty_Name max 1 Literal

My aim is to get it with included data type (that is specified in the range of the data property), eg I am expecting:我的目标是使用包含的数据类型(在数据属性的范围内指定)来获取它,例如我期待:

DataProperty_Name max 1 string

Application that uses the ontology needs to know restriction's data type.使用本体的应用程序需要知道限制的数据类型。 Do you maybe know what I need to change in my code to get data type (eg string) in the restriction instead of Literal?您是否知道我需要在代码中更改什么才能在限制中获取数据类型(例如字符串)而不是文字?

Thanks, Darko谢谢,达科

As suggested, I have asked the question on Jena mail list and have got an answer that Jena doesn't support OWL 2 which is where DataRange is defined.正如所建议的那样,我在 Jena 邮件列表上提出了这个问题,并得到了一个答案,即 Jena 不支持定义了 DataRange 的 OWL 2。 That just means there's not convenience functions but you can still achieve the desired affect by working at the RDF level, it's just verbose.这只是意味着没有方便的功能,但您仍然可以通过在 RDF 级别工作来实现所需的效果,这只是冗长的。 Look up the OWL 2 specs to see what RDF triples are needed to represent your desired DataRange, then use the general Model API to assert those triples.查看 OWL 2 规范以了解需要哪些 RDF 三元组来表示所需的 DataRange,然后使用通用模型 API 来断言这些三元组。

I followed this advice and succeeded to solve my problem with the following code:我遵循了这个建议,并成功地用以下代码解决了我的问题:

MaxCardinalityRestriction restriction = model.createMaxCardinalityRestriction(null, existingDataProperty, 1);                                               
restriction.removeAll(OWL.cardinality);
restriction.addLiteral(OWL2.maxQualifiedCardinality, 1);
restriction.addProperty(OWL2.onDataRange, XSD.xstring);
itemClass.addSuperClass(restriction); 

Thanks, Darko谢谢,达科

You may take a look at ONT-API project.您可以查看ONT-API项目。 There is an analogy of org.apache.jena.ontology.OntModel - com.github.owlcs.ontapi.jena.model.OntModel - but for OWL2 specification (native Jena mode is for OWL1).有一个类比org.apache.jena.ontology.OntModel - com.github.owlcs.ontapi.jena.model.OntModel - 但适用于 OWL2 规范(原生 Jena 模式适用于 OWL1)。

Example:示例:

    String uri = "https://stackoverflow.com/questions/39926809";
    String ns = uri + "#";
    OntModel m = OntManagers.createONT().createGraphModel(uri)
            .setNsPrefixes(OntModelFactory.STANDARD)
            .setNsPrefix("test", ns);

    OntDataProperty prop = m.createDataProperty(ns + "prop").addRange(XSD.xstring);
    OntDataRange dt = prop.ranges().findFirst().orElseThrow(AssertionError::new);
    m.createOntClass(ns + "clazz")
            .addSuperClass(m.createDataMaxCardinality(prop, 1, dt));
    m.write(System.out, "ttl");

Output:输出:

@prefix test:  <https://stackoverflow.com/questions/39926809#> .
@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#> .

<https://stackoverflow.com/questions/39926809>
        a       owl:Ontology .

test:prop  a        owl:DatatypeProperty ;
        rdfs:range  xsd:string .

test:clazz  a            owl:Class ;
        rdfs:subClassOf  [ a                            owl:Restriction ;
                           owl:maxQualifiedCardinality  "1"^^xsd:nonNegativeInteger ;
                           owl:onDataRange              xsd:string ;
                           owl:onProperty               test:prop
                         ] .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM