| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| |
| |
| <title>Apache Jena - Apache Jena SHACL</title> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| |
| <link href="/css/bootstrap.min.css" rel="stylesheet" media="screen"> |
| <link href="/css/bootstrap-icons.css" rel="stylesheet" media="screen"><link rel="stylesheet" type="text/css" href="https://jena.apache.org/sass/jena.1b17c39a117e22b46db4c66f6395dc27c134a60377d87d2d5745b8600eb69722.css" integrity="sha256-GxfDmhF+IrRttMZvY5XcJ8E0pgN32H0tV0W4YA62lyI="> |
| <link rel="shortcut icon" href="/images/favicon.ico" /> |
| |
| </head> |
| |
| <body> |
| |
| <nav class="navbar navbar-expand-lg bg-body-tertiary" role="navigation"> |
| <div class="container"> |
| <div class="navbar-header"> |
| <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> |
| <span class="navbar-toggler-icon"></span> |
| </button> |
| <a class="navbar-brand" href="/index.html"> |
| <img class="logo-menu" src="/images/jena-logo/jena-logo-notext-small.png" alt="jena logo">Apache Jena</a> |
| </div> |
| |
| <div class="collapse navbar-collapse" id="navbarNav"> |
| <ul class="navbar-nav me-auto mb-2 mb-lg-0"> |
| <li id="homepage" class="nav-item"><a class="nav-link" href="/index.html"><span class="bi-house"></span> Home</a></li> |
| <li id="download" class="nav-item"><a class="nav-link" href="/download/index.cgi"><span class="bi-download"></span> Download</a></li> |
| <li class="nav-item dropdown"> |
| <a href="#" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false"><span class="bi-journal"></span> Learn <b class="caret"></b></a> |
| <ul class="dropdown-menu"> |
| <li class="dropdown-header">Tutorials</li> |
| <li><a class="dropdown-item" href="/tutorials/index.html">Overview</a></li> |
| <li><a class="dropdown-item" href="/documentation/fuseki2/index.html">Fuseki Triplestore</a></li> |
| <li><a class="dropdown-item" href="/documentation/notes/index.html">How-To's</a></li> |
| <li><a class="dropdown-item" href="/documentation/query/manipulating_sparql_using_arq.html">Manipulating SPARQL using ARQ</a></li> |
| <li><a class="dropdown-item" href="/tutorials/rdf_api.html">RDF core API tutorial</a></li> |
| <li><a class="dropdown-item" href="/tutorials/sparql.html">SPARQL tutorial</a></li> |
| <li><a class="dropdown-item" href="/tutorials/using_jena_with_eclipse.html">Using Jena with Eclipse</a></li> |
| <li class="dropdown-divider"></li> |
| <li class="dropdown-header">References</li> |
| <li><a class="dropdown-item" href="/documentation/index.html">Overview</a></li> |
| <li><a class="dropdown-item" href="/documentation/query/index.html">ARQ (SPARQL)</a></li> |
| <li><a class="dropdown-item" href="/documentation/io/">RDF I/O</a></li> |
| <li><a class="dropdown-item" href="/documentation/assembler/index.html">Assembler</a></li> |
| <li><a class="dropdown-item" href="/documentation/tools/index.html">Command-line tools</a></li> |
| <li><a class="dropdown-item" href="/documentation/rdfs/">Data with RDFS Inferencing</a></li> |
| <li><a class="dropdown-item" href="/documentation/geosparql/index.html">GeoSPARQL</a></li> |
| <li><a class="dropdown-item" href="/documentation/inference/index.html">Inference API</a></li> |
| <li><a class="dropdown-item" href="/documentation/ontology/">Ontology API</a></li> |
| <li><a class="dropdown-item" href="/documentation/permissions/index.html">Permissions</a></li> |
| <li><a class="dropdown-item" href="/documentation/extras/querybuilder/index.html">Query Builder</a></li> |
| <li><a class="dropdown-item" href="/documentation/rdf/index.html">RDF API</a></li> |
| <li><a class="dropdown-item" href="/documentation/rdfconnection/">RDF Connection - SPARQL API</a></li> |
| <li><a class="dropdown-item" href="/documentation/rdfstar/index.html">RDF-star</a></li> |
| <li><a class="dropdown-item" href="/documentation/shacl/index.html">SHACL</a></li> |
| <li><a class="dropdown-item" href="/documentation/shex/index.html">ShEx</a></li> |
| <li><a class="dropdown-item" href="/documentation/tdb/index.html">TDB</a></li> |
| <li><a class="dropdown-item" href="/documentation/tdb2/index.html">TDB2</a></li> |
| <li><a class="dropdown-item" href="/documentation/query/text-query.html">Text Search</a></li> |
| </ul> |
| </li> |
| |
| <li class="nav-item dropdown"> |
| <a href="#" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false"><span class="bi-journal-code"></span> Javadoc <b class="caret"></b></a> |
| <ul class="dropdown-menu"> |
| <li><a class="dropdown-item" href="/documentation/javadoc.html">All Javadoc</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/arq/">ARQ</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/fuseki2/">Fuseki</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/geosparql/">GeoSPARQL</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/jena/">Jena Core</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/permissions/">Permissions</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/extras/querybuilder/">Query Builder</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/shacl/">SHACL</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/tdb/">TDB</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/text/">Text Search</a></li> |
| </ul> |
| </li> |
| </ul> |
| <form class="d-flex" role="search" action="/search" method="GET"> |
| <div class="input-group"> |
| <input class="form-control border-end-0 border m-0" type="search" name="q" id="search-query" placeholder="Search...." aria-label="Search" style="width: 10rem;"> |
| <button class="btn btn-outline-secondary border-start-0 border" type="submit"> |
| <i class="bi-search"></i> |
| </button> |
| </div> |
| </form> |
| <ul class="navbar-nav"> |
| <li id="ask" class="nav-item"><a class="nav-link" href="/help_and_support/index.html" title="Ask"><span class="bi-patch-question"></span><span class="text-body d-none d-xxl-inline"> Ask</span></a></li> |
| |
| <li class="nav-item dropdown"> |
| <a href="#" title="Get involved" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false"><span class="bi-megaphone"></span><span class="text-body d-none d-xxl-inline"> Get involved </span><b class="caret"></b></a> |
| <ul class="dropdown-menu"> |
| <li><a class="dropdown-item" href="/getting_involved/index.html">Contribute</a></li> |
| <li><a class="dropdown-item" href="/help_and_support/bugs_and_suggestions.html">Report a bug</a></li> |
| <li class="dropdown-divider"></li> |
| <li class="dropdown-header">Project</li> |
| <li><a class="dropdown-item" href="/about_jena/about.html">About Jena</a></li> |
| <li><a class="dropdown-item" href="/about_jena/architecture.html">Architecture</a></li> |
| <li><a class="dropdown-item" href="/about_jena/citing.html">Citing</a></li> |
| <li><a class="dropdown-item" href="/about_jena/team.html">Project team</a></li> |
| <li><a class="dropdown-item" href="/about_jena/contributions.html">Related projects</a></li> |
| <li><a class="dropdown-item" href="/about_jena/roadmap.html">Roadmap</a></li> |
| <li><a class="dropdown-item" href="/about_jena/security-advisories.html">Security Advisories</a></li> |
| <li class="dropdown-divider"></li> |
| <li class="dropdown-header">ASF</li> |
| <li><a class="dropdown-item" href="https://www.apache.org/">Apache Software Foundation</a></li> |
| <li><a class="dropdown-item" href="https://www.apache.org/foundation/sponsorship.html">Become a Sponsor</a></li> |
| <li><a class="dropdown-item" href="https://www.apache.org/licenses/LICENSE-2.0">License</a></li> |
| <li><a class="dropdown-item" href="https://www.apache.org/security/">Security</a></li> |
| <li><a class="dropdown-item" href="https://www.apache.org/foundation/thanks.html">Thanks</a></li> |
| </ul> |
| </li> |
| |
| |
| |
| |
| <li class="nav-item" id="edit"><a class="nav-link" href="https://github.com/apache/jena-site/edit/main/source/documentation/shacl/__index.md" title="Edit this page on GitHub"><span class="bi-pencil-square"></span><span class="text-body d-none d-xxl-inline"> Edit this page</span></a></li> |
| </ul> |
| </div> |
| </div> |
| </nav> |
| |
| <div class="container"> |
| <div class="row"> |
| <div class="col-md-12"> |
| |
| <div id="breadcrumbs"> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| <ol class="breadcrumb mt-4 p-2 bg-body-tertiary">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| <li class="breadcrumb-item"><a href='/documentation'>DOCUMENTATION</a></li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| <li class="breadcrumb-item active">SHACL</li>
|
|
|
|
|
|
|
|
|
| </ol>
|
|
|
|
|
|
|
| |
| </div> |
| <h1 class="title">Apache Jena SHACL</h1> |
| |
| |
| <main class="d-flex flex-xl-row flex-column"> |
| |
| <aside class="text-muted align-self-start mb-3 p-0 d-xl-none d-block"> |
| <h2 class="h6 sticky-top m-0 p-2 bg-body-tertiary">On this page</h2> |
| <nav id="TableOfContents"> |
| <ul> |
| <li><a href="#command-line">Command line</a></li> |
| <li><a href="#integration-with-apache-jena-fuseki">Integration with Apache Jena Fuseki</a></li> |
| <li><a href="#api">API</a></li> |
| <li><a href="#api-examples">API Examples</a></li> |
| <li><a href="#shacl-compact-syntax">SHACL Compact Syntax</a></li> |
| <li><a href="#sparql-based-targets">SPARQL-based targets</a></li> |
| <li><a href="#validationlistener">ValidationListener</a></li> |
| </ul> |
| </nav> |
| </aside> |
| <article class="flex-column me-lg-4"> |
| <p><code>jena-shacl</code> is an implementation of the |
| W3C <a href="https://www.w3.org/TR/shacl/">Shapes Constraint Language (SHACL)</a>. |
| It implements SHACL Core and SHACL SPARQL Constraints.</p> |
| <p>In addition, it provides:</p> |
| <ul> |
| <li><a href="#shacl-compact-syntax">SHACL Compact Syntax</a></li> |
| <li><a href="#sparql-based-targets">SPARQL-based targets</a></li> |
| </ul> |
| <h2 id="command-line">Command line</h2> |
| <p>The command <code>shacl</code> introduces shacl operations; it takes a sub-command |
| argument.</p> |
| <p>To validate:</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>shacl validate --shapes SHAPES.ttl --data DATA.ttl |
| </span></span><span style="display:flex;"><span>shacl v -s SHAPES.ttl -d DATA.ttl |
| </span></span></code></pre></div><p>The shapes and data files can be the same; the <code>--shapes</code> is optional and |
| defaults to the same as <code>--data</code>. This includes running individual W3C Working |
| Group tests.</p> |
| <p>To parse a file:</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>shacl parse FILE |
| </span></span><span style="display:flex;"><span>shacl p FILE |
| </span></span></code></pre></div><p>which writes out a text format.</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>shacl p --out<span style="color:#666">=</span>FMT FILE |
| </span></span></code></pre></div><p>writes out in <code>text</code>(<code>t</code>), <code>compact</code>(<code>c</code>), <code>rdf</code>(<code>r</code>) formats. Multiple formats can be given, |
| separated by “,” and format <code>all</code> outputs all 3 formats.</p> |
| <h2 id="integration-with-apache-jena-fuseki">Integration with Apache Jena Fuseki</h2> |
| <p>Fuseki has a new service operation <code>fuseki:shacl</code>:</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-turtle" data-lang="turtle"><span style="display:flex;"><span><span style="color:#b8860b"><#serviceWithShacl></span>;<span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">rdf:</span><span style="color:#008000;font-weight:bold">type</span><span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">fuseki:</span><span style="color:#008000;font-weight:bold">Service</span><span style="color:#bbb"> </span>;<span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">rdfs:</span><span style="color:#008000;font-weight:bold">label</span><span style="color:#bbb"> </span><span style="color:#b44">"Dataset with SHACL validation"</span><span style="color:#bbb"> </span>;<span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">fuseki:</span><span style="color:#008000;font-weight:bold">name</span><span style="color:#bbb"> </span><span style="color:#b44">"<i>ds</i>"</span><span style="color:#bbb"> </span>;<span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">fuseki:</span><span style="color:#008000;font-weight:bold">serviceReadWriteGraphStore</span><span style="color:#bbb"> </span><span style="color:#b44">""</span><span style="color:#bbb"> </span>;<span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">fuseki:</span><span style="color:#008000;font-weight:bold">endpoint</span><span style="color:#bbb"> </span>[<span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">fuseki:</span><span style="color:#008000;font-weight:bold">operation</span><span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">fuseki:</span><span style="color:#008000;font-weight:bold">shacl</span><span style="color:#bbb"> </span>;<span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">fuseki:</span><span style="color:#008000;font-weight:bold">name</span><span style="color:#bbb"> </span><span style="color:#b44">"shacl"</span><span style="color:#bbb"> </span>]<span style="color:#bbb"> </span>;<span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">fuseki:</span><span style="color:#008000;font-weight:bold">dataset</span><span style="color:#bbb"> </span><span style="color:#b8860b"><#dataset></span><span style="color:#bbb"> </span>;<span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span>.<span style="color:#bbb"> |
| </span></span></span></code></pre></div><p>This requires a “new style” endpoint declaration: see |
| “<a href="/documentation/fuseki2/fuseki-config-endpoint.html">Fuseki Endpoint Configuration</a>”.</p> |
| <p>This is not installed into a dataset setup by default; a configuration file using</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-turtle" data-lang="turtle"><span style="display:flex;"><span><span style="color:#00f;font-weight:bold">fuseki:</span><span style="color:#008000;font-weight:bold">endpoint</span><span style="color:#bbb"> </span>[<span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">fuseki:</span><span style="color:#008000;font-weight:bold">operation</span><span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">fuseki:</span><span style="color:#008000;font-weight:bold">shacl</span><span style="color:#bbb"> </span>;<span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">fuseki:</span><span style="color:#008000;font-weight:bold">name</span><span style="color:#bbb"> </span><span style="color:#b44">"shacl"</span><span style="color:#bbb"> </span>];<span style="color:#bbb"> |
| </span></span></span></code></pre></div><p>is necessary (or programmatic setup for Fuseki Main).</p> |
| <p>The service accepts a shapes graph posted as RDF to <tt>/<i>ds</i>/shacl</tt> with |
| content negotiation.</p> |
| <p>There is a graph argument, <code>?graph=</code>, that specifies the graph to validate. It |
| is the URI of a named graph, <code>default</code> for the unnamed, default graph (and |
| this is the assumed value of <code>?graph</code> if not present), or <code>union</code> for union of |
| all named graphs in the dataset.</p> |
| <p>Further, an argument <tt>target=<i>uri</i></tt> validates a specific node in the data.</p> |
| <p>Upload data in file <code>fu-data.ttl</code>:</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>curl -XPOST --data-binary @fu-data.ttl <span style="color:#b62;font-weight:bold">\ </span> |
| </span></span><span style="display:flex;"><span> --header <span style="color:#b44">'Content-type: text/turtle'</span> <span style="color:#b62;font-weight:bold">\ </span> |
| </span></span><span style="display:flex;"><span> <span style="color:#b44">'http://localhost:3030/ds?default'</span> |
| </span></span></code></pre></div><p>Validate with shapes in <code>fu-shapes.ttl</code> and get back a validation report:</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>curl -XPOST --data-binary @fu-shapes.ttl <span style="color:#b62;font-weight:bold">\ </span> |
| </span></span><span style="display:flex;"><span> --header <span style="color:#b44">'Content-type: text/turtle'</span> <span style="color:#b62;font-weight:bold">\ </span> |
| </span></span><span style="display:flex;"><span> <span style="color:#b44">'http://localhost:3030/ds/shacl?graph=default'</span> |
| </span></span></code></pre></div><h2 id="api">API</h2> |
| <p>The package <code>org.apache.jena.shacl</code> has the main classes.</p> |
| <ul> |
| <li><code>ShaclValidator</code> for parsing and validation</li> |
| <li><code>GraphValidation</code> for updating graphs with validation</li> |
| </ul> |
| <h2 id="api-examples">API Examples</h2> |
| <p><a href="https://github.com/apache/jena/tree/main/jena-examples/src/main/java/shacl/examples/">https://github.com/apache/jena/tree/main/jena-examples/src/main/java/shacl/examples/</a></p> |
| <p>Example |
| <a href="https://github.com/apache/jena/tree/main/jena-shacl/src/main/java/org/apache/jena/shacl/examples/Shacl01_validateGraph.java"><code>Shacl01_validateGraph</code></a> |
| shows validation and printing of the validation report in a text form and in RDF:</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">static</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">main</span><span style="color:#666">(</span>String <span style="color:#666">...</span><span style="color:#b44">args</span><span style="color:#666">)</span> <span style="color:#666">{</span> |
| </span></span><span style="display:flex;"><span> String SHAPES <span style="color:#666">=</span> <span style="color:#b44">"shapes.ttl"</span><span style="color:#666">;</span> |
| </span></span><span style="display:flex;"><span> String DATA <span style="color:#666">=</span> <span style="color:#b44">"data1.ttl"</span><span style="color:#666">;</span> |
| </span></span><span style="display:flex;"><span> |
| </span></span><span style="display:flex;"><span> Graph shapesGraph <span style="color:#666">=</span> RDFDataMgr<span style="color:#666">.</span><span style="color:#b44">loadGraph</span><span style="color:#666">(</span>SHAPES<span style="color:#666">);</span> |
| </span></span><span style="display:flex;"><span> Graph dataGraph <span style="color:#666">=</span> RDFDataMgr<span style="color:#666">.</span><span style="color:#b44">loadGraph</span><span style="color:#666">(</span>DATA<span style="color:#666">);</span> |
| </span></span><span style="display:flex;"><span> |
| </span></span><span style="display:flex;"><span> Shapes shapes <span style="color:#666">=</span> Shapes<span style="color:#666">.</span><span style="color:#b44">parse</span><span style="color:#666">(</span>shapesGraph<span style="color:#666">);</span> |
| </span></span><span style="display:flex;"><span> |
| </span></span><span style="display:flex;"><span> ValidationReport report <span style="color:#666">=</span> ShaclValidator<span style="color:#666">.</span><span style="color:#b44">get</span><span style="color:#666">().</span><span style="color:#b44">validate</span><span style="color:#666">(</span>shapes<span style="color:#666">,</span> dataGraph<span style="color:#666">);</span> |
| </span></span><span style="display:flex;"><span> ShLib<span style="color:#666">.</span><span style="color:#b44">printReport</span><span style="color:#666">(</span>report<span style="color:#666">);</span> |
| </span></span><span style="display:flex;"><span> System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">();</span> |
| </span></span><span style="display:flex;"><span> RDFDataMgr<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">,</span> report<span style="color:#666">.</span><span style="color:#b44">getModel</span><span style="color:#666">(),</span> Lang<span style="color:#666">.</span><span style="color:#b44">TTL</span><span style="color:#666">);</span> |
| </span></span><span style="display:flex;"><span><span style="color:#666">}</span> |
| </span></span></code></pre></div><p>Example |
| <a href="https://github.com/apache/jena/tree/main/jena-shacl/src/main/java/org/apache/jena/shacl/examples/Shacl02_validateTransaction.java"><code>Shacl02_validateTransaction</code></a> |
| shows how to update a graph only if, after the changes, the graph is validated |
| according to the shapes provided.</p> |
| <h2 id="shacl-compact-syntax">SHACL Compact Syntax</h2> |
| <p>Apache Jena supports |
| <a href="https://w3c.github.io/shacl/shacl-compact-syntax/">SHACL Compact Syntax (SHACL-C)</a> |
| for both reading and writing.</p> |
| <p>The file extensions for SHACL-C are <code>.shc</code> and <code>.shaclc</code> and there is a registered language |
| constant <code>Lang.SHACLC</code>.</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>RDFDataMgr<span style="color:#666">.</span><span style="color:#b44">load</span><span style="color:#666">(</span><span style="color:#b44">"shapes.shc"</span><span style="color:#666">);</span> |
| </span></span><span style="display:flex;"><span> |
| </span></span><span style="display:flex;"><span>RDFDataMgr<span style="color:#666">.</span><span style="color:#b44">read</span><span style="color:#666">(</span><span style="color:#b44">"file:compactShapes"</span><span style="color:#666">,</span> Lang<span style="color:#666">.</span><span style="color:#b44">SHACLC</span><span style="color:#666">);</span> |
| </span></span><span style="display:flex;"><span> |
| </span></span><span style="display:flex;"><span>RDFDataMgr<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">,</span> shapesGraph<span style="color:#666">,</span> Lang<span style="color:#666">.</span><span style="color:#b44">SHACLC</span><span style="color:#666">);</span> |
| </span></span></code></pre></div><p>SHACL-C is managed by the SHACL Community Group. It does not cover all possible shapes. |
| When outputting SHACL-C, SHACL shapes not expressible in SHACL-C will cause an |
| exception and data in the RDF graph that is not relevant will not be output. In |
| other words, SHACL-C is a lossy format for RDF.</p> |
| <p>The Jena SHACL-C writer will output any valid SHACL-C document.</p> |
| <p>Extensions:</p> |
| <ul> |
| <li>The <code>constraint</code> grammar rule allows a shape reference to a node shape.</li> |
| <li>The <code>propertyParam</code> grammar rule provides “group”, “order”, “name”, |
| “description” and “defaultValue” to align with <code>nodeParam</code>.</li> |
| <li>The <code>nodeParam</code> grammar rule supports “targetClass” (normally written |
| with the shorthand <code>-></code>) as well as the defined |
| “targetNode”, “targetObjectsOf”, “targetSubjectsOf”</li> |
| </ul> |
| <h2 id="sparql-based-targets">SPARQL-based targets</h2> |
| <p>SPARQL-based targets allow the target nodes to be calculated with a SPARQL |
| <code>SELECT</code> query.</p> |
| <p>See <a href="https://w3c.github.io/shacl/shacl-af/#SPARQLTarget">SPARQL-based targets</a> |
| for details.</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-turtle" data-lang="turtle"><span style="display:flex;"><span><span style="color:#00f;font-weight:bold">ex:</span><span style="color:#008000;font-weight:bold">example</span><span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">sh:</span><span style="color:#008000;font-weight:bold">target</span><span style="color:#bbb"> </span>[<span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#0b0;font-weight:bold">a</span><span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">sh:</span><span style="color:#008000;font-weight:bold">SPARQLTarget</span><span style="color:#bbb"> </span>;<span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#00f;font-weight:bold">sh:</span><span style="color:#008000;font-weight:bold">select</span><span style="color:#bbb"> </span><span style="color:#b44">""" |
| </span></span></span><span style="display:flex;"><span><span style="color:#b44"> SELECT ?this |
| </span></span></span><span style="display:flex;"><span><span style="color:#b44"> WHERE { |
| </span></span></span><span style="display:flex;"><span><span style="color:#b44"> ... |
| </span></span></span><span style="display:flex;"><span><span style="color:#b44"> } |
| </span></span></span><span style="display:flex;"><span><span style="color:#b44"> """ ; |
| </span></span></span><span style="display:flex;"><span><span style="color:#b44"> ] ; |
| </span></span></span></code></pre></div><h2 id="validationlistener">ValidationListener</h2> |
| <p>When given a <code>ValidationListener</code> the SHACL validation code emits events at each step of validation:</p> |
| <ul> |
| <li>when validation of a shape starts or finishes</li> |
| <li>when the focus nodes of the shape have been identified</li> |
| <li>when validation of a constraint begins, ends and yields positive or negative results</li> |
| </ul> |
| <p>For example, the following listener will just record all events in a List:</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">RecordingValidationListener</span> <span style="color:#a2f;font-weight:bold">implements</span> ValidationListener <span style="color:#666">{</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">private</span> <span style="color:#a2f;font-weight:bold">final</span> List<span style="color:#666"><</span>ValidationEvent<span style="color:#666">></span> events <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ArrayList<span style="color:#666"><>();</span> |
| </span></span><span style="display:flex;"><span> |
| </span></span><span style="display:flex;"><span> <span style="color:#a2f">@Override</span> <span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">onValidationEvent</span><span style="color:#666">(</span>ValidationEvent e<span style="color:#666">)</span> <span style="color:#666">{</span> |
| </span></span><span style="display:flex;"><span> events<span style="color:#666">.</span><span style="color:#b44">add</span><span style="color:#666">(</span>e<span style="color:#666">);</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#666">}</span> |
| </span></span><span style="display:flex;"><span> |
| </span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">public</span> List<span style="color:#666"><</span>ValidationEvent<span style="color:#666">></span> <span style="color:#00a000">getEvents</span><span style="color:#666">()</span> <span style="color:#666">{</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">return</span> events<span style="color:#666">;</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#666">}</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#666">}</span> |
| </span></span></code></pre></div><p>The listener must be passed to the constructor of the <code>ValidationContext</code>. |
| The following example validates the <code>dataGraph</code> according to the <code>shapesGraph</code> using the ValidationListener above:</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>Graph shapesGraph <span style="color:#666">=</span> RDFDataMgr<span style="color:#666">.</span><span style="color:#b44">loadGraph</span><span style="color:#666">(</span>shapesGraphUri<span style="color:#666">);</span> <span style="color:#080;font-style:italic">//assuming shapesGraphUri points to an RDF file |
| </span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Graph dataGraph <span style="color:#666">=</span> RDFDataMgr<span style="color:#666">.</span><span style="color:#b44">loadGraph</span><span style="color:#666">(</span>dataGraphUri<span style="color:#666">);</span> <span style="color:#080;font-style:italic">//assuming dataGraphUri points to an RDF file |
| </span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>RecordingValidationListener listener <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> RecordingValidationListener<span style="color:#666">();</span> <span style="color:#080;font-style:italic">// see above |
| </span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>Shapes shapes <span style="color:#666">=</span> Shapes<span style="color:#666">.</span><span style="color:#b44">parse</span><span style="color:#666">(</span>shapesGraph<span style="color:#666">);</span> |
| </span></span><span style="display:flex;"><span>ValidationContext vCtx <span style="color:#666">=</span> ValidationContext<span style="color:#666">.</span><span style="color:#b44">create</span><span style="color:#666">(</span>shapes<span style="color:#666">,</span> dataGraph<span style="color:#666">,</span> listener<span style="color:#666">);</span> <span style="color:#080;font-style:italic">// pass listener here |
| </span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="color:#a2f;font-weight:bold">for</span> <span style="color:#666">(</span>Shape shape <span style="color:#666">:</span> shapes<span style="color:#666">.</span><span style="color:#b44">getTargetShapes</span><span style="color:#666">())</span> <span style="color:#666">{</span> |
| </span></span><span style="display:flex;"><span> Collection<span style="color:#666"><</span>Node<span style="color:#666">></span> focusNodes <span style="color:#666">=</span> VLib<span style="color:#666">.</span><span style="color:#b44">focusNodes</span><span style="color:#666">(</span>dataGraph<span style="color:#666">,</span> shape<span style="color:#666">);</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">for</span> <span style="color:#666">(</span>Node focusNode <span style="color:#666">:</span> focusNodes<span style="color:#666">)</span> <span style="color:#666">{</span> |
| </span></span><span style="display:flex;"><span> VLib<span style="color:#666">.</span><span style="color:#b44">validateShape</span><span style="color:#666">(</span>vCtx<span style="color:#666">,</span> dataGraph<span style="color:#666">,</span> shape<span style="color:#666">,</span> focusNode<span style="color:#666">);</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#666">}</span> |
| </span></span><span style="display:flex;"><span><span style="color:#666">}</span> |
| </span></span><span style="display:flex;"><span>List<span style="color:#666"><</span>ValidationEvent<span style="color:#666">></span> actualEvents <span style="color:#666">=</span> listener<span style="color:#666">.</span><span style="color:#b44">getEvents</span><span style="color:#666">();</span> <span style="color:#080;font-style:italic">// all events have been recorded |
| </span></span></span></code></pre></div><p>The events thus generated might look like this (<code>event.toString()</code>, one per line):</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-turtle" data-lang="turtle"><span style="display:flex;"><span><span style="">FocusNodeValidationStartedEvent</span>{<span style="">focusNode=http</span>:<span style="">//datashapes</span>.<span style="">org/sh/tests/core/node/class</span><span style="color:#666">-001</span>.<span style="">test</span><span style="color:#080;font-style:italic">#Someone, shape=NodeShape[http://datashapes.org/sh/tests/core/node/class-001.test#TestShape]}</span><span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="">ConstraintEvaluationForNodeShapeStartedEvent</span>{<span style="">constraint=ClassConstraint</span>[<span style="color:#b8860b"><http://datashapes.org/sh/tests/core/node/class-001.test#Person></span>],<span style="color:#bbb"> </span><span style="">focusNode=http</span>:<span style="">//datashapes</span>.<span style="">org/sh/tests/core/node/class</span><span style="color:#666">-001</span>.<span style="">test</span><span style="color:#080;font-style:italic">#Someone, shape=NodeShape[http://datashapes.org/sh/tests/core/node/class-001.test#TestShape]}</span><span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="">ConstraintEvaluatedOnFocusNodeEvent</span>{<span style="">constraint=ClassConstraint</span>[<span style="color:#b8860b"><http://datashapes.org/sh/tests/core/node/class-001.test#Person></span>],<span style="color:#bbb"> </span><span style="">focusNode=http</span>:<span style="">//datashapes</span>.<span style="">org/sh/tests/core/node/class</span><span style="color:#666">-001</span>.<span style="">test</span><span style="color:#080;font-style:italic">#Someone, shape=NodeShape[http://datashapes.org/sh/tests/core/node/class-001.test#TestShape], valid=true}</span><span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="">ConstraintEvaluationForNodeShapeFinishedEvent</span>{<span style="">constraint=ClassConstraint</span>[<span style="color:#b8860b"><http://datashapes.org/sh/tests/core/node/class-001.test#Person></span>],<span style="color:#bbb"> </span><span style="">focusNode=http</span>:<span style="">//datashapes</span>.<span style="">org/sh/tests/core/node/class</span><span style="color:#666">-001</span>.<span style="">test</span><span style="color:#080;font-style:italic">#Someone, shape=NodeShape[http://datashapes.org/sh/tests/core/node/class-001.test#TestShape]}</span><span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="">FocusNodeValidationFinishedEvent</span>{<span style="">focusNode=http</span>:<span style="">//datashapes</span>.<span style="">org/sh/tests/core/node/class</span><span style="color:#666">-001</span>.<span style="">test</span><span style="color:#080;font-style:italic">#Someone, shape=NodeShape[http://datashapes.org/sh/tests/core/node/class-001.test#TestShape]}</span><span style="color:#bbb"> |
| </span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span>[...]<span style="color:#bbb"> |
| </span></span></span></code></pre></div><p>Many use cases can be addressed with the <code>HandlerBasedValidationListener</code>, which allows for registering event handlers on a per-event basis. |
| For example:</p> |
| <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>ValidationListener myListener <span style="color:#666">=</span> HandlerBasedValidationListener |
| </span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">builder</span><span style="color:#666">()</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">forEventType</span><span style="color:#666">(</span>FocusNodeValidationStartedEvent<span style="color:#666">.</span><span style="color:#b44">class</span><span style="color:#666">)</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">addSimpleHandler</span><span style="color:#666">(</span>e <span style="color:#666">-></span> <span style="color:#666">{</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#080;font-style:italic">// ... |
| </span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span> <span style="color:#666">})</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">forEventType</span><span style="color:#666">(</span>ConstraintEvaluatedEvent<span style="color:#666">.</span><span style="color:#b44">class</span><span style="color:#666">)</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">addHandler</span><span style="color:#666">(</span>c <span style="color:#666">-></span> c |
| </span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">iff</span><span style="color:#666">(</span>EventPredicates<span style="color:#666">.</span><span style="color:#b44">isValid</span><span style="color:#666">())</span> <span style="color:#080;font-style:italic">// use a Predicate<ValidationEvent> to select events |
| </span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span> <span style="color:#666">.</span><span style="color:#b44">handle</span><span style="color:#666">(</span>e <span style="color:#666">-></span> <span style="color:#666">{</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#080;font-style:italic">// ... |
| </span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span> <span style="color:#666">})</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#666">)</span> |
| </span></span><span style="display:flex;"><span> <span style="color:#666">.</span><span style="color:#b44">build</span><span style="color:#666">();</span> |
| </span></span></code></pre></div> |
| </article> |
| |
| <aside class="text-muted align-self-start mb-3 mb-xl-5 p-0 d-none d-xl-flex flex-column sticky-top"> |
| <h2 class="h6 sticky-top m-0 p-2 bg-body-tertiary">On this page</h2> |
| <nav id="TableOfContents"> |
| <ul> |
| <li><a href="#command-line">Command line</a></li> |
| <li><a href="#integration-with-apache-jena-fuseki">Integration with Apache Jena Fuseki</a></li> |
| <li><a href="#api">API</a></li> |
| <li><a href="#api-examples">API Examples</a></li> |
| <li><a href="#shacl-compact-syntax">SHACL Compact Syntax</a></li> |
| <li><a href="#sparql-based-targets">SPARQL-based targets</a></li> |
| <li><a href="#validationlistener">ValidationListener</a></li> |
| </ul> |
| </nav> |
| </aside> |
| </main> |
| |
| </div> |
| </div> |
| </div> |
| |
| <footer class="bd-footer py-4 py-md-5 mt-4 mt-lg-5 bg-body-tertiary"> |
| <div class="container" style="font-size:80%" > |
| <p> |
| Copyright © 2011–2024 The Apache Software Foundation, Licensed under the |
| <a href="https://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>. |
| </p> |
| <p> |
| Apache Jena, Jena, the Apache Jena project logo, Apache and the Apache feather logos are trademarks of |
| The Apache Software Foundation. |
| <br/> |
| <a href="https://privacy.apache.org/policies/privacy-policy-public.html" |
| >Apache Software Foundation Privacy Policy</a>. |
| </p> |
| </div> |
| </footer> |
| |
| <script src="/js/popper.min.js.js" type="text/javascript"></script> |
| <script src="/js/bootstrap.min.js" type="text/javascript"></script> |
| <script src="/js/improve.js" type="text/javascript"></script> |
| |
| <script type="text/javascript"> |
| (function() { |
| 'use strict' |
| |
| |
| |
| const links = document.querySelectorAll(`a[href="${window.location.pathname}"]`) |
| if (links !== undefined && links !== null) { |
| for (const link of links) { |
| |
| link.classList.add('active') |
| let parentElement = link.parentElement |
| let count = 0 |
| const levelsLimit = 4 |
| |
| |
| |
| |
| |
| while (['UL', 'LI'].includes(parentElement.tagName) && count <= levelsLimit) { |
| if (parentElement.tagName === 'LI') { |
| |
| |
| |
| parentElement.querySelector('a:first-child').classList.add('active') |
| } |
| parentElement = parentElement.parentElement |
| count++ |
| } |
| } |
| } |
| })() |
| </script> |
| |
| </body> |
| </html> |