blob: 19a6b06506fbc1d85dc762138a98f1f49abe0b3f [file] [log] [blame]
<!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-extension.css" rel="stylesheet" type="text/css">
<link href="/css/jena.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="/images/favicon.ico" />
<script src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<script src="/js/jena-navigation.js" type="text/javascript"></script>
<script src="/js/bootstrap.min.js" type="text/javascript"></script>
<script src="/js/improve.js" type="text/javascript"></script>
</head>
<body>
<nav class="navbar navbar-default" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></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 navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li id="homepage"><a href="/index.html"><span class="glyphicon glyphicon-home"></span> Home</a></li>
<li id="download"><a href="/download/index.cgi"><span class="glyphicon glyphicon-download-alt"></span> Download</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-book"></span> Learn <b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="dropdown-header">Tutorials</li>
<li><a href="/tutorials/index.html">Overview</a></li>
<li><a href="/documentation/fuseki2/index.html">Fuseki Triplestore</a></li>
<li><a href="/documentation/notes/index.html">How-To's</a></li>
<li><a href="/documentation/query/manipulating_sparql_using_arq.html">Manipulating SPARQL using ARQ</a></li>
<li><a href="/tutorials/rdf_api.html">RDF core API tutorial</a></li>
<li><a href="/tutorials/sparql.html">SPARQL tutorial</a></li>
<li><a href="/tutorials/using_jena_with_eclipse.html">Using Jena with Eclipse</a></li>
<li class="divider"></li>
<li class="dropdown-header">References</li>
<li><a href="/documentation/index.html">Overview</a></li>
<li><a href="/documentation/query/index.html">ARQ (SPARQL)</a></li>
<li><a href="/documentation/assembler/index.html">Assembler</a></li>
<li><a href="/documentation/tools/index.html">Command-line tools</a></li>
<li><a href="/documentation/rdfs/">Data with RDFS Inferencing</a></li>
<li><a href="/documentation/geosparql/index.html">GeoSPARQL</a></li>
<li><a href="/documentation/inference/index.html">Inference API</a></li>
<li><a href="/documentation/javadoc.html">Javadoc</a></li>
<li><a href="/documentation/ontology/">Ontology API</a></li>
<li><a href="/documentation/permissions/index.html">Permissions</a></li>
<li><a href="/documentation/extras/querybuilder/index.html">Query Builder</a></li>
<li><a href="/documentation/rdf/index.html">RDF API</a></li>
<li><a href="/documentation/rdfconnection/">RDF Connection - SPARQL API</a></li>
<li><a href="/documentation/io/">RDF I/O</a></li>
<li><a href="/documentation/rdfstar/index.html">RDF-star</a></li>
<li><a href="/documentation/shacl/index.html">SHACL</a></li>
<li><a href="/documentation/shex/index.html">ShEx</a></li>
<li><a href="/documentation/jdbc/index.html">SPARQL over JDBC</a></li>
<li><a href="/documentation/tdb/index.html">TDB</a></li>
<li><a href="/documentation/tdb2/index.html">TDB2</a></li>
<li><a href="/documentation/query/text-query.html">Text Search</a></li>
</ul>
</li>
<li class="drop down">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-book"></span> Javadoc <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="/documentation/javadoc.html">All Javadoc</a></li>
<li><a href="/documentation/javadoc/arq/">ARQ</a></li>
<li><a href="/documentation/javadoc_elephas.html">Elephas</a></li>
<li><a href="/documentation/javadoc/fuseki2/">Fuseki</a></li>
<li><a href="/documentation/javadoc/geosparql/">GeoSPARQL</a></li>
<li><a href="/documentation/javadoc/jdbc/">JDBC</a></li>
<li><a href="/documentation/javadoc/jena/">Jena Core</a></li>
<li><a href="/documentation/javadoc/permissions/">Permissions</a></li>
<li><a href="/documentation/javadoc/extras/querybuilder/">Query Builder</a></li>
<li><a href="/documentation/javadoc/shacl/">SHACL</a></li>
<li><a href="/documentation/javadoc/tdb/">TDB</a></li>
<li><a href="/documentation/javadoc/text/">Text Search</a></li>
</ul>
</li>
<li id="ask"><a href="/help_and_support/index.html"><span class="glyphicon glyphicon-question-sign"></span> Ask</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-bullhorn"></span> Get involved <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="/getting_involved/index.html">Contribute</a></li>
<li><a href="/help_and_support/bugs_and_suggestions.html">Report a bug</a></li>
<li class="divider"></li>
<li class="dropdown-header">Project</li>
<li><a href="/about_jena/about.html">About Jena</a></li>
<li><a href="/about_jena/architecture.html">Architecture</a></li>
<li><a href="/about_jena/citing.html">Citing</a></li>
<li><a href="/about_jena/team.html">Project team</a></li>
<li><a href="/about_jena/contributions.html">Related projects</a></li>
<li><a href="/about_jena/roadmap.html">Roadmap</a></li>
<li class="divider"></li>
<li class="dropdown-header">ASF</li>
<li><a href="http://www.apache.org/">Apache Software Foundation</a></li>
<li><a href="http://www.apache.org/foundation/sponsorship.html">Become a Sponsor</a></li>
<li><a href="http://www.apache.org/licenses/LICENSE-2.0">License</a></li>
<li><a href="http://www.apache.org/security/">Security</a></li>
<li><a href="http://www.apache.org/foundation/thanks.html">Thanks</a></li>
</ul>
</li>
<li id="edit"><a href="https://github.com/apache/jena-site/edit/main/source/documentation/shacl/__index.md" title="Edit this page on GitHub"><span class="glyphicon glyphicon-pencil"></span> Edit this page</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="breadcrumbs">
<ol class="breadcrumb">
<li><a href='/documentation'>DOCUMENTATION</a></li>
<li class="active">SHACL</li>
</ol>
</div>
<h1 class="title">Apache Jena SHACL</h1>
<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>
<pre>shacl validate --shapes <i>SHAPES.ttl</i> --data <i>DATA.ttl</i></pre>
<pre>shacl v -s <i>SHAPES.ttl</i> -d <i>DATA.ttl</i></pre>
<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>
<pre>shacl parse <i>FILE</i></pre>
<pre>shacl p <i>FILE</i></pre>
<p>which writes out a text format.</p>
<pre>shacl p <i>--out=FMT</i> <i>FILE</i></pre>
<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 &ldquo;,&rdquo; 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>
<pre>
&lt;#serviceWithShacl&gt; rdf:type fuseki:Service ;
rdfs:label "Dataset with SHACL validation" ;
fuseki:name "<i>ds</i>" ;
fuseki:serviceReadWriteGraphStore "" ;
fuseki:endpoint [ fuseki:operation fuseki:shacl ; fuseki:name "shacl" ] ;
fuseki:dataset &lt;#dataset&gt; ;
.
</pre>
<p>This requires a &ldquo;new style&rdquo; endpoint declaration: see
&ldquo;<a href="/documentation/fuseki2/fuseki-config-endpoint.html">Fuseki Endpoint Configuration</a>&quot;.</p>
<p>This is not installed into a dataset setup by default; a configuration file using</p>
<pre><code>fuseki:endpoint [ fuseki:operation fuseki:shacl ;
fuseki:name &quot;shacl&quot; ];
</code></pre><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>
<pre><code>curl -XPOST --data-binary @fu-data.ttl \
--header 'Content-type: text/turtle' \
'http://localhost:3030/ds?default'
</code></pre>
<p>Validate with shapes in <code>fu-shapes.ttl</code> and get back a validation report:</p>
<pre><code>curl -XPOST --data-binary @fu-shapes.ttl \
--header 'Content-type: text/turtle' \
'http://localhost:3030/ds/shacl?graph=default'
</code></pre>
<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>
<pre><code>public static void main(String ...args) {
String SHAPES = &quot;shapes.ttl&quot;;
String DATA = &quot;data1.ttl&quot;;
Graph shapesGraph = RDFDataMgr.loadGraph(SHAPES);
Graph dataGraph = RDFDataMgr.loadGraph(DATA);
Shapes shapes = Shapes.parse(shapesGraph);
ValidationReport report = ShaclValidator.get().validate(shapes, dataGraph);
ShLib.printReport(report);
System.out.println();
RDFDataMgr.write(System.out, report.getModel(), Lang.TTL);
}
</code></pre>
<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>
<pre><code>RDFDataMgr.load(&quot;shapes.shc&quot;);
RDFDataMgr.read(&quot;file:compactShapes&quot;, Lang.SHACLC);
RDFDataMgr.write(System.out, shapesGraph, Lang.SHACLC);
</code></pre>
<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 &ldquo;group&rdquo;, &ldquo;order&rdquo;, &ldquo;name&rdquo;,
&ldquo;description&rdquo; and &ldquo;defaultValue&rdquo; to align with <code>nodeParam</code>.</li>
<li>The <code>nodeParam</code> grammar rule supports &ldquo;targetClass&rdquo; (normally written
with the shorthand <code>-&gt;</code>) as well as the defined
&ldquo;targetNode&rdquo;, &ldquo;targetObjectsOf&rdquo;, &ldquo;targetSubjectsOf&rdquo;</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>
<pre><code>ex:example
sh:target [
a sh:SPARQLTarget ;
sh:select &quot;&quot;&quot;
SELECT ?this
WHERE {
...
}
&quot;&quot;&quot; ;
] ;
</code></pre>
</div>
</div>
</div>
<footer class="footer">
<div class="container" style="font-size:80%" >
<p>
Copyright &copy; 2011&ndash;2022 The Apache Software Foundation, Licensed under the
<a href="http://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 type="text/javascript">
var link = $('a[href="' + this.location.pathname + '"]');
if (link != undefined)
link.parents('li,ul').addClass('active');
</script>
</body>
</html>