SHACL with Jena,如何从形状中获取SPARQL CONSTRUCT之后的模型?

SHACL with Jena, how to get the Model after SPARQL CONSTRUCT from the shape?

我有这个数据文件:

@prefix ex: <http://example.com/ns#> .

ex:John
   a ex:Person ;
   a ex:parent ;
   a ex:male .

还有这个形状文件:

@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#> .
@prefix ex: <http://example.com/ns#> .

ex:RuleOrderExampleShape
    a sh:NodeShape ;
    sh:targetClass ex:Person ;
    sh:rule [
        a sh:SPARQLRule;
        rdfs:label "Construct a father if someone is a parent and a male";
        sh:prefixes ex: ;
        sh:construct """
            CONSTRUCT {
                $this a ex:uncle .
            }
            WHERE {
                $this a ex:parent .
                $this a ex:male .
            }
            """
    ] .

我的代码目前是:

Model dataModel = ModelFactory.createDefaultModel();
dataModel.read(data);
Model shapeModel = ModelFactory.createDefaultModel();
shapeModel.read(shape);

Resource reportResource = ValidationUtil.validateModel(dataModel, shapeModel, true);

如何获得包含新三元组的模型(例如:John a ex:father)?

假设您已在 pom.xml 中包含 SHACL

的依赖项
<dependency>
  <groupId>org.topbraid</groupId>
  <artifactId>shacl</artifactId>
  <version>1.0.1</version>
</dependency>

您可以使用以下代码:

Model shapeModel = JenaUtil.createDefaultModel();
shapeModel.read(strShapeFile);
Model inferenceModel = JenaUtil.createDefaultModel(); 
inferenceModel = RuleUtil.executeRules(infModel, shapeModel, inferenceModel, null);        

inferenceModel 将包含新的三元组。

我也在我的博客上写过这个。例如,参见 SHACL rule execution,您可以在其中找到完整的代码示例。