我无法使用 Apache Jena 创建 NodeShape。我想定义一个针对属于 owl:Class 的所有命名个体的形状(因此不包括空白节点)。例如,这种形状有助于评估一个本体的所有类别是否至少有一个标签。
我定义了这个形状
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix os: <https://w3id.org/OWLunit/shapes/ontology.ttl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
os:ClassShape
a sh:NodeShape ;
sh:target [
a sh:SPARQLTarget ;
sh:select """
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?this
WHERE {
?this a owl:Class .
FILTER(!isBlank(?this))
}
""" ;
] ;
sh:property [
sh:path rdfs:label ;
sh:minCount 1 ;
] .
并且,按照此处提供的说明,我编写了以下代码来评估DUL 本体上的形状。
public static void test() {
String SHAPES = "test_shape.ttl";
String DATA = "http://www.ontologydesignpatterns.org/ont/dul/DUL.owl";
String q = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
+ "PREFIX owl: <http://www.w3.org/2002/07/owl#> "
+ "SELECT ?this WHERE {"
+ " ?this a owl:Class . FILTER NOT EXISTS {?this rdfs:label ?l}"
+ "FILTER (!isBlank(?this))}";
Graph shapesGraph = RDFDataMgr.loadGraph(SHAPES);
Graph dataGraph = RDFDataMgr.loadGraph(DATA, Lang.RDFXML);
System.out.println(ResultSetFormatter
.asText(QueryExecutionFactory.create(q, ModelFactory.createModelForGraph(dataGraph)).execSelect()));
Shapes shapes = Shapes.parse(shapesGraph);
System.out.println("Number of shapes " + shapes.getTargetShapes().size());
ValidationReport report = ShaclValidator.get().validate(shapes, dataGraph);
System.out.println(report.conforms());
ShLib.printReport(report);
}
方法 test() 的执行产生以下结果:
--------------------------------------------------------------------------------
| this |
================================================================================
| <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#DesignedSubstance> |
| <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#TimeIndexedRelation> |
| <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#ObjectAggregate> |
| <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#SpatioTemporalRegion> |
| <http://www.w3.org/2002/07/owl#Thing> |
| <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#InformationEntity> |
--------------------------------------------------------------------------------
Number of shapes 0
true
Conforms
请注意,本体中定义的 6 个类没有标签(查询 q 打印它们),因此本体不符合形状。没有加载任何形状,因此 DUL 结果符合形状。
这里出了点问题。是因为形状还是代码?
PS:我正在使用 apache-jena 4.0.0