在尝试声明类时有选择地调用形状/规则的过程中,我正在使用以下示例形状定义(在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
和上面表示shapes文件的shapesGraph在此方法的调试器中执行。在分配了两个布尔值之后,断点在for循环中处于条件条件。 shape
的第一个值为familyShapes:GrandpaRuleShape
。但是,布尔值shapeFocused
是false
。布尔值shapeHasRule
为true
。
我期望shapeFocused
在此执行点将是true
。在更高的级别上,我期望此方法将返回至少包含祖父形状的列表,但它返回空值。我想我必须对这个方法的调用设置不正确,但是我不确定自己做错了什么。有什么建议吗?
答案 0 :(得分:2)
我认为它可以正常工作。 Person是一个类,规则形状具有sh:targetClass Person。这意味着焦点/目标节点是该类的 instances 。如果您使用特定的Person实例调用该函数,则它应该可以工作。