1

在断言类时尝试有选择地调用形状/规则的过程中,我正在使用以下示例形状定义(在 TTL 中):

# baseURI: http://example.org/familyShapes
# imports: http://datashapes.org/dash
# prefix: familyShapes

@prefix dash: <http://datashapes.org/dash#> .
@prefix familyShapes: <http://example.org/familyShapes#> .
@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 sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://example.org/familyShapes>
  rdf:type owl:Ontology ;
  owl:imports <http://datashapes.org/dash> ;
.
familyShapes:FemaleShape
  rdf:type sh:NodeShape ;
  rdfs:label "Female shape" ;
  sh:property [
      sh:path familyShapes:gender ;
      sh:hasValue familyShapes:female ;
    ] ;
.
familyShapes:Gender
  rdf:type rdfs:Class ;
  rdfs:label "Gender" ;
  rdfs:subClassOf rdfs:Resource ;
.
familyShapes:GrandpaRuleShape
  rdf:type sh:NodeShape ;
  rdfs:label "Grandpa rule shape" ;
  sh:rule [
      rdf:type sh:SPARQLRule ;
      rdfs:comment "Shape to infer grandpa/grandchild relationship" ;
      rdfs:label "Infer grandpas and grandchildren" ;
      sh:construct """
        PREFIX familyShapes: <http://example.org/familyShapes#>

        CONSTRUCT {
          ?child familyShapes:grandPa $this .
          $this familyShapes:grandChild ?child .
        }
        WHERE {
        {
           ?child familyShapes:mother ?mom .
           ?mom familyShapes:father $this .
        }
        UNION 
        {
           ?child familyShapes:father ?dad .
           ?dad familyShapes:father $this .
        }
      }
   """ ;
      sh:order 10 ;
    ] ;
  sh:targetClass familyShapes:Person ;
.
familyShapes:MaleShape
  rdf:type sh:NodeShape ;
  rdfs:label "Male shape" ;
  sh:property [
      sh:path familyShapes:gender ;
      sh:hasValue familyShapes:male ;
    ] ;
.
familyShapes:Person
  rdf:type rdfs:Class ;
  rdf:type sh:NodeShape ;
  rdfs:label "Person" ;
  rdfs:subClassOf rdfs:Resource ;
  sh:property [
      rdf:type sh:PropertyShape ;
      sh:path familyShapes:father ;
      sh:class familyShapes:Person ;
      sh:description "A Person's father." ;
      sh:maxCount 1 ;
      sh:name "father" ;
      sh:node familyShapes:MaleShape ;
      sh:nodeKind sh:IRI ;
      sh:sparql [
          sh:message "A person cannot be a father to that same person." ;
          sh:select """PREFIX familyShapes: <http://example.org/familyShapes#>

SELECT $this
WHERE {
    $this familyShapes:father $this .
}""" ;
        ] ;
    ] ;
  sh:property [
      rdf:type sh:PropertyShape ;
      sh:path familyShapes:firstName ;
      sh:datatype xsd:string ;
      sh:description "A Person's first name (aka given name)." ;
      sh:minCount 1 ;
      sh:name "first name" ;
    ] ;
  sh:property [
      rdf:type sh:PropertyShape ;
      sh:path familyShapes:gender ;
      sh:class familyShapes:Gender ;
      sh:description "A Person's gender." ;
      sh:maxCount 1 ;
      sh:minCount 1 ;
      sh:name "gender" ;
    ] ;
  sh:property [
      rdf:type sh:PropertyShape ;
      sh:path familyShapes:lastName ;
      sh:datatype xsd:string ;
      sh:description "A Person's last name (aka family name)." ;
      sh:maxCount 1 ;
      sh:minCount 1 ;
      sh:name "last name" ;
    ] ;
  sh:property [
      rdf:type sh:PropertyShape ;
      sh:path familyShapes:mother ;
      sh:class familyShapes:Person ;
      sh:description "A Person's mother." ;
      sh:maxCount 1 ;
      sh:name "mother" ;
      sh:node familyShapes:FemaleShape ;
      sh:nodeKind sh:IRI ;
      sh:sparql [
          rdfs:comment "A person cannot be that same person's mother." ;
          sh:message "A person cannot be that same person's mother." ;
          sh:select """PREFIX familyShapes: <http://example.org/familyShapes#>

SELECT $this 
WHERE {
    $this familyShapes:mother $this .
}""" ;
        ] ;
    ] ;
  sh:rule [
      rdf:type sh:SPARQLRule ;
      rdfs:label "Infer grandmas and grandchildren" ;
      sh:construct """PREFIX familyShapes: <http://example.org/familyShapes#>

CONSTRUCT {
      ?child familyShapes:grandMa $this .
      $this familyShapes:grandChild ?child .
}
WHERE {
      {
        ?child familyShapes:mother ?mom .
        ?mom familyShapes:mother $this .
      }
      UNION 
      {
        ?child familyShapes:father ?dad .
        ?dad familyShapes:mother $this .
      }
}
        """ ;
    ] ;
