Search code examples
javajenashacl

How to programmatically read the details of a SHACL shapes graph in Jena?


Let's say I have the following shapes graph

camo:exampleShape
    a sh:NodeShape;
    sh:targetClass cm:Moving;
    sh:property [
        a sh:PropertyShape;
        sh:path cm:dofNumber ;
        sh:minInclusive 1;
        ] .

This shapes file is stored with appropriate prefixes (not shown) and I read it using the Jena API the following way.

import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.shacl.Shapes;

Graph shapesGraph = RDFDataMgr.loadGraph("path/to/shape.ttl");
Shapes shapes = Shape.parse(shapesGraph);

What I would like to know is how to programmatically read the following using the Jena API:

  • What kind of shape is it? (i.e. Node Shape) ?
  • What is the target class ? (i.e. cm:Moving) ?
  • Does it have any property shapes and along what path (i.e. cm:dofNumber) ?

Thanks in advance!


Solution

  • You have two choices: either navigate the parse tree or work with the RDF graph (e.g. with SPARQL). The Shapes has "getGraph" to access the shapes graph.