如何将推断的三元组输入其他SHACL规则?

问题描述

我最近联系了SHACL,我真的很喜欢。我对SHACL规则有疑问,我想知道是否有人可以帮助我。

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

Text

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

  • hasLegalBasis(域:PersonalDataProcessing,范围:LegalBasis)。
  • hasAgent(域:GiveConsent,范围:DataSubject)
  • 有患者(域:GiveConsent,范围:同意)
  • objectOfConsent(域:同意,范围:PersonalDataProcessing)

在PersonalDataProcessing上定义了一个数据类型(布尔)属性,称为“ isLawful”:每个PersonalDataProcessing可以合法(isLawful = true),也可以合法(isLawful = false)。

我在GiveConsent类中创建了一个单独的“ gc”。 “ gc”有一个代理“ John”(他是一个DataSubject)和一个病人“ c”(这是一个同意书)。同意“ c”通过objectOfConsent属性连接到另一个个人“ pdp”,这是一个PersonalDataProcessing。

然后我有两个SHACL规则。其中一个具有“ sh:order 1”,因此应在另一个认值为sh:order等于0)之后执行

# 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,则该同意是PersonalDataProcessing的法律依据。第二条规则(带有“ sh:order 1;”)指出,每个具有法律依据的PersonalDataProcessing都是合法的。

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

# 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 ;
.

我写信给您,是因为第一个规则通过上面的Java代码正确创建了三重“ pdp hasLegalBasis c”:

    //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,FileUtils.langTurtle);
        
    //Executing the rules
Model inferredModel = RuleUtil.executeRules(ontology,rules,null,null);
        
    //Print
System.out.println(ModelPrinter.get().print(inferredModel));

但是,在推断出该三元组之后,第二条规则 NOT 不会触发:isLawful是 NOT 设置为true。

另一方面,如果我在本体中手动添加元组“ pdp hasLegalBasis c”,则这两个规则都会触发:

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

我在做什么错?你们中的任何一个可以帮助我吗?

非常感谢您

解决方法

首先,当您从TopBraid Composer执行时,您的示例可以正常工作,它会自动进行多次迭代。因此,我怀疑这与规则的顺序有关。 sh:order仅用于相同形状内的规则,但不会通知形状间的“外部”循环。结果,示例中的sh:order值无效。

作为一般选择,请尝试两次调用规则引擎,并将第一次迭代的推论作为第二轮的输入。为此,您需要在对RuleUtil的调用之外构造推理模型,类似于在将InferencesModel保留为null的情况下,RuleUtil进行的操作。请参阅RuleUtil类的源代码。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...