【问题标题】:How to verify if an OWLDataRange object contains a specified value?如何验证 OWLDataRange 对象是否包含指定值?
【发布时间】:2016-09-07 15:02:43
【问题描述】:

我有一个在 Protege 4.3.0 中创建并存储在 OWL 文件中的本体。该本体的一些数据属性的范围定义如下:

({"absent"} or {"value1" , "value2" , "value3"})

我会搜索可能在其范围内具有指定值的数据属性,因此我编写了以下代码示例,但我不知道如何查询OWLDataRange 对象以查看它是否包含指定值(例如字符串"value1")。

final OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
final OWLOntology ontology = manager.loadOntologyFromOntologyDocument(file);
final OWLReasonerFactory rf = new StructuralReasonerFactory();
final OWLReasoner reasoner = rf.createReasoner(ontology);

// ...

// iterate over all data properties
for (OWLDataProperty topDataProperty : reasoner.getTopDataPropertyNode()) {
    for(OWLDataProperty property: reasoner.getSubDataProperties(topDataProperty, false).getFlattened()) {

        // iterate over all data ranges for the current data property
        for (OWLDataRange dataRange : property.getRanges(ontology)) {

            // I would check if the current data property contains a specified value in their ranges.
            // ...

        }
    }
}

【问题讨论】:

标签: java rdf owl protege owl-api


【解决方案1】:

我对这类问题的解决方案是使用推理器 (This Pellet fork)。

这个想法是创建一个表示“具有要检查范围的数据属性的个人”的类和另一个表示“具有确切属性/文字的个人”的类。然后使用推理器检查这两个类的交集是否非空。

因为有一些技巧可以让它正常工作,所以这是我的完整解决方案:

import java.util.function.BiFunction;
import org.semanticweb.owlapi.model.*;
import openllet.owlapi.*;

public class RangeInclusionTest
{
    public static void main(final String[] args)
    {
        try (final OWLManagerGroup group = new OWLManagerGroup())
        {
            final OWLOntologyID ontId = OWLHelper.getVersion(IRI.create("http://test.org#entail-class-restrict-to-some-range"), 1.0);
            final OWLHelper owl = new OWLGenericTools(group, ontId, true);

            // This declaration is vital since this reasoner have problems with pure anonymous reasoning.
            // You can remove this property after yours tests, (or better use one of your already existing properties).
            final OWLDataProperty prop = OWL.DataProperty("http://test.org#dummyProp");
            owl.addAxiom(OWL.declaration(prop));

            final OWLLiteral un = OWL.constant(1);
            final OWLLiteral deux = OWL.constant(2);
            final OWLLiteral trois = OWL.constant(3);
            final OWLLiteral quatre = OWL.constant(4);
            final OWLDataRange dataRange = OWL.dataOr(OWL.oneOf(un), OWL.oneOf(deux), OWL.oneOf(trois));

            final BiFunction<OWLDataRange, OWLLiteral, Boolean> isIncludeInRange = //
                    (range, literal) -> owl.getReasoner().isSatisfiable(//
                            OWL.and(// You must be of all the following class
                                    OWL.some(prop, OWL.oneOf(literal)), // The class of the 'literal'
                                    OWL.some(prop, range), // The class of the range.
                                    OWL.max(prop, 1))// But you can have the property only once.
                    );

            System.out.println("[A] " + (isIncludeInRange.apply(dataRange, un)));
            System.out.println("[B] " + (isIncludeInRange.apply(dataRange, deux)));
            System.out.println("[C] " + (isIncludeInRange.apply(dataRange, trois)));
            System.out.println("[D] " + (isIncludeInRange.apply(dataRange, quatre)));

        } catch (final Exception e)
        {
            e.printStackTrace();
        }
    }
}

首先你必须定义你的范围。然后使用推理器的“isSatisfiable”方法。一种技巧是通过在交集类上添加限制“OWL.max(prop, 1)”来强制使用该属性的一个实例。

输出必须是

[A] true
[B] true
[C] true
[D] false

因为文字“quatre”不包括“dataRange”,所以答案“[D]”为假。 正如您所看到的,此解决方案很容易允许将一个范围的测试包含在另一个范围内。

不需要对本体进行更改的解决方案可能是创建一个特殊的 swrl 规则并检查一个本体(甚至是空的)是否包含该规则,但目前没有 dl-reasoner 支持对 swrl 的蕴含。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    相关资源
    最近更新 更多