如何将推断的三元组输入(其他)SHACL 规则? [英] How to input inferred triples to (other) SHACL rules?

查看:66
本文介绍了如何将推断的三元组输入(其他)SHACL 规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近接触了 SHACL,我非常喜欢它.我对 SHACL 规则有疑问,不知道你们中是否有人可以帮助我.

I recently approached SHACL and I really like it. I have a problem with SHACL rules and I wonder if any of you could help me.

我创建了这个小本体,这是我正在研究的 GDPR 的更大本体的一部分.

I created this small ontology, a portion of a much bigger ontology for the GDPR that I am working on.

# baseURI: http://w3.org/ns/temp
# prefix: temp

@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix temp: <http://w3.org/ns/temp#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://w3.org/ns/temp>
  rdf:type owl:Ontology ;
  owl:versionInfo "Created with TopBraid Composer" ;
.
temp:Action
  rdf:type owl:Class ;
  rdfs:subClassOf owl:Thing ;
.
temp:Consent
  rdf:type owl:Class ;
  rdfs:subClassOf owl:Thing ;
.
temp:DataSubject
  rdf:type owl:Class ;
  rdfs:subClassOf owl:Thing ;
.
temp:GiveConsent
  rdf:type owl:Class ;
  rdfs:subClassOf temp:Action ;
.
temp:John
  rdf:type temp:DataSubject ;
.
temp:LegalBasis
  rdf:type owl:Class ;
  rdfs:subClassOf owl:Thing ;
.
temp:PersonalDataProcessing
  rdf:type owl:Class ;
  rdfs:subClassOf owl:Thing ;
.
temp:c
  rdf:type temp:Consent ;
  temp:objectOfConsent temp:pdp ;
.
temp:gc
  rdf:type temp:GiveConsent ;
  temp:hasAgent temp:John ;
  temp:hasPatient temp:c ;
.
temp:hasAgent
  rdf:type owl:FunctionalProperty ;
  rdfs:domain temp:GiveConsent ;
  rdfs:range temp:DataSubject ;
.
temp:hasLegalBasis
  rdf:type owl:FunctionalProperty ;
  rdfs:domain temp:PersonalDataProcessing ;
  rdfs:range temp:LegalBasis ;
.
temp:hasPatient
  rdf:type owl:FunctionalProperty ;
  rdfs:domain temp:GiveConsent ;
  rdfs:range temp:Consent ;
.
temp:isLawful
  rdf:type owl:DatatypeProperty ;
  rdfs:domain temp:PersonalDataProcessing ;
  rdfs:range xsd:boolean ;
.
temp:objectOfConsent
  rdf:type owl:FunctionalProperty ;
  rdfs:domain temp:Consent ;
  rdfs:range temp:PersonalDataProcessing ;
.
temp:pdp
  rdf:type temp:PersonalDataProcessing ;
.

有五个主要类:PersonalDataProcessing、DataSubject、LegalBasis、Consent 和 GiveConsent.并且,有四个(功能性)对象属性:

There are five main classes: PersonalDataProcessing, DataSubject, LegalBasis, Consent, and GiveConsent. And, there are four (functional) object properties:

  • hasLegalBasis(域:PersonalDataProcessing,范围:LegalBasis).
  • hasAgent(域:GiveConsent,范围:DataSubject)
  • hasPatient(域:GiveConsent,范围:Consent)
  • objectOfConsent(域:Consent,范围:PersonalDataProcessing)

并且有一种数据类型(布尔值)属性称为isLawful".并在 PersonalDataProcessing 上定义:每个 PersonalDataProcessing 都可以合法 (isLawful=true) 或不合法 (isLawful=false).

And there is one datatype (boolean) property called "isLawful" and defined on PersonalDataProcessing: every PersonalDataProcessing can be lawful (isLawful=true) or not (isLawful=false).

我创建了一个单独的gc"在类 GiveConsent 中.gc"有一个代理人约翰"(谁是数据主体)和患者c"(这是一个同意).同意c"通过属性 objectOfConsent 连接到另一个个体pdp",这是一个 PersonalDataProcessing.

I created an individual "gc" within the class GiveConsent. "gc" has an agent "John" (who is a DataSubject) and a patient "c" (which is a Consent). The Consent "c" is connected via the property objectOfConsent to another individual "pdp", which is a PersonalDataProcessing.

然后我有两个 SHACL 规则.其中一个有sh:order 1",所以它应该在另一个之后执行(默认sh:order等于0);

Then I have two SHACL rules. One of them has "sh:order 1", so it should be executed after the other one (which has default sh:order equal to 0);

# baseURI: http://w3.org/ns/rules
# imports: http://w3.org/ns/temp
# prefix: rules

