【问题标题】:SHACL with Jena, how to get the Model after SPARQL CONSTRUCT from the shape?SHACL with Jena,如何从形状中获取 SPARQL CONSTRUCT 后的模型?
【发布时间】:2018-06-06 15:36:09
【问题描述】:

我有这个数据文件:

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

【问题讨论】:

    标签: java jena shacl


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-06-29
      • 2016-02-02
      • 1970-01-01
      • 2022-01-20
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多