2

我有这个数据文件:

@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)?

4

1 回答 1

4

假设您已在 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 规则执行,您可以在其中找到完整的代码示例。

于 2018-06-06T18:04:12.977 回答