@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rules: <http://w3.org/ns/rules#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix temp: <http://w3.org/ns/temp#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://w3.org/ns/rules>
  rdf:type owl:Ontology ;
  owl:imports <http://w3.org/ns/temp> ;
  owl:versionInfo "Created with TopBraid Composer" ;
.
rules:givenConsentIsLegalBasis
  rdf:type sh:NodeShape ;
  sh:rule [
      rdf:type sh:TripleRule ;
      sh:object [
          sh:path temp:hasPatient ;
        ] ;
      sh:predicate temp:hasLegalBasis ;
      sh:subject [
          sh:path (
              temp:hasPatient
              temp:objectOfConsent
            ) ;
        ] ;
    ] ;
  sh:targetClass temp:GiveConsent ;
.
rules:legalBasisEntailLawful
  rdf:type sh:NodeShape ;
  sh:rule [
      rdf:type sh:TripleRule ;
      sh:order 1 ;
      sh:condition [
          sh:property [
              sh:path temp:hasLegalBasis ;
              sh:minCount 1 ;
            ] ;
        ] ;
      sh:object "true"^^xsd:boolean ;
      sh:predicate temp:isLawful ;
      sh:subject sh:this ;
    ] ;
  sh:targetClass temp:PersonalDataProcessing ;
.

上述第一条规则规定,如果有人同意 PersonalDataProcessing,则该同意是 PersonalDataProcessing 的法律依据.第二条规则(带有sh:order 1 ")规定,每个具有法律依据的个人数据处理都是合法的.

The first rule above states that if someone has given consent to a PersonalDataProcessing, that consent is a legal basis for the PersonalDataProcessing. The second rule (with "sh:order 1 ;") states that every PersonalDataProcessing that has a legal basis is lawful.

最后我写了一个Java文件来执行规则:

Finally I wrote a Java file to execute the rules:

    //Load the ontology
Model ontology = JenaUtil.createMemoryModel();
FileInputStream fisOntology = new FileInputStream("./ontology.ttl");
ontology.read(fisOntology, "urn:dummy", FileUtils.langTurtle);
        
    //Load the rules
Model rules = JenaUtil.createMemoryModel();
FileInputStream fisRules = new FileInputStream("./rules.ttl");
rules.read(fisRules, "urn:dummy", FileUtils.langTurtle);
        
    //Executing the rules
Model inferredModel = RuleUtil.executeRules(ontology, rules, null, null);
        
    //Print
System.out.println(ModelPrinter.get().print(inferredModel));

我给你写信是因为第一条规则正确地创建了三重pdp hasLegalBasis c";通过上面的Java代码:

I am writing you because the first rule correctly creates the triple "pdp hasLegalBasis c" via the Java code above:

<http://w3.org/ns/temp#pdp>
        <http://w3.org/ns/temp#hasLegalBasis>
                <http://w3.org/ns/temp#c> ;

然而,在推断出这个三元组之后,第二条规则NOT触发:isLawful NOT 设置为 true.

However, the second rule do NOT trigger after this triple is inferred: isLawful is NOT set to true.

另一方面,如果我手动添加三重pdp hasLegalBasis c"在本体中,两个规则都触发:

On the other hand, if I manually add the triple "pdp hasLegalBasis c" in the ontology, both rules triggers:

<http://w3.org/ns/temp#pdp>
        <http://w3.org/ns/temp#hasLegalBasis>
                <http://w3.org/ns/temp#c> ;
        <http://w3.org/ns/temp#isLawful>
                true .

我做错了什么?你们有人能帮我吗?

What am I doing wrong? Can any of you help me?

非常感谢

推荐答案

FWIW 您的示例在从 TopBraid Composer 执行时确实有效,TopBraid Composer 会自动进行多次迭代.所以我怀疑这是关于规则的顺序.sh:order 只用于相同shape内的规则,不会通知outer"在形状上循环.因此,示例中的 sh:order 值无效.

FWIW your example does work when executed from TopBraid Composer, which does multiple iterations automatically. So I suspect it's about the order of rules. sh:order is only used for the rules within the same shape, but will not inform the "outer" loop across shapes. As a result, the sh:order values in your example have no effect.

作为一般替代方法,尝试调用规则引擎两次,并将第一次迭代的推理作为第二轮的输入.为此,您需要在对 RuleUtil 的调用之外构建推理模型,这与将 inferencesModel 保留为 null 时 RuleUtil 所做的类似.查看 RuleUtil 类的源代码.

As a general alternative, try calling the rule engine twice with the inferences from the first iteration serving as input to the second round. To do that, you need to construct the inferences Model outside of the calls to RuleUtil similar to what RuleUtil does if you leave inferencesModel null. See the source code of the RuleUtil class.

这篇关于如何将推断的三元组输入(其他)SHACL 规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