1

我最近接触了 SHACL,我真的很喜欢它。我对 SHACL 规则有疑问,我想知道你们是否可以帮助我。

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

# 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。并且,有四个(功能)对象属性:

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

并且有一个名为“isLawful”并在 PersonalDataProcessing 上定义的数据类型(布尔)属性:每个 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/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 ;”)规定每个具有法律依据的 PersonalDataProcessing 都是合法的。

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

    //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));

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

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

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

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

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

我究竟做错了什么?你们中的任何人都可以帮助我吗?

非常感谢

4

1 回答 1

1

FWIW 您的示例在从自动执行多次迭代的 TopBraid Composer 执行时确实有效。所以我怀疑这是关于规则的顺序。sh:order 仅用于同一形状内的规则,但不会通知形状之间的“外部”循环。因此,示例中的 sh:order 值无效。

作为一般替代方案,尝试调用规则引擎两次,将第一次迭代的推论用作第二轮的输入。为此,您需要在对 RuleUtil 的调用之外构建推理模型,类似于将推理模型保留为 null 时 RuleUtil 所做的事情。请参阅 RuleUtil 类的源代码。

于 2020-09-23T23:42:46.997 回答