.
familyShapes:female
  rdf:type familyShapes:Gender ;
  rdfs:label "female" ;
.
familyShapes:firstName
  rdf:type rdf:Property ;
  rdfs:comment "A Person's first name (aka given name)." ;
  rdfs:label "first name" ;
.
familyShapes:grandChild
  rdf:type owl:ObjectProperty ;
  rdfs:domain familyShapes:Person ;
  rdfs:label "grand child" ;
  rdfs:range familyShapes:Person ;
.
familyShapes:grandMa
  rdf:type owl:ObjectProperty ;
  rdfs:domain familyShapes:Person ;
  rdfs:label "grand ma" ;
  rdfs:range familyShapes:Person ;
.
familyShapes:grandPa
  rdf:type owl:ObjectProperty ;
  rdfs:domain familyShapes:Person ;
  rdfs:label "grand pa" ;
  rdfs:range familyShapes:Person ;
.
familyShapes:male
  rdf:type familyShapes:Gender ;
  rdfs:label "male" ;
.
familyShapes:mother
  rdf:type rdf:Property ;
  rdfs:comment "A Person's mother." ;
  rdfs:label "mother" ;
.

我现在专注于familyShapes:GrandpaRuleShape形状(从第 30 行开始),我相信第 58 行针对familyShapes:Person类。

SHACL API 的RuleUtil.getShapesWithTargetNode方法返回一个空列表,这不是我所期望的结果,因此我创建了该RuleUtil.getShapesWithTargetNode方法的临时本地副本,如下所示,以帮助我调试自己的代码。

private static List<Shape> getShapesWithTargetNode(RDFNode focusNode, ShapesGraph shapesGraph) {
    // TODO: Not a particularly smart algorithm - walks all shapes that have rules
    List<Shape> shapes = new ArrayList<>();
    for(Shape shape : shapesGraph.getRootShapes()) {
        SHShape sr = shape.getShapeResource();
        boolean shapeHasRule = sr.hasProperty(SH.rule);
        boolean shapeFocused = sr.hasTargetNode(focusNode);
        if(shapeHasRule && shapeFocused) {
            shapes.add(shape);
        }
    }
    return shapes;
}

在这个方法中,我已经停止在调试器中执行focusNode=http://example.org/familyShapes#Person和代表上述形状文件的 shapeGraph。在分配了两个布尔值之后,断点位于 for 循环中的条件处。shape的第一个值为familyShapes:GrandpaRuleShape。但是,布尔值shapeFocusedfalse. 布尔值shapeHasRuletrue预期的一样。

我期待这shapeFocused将是true在执行的时候。在更高的层次上,我期望这个方法会返回一个至少包含爷爷形状的列表,但它返回的是空的。我想我一定是错误地设置了对这个方法的调用,但我不确定我做错了什么。有什么建议么?

4

1 回答 1

2

我认为它工作正常。Person 是一个类,规则形状具有 sh:targetClass Person。这意味着焦点/目标节点是该类的实例。如果您使用 Person 的特定实例调用该函数,那么它应该可以工作。

于 2018-08-23T01:37:12.300 回答